From 058bab0d7097405126566360308ace986c18ff8e Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 19 Jul 2018 17:08:23 +0200 Subject: Refactoring ; template for block store --- lib/net/manager.ex | 65 ------------------------------------------------------ lib/net/tcpconn.ex | 10 ++++----- 2 files changed, 5 insertions(+), 70 deletions(-) delete mode 100644 lib/net/manager.ex (limited to 'lib/net') diff --git a/lib/net/manager.ex b/lib/net/manager.ex deleted file mode 100644 index 4b1ce94..0000000 --- a/lib/net/manager.ex +++ /dev/null @@ -1,65 +0,0 @@ -defmodule SNet.Manager do - use GenServer - - def start_link(my_port) do - GenServer.start_link(__MODULE__, my_port, name: __MODULE__) - end - - def init(my_port) do - state = %{ - peers: %{}, - my_port: my_port - } - {:ok, state} - end - - def handle_cast({:peer_up, pk, pid, addr, ip}, state) do - new_peers = Map.put(state.peers, pk, {pid, addr, ip}) - new_state = %{ state | peers: new_peers } - {:noreply, new_state} - end - - def handle_cast({:peer_down, pk, addr, ip}, state) do - new_peers = Map.put(state.peers, pk, {nil, addr, ip}) - new_state = %{ state | peers: new_peers } - {:noreply, new_state} - end - - def handle_cast({:add_peer, ip, port}, state) do - add_peer(ip, port, state.my_port) - {:noreply, state} - end - - def handle_cast({:try_connect, pk_list}, state) do - for pk <- pk_list do - case state.peers[pk] do - {nil, ip, port} -> add_peer(ip, port) - _ -> nil - end - end - {:noreply, state} - end - - def handle_call(:get_all, _from, state) do - pid_list = (for {_, {pid, _, _}} <- state.peers, pid != nil, do: pid) - {:reply, pid_list, state} - end - - def handle_call({:get_connections, pk_list}, _from, state) do - pid_list = (for pk <- pk_list, Map.has_key?(state.peers, pk), do: state.peers[pk]) - |> Enum.map(fn {pid, _, _} -> pid end) - |> Enum.filter(&(&1 != nil)) - {:ok, pid_list, state} - end - - def add_peer(ip, port) do - GenServer.cast(__MODULE__, {:add_peer, ip, port}) - end - - def add_peer(ip, port, my_port) do - {:ok, client} = :gen_tcp.connect(ip, port, [:binary, packet: 2, active: false]) - {:ok, pid} = DynamicSupervisor.start_child(Shard.DynamicSupervisor, {SNet.TCPConn, %{socket: client, my_port: my_port}}) - :ok = :gen_tcp.controlling_process(client, pid) - pid - end -end diff --git a/lib/net/tcpconn.ex b/lib/net/tcpconn.ex index 64c85e9..a16a62e 100644 --- a/lib/net/tcpconn.ex +++ b/lib/net/tcpconn.ex @@ -53,7 +53,7 @@ defmodule SNet.TCPConn do addr: addr, port: port } - GenServer.cast(SNet.Manager, {:peer_up, cli_pkey, self(), addr, his_port}) + 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) @@ -66,7 +66,7 @@ defmodule SNet.TCPConn do end def handle_cast(:init_pull, state) do - id_list = (for {id, _} <- GenServer.call(Shard.Manager, :list), do: id) + id_list = (for {id, _, _} <- :ets.tab2list(:shard_db), do: id) send_msg(state, {:interested, id_list}) {:noreply, state} end @@ -98,16 +98,16 @@ defmodule SNet.TCPConn do def handle_info({:tcp_closed, _socket}, state) do Logger.info "Disconnected: #{print_id state} at #{inspect state.addr}:#{state.port}" - GenServer.cast(SNet.Manager, {:peer_down, state.his_pkey, state.addr, state.port}) + GenServer.cast(Shard.Manager, {:peer_down, state.his_pkey, state.addr, state.port}) exit(:normal) end defp handle_packet({:interested, shards}, state) do - GenServer.cast(Shard.Manager, {:interested, state.his_pkey, self(), shards}) + GenServer.cast(Shard.Manager, {:interested, state.his_pkey, shards}) end defp handle_packet({shard, msg}, state) do - GenServer.cast(Shard.Manager, {:dispatch, state.his_pkey, self(), shard, msg}) + Shard.Manager.dispatch(state.his_pkey, shard, msg) end defp print_id(state) do -- cgit v1.2.3