diff options
Diffstat (limited to 'shard/lib/app/chat.ex')
-rw-r--r-- | shard/lib/app/chat.ex | 87 |
1 files changed, 36 insertions, 51 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 |