Refactor code

This commit is contained in:
Mike Cifelli 2024-03-27 14:23:08 -04:00
parent 13e58e406b
commit d726089705
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
2 changed files with 30 additions and 39 deletions

View File

@ -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

View File

@ -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