diff --git a/lib/chronoscope/nts.ex b/lib/chronoscope/nts.ex index 06d7798..0f56ea7 100644 --- a/lib/chronoscope/nts.ex +++ b/lib/chronoscope/nts.ex @@ -24,9 +24,9 @@ defmodule Chronoscope.NTS do name = String.to_atom("#{host}:#{port}") if GenServer.whereis(name) == nil do - DynamicSupervisor.start_child(Chronoscope.DynamicSupervisor, {NTS.Client, name: name}) + DynamicSupervisor.start_child(Chronoscope.DynamicSupervisor, {NTS.Client, host: host, port: port, name: name}) end - GenServer.call(name, {:key_establishment, %{host: host, port: port}}) + GenServer.call(name, :key_establishment) end end diff --git a/lib/chronoscope/nts/client.ex b/lib/chronoscope/nts/client.ex index ca2b66b..1752435 100644 --- a/lib/chronoscope/nts/client.ex +++ b/lib/chronoscope/nts/client.ex @@ -5,13 +5,21 @@ defmodule Chronoscope.NTS.Client do @interval_in_seconds 30 - def start_link(name: name) do - GenServer.start_link(__MODULE__, :ok, name: name) + def start_link(host: host, port: port, name: name) do + GenServer.start_link(__MODULE__, %{host: host, port: port}, name: name) end @impl true - def init(_) do - {:ok, %{}} + def init(%{host: host, port: port}) do + now = DateTime.utc_now() + + {:ok, + %{ + host: host, + port: port, + key_establishment_response: {:error, "initializing"}, + last_key_establishment: DateTime.add(now, -@interval_in_seconds, :second) + }} end @impl true @@ -20,54 +28,37 @@ defmodule Chronoscope.NTS.Client do end @impl true - def handle_call({:key_establishment, server}, _from, state) do - new_state = update_state(server, state) + def handle_call(:key_establishment, _from, state) do + new_state = update_state(state) - {:reply, get_response(server, new_state), new_state} + {:reply, new_state.key_establishment_response, new_state} end - defp update_state(server, state) do + defp update_state(state) do now = DateTime.utc_now() - last_key_establishment = get_last_key_establishment(server, state, now) - if interval_surpassed?(now, last_key_establishment) do - Map.put(state, server, current_data(server, now)) + if interval_surpassed?(now, state.last_key_establishment) do + Map.merge(state, current_data(state, now)) else state end end - defp get_response(server, state) do - state - |> Map.get(server) - |> Map.get(:key_establishment_response) - end - - defp get_last_key_establishment(server, state, now) do - state - |> Map.get(server, initial_data(now)) - |> Map.get(:last_key_establishment) - end - - defp initial_data(now) do + defp current_data(state, now) do %{ - key_establishment_response: {:error, "initializing"}, - last_key_establishment: DateTime.add(now, -@interval_in_seconds, :second) - } - end - - defp current_data(server, now) do - response = - Chronoscope.TaskSupervisor - |> Task.Supervisor.async(fn -> KeyEstablishmentClient.key_establishment(server) end) - |> Task.await() - - %{ - key_establishment_response: response, + key_establishment_response: server_response(state), last_key_establishment: now } end + defp server_response(state) do + server = Map.take(state, [:host, :port]) + + Chronoscope.TaskSupervisor + |> Task.Supervisor.async(fn -> KeyEstablishmentClient.key_establishment(server) end) + |> Task.await() + end + defp interval_surpassed?(now, last_key_establishment) do DateTime.diff(now, last_key_establishment, :second) >= @interval_in_seconds end