Skip to main content

datex_core/shared_values/subscriber/
mod.rs

1use crate::collections::HashMap;
2
3use crate::{
4    runtime::execution::context::RemoteExecutionContext,
5    shared_values::{
6        ReferenceMutability, base_shared_value_container::observers::ObserverId,
7    },
8    values::core_values::endpoint::Endpoint,
9};
10
11#[derive(Debug)]
12pub struct Subscribers {
13    lookup: HashMap<Endpoint, SubscriptionMetadata>,
14    observer_id: Option<ObserverId>,
15    remote_execution_context: RemoteExecutionContext,
16}
17
18impl Subscribers {
19    pub fn new(
20        observer_id: Option<ObserverId>,
21        remote_execution_context: RemoteExecutionContext,
22    ) -> Self {
23        Self {
24            lookup: HashMap::new(),
25            observer_id,
26            remote_execution_context,
27        }
28    }
29
30    /// Adds a subscriber to the list of subscribers.
31    /// If the subscriber already exists, the access rights are updated.
32    pub fn add_subscriber(
33        &mut self,
34        endpoint: &Endpoint,
35        access_rights: ReferenceMutability,
36    ) {
37        // FIXME endpoint instance handling
38        self.lookup.insert(
39            endpoint.clone(),
40            SubscriptionMetadata { access_rights }, // TODO merge if other properties relevant
41        );
42    }
43    pub fn observer_id(&self) -> Option<ObserverId> {
44        self.observer_id
45    }
46
47    pub fn remove_subscriber(&mut self, endpoint: &Endpoint) {
48        self.lookup.remove(&endpoint.any_instance());
49    }
50
51    pub fn subscriber_metadata(
52        &self,
53        endpoint: &Endpoint,
54    ) -> Option<&SubscriptionMetadata> {
55        self.lookup.get(&endpoint.any_instance())
56    }
57
58    pub fn lookup(&self) -> &HashMap<Endpoint, SubscriptionMetadata> {
59        &self.lookup
60    }
61
62    pub fn endpoints(&self) -> impl Iterator<Item = &Endpoint> {
63        self.lookup.keys()
64    }
65
66    pub fn remote_execution_context(&self) -> &RemoteExecutionContext {
67        &self.remote_execution_context
68    }
69
70    pub fn remote_execution_context_mut(
71        &mut self,
72    ) -> &mut RemoteExecutionContext {
73        &mut self.remote_execution_context
74    }
75}
76
77#[derive(Debug, PartialEq)]
78pub struct SubscriptionMetadata {
79    pub access_rights: ReferenceMutability,
80}