diff options
author | Alex Auvolat <alex@adnab.me> | 2018-07-04 13:17:26 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-07-04 13:27:36 +0200 |
commit | b234a360dafa0a65d797614aad5a4514570784f8 (patch) | |
tree | c636e7398fc8f3aba43b40c2fe67fc8264c56374 /lib/net/tcpconn.ex | |
parent | ece5faa69da724ddfdee7b92016bac73dbdbff70 (diff) | |
download | shard-b234a360dafa0a65d797614aad5a4514570784f8.tar.gz shard-b234a360dafa0a65d797614aad5a4514570784f8.zip |
Multiple chat rooms (no web UI yet)
Diffstat (limited to 'lib/net/tcpconn.ex')
-rw-r--r-- | lib/net/tcpconn.ex | 34 |
1 files changed, 9 insertions, 25 deletions
diff --git a/lib/net/tcpconn.ex b/lib/net/tcpconn.ex index 301e931..64c85e9 100644 --- a/lib/net/tcpconn.ex +++ b/lib/net/tcpconn.ex @@ -53,10 +53,9 @@ defmodule SNet.TCPConn do addr: addr, port: port } - GenServer.cast(SNet.Manager, {:peer_up, self(), cli_pkey, addr, his_port}) + GenServer.cast(SNet.Manager, {:peer_up, cli_pkey, self(), addr, his_port}) Logger.info "New peer: #{print_id state} at #{inspect addr}:#{port}" - - GenServer.cast(self(), :init_push) + GenServer.cast(self(), :init_pull) {:noreply, state} end @@ -66,8 +65,9 @@ defmodule SNet.TCPConn do {:noreply, state} end - def handle_cast(:init_push, state) do - push_messages(state, nil, 10) + def handle_cast(:init_pull, state) do + id_list = (for {id, _} <- GenServer.call(Shard.Manager, :list), do: id) + send_msg(state, {:interested, id_list}) {:noreply, state} end @@ -102,28 +102,12 @@ defmodule SNet.TCPConn do exit(:normal) end - defp push_messages(state, start, num) do - case GenServer.call(SApp.Chat.Log, {:read, start, num}) do - {:ok, list, rest} -> - send_msg(state, {:info, start, list, rest}) - _ -> nil - end + defp handle_packet({:interested, shards}, state) do + GenServer.cast(Shard.Manager, {:interested, state.his_pkey, self(), shards}) end - defp handle_packet(msg, state) do - # Logger.info "Message: #{inspect msg}" - case msg do - :get_top -> push_messages(state, nil, 10) - {:get, start} -> push_messages(state, start, 20) - {:info, _start, list, rest} -> - if rest != nil and not GenServer.call(SApp.Chat.Log, {:has, rest}) do - send_msg(state, {:get, rest}) - end - spawn_link(fn -> - Process.sleep 1000 - GenServer.cast(SApp.Chat.Log, {:insert_many, list, &SApp.Chat.msg_callback/1}) - end) - end + defp handle_packet({shard, msg}, state) do + GenServer.cast(Shard.Manager, {:dispatch, state.his_pkey, self(), shard, msg}) end defp print_id(state) do |