Skip to content

Commit

Permalink
check time synchronization before socket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
joshk committed May 24, 2024
1 parent b194c38 commit 71949fd
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
10 changes: 6 additions & 4 deletions lib/nerves_hub_link/configurator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ defmodule NervesHubLink.Configurator do
defmodule Config do
defstruct archive_public_keys: [],
connect: true,
connect_wait_for_network: true,
data_path: "/data/nerves-hub",
device_api_host: nil,
device_api_port: 443,
Expand All @@ -25,12 +24,13 @@ defmodule NervesHubLink.Configurator do
request_fwup_public_keys: false,
shared_secret: [],
socket: [],
ssl: []
ssl: [],
wait_for_network: true,
wait_for_time_sync: true

@type t() :: %__MODULE__{
archive_public_keys: [binary()],
connect: boolean(),
connect_wait_for_network: boolean(),
data_path: Path.t(),
device_api_host: String.t(),
device_api_port: String.t(),
Expand All @@ -46,7 +46,9 @@ defmodule NervesHubLink.Configurator do
request_fwup_public_keys: boolean(),
shared_secret: [product_key: String.t(), product_secret: String.t()],
socket: any(),
ssl: [:ssl.tls_client_option()]
ssl: [:ssl.tls_client_option()],
wait_for_network: boolean(),
wait_for_time_sync: boolean()
}
end

Expand Down
63 changes: 48 additions & 15 deletions lib/nerves_hub_link/socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,8 @@ defmodule NervesHubLink.Socket do
|> assign(connected_at: nil)
|> assign(joined_at: nil)

if config.connect_wait_for_network do
schedule_network_availability_check()
{:ok, socket}
else
{:ok, socket, {:continue, :connect}}
end
schedule_network_and_time_check()
{:ok, socket}
end

@impl Slipstream
Expand Down Expand Up @@ -388,15 +384,25 @@ defmodule NervesHubLink.Socket do
end

@impl Slipstream
def handle_info(:connect_check_network_availability, socket) do
case :inet.gethostbyname(to_charlist(socket.assigns.config.device_api_host)) do
{:ok, _} ->
def handle_info(:check_network_and_time, socket) do
cond do
network_connected?(socket) && time_synchronized?(socket) ->
{:noreply, socket, {:continue, :connect}}

_ ->
Logger.info("[NervesHubLink] waiting for network to become available")
schedule_network_availability_check(2_000)
{:noreply, socket}
!network_connected?(socket) && !time_synchronized?(socket) ->
network_check_with_message(
"[NervesHubLink] waiting for network to become available and time to sync",
socket
)

!network_connected?(socket) ->
network_check_with_message(
"[NervesHubLink] waiting for network to become available",
socket
)

!time_synchronized?(socket) ->
network_check_with_message("[NervesHubLink] waiting for time to sync", socket)
end
end

Expand Down Expand Up @@ -447,6 +453,33 @@ defmodule NervesHubLink.Socket do
{:noreply, socket}
end

defp network_connected?(%{assigns: %{config: config}}) do
if config.wait_for_network do
host = to_charlist(config.device_api_host)

case :inet.gethostbyname(host) do
{:ok, _} -> true
_ -> false
end
else
true
end
end

defp time_synchronized?(%{assigns: %{config: config}}) do
if config.wait_for_time_sync do
NervesTime.synchronized?()
else
true
end
end

defp network_check_with_message(msg, socket) do
Logger.info(msg)
schedule_network_and_time_check(2_000)
{:noreply, socket}
end

@impl Slipstream
def handle_topic_close(topic, reason, socket) when reason != :left do
if topic == @device_topic do
Expand Down Expand Up @@ -485,8 +518,8 @@ defmodule NervesHubLink.Socket do
disconnect(socket)
end

defp schedule_network_availability_check(delay \\ 100) do
Process.send_after(self(), :connect_check_network_availability, delay)
defp schedule_network_and_time_check(delay \\ 100) do
Process.send_after(self(), :check_network_and_time, delay)
end

defp handle_join_reply(%{"firmware_url" => url} = update, socket) when is_binary(url) do
Expand Down

0 comments on commit 71949fd

Please sign in to comment.