From 981f15fe87bf268a55e5caabbc2f4e4d81cae873 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 31 Aug 2018 17:51:30 +0200 Subject: Some refactoring, more functionality for the manager --- lib/net/tcpconn.ex | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) (limited to 'lib/net/tcpconn.ex') 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) -- cgit v1.2.3