Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to instance socket connection in order to test multiple connections #127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason for this change? This could be considered breaking since reconnect() has been around a while

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah use Slipstream imports a reconnect/2 function. We could make NervesHubLink.reconnect stay the same while the socket module remains the new function.


@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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def send_update_status(status), do: Socket.send_update_status(Socket, status)
defdelegate send_update_status(socket \\ Socket, status), to: Socket

I'm pretty sure you can delegate the default and it would work with dialyzer as well

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
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) :: boolean()
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