srad_eon/
builder.rs

1use std::sync::Arc;
2
3use srad_client::{Client, DynClient, DynEventLoop, EventLoop};
4
5use crate::{
6    metric_manager::manager::{DynNodeMetricManager, NoMetricManager, NodeMetricManager},
7    EoN, NodeHandle,
8};
9
10/// A builder for creating and configuring Edge of Network (EoN) instances.
11pub struct EoNBuilder {
12    pub(crate) group_id: Option<String>,
13    pub(crate) node_id: Option<String>,
14    pub(crate) eventloop_client: (Box<DynEventLoop>, Arc<DynClient>),
15    pub(crate) metric_manager: Box<DynNodeMetricManager>,
16}
17
18impl EoNBuilder {
19    /// Creates a new builder with the specified event loop and client.
20    ///
21    /// Initializes a builder with default values and a no-op metric manager.
22    pub fn new<E: EventLoop + Send + 'static, C: Client + Send + Sync + 'static>(
23        eventloop: E,
24        client: C,
25    ) -> Self {
26        Self {
27            group_id: None,
28            node_id: None,
29            eventloop_client: (Box::new(eventloop), Arc::new(client)),
30            metric_manager: Box::new(NoMetricManager::new()),
31        }
32    }
33
34    /// Sets the group ID for the EoN instance.
35    ///
36    /// The group ID identifies the group to which this node belongs.
37    pub fn with_group_id<S: Into<String>>(mut self, group_id: S) -> Self {
38        self.group_id = Some(group_id.into());
39        self
40    }
41
42    /// Sets the node ID for the EoN instance.
43    ///
44    /// The node ID uniquely identifies this node within its group.
45    pub fn with_node_id<S: Into<String>>(mut self, node_id: S) -> Self {
46        self.node_id = Some(node_id.into());
47        self
48    }
49
50    /// Sets a custom metric manager for the EoN instance.
51    ///
52    /// Replaces the default no-op metric manager with the provided implementation.
53    pub fn with_metric_manager<M: NodeMetricManager + Send + Sync + 'static>(
54        mut self,
55        metric_manager: M,
56    ) -> Self {
57        self.metric_manager = Box::new(metric_manager);
58        self
59    }
60
61    /// Builds the EoN instance with the configured settings.
62    ///
63    /// Creates and returns a new EoN instance and its associated NodeHandle.
64    /// This method will return an error if required configuration is missing
65    /// or if there are other issues with the configuration.
66    pub fn build(self) -> Result<(EoN, NodeHandle), String> {
67        EoN::new_from_builder(self)
68    }
69}