2024-05-28 18:27:02 -04:00
|
|
|
defmodule ChronoscopeWeb.IndexLive do
|
|
|
|
use ChronoscopeWeb, :live_view
|
|
|
|
|
2024-05-29 15:25:05 -04:00
|
|
|
alias Chronoscope.NTS
|
2024-06-01 10:30:35 -04:00
|
|
|
alias Chronoscope.NTS.KeyEstablishmentResponse
|
2024-06-05 14:21:26 -04:00
|
|
|
alias ChronoscopeWeb.ClientActivator
|
2024-05-29 15:25:05 -04:00
|
|
|
|
2024-06-04 20:03:02 -04:00
|
|
|
@topic Application.compile_env(:chronoscope, :nts_topic)
|
2024-05-28 18:27:02 -04:00
|
|
|
|
|
|
|
def mount(_params, _session, socket) do
|
|
|
|
ChronoscopeWeb.Endpoint.subscribe(@topic)
|
2024-06-05 13:45:01 -04:00
|
|
|
{:ok, assign(socket, %{servers: server_list(), clients: client_list()})}
|
2024-05-28 18:27:02 -04:00
|
|
|
end
|
|
|
|
|
2024-06-05 12:23:25 -04:00
|
|
|
def handle_info(%{topic: @topic, event: "key-exchange", payload: client}, socket) do
|
2024-06-05 13:45:01 -04:00
|
|
|
if client.server in socket.assigns.servers do
|
|
|
|
{:noreply, update(socket, :clients, &update_client(&1, client))}
|
|
|
|
else
|
|
|
|
{:noreply, socket}
|
|
|
|
end
|
2024-06-04 20:03:02 -04:00
|
|
|
end
|
|
|
|
|
2024-06-05 12:23:25 -04:00
|
|
|
defp update_client(client_list, client) do
|
|
|
|
Enum.map(client_list, &if(client.server == &1.server, do: client, else: &1))
|
|
|
|
end
|
|
|
|
|
2024-06-05 13:45:01 -04:00
|
|
|
defp server_list() do
|
2024-06-05 14:21:26 -04:00
|
|
|
GenServer.call(ClientActivator, :get_nts_servers)
|
2024-06-05 13:45:01 -04:00
|
|
|
end
|
|
|
|
|
2024-06-05 12:23:25 -04:00
|
|
|
defp client_list() do
|
2024-06-05 13:45:01 -04:00
|
|
|
server_list()
|
2024-06-04 20:03:02 -04:00
|
|
|
|> NTS.list_clients()
|
2024-05-28 18:27:02 -04:00
|
|
|
end
|
|
|
|
end
|