Skip to main content

srad_eon/
builder.rs

1use std::{sync::Arc, time::Duration};
2
3use srad_client::{Client, DynClient, DynEventLoop, EventLoop};
4use srad_types::Template;
5
6use crate::{
7    metric_manager::manager::{DynNodeMetricManager, NoMetricManager, NodeMetricManager},
8    node::TemplateRegistry,
9    EoN, NodeHandle,
10};
11
12/// A builder for creating and configuring Edge of Network (EoN) instances.
13pub struct EoNBuilder {
14    pub(crate) group_id: Option<String>,
15    pub(crate) node_id: Option<String>,
16    pub(crate) eventloop_client: (Box<DynEventLoop>, Arc<DynClient>),
17    pub(crate) metric_manager: Box<DynNodeMetricManager>,
18    pub(crate) node_rebirth_request_cooldown: Duration,
19    pub(crate) templates: TemplateRegistry,
20}
21
22impl EoNBuilder {
23    /// Creates a new builder with the specified event loop and client.
24    ///
25    /// Initializes a builder with default values and a no-op metric manager.
26    pub fn new<E: EventLoop + Send + 'static, C: Client + Send + Sync + 'static>(
27        eventloop: E,
28        client: C,
29    ) -> Self {
30        Self {
31            group_id: None,
32            node_id: None,
33            eventloop_client: (Box::new(eventloop), Arc::new(client)),
34            metric_manager: Box::new(NoMetricManager::new()),
35            node_rebirth_request_cooldown: Duration::from_secs(5),
36            templates: TemplateRegistry::new(),
37        }
38    }
39
40    /// Sets the group ID for the EoN instance.
41    ///
42    /// The group ID identifies the group to which this node belongs.
43    pub fn with_group_id<S: Into<String>>(mut self, group_id: S) -> Self {
44        self.group_id = Some(group_id.into());
45        self
46    }
47
48    /// Sets the node ID for the EoN instance.
49    ///
50    /// The node ID uniquely identifies this node within its group.
51    pub fn with_node_id<S: Into<String>>(mut self, node_id: S) -> Self {
52        self.node_id = Some(node_id.into());
53        self
54    }
55
56    /// Set the cooldown for the time to respond to CMD rebirth requests
57    pub fn with_rebirth_cmd_cooldown(mut self, cooldown: Duration) -> Self {
58        self.node_rebirth_request_cooldown = cooldown;
59        self
60    }
61
62    /// Sets a custom metric manager for the EoN instance.
63    ///
64    /// Replaces the default no-op metric manager with the provided implementation.
65    pub fn with_metric_manager<M: NodeMetricManager + Send + Sync + 'static>(
66        mut self,
67        metric_manager: M,
68    ) -> Self {
69        self.metric_manager = Box::new(metric_manager);
70        self
71    }
72
73    /// Register a template definition with the node.
74    ///
75    /// Updates the initial [TemplateRegistry] that the node will use.
76    /// Will panic if a template with the same [srad_types::TemplateMetadata::template_definition_metric_name] has been registered
77    /// See [TemplateRegistry::register] for more details.
78    pub fn register_template<T: Template>(mut self) -> Self {
79        if self.templates.register::<T>().is_err() {
80            panic!(
81                "Template with duplicate metric definition name registered. name={}",
82                T::template_definition_metric_name()
83            )
84        }
85        self
86    }
87
88    /// Builds the EoN instance with the configured settings.
89    ///
90    /// Creates and returns a new EoN instance and its associated NodeHandle.
91    /// This method will return an error if required configuration is missing
92    /// or if there are other issues with the configuration.
93    pub fn build(self) -> Result<(EoN, NodeHandle), String> {
94        EoN::new_from_builder(self)
95    }
96}