Skip to content

Commit

Permalink
[eclipse-iceoryx#231] Properties can be set via service builder
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Jun 10, 2024
1 parent fd04244 commit 3f0fa98
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
19 changes: 17 additions & 2 deletions iceoryx2/src/service/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,28 @@ impl std::error::Error for ReadStaticStorageFailure {}
#[derive(Debug)]
pub struct Builder<S: Service> {
name: ServiceName,
properties: ServiceProperties,
_phantom_s: PhantomData<S>,
}

impl<S: Service> Builder<S> {
pub(crate) fn new(name: &ServiceName) -> Self {
Self {
name: name.clone(),
properties: ServiceProperties::new(),
_phantom_s: PhantomData,
}
}

/// Defines a property requirement. If a new [`Service`] is created all properties will be
/// added. If an existing [`Service`] is opened those properties are interpreted as
/// requirements that the service has to satisfy. If a property does not match or does not
/// exist the open process will fail.
pub fn add_property(mut self, key: &str, value: &str) -> Self {
self.properties.add(key, value);
self
}

/// Create a new builder to create a
/// [`MessagingPattern::PublishSubscribe`](crate::service::messaging_pattern::MessagingPattern::PublishSubscribe) [`Service`].
pub fn publish_subscribe<PayloadType: Debug + ?Sized>(
Expand All @@ -125,7 +136,11 @@ impl<S: Service> Builder<S> {
config: &config::Config,
) -> publish_subscribe::Builder<PayloadType, S> {
BuilderWithServiceType::new(
StaticConfig::new_publish_subscribe::<S::ServiceNameHasher>(&self.name, config),
StaticConfig::new_publish_subscribe::<S::ServiceNameHasher>(
&self.name,
config,
self.properties,
),
Arc::new(config.clone()),
)
.publish_subscribe()
Expand All @@ -142,7 +157,7 @@ impl<S: Service> Builder<S> {
/// with a custom [`config::Config`]
pub fn event_with_custom_config(self, config: &config::Config) -> event::Builder<S> {
BuilderWithServiceType::new(
StaticConfig::new_event::<S::ServiceNameHasher>(&self.name, config),
StaticConfig::new_event::<S::ServiceNameHasher>(&self.name, config, self.properties),
Arc::new(config.clone()),
)
.event()
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2/src/service/port_factory/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<Service: service::Service> PortFactory<Service> {
}

/// Returns the value of a property
pub fn property(&self, key: &str) -> Option<&str> {
pub fn property(&self, key: &str) -> Vec<&str> {
self.service.state().static_config.property(key)
}

Expand Down
2 changes: 1 addition & 1 deletion iceoryx2/src/service/port_factory/publish_subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<Service: service::Service, PayloadType: Debug + ?Sized> PortFactory<Service
}

/// Returns the value of a property
pub fn property(&self, key: &str) -> Option<&str> {
pub fn property(&self, key: &str) -> Vec<&str> {
self.service.state().static_config.property(key)
}

Expand Down
37 changes: 29 additions & 8 deletions iceoryx2/src/service/static_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,34 @@ use crate::config;

use super::service_name::ServiceName;

#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
pub(crate) struct ServiceProperties(Vec<(String, String)>);

impl ServiceProperties {
pub(crate) fn new() -> Self {
Self(Vec::new())
}

pub(crate) fn add(&mut self, key: &str, value: &str) {
self.0.push((key.into(), value.into()));
self.0.sort();
}

pub(crate) fn get(&self, key: &str) -> Vec<&str> {
self.0
.iter()
.filter(|(k, _)| k == key)
.map(|(_, value)| value.as_str())
.collect()
}
}

/// Defines a common set of static service configuration details every service shares.
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
pub struct StaticConfig {
uuid: String,
service_name: ServiceName,
properties: Vec<(String, String)>,
properties: ServiceProperties,
pub(crate) messaging_pattern: MessagingPattern,
}

Expand All @@ -56,6 +78,7 @@ impl StaticConfig {
pub(crate) fn new_event<Hasher: Hash>(
service_name: &ServiceName,
config: &config::Config,
properties: ServiceProperties,
) -> Self {
let messaging_pattern = MessagingPattern::Event(event::StaticConfig::new(config));
Self {
Expand All @@ -64,13 +87,14 @@ impl StaticConfig {
.into(),
service_name: service_name.clone(),
messaging_pattern,
properties: Vec::new(),
properties,
}
}

pub(crate) fn new_publish_subscribe<Hasher: Hash>(
service_name: &ServiceName,
config: &config::Config,
properties: ServiceProperties,
) -> Self {
let messaging_pattern =
MessagingPattern::PublishSubscribe(publish_subscribe::StaticConfig::new(config));
Expand All @@ -80,16 +104,13 @@ impl StaticConfig {
.into(),
service_name: service_name.clone(),
messaging_pattern,
properties: Vec::new(),
properties,
}
}

/// Returns the value of a property
pub(crate) fn property(&self, key: &str) -> Option<&str> {
self.properties
.iter()
.find(|&v| v.0 == key)
.and_then(|v| Some(v.1.as_str()))
pub(crate) fn property(&self, key: &str) -> Vec<&str> {
self.properties.get(key)
}

/// Returns the uuid of the [`crate::service::Service`]
Expand Down

0 comments on commit 3f0fa98

Please sign in to comment.