aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lkn/core/puppeteer.ex
blob: 5044865e134607212a2d1268cf5ff11d767c5310 (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
#
# 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

  @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 __using__(state: state_t) do
    quote do
      use Lkn.Prelude
      use GenServer

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

      @type state :: unquote(state_t)

      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:         unquote(state_t),
        }

        @spec new(Puppeteer.t, unquote(state_t)) :: 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({: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({:specific, msg}, state) do
        s2 = puppeteer_handle_cast(msg, state.state)
        {:noreply, %State{state|state: s2}}
      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 puppeteer_handle_cast(_msg, state) do
        state
      end

      def handle_info(msg, state) do
        state = %State{state|state: handle_message(state.state, msg)}

        {:noreply, state}
      end

      def cast(puppeteer_key, msg) do
        GenServer.cast(Name.puppeteer(puppeteer_key), {:specific, msg})
      end

      @behaviour Lkn.Core.Puppeteer

      defoverridable [
        puppeteer_handle_cast: 2,
      ]
    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 handle_message(state, any) :: state
  @callback init_state(init_args) :: {:ok, state}|:error
  @callback leave_instance(state, Instance.k) :: 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
end