aboutsummaryrefslogtreecommitdiff
path: root/lib/net/tcpconn.ex
diff options
context:
space:
mode:
Diffstat (limited to 'lib/net/tcpconn.ex')
-rw-r--r--lib/net/tcpconn.ex34
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