-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #105 from lucacorti/cleanup/rde-gen_statem
Convert RDE to gen_statem
- Loading branch information
Showing
1 changed file
with
27 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,44 @@ | ||
defmodule BGP.Server.RDE do | ||
@moduledoc false | ||
|
||
@behaviour :gen_statem | ||
|
||
alias BGP.{Message.UPDATE, Server} | ||
|
||
use GenServer | ||
require Logger | ||
|
||
@enforce_keys [:config] | ||
defstruct [:config] | ||
|
||
@spec start_link(Keyword.t()) :: GenServer.on_start() | ||
def start_link(args), | ||
do: GenServer.start_link(__MODULE__, args, name: Server.rde_for(args[:server])) | ||
do: :gen_statem.start_link({:local, Server.rde_for(args[:server])}, __MODULE__, args, []) | ||
|
||
@doc false | ||
def child_spec({args, opts}), | ||
do: %{id: make_ref(), start: {__MODULE__, :start_link, [{args, opts}]}} | ||
|
||
def child_spec(opts), | ||
do: %{id: make_ref(), start: {__MODULE__, :start_link, [opts]}} | ||
|
||
@spec process_update(Server.t(), UPDATE.t()) :: :ok | ||
def process_update(server, update), | ||
do: GenServer.call(Server.rde_for(server), {:process_update, update}) | ||
|
||
@impl GenServer | ||
def init(_args) do | ||
{:ok, %{rib: MapSet.new(), rib_in: MapSet.new(), rib_out: MapSet.new()}} | ||
@impl :gen_statem | ||
def callback_mode, do: [:handle_event_function, :state_enter] | ||
|
||
@impl :gen_statem | ||
def init(args), do: {:ok, :ready, %__MODULE__{config: args}} | ||
|
||
@impl :gen_statem | ||
def handle_event(:enter, old_state, new_state, %__MODULE__{}) do | ||
Logger.info("#{old_state} -> #{new_state}") | ||
:keep_state_and_data | ||
end | ||
|
||
@impl GenServer | ||
def handle_call({:process_update, %UPDATE{}}, _from, state) do | ||
{:reply, :ok, state} | ||
@impl :gen_statem | ||
def handle_event({:call, from}, {:process_update, %UPDATE{}}, _state, _data) do | ||
{:keep_state_and_data, {:reply, from, :ok}} | ||
end | ||
end |