Skip to content

Commit

Permalink
Update client connection when receive a request
Browse files Browse the repository at this point in the history
  • Loading branch information
Neylix committed Jul 8, 2022
1 parent f1ab364 commit 1e52172
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/archethic/p2p/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ defmodule Archethic.P2P.Client do
{:ok, Message.response()}
| {:error, :timeout}
| {:error, :closed}

@callback set_connected(Crypto.key()) :: :ok
end
49 changes: 49 additions & 0 deletions lib/archethic/p2p/client/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,22 @@ defmodule Archethic.P2P.Client.Connection do
end
end

@doc """
Set the sate to connected
"""
@spec set_connected(Crypto.key()) :: :ok
def set_connected(public_key) do
GenStateMachine.cast(via_tuple(public_key), {:connect, self()})

receive do
:ok ->
:ok
after
500 ->
:ok
end
end

# fetch cnnoection details from registery for a node from its public key
defp via_tuple(public_key), do: {:via, Registry, {ConnectionRegistry, public_key}}

Expand Down Expand Up @@ -127,6 +143,39 @@ defmodule Archethic.P2P.Client.Connection do
{:keep_state_and_data, actions}
end

def handle_event({:timeout, :reconnect}, _event_data, {:connected, _socket}, _data) do
:keep_state_and_data
end

def handle_event(
:cast,
{:connect, from},
state,
data = %{
ip: ip,
port: port,
transport: transport
}
) do
next_state =
case state do
:disconnected ->
case transport.handle_connect(ip, port) do
{:ok, socket} ->
{:next_state, {:connected, socket}, data}

{:error, _reason} ->
:keep_state_and_data
end

_ ->
:keep_state_and_data
end

send(from, :ok)
next_state
end

def handle_event(:cast, {:send_message, ref, from, _msg, _timeout}, :disconnected, _data) do
send(from, {ref, {:error, :closed}})
:keep_state_and_data
Expand Down
9 changes: 9 additions & 0 deletions lib/archethic/p2p/client/default_impl.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,13 @@ defmodule Archethic.P2P.Client.DefaultImpl do
end
end
end

@doc """
When receiving a message from a node, we set it connected
"""
@impl Client
@spec set_connected(Crypto.key()) :: :ok
def set_connected(node_public_key) do
Connection.set_connected(node_public_key)
end
end
2 changes: 2 additions & 0 deletions lib/archethic/p2p/listener_protocol/broadway_pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule Archethic.P2P.ListenerProtocol.BroadwayPipeline do
alias Archethic.P2P.MemTable
alias Archethic.P2P.Message
alias Archethic.P2P.MessageEnvelop
alias Archethic.P2P.Client

alias Broadway.Message, as: BroadwayMessage

Expand Down Expand Up @@ -59,6 +60,7 @@ defmodule Archethic.P2P.ListenerProtocol.BroadwayPipeline do
} = MessageEnvelop.decode(data)

MemTable.increase_node_availability(sender_public_key)
Client.set_connected(sender_public_key)
{System.monotonic_time(:millisecond), message_id, message, sender_public_key}
end

Expand Down

0 comments on commit 1e52172

Please sign in to comment.