Skip to content

Commit

Permalink
Merge pull request #105 from lucacorti/cleanup/rde-gen_statem
Browse files Browse the repository at this point in the history
Convert RDE to gen_statem
  • Loading branch information
lucacorti authored Nov 12, 2024
2 parents 26fa12d + 1337c40 commit 59e4db5
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions lib/bgp/server/rde.ex
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

0 comments on commit 59e4db5

Please sign in to comment.