aboutsummaryrefslogtreecommitdiff
path: root/lib/net/manager.ex
blob: 4b1ce9486109a1c7fae32f6a39e382b845c0e3b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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