Refactor code
This commit is contained in:
parent
13e58e406b
commit
d726089705
|
@ -24,9 +24,9 @@ defmodule Chronoscope.NTS do
|
||||||
name = String.to_atom("#{host}:#{port}")
|
name = String.to_atom("#{host}:#{port}")
|
||||||
|
|
||||||
if GenServer.whereis(name) == nil do
|
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
|
end
|
||||||
|
|
||||||
GenServer.call(name, {:key_establishment, %{host: host, port: port}})
|
GenServer.call(name, :key_establishment)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,13 +5,21 @@ defmodule Chronoscope.NTS.Client do
|
||||||
|
|
||||||
@interval_in_seconds 30
|
@interval_in_seconds 30
|
||||||
|
|
||||||
def start_link(name: name) do
|
def start_link(host: host, port: port, name: name) do
|
||||||
GenServer.start_link(__MODULE__, :ok, name: name)
|
GenServer.start_link(__MODULE__, %{host: host, port: port}, name: name)
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def init(_) do
|
def init(%{host: host, port: port}) do
|
||||||
{:ok, %{}}
|
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
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
@ -20,52 +28,35 @@ defmodule Chronoscope.NTS.Client do
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_call({:key_establishment, server}, _from, state) do
|
def handle_call(:key_establishment, _from, state) do
|
||||||
new_state = update_state(server, state)
|
new_state = update_state(state)
|
||||||
|
|
||||||
{:reply, get_response(server, new_state), new_state}
|
{:reply, new_state.key_establishment_response, new_state}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp update_state(server, state) do
|
defp update_state(state) do
|
||||||
now = DateTime.utc_now()
|
now = DateTime.utc_now()
|
||||||
last_key_establishment = get_last_key_establishment(server, state, now)
|
|
||||||
|
|
||||||
if interval_surpassed?(now, last_key_establishment) do
|
if interval_surpassed?(now, state.last_key_establishment) do
|
||||||
Map.put(state, server, current_data(server, now))
|
Map.merge(state, current_data(state, now))
|
||||||
else
|
else
|
||||||
state
|
state
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_response(server, state) do
|
defp current_data(state, now) 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
|
|
||||||
%{
|
%{
|
||||||
key_establishment_response: {:error, "initializing"},
|
key_establishment_response: server_response(state),
|
||||||
last_key_establishment: DateTime.add(now, -@interval_in_seconds, :second)
|
last_key_establishment: now
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp current_data(server, now) do
|
defp server_response(state) do
|
||||||
response =
|
server = Map.take(state, [:host, :port])
|
||||||
|
|
||||||
Chronoscope.TaskSupervisor
|
Chronoscope.TaskSupervisor
|
||||||
|> Task.Supervisor.async(fn -> KeyEstablishmentClient.key_establishment(server) end)
|
|> Task.Supervisor.async(fn -> KeyEstablishmentClient.key_establishment(server) end)
|
||||||
|> Task.await()
|
|> Task.await()
|
||||||
|
|
||||||
%{
|
|
||||||
key_establishment_response: response,
|
|
||||||
last_key_establishment: now
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp interval_surpassed?(now, last_key_establishment) do
|
defp interval_surpassed?(now, last_key_establishment) do
|
||||||
|
|
Loading…
Reference in New Issue