From 1337c4001fc0e384fd9e02ac814484af45a47009 Mon Sep 17 00:00:00 2001 From: Luca Corti Date: Sun, 10 Nov 2024 13:06:29 +0100 Subject: [PATCH] Convert RDE to gen_statem --- lib/bgp/server/rde.ex | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/lib/bgp/server/rde.ex b/lib/bgp/server/rde.ex index 7540a39..9ada298 100644 --- a/lib/bgp/server/rde.ex +++ b/lib/bgp/server/rde.ex @@ -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