diff options
Diffstat (limited to 'lib/app/chat.ex')
-rw-r--r-- | lib/app/chat.ex | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/lib/app/chat.ex b/lib/app/chat.ex index e86f739..fe2777e 100644 --- a/lib/app/chat.ex +++ b/lib/app/chat.ex @@ -14,6 +14,7 @@ defmodule SApp.Chat do - use a DHT to find peers that are interested in this channel - epidemic broadcast (carefull not to be too costly, maybe by limiting the number of peers we talk to) + - partial synchronization only == data distributed over peers """ use GenServer @@ -104,15 +105,17 @@ defmodule SApp.Chat do """ def handle_cast({:msg, peer_id, peer_pid, msg}, state) do case msg do + {:get_manifest} -> send(state, peer_pid, {:manifest, state.manifest}) {:get, start} -> push_messages(peer_id, state, start, 20) {:info, _start, list, rest} -> if rest != nil and not GenServer.call(state.store, {:has, rest}) do - GenServer.cast(peer_pid, {:send_msg, {state.id, {:get, rest}}}) + send(state, peer_pid, {:get, rest}) end spawn_link(fn -> Process.sleep 1000 GenServer.cast(state.store, {:insert_many, list, (fn msg -> msg_callback(state.channel, msg) end)}) end) + _ -> nil end if Map.has_key?(state.peers, peer_id) do @@ -122,11 +125,15 @@ defmodule SApp.Chat do end end + defp send(state, to, msg) do + GenServer.cast(to, {:send_msg, {state.id, msg}}) + end + defp push_messages(state, to, start, num) do case GenServer.call(state.store, {:read, start, num}) do {:ok, list, rest} -> - GenServer.cast(to, {:send_msg, {state.id, {:info, start, list, rest}}}) + send(state, to, {:info, start, list, rest}) _ -> nil end end |