Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove gen_state_machine dependency #558

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 24 additions & 33 deletions lib/vintage_net/interface.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule VintageNet.Interface do
The actual code that supplies the configuration implements the `VintageNet.Technology`
behaviour.
"""
use GenStateMachine
@behaviour :gen_statem

alias VintageNet.Interface.CommandRunner
alias VintageNet.Interface.RawConfig
Expand All @@ -28,6 +28,18 @@ defmodule VintageNet.Interface do
waiters: [],
inflight_ioctls: %{}

@doc false
@spec child_spec(VintageNet.ifname()) :: Supervisor.child_spec()
def child_spec(init_arg) do
%{
id: __MODULE__,
start: {__MODULE__, :start_link, [init_arg]},
restart: :permanent,
shutdown: 5000,
type: :worker
}
end

@doc """
Start up an interface

Expand All @@ -37,7 +49,7 @@ defmodule VintageNet.Interface do
"""
@spec start_link(VintageNet.ifname()) :: GenServer.on_start()
def start_link(ifname) do
GenStateMachine.start_link(__MODULE__, ifname, name: via_name(ifname))
:gen_statem.start_link(via_name(ifname), __MODULE__, ifname, [])
end

defp via_name(ifname) do
Expand All @@ -51,7 +63,7 @@ defmodule VintageNet.Interface do
"""
@spec stop(VintageNet.ifname()) :: :ok
def stop(ifname) do
GenStateMachine.stop(via_name(ifname))
:gen_statem.stop(via_name(ifname))
end

@doc """
Expand Down Expand Up @@ -112,7 +124,7 @@ defmodule VintageNet.Interface do
persist_configuration(ifname, normalized_config, options),
PropertyTable.put(VintageNet, ["interface", ifname, "config"], normalized_config),
{:error, :already_started} <- maybe_start_interface(ifname) do
GenStateMachine.call(via_name(raw_config.ifname), {:configure, raw_config})
:gen_statem.call(via_name(raw_config.ifname), {:configure, raw_config})
end
end

Expand Down Expand Up @@ -160,15 +172,15 @@ defmodule VintageNet.Interface do
"""
@spec wait_until_configured(VintageNet.ifname()) :: :ok
def wait_until_configured(ifname) do
GenStateMachine.call(via_name(ifname), :wait)
:gen_statem.call(via_name(ifname), :wait)
end

@doc """
Run an I/O command on the specified interface
"""
@spec ioctl(VintageNet.ifname(), atom(), any()) :: :ok | {:ok, any()} | {:error, any()}
def ioctl(ifname, command, args) do
GenStateMachine.call(via_name(ifname), {:ioctl, command, args})
:gen_statem.call(via_name(ifname), {:ioctl, command, args})
end

defp debug(data, message), do: log(:debug, data.ifname, message)
Expand All @@ -177,7 +189,7 @@ defmodule VintageNet.Interface do
Logger.log(level, ["VintageNet(", ifname, "): ", message])
end

@impl GenStateMachine
@impl :gen_statem
def init(ifname) do
Process.flag(:trap_exit, true)

Expand Down Expand Up @@ -209,9 +221,11 @@ defmodule VintageNet.Interface do
end
end

# :configuring
@impl :gen_statem
def callback_mode(), do: :handle_event_function

@impl GenStateMachine
# :configuring
@impl :gen_statem
def handle_event(:info, {:commands_done, :ok}, :configuring, %__MODULE__{} = data) do
# debug(data, ":configuring -> done success")
{new_data, actions} = reply_to_waiters(data)
Expand All @@ -228,7 +242,6 @@ defmodule VintageNet.Interface do
{:next_state, :configured, new_data, actions}
end

@impl GenStateMachine
def handle_event(
:info,
{:commands_done, {:error, _reason}},
Expand All @@ -243,7 +256,6 @@ defmodule VintageNet.Interface do
{:next_state, :retrying, new_data, actions}
end

@impl GenStateMachine
def handle_event(
:info,
{:EXIT, pid, reason},
Expand All @@ -262,7 +274,6 @@ defmodule VintageNet.Interface do
{:next_state, :retrying, new_data, actions}
end

@impl GenStateMachine
def handle_event(
:state_timeout,
_event,
Expand All @@ -279,7 +290,6 @@ defmodule VintageNet.Interface do
{:next_state, :retrying, new_data, actions}
end

@impl GenStateMachine
def handle_event(
{:call, from},
{:configure, new_config},
Expand All @@ -299,7 +309,6 @@ defmodule VintageNet.Interface do
start_configuring(new_config, new_data, actions)
end

@impl GenStateMachine
def handle_event(
:info,
{VintageNet, ["interface", an_ifname, "present"], _old_value, nil, _meta},
Expand All @@ -323,7 +332,6 @@ defmodule VintageNet.Interface do
{:keep_state, data, {:reply, from, :ok}}
end

@impl GenStateMachine
def handle_event(
:internal,
{:configure, new_config},
Expand All @@ -344,7 +352,6 @@ defmodule VintageNet.Interface do
{:next_state, :reconfiguring, %{new_data | next_config: new_config}, actions}
end

@impl GenStateMachine
def handle_event(
{:call, from},
{:configure, new_config},
Expand All @@ -366,7 +373,6 @@ defmodule VintageNet.Interface do
{:next_state, :reconfiguring, %{new_data | next_config: new_config}, actions}
end

@impl GenStateMachine
def handle_event(
{:call, from},
{:ioctl, command, args},
Expand All @@ -382,7 +388,6 @@ defmodule VintageNet.Interface do
{:keep_state, new_data}
end

@impl GenStateMachine
def handle_event(
:info,
{:ioctl_done, ioctl_pid, result},
Expand All @@ -398,7 +403,6 @@ defmodule VintageNet.Interface do
{:keep_state, new_data, action}
end

@impl GenStateMachine
def handle_event(
:info,
{:EXIT, pid, reason},
Expand All @@ -421,7 +425,6 @@ defmodule VintageNet.Interface do
end
end

@impl GenStateMachine
def handle_event(
:info,
{VintageNet, ["interface", an_ifname, "present"], _old_value, nil, _meta},
Expand All @@ -444,7 +447,6 @@ defmodule VintageNet.Interface do

# :reconfiguring

@impl GenStateMachine
def handle_event(
:info,
{:commands_done, :ok},
Expand Down Expand Up @@ -472,7 +474,6 @@ defmodule VintageNet.Interface do
end
end

@impl GenStateMachine
def handle_event(
:info,
{:commands_done, {:error, _reason}},
Expand All @@ -499,7 +500,6 @@ defmodule VintageNet.Interface do
end
end

@impl GenStateMachine
def handle_event(
:info,
{:EXIT, pid, reason},
Expand All @@ -526,7 +526,6 @@ defmodule VintageNet.Interface do
end
end

@impl GenStateMachine
def handle_event(
:state_timeout,
_event,
Expand Down Expand Up @@ -555,7 +554,6 @@ defmodule VintageNet.Interface do

# :retrying

@impl GenStateMachine
def handle_event(:state_timeout, _event, :retrying, %__MODULE__{config: new_config} = data) do
if interfaces_available?(data) do
start_configuring(new_config, data, [])
Expand All @@ -564,7 +562,6 @@ defmodule VintageNet.Interface do
end
end

@impl GenStateMachine
def handle_event(
:info,
{VintageNet, ["interface", an_ifname, "present"], _old_value, true, _meta},
Expand All @@ -580,7 +577,6 @@ defmodule VintageNet.Interface do
end
end

@impl GenStateMachine
def handle_event(
{:call, from},
{:configure, new_config},
Expand All @@ -600,7 +596,6 @@ defmodule VintageNet.Interface do
end
end

@impl GenStateMachine
def handle_event(:info, {:commands_done, _}, :retrying, %__MODULE__{} = data) do
# This is a latent message that didn't get processed because a crash
# got handled first. It can be produced by getting one of an
Expand All @@ -610,14 +605,12 @@ defmodule VintageNet.Interface do
end

# Catch all event handlers
@impl GenStateMachine
def handle_event(:info, {:EXIT, _pid, _reason}, _state, data) do
# Ignore latent or expected command runner and ioctl exits
# debug(data, "#{inspect(state)} -> process exit (ignoring)")
{:keep_state, data}
end

@impl GenStateMachine
def handle_event(
{:call, from},
:wait,
Expand All @@ -628,7 +621,6 @@ defmodule VintageNet.Interface do
{:keep_state, %{data | waiters: [from | waiters]}}
end

@impl GenStateMachine
def handle_event(
{:call, from},
{:ioctl, _command, _args},
Expand All @@ -639,7 +631,6 @@ defmodule VintageNet.Interface do
{:keep_state, data, {:reply, from, {:error, :unconfigured}}}
end

@impl GenStateMachine
def handle_event(
:info,
{VintageNet, ["interface", ifname, "present"], _old_value, present, _meta},
Expand All @@ -650,7 +641,7 @@ defmodule VintageNet.Interface do
{:keep_state, data}
end

@impl GenStateMachine
@impl :gen_statem
def terminate(_reason, _state, %{ifname: ifname}) do
PropertyTable.delete(VintageNet, ["interface", ifname, "type"])
PropertyTable.delete(VintageNet, ["interface", ifname, "state"])
Expand Down
1 change: 0 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ defmodule VintageNet.MixProject do
[
# Runtime dependencies
{:beam_notify, "~> 1.0 or ~> 0.2.0"},
{:gen_state_machine, "~> 2.0.0 or ~> 2.1.0 or ~> 3.0.0"},
{:muontrap, "~> 1.0 or ~> 0.5.1 or ~> 0.6.0"},
{:property_table, "~> 0.2.0 or ~> 0.3.0"},
# Build dependencies
Expand Down
1 change: 0 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"erlex": {:hex, :erlex, "0.2.7", "810e8725f96ab74d17aac676e748627a07bc87eb950d2b83acd29dc047a30595", [:mix], [], "hexpm", "3ed95f79d1a844c3f6bf0cea61e0d5612a42ce56da9c03f01df538685365efb0"},
"ex_doc": {:hex, :ex_doc, "0.37.1", "65ca30d242082b95aa852b3b73c9d9914279fff56db5dc7b3859be5504417980", [:mix], [{:earmark_parser, "~> 1.4.42", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "6774f75477733ea88ce861476db031f9399c110640752ca2b400dbbb50491224"},
"file_system": {:hex, :file_system, "1.1.0", "08d232062284546c6c34426997dd7ef6ec9f8bbd090eb91780283c9016840e8f", [:mix], [], "hexpm", "bfcf81244f416871f2a2e15c1b515287faa5db9c6bcf290222206d120b3d43f6"},
"gen_state_machine": {:hex, :gen_state_machine, "3.0.0", "1e57f86a494e5c6b14137ebef26a7eb342b3b0070c7135f2d6768ed3f6b6cdff", [:mix], [], "hexpm", "0a59652574bebceb7309f6b749d2a41b45fdeda8dbb4da0791e355dd19f0ed15"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"},
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},
Expand Down