diff options
author | Alex Auvolat <alex@adnab.me> | 2018-10-15 12:18:05 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-10-15 12:18:05 +0200 |
commit | 8c49dd71d29359447c24b1cd4f48a8faf0c4fdca (patch) | |
tree | e2d8b61139d6b72e6abfda6277918f54fdae36ef /shard/lib/app | |
parent | 7a9678843647de930885792149b279ef105f67b6 (diff) | |
download | shard-8c49dd71d29359447c24b1cd4f48a8faf0c4fdca.tar.gz shard-8c49dd71d29359447c24b1cd4f48a8faf0c4fdca.zip |
Refactor shard starting/stopping
Diffstat (limited to 'shard/lib/app')
-rw-r--r-- | shard/lib/app/chat.ex | 87 | ||||
-rw-r--r-- | shard/lib/app/identity.ex | 45 | ||||
-rw-r--r-- | shard/lib/app/pagestore.ex | 2 |
3 files changed, 56 insertions, 78 deletions
diff --git a/shard/lib/app/chat.ex b/shard/lib/app/chat.ex index 2e5e076..d253030 100644 --- a/shard/lib/app/chat.ex +++ b/shard/lib/app/chat.ex @@ -36,15 +36,12 @@ defmodule SApp.Chat do """ defstruct [:channel] - end - defimpl Shard.Manifest, for: Manifest do - def start(m) do - DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Chat, m}) + defimpl Shard.Manifest do + def module(_m), do: SApp.Chat end end - defmodule PrivChat.Manifest do @moduledoc""" Manifest for a private chat room defined by the list of participants. @@ -60,11 +57,9 @@ defmodule SApp.Chat do def new(pk_list) do %__MODULE__{pk_list: pk_list |> Enum.sort |> Enum.uniq} end - end - defimpl Shard.Manifest, for: PrivChat.Manifest do - def start(m) do - DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Chat, m}) + defimpl Shard.Manifest do + def module(_m), do: SApp.Chat end end @@ -85,44 +80,39 @@ defmodule SApp.Chat do def init(manifest) do id = SData.term_hash manifest - case Shard.Manager.register(id, manifest, self()) do - :ok -> - netgroup = case manifest do - %Manifest{channel: _channel} -> - %SNet.PubShardGroup{id: id} - %PrivChat.Manifest{pk_list: pk_list} -> - %SNet.PrivGroup{pk_list: pk_list} - end - Shard.Manager.dispatch_to(id, nil, self()) - {:ok, page_store} = SApp.PageStore.start_link(id, :page_store, netgroup) - {root, read} = case Shard.Manager.load_state id do - %{root: root, read: read} -> {root, read} - _ -> {nil, nil} - end - root = cond do - root == nil -> nil - GenServer.call(page_store, {:have_rec, root}) -> root - true -> - Logger.warn "Not all pages for saved root were saved, restarting from an empty state!" - nil - end - mst = %MST{store: %SApp.PageStore{pid: page_store}, - cmp: &msg_cmp/2, - root: root} - SNet.Group.init_lookup(netgroup, self()) - {:ok, - %{id: id, - netgroup: netgroup, - manifest: manifest, - page_store: page_store, - mst: mst, - subs: MapSet.new, - read: read, - } - } - :redundant -> - exit(:redundant) + netgroup = case manifest do + %Manifest{channel: _channel} -> + %SNet.PubShardGroup{id: id} + %PrivChat.Manifest{pk_list: pk_list} -> + %SNet.PrivGroup{pk_list: pk_list} + end + Shard.Manager.dispatch_to(id, nil, self()) + {:ok, page_store} = SApp.PageStore.start_link(id, :page_store, netgroup) + {root, read} = case Shard.Manager.load_state id do + %{root: root, read: read} -> {root, read} + _ -> {nil, nil} end + root = cond do + root == nil -> nil + GenServer.call(page_store, {:have_rec, root}) -> root + true -> + Logger.warn "Not all pages for saved root were saved, restarting from an empty state!" + nil + end + mst = %MST{store: %SApp.PageStore{pid: page_store}, + cmp: &msg_cmp/2, + root: root} + SNet.Group.init_lookup(netgroup, self()) + {:ok, + %{id: id, + netgroup: netgroup, + manifest: manifest, + page_store: page_store, + mst: mst, + subs: MapSet.new, + read: read, + } + } end @doc """ @@ -215,11 +205,6 @@ defmodule SApp.Chat do @doc """ Implementation of the :msg handler, which is the main handler for messages comming from other peers concerning this chat room. - - Messages are: - - `{:get, start}`: get some messages starting at a given Merkle hash - - `{:info, start, list, rest}`: put some messages and informs of the - Merkle hash of the store of older messages. """ def handle_cast({:msg, conn_pid, auth, _shard_id, nil, msg}, state) do if not SNet.Group.in_group?(state.netgroup, conn_pid, auth) do diff --git a/shard/lib/app/identity.ex b/shard/lib/app/identity.ex index 42d1bf8..bacfdab 100644 --- a/shard/lib/app/identity.ex +++ b/shard/lib/app/identity.ex @@ -24,11 +24,9 @@ defmodule SApp.Identity do """ defstruct [:pk] - end - defimpl Shard.Manifest, for: Manifest do - def start(m) do - DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SApp.Identity, m.pk}) + defimpl Shard.Manifest do + def module(_m), do: SApp.Identity end end @@ -36,33 +34,28 @@ defmodule SApp.Identity do defstruct [:info, :rev, :signed] end - def start_link(pk) do - GenServer.start_link(__MODULE__, pk) + def start_link(manifest) do + GenServer.start_link(__MODULE__, manifest) end - def init(pk) do - manifest = %Manifest{pk: pk} + def init(manifest) do + %Manifest{pk: pk} = manifest id = SData.term_hash manifest - case Shard.Manager.register(id, manifest, self()) do - :ok -> - Shard.Manager.dispatch_to(id, nil, self()) - state = case Shard.Manager.load_state(id) do - nil -> - info = %{nick: default_nick(pk)} - SData.SignRev.new info - st -> - st - end - netgroup = %SNet.PubShardGroup{id: id} - SNet.Group.init_lookup(netgroup, self()) - if Shard.Keys.have_sk? pk do - GenServer.cast(self(), :update_peer_info) - end - {:ok, %{pk: pk, id: id, state: state, netgroup: netgroup}} - :redundant -> - exit(:redundant) + Shard.Manager.dispatch_to(id, nil, self()) + state = case Shard.Manager.load_state(id) do + nil -> + info = %{nick: default_nick(pk)} + SData.SignRev.new info + st -> + st + end + netgroup = %SNet.PubShardGroup{id: id} + SNet.Group.init_lookup(netgroup, self()) + if Shard.Keys.have_sk? pk do + GenServer.cast(self(), :update_peer_info) end + {:ok, %{pk: pk, id: id, state: state, netgroup: netgroup}} end def handle_call(:manifest, _from, state) do diff --git a/shard/lib/app/pagestore.ex b/shard/lib/app/pagestore.ex index 8f6be59..5165b84 100644 --- a/shard/lib/app/pagestore.ex +++ b/shard/lib/app/pagestore.ex @@ -125,7 +125,7 @@ defmodule SApp.PageStore do def handle_cast({:msg, conn_pid, auth, _shard_id, _path, msg}, state) do if not SNet.Group.in_group?(state.netgroup, conn_pid, auth) do - state + {:noreply, state} else state = case msg do {:get, key} -> |