iceoryx2/service/static_config/
mod.rs

1// Copyright (c) 2023 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13/// The static service configuration of an
14/// [`MessagingPattern::Event`]
15/// based service.
16pub mod event;
17
18/// The static service configuration of an
19/// [`MessagingPattern::PublishSubscribe`]
20/// based service.
21pub mod publish_subscribe;
22
23/// Contains the size, alignment and name of the header and payload type
24/// and the type variant
25pub mod message_type_details;
26
27pub mod request_response;
28
29pub mod messaging_pattern;
30
31pub mod blackboard;
32
33use iceoryx2_bb_derive_macros::ZeroCopySend;
34use iceoryx2_bb_elementary_traits::zero_copy_send::ZeroCopySend;
35use iceoryx2_bb_log::fatal_panic;
36use iceoryx2_cal::hash::Hash;
37use serde::{Deserialize, Serialize};
38
39use crate::config;
40
41use self::messaging_pattern::MessagingPattern;
42
43use super::{attribute::AttributeSet, service_id::ServiceId, service_name::ServiceName};
44
45/// Defines a common set of static service configuration details every service shares.
46#[derive(Debug, Eq, PartialEq, Clone, ZeroCopySend, Serialize, Deserialize)]
47#[repr(C)]
48pub struct StaticConfig {
49    service_id: ServiceId,
50    service_name: ServiceName,
51    pub(crate) attributes: AttributeSet,
52    pub(crate) messaging_pattern: MessagingPattern,
53}
54
55impl StaticConfig {
56    pub(crate) fn new_request_response<Hasher: Hash>(
57        service_name: &ServiceName,
58        config: &config::Config,
59    ) -> Self {
60        let messaging_pattern =
61            MessagingPattern::RequestResponse(request_response::StaticConfig::new(config));
62        Self {
63            service_id: ServiceId::new::<Hasher>(
64                service_name,
65                crate::service::messaging_pattern::MessagingPattern::RequestResponse,
66            ),
67            service_name: service_name.clone(),
68            messaging_pattern,
69            attributes: AttributeSet::new(),
70        }
71    }
72
73    pub(crate) fn new_event<Hasher: Hash>(
74        service_name: &ServiceName,
75        config: &config::Config,
76    ) -> Self {
77        let messaging_pattern = MessagingPattern::Event(event::StaticConfig::new(config));
78        Self {
79            service_id: ServiceId::new::<Hasher>(
80                service_name,
81                crate::service::messaging_pattern::MessagingPattern::Event,
82            ),
83            service_name: service_name.clone(),
84            messaging_pattern,
85            attributes: AttributeSet::new(),
86        }
87    }
88
89    pub(crate) fn new_publish_subscribe<Hasher: Hash>(
90        service_name: &ServiceName,
91        config: &config::Config,
92    ) -> Self {
93        let messaging_pattern =
94            MessagingPattern::PublishSubscribe(publish_subscribe::StaticConfig::new(config));
95        Self {
96            service_id: ServiceId::new::<Hasher>(
97                service_name,
98                crate::service::messaging_pattern::MessagingPattern::PublishSubscribe,
99            ),
100            service_name: service_name.clone(),
101            messaging_pattern,
102            attributes: AttributeSet::new(),
103        }
104    }
105
106    pub(crate) fn new_blackboard<Hasher: Hash>(
107        service_name: &ServiceName,
108        config: &config::Config,
109    ) -> Self {
110        let messaging_pattern = MessagingPattern::Blackboard(blackboard::StaticConfig::new(config));
111        Self {
112            service_id: ServiceId::new::<Hasher>(
113                service_name,
114                crate::service::messaging_pattern::MessagingPattern::Blackboard,
115            ),
116            service_name: service_name.clone(),
117            messaging_pattern,
118            attributes: AttributeSet::new(),
119        }
120    }
121
122    /// Returns the attributes of the [`crate::service::Service`]
123    pub fn attributes(&self) -> &AttributeSet {
124        &self.attributes
125    }
126
127    /// Returns the uuid of the [`crate::service::Service`]
128    pub fn service_id(&self) -> &ServiceId {
129        &self.service_id
130    }
131
132    /// Returns the [`ServiceName`] of the [`crate::service::Service`]
133    pub fn name(&self) -> &ServiceName {
134        &self.service_name
135    }
136
137    /// Returns the [`MessagingPattern`] of the [`crate::service::Service`]
138    pub fn messaging_pattern(&self) -> &MessagingPattern {
139        &self.messaging_pattern
140    }
141
142    pub(crate) fn has_same_messaging_pattern(&self, rhs: &StaticConfig) -> bool {
143        self.messaging_pattern
144            .is_same_pattern(&rhs.messaging_pattern)
145    }
146
147    /// Unwrap the RequestResponse static configuration.
148    pub fn request_response(&self) -> &request_response::StaticConfig {
149        match &self.messaging_pattern {
150            MessagingPattern::RequestResponse(ref v) => v,
151            m => {
152                fatal_panic!(from self, "This should never happen! Trying to access request_response::StaticConfig when the messaging pattern is actually {:?}!", m)
153            }
154        }
155    }
156
157    pub(crate) fn request_response_mut(&mut self) -> &mut request_response::StaticConfig {
158        let origin = format!("{self:?}");
159        match &mut self.messaging_pattern {
160            MessagingPattern::RequestResponse(ref mut v) => v,
161            m => {
162                fatal_panic!(from origin, "This should never happen! Trying to access request_response::StaticConfig when the messaging pattern is actually {:?}!", m)
163            }
164        }
165    }
166
167    /// Unwrap the Event static configuration.
168    pub fn event(&self) -> &event::StaticConfig {
169        match &self.messaging_pattern {
170            MessagingPattern::Event(ref v) => v,
171            m => {
172                fatal_panic!(from self, "This should never happen! Trying to access event::StaticConfig when the messaging pattern is actually {:?}!", m)
173            }
174        }
175    }
176
177    pub(crate) fn event_mut(&mut self) -> &mut event::StaticConfig {
178        let origin = format!("{self:?}");
179        match &mut self.messaging_pattern {
180            MessagingPattern::Event(ref mut v) => v,
181            m => {
182                fatal_panic!(from origin, "This should never happen! Trying to access event::StaticConfig when the messaging pattern is actually {:?}!", m)
183            }
184        }
185    }
186
187    /// Unwrap the PublishSubscribe static configuration.
188    pub fn publish_subscribe(&self) -> &publish_subscribe::StaticConfig {
189        match &self.messaging_pattern {
190            MessagingPattern::PublishSubscribe(ref v) => v,
191            m => {
192                fatal_panic!(from self, "This should never happen! Trying to access publish_subscribe::StaticConfig when the messaging pattern is actually {:?}!", m)
193            }
194        }
195    }
196
197    pub(crate) fn publish_subscribe_mut(&mut self) -> &mut publish_subscribe::StaticConfig {
198        let origin = format!("{self:?}");
199        match &mut self.messaging_pattern {
200            MessagingPattern::PublishSubscribe(ref mut v) => v,
201            m => {
202                fatal_panic!(from origin, "This should never happen! Trying to access publish_subscribe::StaticConfig when the messaging pattern is actually {:?}!", m)
203            }
204        }
205    }
206
207    /// Unwrap the Blackboard static configuration.
208    pub fn blackboard(&self) -> &blackboard::StaticConfig {
209        match &self.messaging_pattern {
210            MessagingPattern::Blackboard(ref v) => v,
211            m => {
212                fatal_panic!(from self, "This should never happen! Trying to access blackboard::StaticConfig when the messaging pattern is actually {:?}!", m)
213            }
214        }
215    }
216
217    pub(crate) fn blackboard_mut(&mut self) -> &mut blackboard::StaticConfig {
218        let origin = format!("{self:?}");
219        match &mut self.messaging_pattern {
220            MessagingPattern::Blackboard(ref mut v) => v,
221            m => {
222                fatal_panic!(from origin, "This should never happen! Trying to access blackboard::StaticConfig when the messaging pattern is actually {:?}!", m)
223            }
224        }
225    }
226}