Skip to content

Commit

Permalink
Switch to gen_statem
Browse files Browse the repository at this point in the history
  • Loading branch information
bokner committed Jul 11, 2023
1 parent 903d779 commit fd2c664
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
33 changes: 20 additions & 13 deletions lib/mllp/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ defmodule MLLP.Client do

@behaviour MLLP.ClientContract

use GenStateMachine, callback_mode: [:state_functions, :state_enter]
@behaviour :gen_statem



@type pid_ref :: atom | pid | {atom, any} | {:via, atom, any}
@type ip_address :: :inet.socket_address() | String.t()
Expand Down Expand Up @@ -253,23 +255,24 @@ defmodule MLLP.Client do
) :: {:ok, pid()}

def start_link(address, port, options \\ []) do
GenStateMachine.start_link(
:gen_statem.start_link(
__MODULE__,
[address: normalize_address!(address), port: port] ++ options
[address: normalize_address!(address), port: port] ++ options,
[]
)
end

@doc """
Returns true if the connection is open and established, otherwise false.
"""
@spec is_connected?(pid :: pid()) :: boolean()
def is_connected?(pid) when is_pid(pid), do: GenStateMachine.call(pid, :is_connected)
def is_connected?(pid) when is_pid(pid), do: :gen_statem.call(pid, :is_connected)

@doc """
Instructs the client to disconnect (if connected) and attempt a reconnect.
"""
@spec reconnect(pid :: pid()) :: :ok
def reconnect(pid), do: GenStateMachine.call(pid, :reconnect)
def reconnect(pid), do: :gen_statem.call(pid, :reconnect)

@doc """
Sends a message and receives a response.
Expand Down Expand Up @@ -304,7 +307,7 @@ defmodule MLLP.Client do
def send(pid, %HL7.Message{} = payload, options, timeout) do
raw_message = to_string(payload)

case GenStateMachine.call(pid, {:send, raw_message, options}, timeout) do
case :gen_statem.call(pid, {:send, raw_message, options}, timeout) do
{:ok, reply} ->
verify_ack(reply, raw_message)

Expand All @@ -314,7 +317,7 @@ defmodule MLLP.Client do
end

def send(pid, payload, options, timeout) do
case GenStateMachine.call(pid, {:send, payload, options}, timeout) do
case :gen_statem.call(pid, {:send, payload, options}, timeout) do
{:ok, wrapped_message} ->
{:ok, MLLP.Envelope.unwrap_message(wrapped_message)}

Expand All @@ -337,22 +340,26 @@ defmodule MLLP.Client do
end

def send_async(pid, payload, timeout) when is_binary(payload) do
GenStateMachine.call(pid, {:send_async, payload, []}, timeout)
:gen_statem.call(pid, {:send_async, payload, []}, timeout)
end

@doc """
Stops an MLLP.Client given a MLLP.Client pid.
This function will always return `:ok` per `GenStateMachine.stop/1`, thus
This function will always return `:ok` per `:gen_statem.stop/1`, thus
you may give it a pid that references a client which is already stopped.
"""
@spec stop(pid :: pid()) :: :ok
def stop(pid), do: GenStateMachine.stop(pid)
def stop(pid), do: :gen_statem.stop(pid)

@header MLLP.Envelope.sb()
@trailer MLLP.Envelope.eb_cr()

## GenStateMachine callbacks
## :gen_statem callbacks
@impl true
def callback_mode() do
[:state_functions, :state_enter]
end

@impl true
@spec init(Keyword.t()) ::
Expand Down Expand Up @@ -540,7 +547,7 @@ defmodule MLLP.Client do
end

########################################
### End of GenStateMachine callbacks ###
### End of :gen_statem callbacks ###
########################################

defp unexpected_message(state, event, message) do
Expand Down Expand Up @@ -578,7 +585,7 @@ defmodule MLLP.Client do
end

defp reply_to_caller(reply, %{caller: caller, context: context} = state) do
caller && GenStateMachine.reply(caller, format_reply(reply, context))
caller && :gen_statem.reply(caller, format_reply(reply, context))
reply_cleanup(state)
end

Expand Down
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ defmodule MLLP.MixProject do
{:ranch, "~> 1.8.0"},
{:elixir_hl7, "== 0.6.2"},
{:backoff, "~> 1.1.6"},
{:gen_state_machine, "~> 3.0"},
{:ex_doc, "~> 0.24.2", only: :dev, runtime: false},
{:dialyxir, "~> 1.1.0", only: [:dev, :test], runtime: false},
{:mix_test_watch, "~> 1.0.2", only: :dev, runtime: false},
Expand Down
1 change: 0 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"ex_doc": {:hex, :ex_doc, "0.24.2", "e4c26603830c1a2286dae45f4412a4d1980e1e89dc779fcd0181ed1d5a05c8d9", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e134e1d9e821b8d9e4244687fb2ace58d479b67b282de5158333b0d57c6fb7da"},
"excoveralls": {:hex, :excoveralls, "0.14.6", "610e921e25b180a8538229ef547957f7e04bd3d3e9a55c7c5b7d24354abbba70", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "0eceddaa9785cfcefbf3cd37812705f9d8ad34a758e513bb975b081dce4eb11e"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"gen_state_machine": {:hex, :gen_state_machine, "3.0.0", "1e57f86a494e5c6b14137ebef26a7eb342b3b0070c7135f2d6768ed3f6b6cdff", [:mix], [], "hexpm", "0a59652574bebceb7309f6b749d2a41b45fdeda8dbb4da0791e355dd19f0ed15"},
"hackney": {:hex, :hackney, "1.18.1", "f48bf88f521f2a229fc7bae88cf4f85adc9cd9bcf23b5dc8eb6a1788c662c4f6", [:rebar3], [{:certifi, "~>2.9.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~>6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~>1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~>1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a4ecdaff44297e9b5894ae499e9a070ea1888c84afdd1fd9b7b2bc384950128e"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"},
Expand Down

0 comments on commit fd2c664

Please sign in to comment.