From 113b1ad2d6a38723460a142d2182bf8b952807c5 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Thu, 9 May 2024 11:06:47 -0400 Subject: [PATCH] Refactor code --- lib/chronoscope/gemini/connection_client.ex | 9 ++++++--- lib/chronoscope/nts.ex | 1 - lib/chronoscope/nts/key_establishment_client.ex | 10 ++++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lib/chronoscope/gemini/connection_client.ex b/lib/chronoscope/gemini/connection_client.ex index c5406c0..a7032be 100644 --- a/lib/chronoscope/gemini/connection_client.ex +++ b/lib/chronoscope/gemini/connection_client.ex @@ -43,18 +43,21 @@ defmodule Chronoscope.Gemini.ConnectionClient do :ok = @ssl.send(socket, Request.create(url)) {:ok, peercert} = @ssl.peercert(socket) + peercert + |> await_response() + |> tap(fn _ -> @ssl.close(socket) end) + end + + defp await_response(peercert) do receive do {:ssl, _socket, response} -> - @ssl.close(socket) parse_response(response, peercert) msg -> - @ssl.close(socket) Logger.error("received unexpected message: #{inspect(msg)}") {:error, :no_response} after @timeout_in_milliseconds -> - @ssl.close(socket) Logger.error("timed out waiting for response") {:error, :timeout} end diff --git a/lib/chronoscope/nts.ex b/lib/chronoscope/nts.ex index 5503a24..5866dde 100644 --- a/lib/chronoscope/nts.ex +++ b/lib/chronoscope/nts.ex @@ -2,7 +2,6 @@ defmodule Chronoscope.NTS.Behaviour do @callback(key_establishment(host :: String.t(), port :: integer()) :: {:ok, Map.t()}, {:error, any()}) end -# TODO - create macro defmodule Chronoscope.NTS do @behaviour Chronoscope.NTS.Behaviour diff --git a/lib/chronoscope/nts/key_establishment_client.ex b/lib/chronoscope/nts/key_establishment_client.ex index 626bcea..102a62d 100644 --- a/lib/chronoscope/nts/key_establishment_client.ex +++ b/lib/chronoscope/nts/key_establishment_client.ex @@ -36,19 +36,21 @@ defmodule Chronoscope.NTS.KeyEstablishmentClient do :ok = @ssl.send(socket, KeyEstablishmentRequest.create()) {:ok, peercert} = @ssl.peercert(socket) - # TODO - refactor? + peercert + |> await_response() + |> tap(fn _ -> @ssl.close(socket) end) + end + + defp await_response(peercert) do receive do {:ssl, _socket, response} -> - @ssl.close(socket) parse_response(response, peercert) msg -> - @ssl.close(socket) Logger.error("received unexpected message: #{inspect(msg)}") {:error, :no_response} after @timeout_in_milliseconds -> - @ssl.close(socket) Logger.error("timed out waiting for response") {:error, :timeout} end