1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
#
# lkn.core: an entity-component-system (ecs) implementation for lyxan
# Copyright (C) 2017 Thomas Letan <contact@thomasletan.fr>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
defmodule Lkn.Core.Puppeteer do
alias Lkn.Core.Instance
alias Lkn.Core.Name
alias Lkn.Core.Specs
@typedoc """
A key to identify and reach a given Puppeteer.
"""
@type k :: any
@typedoc """
A module which implements the Puppeteer behaviour.
"""
@type m :: module
@type init_args :: any
@type state :: any
defmacro defpuppeteer(name, do: block) do
state_type = quote do Lkn.Core.Puppeteer.state end
key_type = quote do Lkn.Core.Puppeteer.k end
key_to_name = &(Lkn.Core.Name.puppeteer(&1))
quote do
defmodule unquote(name) do
unquote(Specs.gen_server_from_specs(
block,
key_type,
key_to_name,
state_type,
additional_args: [
quote do unquote(Specs.var_name("instance_key")) :: Lkn.Core.Instance.k end,
]
)
)
defmacro __using__(args) do
plugin_clients = case args do
[do: use_block] ->
Specs.gen_server_plugin_entry_point(
use_block,
quote do Lkn.Core.Puppeteer.k end,
&(Lkn.Core.Name.puppeteer(&1)),
[quote do unquote(Specs.var_name("instance_key")) :: Lkn.Core.Instance.k end],
quote do Lkn.Core.Puppeteer.state end
)
_ ->
quote do
end
end
quote do
@behaviour unquote(__MODULE__)
@behaviour Lkn.Core.Puppeteer
use GenServer
alias Lkn.Core.Instance
alias Lkn.Core, as: L
defmodule State do
@moduledoc false
defstruct [
:puppeteer_key,
:map_key,
:instance_key,
:state,
]
@type t :: %State{
puppeteer_key: Puppeteer.k,
instance_key: Lkn.Prelude.Option.t(Instance.k),
map_key: Lkn.Prelude.Option.t(L.Map.k),
state: Lkn.Core.Puppeteer.state,
}
@spec new(Puppeteer.t, Lkn.Core.Puppeteer.state) :: t
def new(pk, s) do
%State{
puppeteer_key: pk,
instance_key: Lkn.Prelude.Option.nothing(),
map_key: Lkn.Prelude.Option.nothing(),
state: s,
}
end
end
def init({puppeteer_key, args}) do
{:ok, s} = init_state(args)
{:ok, State.new(puppeteer_key, s)}
end
def handle_cast({:puppet_enter, instance_key, puppet_key, digest}, state) do
if state.instance_key == Lkn.Prelude.Option.some(instance_key) do
s2 = puppet_enter(state.state, state.instance_key, puppet_key, digest)
{:noreply, %State{state|state: s2}}
else
{:noreply, state}
end
end
def handle_cast({:puppet_leave, instance_key, puppet_key}, state) do
if state.instance_key == Lkn.Prelude.Option.some(instance_key) do
s2 = puppet_leave(state.state, state.instance_key, puppet_key)
{:noreply, %State{state|state: s2}}
else
{:noreply, state}
end
end
def handle_cast({:instance_digest, instance_key, map_key, map, puppets}, state) do
if state.instance_key == Lkn.Prelude.Option.some(instance_key) do
s2 = instance_digest(state.state, instance_key, map_key, map, puppets)
{:noreply, %State{state|state: s2}}
else
{:noreply, state}
end
end
def handle_cast({:leave_instance, instance_key}, state) do
if state.instance_key == Lkn.Prelude.Option.some(instance_key) do
s2 = leave_instance(state.state, instance_key)
Instance.unregister_puppeteer(instance_key, state.puppeteer_key)
{:noreply, %State{state|instance_key: Lkn.Prelude.Option.nothing(), state: s2}}
else
{:noreply, state}
end
end
def handle_cast({:spec, {name, args}}, state) do
s = :erlang.apply(__MODULE__, name, [state.puppeteer_key|args]++[state.instance_key, state.state])
{:noreply, %State{state|state: s}}
end
def handle_cast({:plugin, {name, args}}, state) do
name = String.to_atom(String.replace_suffix(Atom.to_string(name), "", "_plugin"))
s = :erlang.apply(__MODULE__, name, [state.puppeteer_key|args]++[state.instance_key, state.state])
{:noreply, %State{state|state: s}}
end
def handle_call({:find_instance, map_key}, _from, state) do
instance_key = Lkn.Core.Pool.register_puppeteer(map_key, state.puppeteer_key, __MODULE__)
state = %State{state|instance_key: Lkn.Prelude.Option.some(instance_key)}
{:reply, instance_key, state}
end
def handle_call({:spec, {name, args}}, _call, state) do
{res, s} = :erlang.apply(__MODULE__, name, [state.puppeteer_key|args] ++ [state.state])
{:reply, res, %State{state|state: s}}
end
unquote(plugin_clients)
def terminate(reason, state) do
destroy(state.puppeteer_key, state.state, state.instance_key, reason)
end
end
end
end
end
end
@spec start_link(m, k, init_args) :: GenServer.on_start
def start_link(module, puppeteer_key, args) do
GenServer.start_link(module, {puppeteer_key, args}, name: Name.puppeteer(puppeteer_key))
end
@callback init_state(init_args) :: {:ok, state}|:error
@callback leave_instance(s :: state, instance_key :: Lkn.Core.Instance.k) :: state
@callback puppet_enter(s :: state, instance_key :: Lkn.Core.Instance.k, puppet_key :: Lkn.Core.Puppet.k, digest :: Lkn.Core.Entity.digest) :: state
@callback puppet_leave(s :: state, instance_key :: Lkn.Core.Instance.k, puppet_key :: Lkn.Core.Puppet.k) :: state
@callback destroy(puppeteer_key :: k, s :: state, instance_key :: Option.t(Lkn.Core.Instance.k), reason :: any) :: term
@callback instance_digest(
s :: state,
instance_key :: Lkn.Core.Instance,
map_key :: Lkn.Core.Map.k,
map :: Lkn.Core.Entity.digest,
puppets :: %{Lkn.Core.Puppet.k => Lkn.Core.Entity.digest}
) :: state
@spec leave_instance(k, Instance.k) :: :ok
def leave_instance(puppeteer_key, instance_key) do
GenServer.cast(Name.puppeteer(puppeteer_key), {:leave_instance, instance_key})
end
@spec find_instance(k, L.Map.k) :: Instance.k
def find_instance(puppeteer_key, map_key) do
GenServer.call(Name.puppeteer(puppeteer_key), {:find_instance, map_key})
end
@doc false
def instance_digest(puppeteer_key, instance_key, map_key, map, puppets) do
GenServer.cast(Name.puppeteer(puppeteer_key), {:instance_digest, instance_key, map_key, map, puppets})
end
@doc false
def puppet_enter(puppeteer_key, instance_key, puppet_key, digest) do
GenServer.cast(Name.puppeteer(puppeteer_key), {:puppet_enter, instance_key, puppet_key, digest})
end
@doc false
def puppet_leave(puppeteer_key, instance_key, puppet_key) do
GenServer.cast(Name.puppeteer(puppeteer_key), {:puppet_leave, instance_key, puppet_key})
end
def stop(puppeteer_key, reason \\ :normal) do
GenServer.stop(Name.puppeteer(puppeteer_key), reason)
end
end
|