Refactor code

This commit is contained in:
Mike Cifelli 2024-05-09 11:06:47 -04:00
parent a07ce6e410
commit 113b1ad2d6
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
3 changed files with 12 additions and 8 deletions

View File

@ -43,18 +43,21 @@ defmodule Chronoscope.Gemini.ConnectionClient do
:ok = @ssl.send(socket, Request.create(url)) :ok = @ssl.send(socket, Request.create(url))
{:ok, peercert} = @ssl.peercert(socket) {:ok, peercert} = @ssl.peercert(socket)
peercert
|> await_response()
|> tap(fn _ -> @ssl.close(socket) end)
end
defp await_response(peercert) do
receive do receive do
{:ssl, _socket, response} -> {:ssl, _socket, response} ->
@ssl.close(socket)
parse_response(response, peercert) parse_response(response, peercert)
msg -> msg ->
@ssl.close(socket)
Logger.error("received unexpected message: #{inspect(msg)}") Logger.error("received unexpected message: #{inspect(msg)}")
{:error, :no_response} {:error, :no_response}
after after
@timeout_in_milliseconds -> @timeout_in_milliseconds ->
@ssl.close(socket)
Logger.error("timed out waiting for response") Logger.error("timed out waiting for response")
{:error, :timeout} {:error, :timeout}
end end

View File

@ -2,7 +2,6 @@ defmodule Chronoscope.NTS.Behaviour do
@callback(key_establishment(host :: String.t(), port :: integer()) :: {:ok, Map.t()}, {:error, any()}) @callback(key_establishment(host :: String.t(), port :: integer()) :: {:ok, Map.t()}, {:error, any()})
end end
# TODO - create macro
defmodule Chronoscope.NTS do defmodule Chronoscope.NTS do
@behaviour Chronoscope.NTS.Behaviour @behaviour Chronoscope.NTS.Behaviour

View File

@ -36,19 +36,21 @@ defmodule Chronoscope.NTS.KeyEstablishmentClient do
:ok = @ssl.send(socket, KeyEstablishmentRequest.create()) :ok = @ssl.send(socket, KeyEstablishmentRequest.create())
{:ok, peercert} = @ssl.peercert(socket) {:ok, peercert} = @ssl.peercert(socket)
# TODO - refactor? peercert
|> await_response()
|> tap(fn _ -> @ssl.close(socket) end)
end
defp await_response(peercert) do
receive do receive do
{:ssl, _socket, response} -> {:ssl, _socket, response} ->
@ssl.close(socket)
parse_response(response, peercert) parse_response(response, peercert)
msg -> msg ->
@ssl.close(socket)
Logger.error("received unexpected message: #{inspect(msg)}") Logger.error("received unexpected message: #{inspect(msg)}")
{:error, :no_response} {:error, :no_response}
after after
@timeout_in_milliseconds -> @timeout_in_milliseconds ->
@ssl.close(socket)
Logger.error("timed out waiting for response") Logger.error("timed out waiting for response")
{:error, :timeout} {:error, :timeout}
end end