Refactor code
This commit is contained in:
parent
a07ce6e410
commit
113b1ad2d6
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue