-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* WIP - linking to connections and determening cause * WIP - testing notification and kademlia route * fixing test because of new peer supervisor interface * remove vscode folder ignore * code style and format * adding is outbound to log
- Loading branch information
Ino Murko
authored
Dec 4, 2018
1 parent
cfe6055
commit 8bbacfa
Showing
18 changed files
with
267 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
defmodule ExWire.ConnectionObserver do | ||
@moduledoc """ | ||
Observing the inbound and outbound connections and gets notified if a connection gets dropped. | ||
It also stores all Peers (both inbound and outbound) for further use. | ||
""" | ||
use GenServer | ||
alias ExWire.Config | ||
alias ExWire.Kademlia | ||
alias ExWire.P2P.Connection | ||
alias ExWire.PeerSupervisor | ||
alias ExWire.Struct.Peer | ||
require Logger | ||
|
||
@type t :: %__MODULE__{ | ||
outbound_peers: MapSet.t(Peer.t()), | ||
inbound_peers: MapSet.t(Peer.t()) | ||
} | ||
defstruct outbound_peers: MapSet.new(), inbound_peers: MapSet.new() | ||
|
||
@kademlia Application.get_env(:ex_wire, :kademlia_mock, Kademlia) | ||
|
||
def notify(:discovery_round) do | ||
GenServer.cast(__MODULE__, :kademlia_discovery_round) | ||
end | ||
|
||
@doc """ | ||
Starts the observer process. | ||
""" | ||
@spec start_link(:ok) :: GenServer.on_start() | ||
def start_link(:ok) do | ||
GenServer.start_link(__MODULE__, %{}, name: __MODULE__) | ||
end | ||
|
||
def init(_) do | ||
Process.flag(:trap_exit, true) | ||
{:ok, %__MODULE__{}} | ||
end | ||
|
||
# getting exits of connections from us to them | ||
def handle_info( | ||
{:EXIT, _pid, {_, %Connection{is_outbound: true, peer: peer}}}, | ||
state | ||
) do | ||
:ok = start_new_outbound_connections() | ||
{:noreply, %{state | outbound_peers: MapSet.put(state.outbound_peers, peer)}} | ||
end | ||
|
||
# getting exits of connections from them to us | ||
def handle_info( | ||
{:EXIT, _pid, {_, %Connection{is_outbound: false, peer: peer}}}, | ||
state | ||
) do | ||
{:noreply, %{state | inbound_peers: MapSet.put(state.inbound_peers, peer)}} | ||
end | ||
|
||
# Kademlia server process notifies us of their discovery round. | ||
# We start new connections to peers from the discovery results. | ||
def handle_cast(:kademlia_discovery_round, state) do | ||
:ok = start_new_outbound_connections() | ||
|
||
{:noreply, state} | ||
end | ||
|
||
@spec start_new_outbound_connections() :: :ok | ||
defp start_new_outbound_connections() do | ||
this_round_nodes = @kademlia.get_peers() | ||
|
||
_ = | ||
if Config.perform_sync?() do | ||
for node <- this_round_nodes do | ||
node | ||
|> Peer.from_node() | ||
|> PeerSupervisor.new_peer(__MODULE__) | ||
end | ||
end | ||
|
||
:ok | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.