diff options
author | Alex Auvolat <alex@adnab.me> | 2018-08-31 17:51:30 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2018-08-31 17:51:30 +0200 |
commit | 981f15fe87bf268a55e5caabbc2f4e4d81cae873 (patch) | |
tree | 0a94e5efca9c46355a026f72a5b0c7e647019522 /lib/net/tcpconn.ex | |
parent | 9d161648cc0b30dd9e0ead12f3bd5a379b26c815 (diff) | |
download | shard-981f15fe87bf268a55e5caabbc2f4e4d81cae873.tar.gz shard-981f15fe87bf268a55e5caabbc2f4e4d81cae873.zip |
Some refactoring, more functionality for the manager
Diffstat (limited to 'lib/net/tcpconn.ex')
-rw-r--r-- | lib/net/tcpconn.ex | 28 |
1 files changed, 5 insertions, 23 deletions
diff --git a/lib/net/tcpconn.ex b/lib/net/tcpconn.ex index 7d82601..6fc4b1a 100644 --- a/lib/net/tcpconn.ex +++ b/lib/net/tcpconn.ex @@ -56,19 +56,14 @@ defmodule SNet.TCPConn do } GenServer.cast(Shard.Manager, {:peer_up, cli_pkey, self(), addr, his_port}) Logger.info "New peer: #{print_id state} at #{inspect addr}:#{port}" - GenServer.cast(self(), :init_pull) {:noreply, state} end def handle_cast({:send_msg, msg}, state) do - send_msg(state, msg) - {:noreply, state} - end - - def handle_cast(:init_pull, state) do - id_list = (for {id, _, _} <- :ets.tab2list(:shard_db), do: id) - send_msg(state, {:interested, id_list}) + msgbin = :erlang.term_to_binary msg + enc = encode_pkt(msgbin, state.conn_his_pkey, state.conn_my_skey) + :gen_tcp.send(state.socket, enc) {:noreply, state} end @@ -85,15 +80,10 @@ defmodule SNet.TCPConn do msg end - defp send_msg(state, msg) do - msgbin = :erlang.term_to_binary msg - enc = encode_pkt(msgbin, state.conn_his_pkey, state.conn_my_skey) - :gen_tcp.send(state.socket, enc) - end - def handle_info({:tcp, _socket, raw_data}, state) do msg = decode_pkt(raw_data, state.conn_his_pkey, state.conn_my_skey) - handle_packet(:erlang.binary_to_term(msg, [:safe]), state) + msg_data = :erlang.binary_to_term(msg, [:safe]) + Shard.Manager.dispatch(state.his_pkey, msg_data) {:noreply, state} end @@ -103,14 +93,6 @@ defmodule SNet.TCPConn do exit(:normal) end - defp handle_packet({:interested, shards}, state) do - GenServer.cast(Shard.Manager, {:interested, state.his_pkey, shards}) - end - - defp handle_packet({shard, msg}, state) do - Shard.Manager.dispatch(state.his_pkey, shard, msg) - end - defp print_id(state) do state.his_pkey |> binary_part(0, 8) |