Skip to content

Commit

Permalink
add ability to instance socket connection in order to test multiple c…
Browse files Browse the repository at this point in the history
…onnections

Signed-off-by: Connor Rigby <[email protected]>
  • Loading branch information
Connor Rigby committed Jun 28, 2023
1 parent 65dec0d commit ce07fd8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 27 deletions.
18 changes: 9 additions & 9 deletions lib/nerves_hub_link.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ defmodule NervesHubLink do
"""
@spec connected? :: boolean()
def connected?() do
Socket.check_connection(:device)
Socket.check_connection(Socket, :device)
end

def console_connected?() do
Socket.check_connection(:console)
Socket.check_connection(Socket, :console)
end

@doc """
Checks if the device has a socket connection with NervesHub
"""
def socket_connected?() do
Socket.check_connection(:socket)
Socket.check_connection(Socket, :socket)
end

@doc """
Return whether there's currently an active console session
"""
@spec console_active?() :: boolean()
defdelegate console_active?, to: Socket
def console_active?, do: Socket.console_active?(Socket)

@doc """
Current status of the update manager
"""
@spec status :: NervesHubLink.UpdateManager.State.status()
defdelegate status(), to: NervesHubLink.UpdateManager
def status(), do: NervesHubLink.UpdateManager.status(Socket)

@doc """
Restart the socket and device channel
"""
@spec reconnect() :: :ok
defdelegate reconnect(), to: Socket
@spec reconnect_socket() :: :ok
def reconnect_socket(), do: Socket.reconnect_socket(Socket)

@doc """
Send update progress percentage for display in web
"""
@spec send_update_progress(non_neg_integer()) :: :ok
defdelegate send_update_progress(progress), to: Socket
def send_update_progress(progress), do: Socket.send_update_progress(Socket, progress)

@doc """
Send an update status to web
"""
@spec send_update_status(String.t() | atom()) :: :ok
defdelegate send_update_status(status), to: Socket
def send_update_status(status), do: Socket.send_update_status(Socket, status)
end
6 changes: 4 additions & 2 deletions lib/nerves_hub_link/configurator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ defmodule NervesHubLink.Configurator do
params: %{},
remote_iex: false,
socket: [],
ssl: []
ssl: [],
rejoin_after: 5000

@type t() :: %__MODULE__{
connect: boolean(),
Expand All @@ -32,7 +33,8 @@ defmodule NervesHubLink.Configurator do
params: map(),
remote_iex: boolean(),
socket: any(),
ssl: [:ssl.tls_client_option()]
ssl: [:ssl.tls_client_option()],
rejoin_after: pos_integer()
}
end

Expand Down
23 changes: 23 additions & 0 deletions lib/nerves_hub_link/configurators/load_test.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule NervesHubLink.Configurator.LoadTest do
@behaviour NervesHubLink.Configurator

alias NervesHubLink.{Certificate, Configurator.Config}

@impl NervesHubLink.Configurator
def build(%Config{} = config) do
ssl =
config.ssl
|> maybe_add_cacerts()
|> maybe_add_sni(config)

%{config | ssl: ssl}
end

defp maybe_add_cacerts(socket_opts) do
Keyword.put_new(socket_opts, :cacerts, Certificate.ca_certs())
end

defp maybe_add_sni(socket_opts, %{device_api_sni: sni}) do
Keyword.put_new(socket_opts, :server_name_indication, to_charlist(sni))
end
end
34 changes: 18 additions & 16 deletions lib/nerves_hub_link/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,44 @@ defmodule NervesHubLink.Socket do
@console_topic "console"
@device_topic "device"

def start_link(config) do
GenServer.start_link(__MODULE__, config, name: __MODULE__)
def start_link(config, opts \\ [name: __MODULE__]) do
GenServer.start_link(__MODULE__, config, opts)
end

def reconnect() do
GenServer.cast(__MODULE__, :reconnect)
@spec reconnect_socket(GenServer.server()) :: :ok
def reconnect_socket(socket \\ __MODULE__) do
GenServer.cast(socket, :reconnect)
end

def send_update_progress(progress) do
GenServer.cast(__MODULE__, {:send_update_progress, progress})
@spec send_update_progress(GenServer.server()) :: :ok
def send_update_progress(socket \\ __MODULE__, progress) do
GenServer.cast(socket, {:send_update_progress, progress})
end

def send_update_status(status) do
GenServer.cast(__MODULE__, {:send_update_status, status})
@spec send_update_status(GenServer.server(), atom | String.t()) :: :ok
def send_update_status(socket \\ __MODULE__, status) do
GenServer.cast(socket, {:send_update_status, status})
end

def check_connection(type) do
GenServer.call(__MODULE__, {:check_connection, type})
@spec check_connection(GenServer.server(), :console | :device | :socket) :: :ok
def check_connection(socket \\ __MODULE__, type) do
GenServer.call(socket, {:check_connection, type})
end

@doc """
Return whether an IEx or other console session is active
"""
@spec console_active?() :: boolean()
def console_active?() do
GenServer.call(__MODULE__, :console_active?)
@spec console_active?(GenServer.server()) :: boolean()
def console_active?(socket \\ __MODULE__) do
GenServer.call(socket, :console_active?)
end

@impl Slipstream
def init(config) do
rejoin_after = Application.get_env(:nerves_hub_link, :rejoin_after, 5_000)

opts = [
mint_opts: [protocols: [:http1], transport_opts: config.ssl],
uri: config.socket[:url],
rejoin_after_msec: [rejoin_after],
rejoin_after_msec: [config.rejoin_after],
reconnect_after_msec: config.socket[:reconnect_after_msec]
]

Expand Down

0 comments on commit ce07fd8

Please sign in to comment.