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

check time synchronization before socket connection #195

Closed
wants to merge 1 commit into from
Closed
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
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_preconnect_checks()
{: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(:preconnect_checks, socket) do
network? = network_connected?(socket)
time? = time_synchronized?(socket)

cond do
network? && time? ->
{:noreply, socket, {:continue, :connect}}

_ ->
Logger.info("[NervesHubLink] waiting for network to become available")
schedule_network_availability_check(2_000)
{:noreply, socket}
!network? ->
log_and_reschedule_checks(
"[NervesHubLink] waiting for network to become available",
socket
)

!time? ->
log_and_reschedule_checks(
"[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 log_and_reschedule_checks(msg, socket) do
Logger.info(msg)
schedule_preconnect_checks(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_preconnect_checks(delay \\ 100) do
Process.send_after(self(), :preconnect_checks, delay)
end

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