aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lkn/core/pool.ex
blob: aabef1d4307f09a341ef1ac6fcd3b726d8418c6c (plain)
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
#
# 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.Pool.Supervisor do
  @moduledoc false

  use Supervisor

  @name Lkn.Core.Pool.Supervisor

  def start_link(_arg \\ []) do
    Supervisor.start_link(__MODULE__, [], name: @name)
  end

  def init(_) do
    children = [
      supervisor(Lkn.Core.Pool, [])
    ]

    supervise(children, strategy: :simple_one_for_one)
  end

end

defmodule Lkn.Core.Pool.GenServer do
  @moduledoc false

  require Logger

  alias Lkn.Core, as: L
  alias Lkn.Core.Instance
  alias Lkn.Core.Name
  alias Lkn.Core.Puppeteer

  use GenServer

  defmodule State do
    @moduledoc false

    defstruct [
      :map_key,
      :instances,
    ]

    @type t :: %State{
      map_key: L.Map.k,
      instances: [Instance.k],
    }

    @spec new(L.Map.k) :: t
    def new(map_key) do
      %State{
        map_key: map_key,
        instances: [],
      }
    end

    @spec supervise?(t, Instance.k) :: boolean
    def supervise?(state, instance_key) do
      Enum.member?(state.instances, instance_key)
    end

    @spec add_instance(t, Instance.k) :: t
    def add_instance(state, instance_key) do
      %State{state|instances: [instance_key|state.instances]}
    end

    @spec remove_instance(t, Instance.k) :: t
    def remove_instance(state, instance_key) do
      %State{state|instances: List.delete(state.instances, instance_key)}
    end

    @spec find_instance(t, Puppeteer.k, Puppeteer.m) :: {t, Instance.k}
    def find_instance(state, puppeteer_key, puppeteer_mod) do
      find_instance_or_spawn(state, state.instances, puppeteer_key, puppeteer_mod)
    end

    @spec find_instance_or_spawn(t, [Instance.k], Puppeteer.k, Puppeteer.m) :: {t, Instance.k}
    defp find_instance_or_spawn(state, [instance_key|instances], puppeteer_key, puppeteer_mod) do
      if Instance.register_puppeteer(instance_key, puppeteer_key, puppeteer_mod) do
        {state, instance_key}
      else
        find_instance_or_spawn(state, instances, puppeteer_key, puppeteer_mod)
      end
    end
    defp find_instance_or_spawn(state, [], puppeteer_key, puppeteer_mod) do
      instance_key = UUID.uuid4()
      {:ok, _} = Instance.spawn_instance(state.map_key, instance_key)
      if !Instance.register_puppeteer(instance_key, puppeteer_key, puppeteer_mod) do
        Logger.error("[lkn.core]: [Pool(#{inspect(state.map_key)})] spawns an instance that refuses to register a new puppeteer")
        raise "New spawned instance refuses to register a new puppeteer"
      end

      {add_instance(state, instance_key), instance_key}
    end
  end

  @doc false
  def start_link(map_key) do
    GenServer.start_link(__MODULE__, map_key, name: Name.pool(map_key))
  end

  def init(map_key) do
    {:ok, State.new(map_key)}
  end

  def handle_call({:register, puppeteer_key, puppeteer_module}, _from, state) do
    {state, instance_key} = State.find_instance(state, puppeteer_key, puppeteer_module)
    {:reply, instance_key, state}
  end

  def handle_cast({:kill_request, instance_key}, state) do
    if State.supervise?(state, instance_key) do
      if Instance.kill(instance_key) do
        {:noreply, State.remove_instance(state, instance_key)}
      else
        {:noreply, state}
      end
    else
      Logger.warn("[lkn.core]: [Pool(#{inspect state.map_key})] receives a request to kill [Instance(#{inspect instance_key})] but it does not supervise it.")

      {:noreply, state}
    end
  end
end

defmodule Lkn.Core.Pool do
  use Supervisor

  @moduledoc """
  A specialized Supervisor which spawns new Map Instance when required.
  """

  alias Lkn.Core.Name

  @doc false
  def start_link(map_key) do
    Supervisor.start_link(__MODULE__, map_key)
  end

  def init(map_key) do
    children = [
      supervisor(Lkn.Core.Instance.Supervisor,  [map_key]),
      worker(Lkn.Core.Pool.GenServer, [map_key]),
    ]

    supervise(children, strategy: :one_for_all)
  end

  @doc false
  @spec kill_request(L.Map.k, Instance.k) :: :ok
  def kill_request(map_key, instance_key) do
    GenServer.cast(Name.pool(map_key), {:kill_request, instance_key})
  end

  @doc """
  Register a given Puppeteer to an Instance of a given Map.

  If required, the Pool will spawn a new Instance, so the Puppeteer do not have
  to worry about that.  The Pool will return the Instance key.
  """
  @spec register_puppeteer(Lkn.Core.Map.k, Lkn.Core.Puppeteer.k, Lkn.Core.Puppeteer.m) :: Lkn.Core.Instance.k
  def register_puppeteer(map_key, puppeteer_key, puppeteer_module) do
    instance_key = GenServer.call(Name.pool(map_key), {:register, puppeteer_key, puppeteer_module})

    Registry.register(Lkn.Core.Notifier, Name.notify_group(instance_key), puppeteer_key)

    instance_key
  end

  @doc """
  Spwans a new Instances Pool for a given `Lkn.Core.Map`.

  The Map is expected to be alive before this function is called. After that, it
  is possible for a Puppeteer to join an Instance of this Map using
  `register_puppeteer`.

  __Note:__ The Pool lives inside a dedicated Supervisor tree.
  """
  @spec spawn_pool(Lkn.Core.Map.k) :: :ok
  def spawn_pool(map_key) do
    {:ok, _} = Supervisor.start_child(Lkn.Core.Pool.Supervisor, [map_key])
    :ok
  end
end