Skip to content

Commit

Permalink
[eclipse-iceoryx#195] Add TypeDetails enum for pub sub services
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Apr 23, 2024
1 parent 0b85089 commit d18a304
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 49 deletions.
2 changes: 1 addition & 1 deletion iceoryx2/src/port/details/publisher_connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<Service: service::Service> Connection<Service> {
.receiver_max_borrowed_samples(this.static_config.subscriber_max_borrowed_samples)
.enable_safe_overflow(this.static_config.enable_safe_overflow)
.number_of_samples(number_of_samples)
.create_receiver(this.static_config.type_size),
.create_receiver(this.static_config.type_details().layout().size()),
"{} since the zero copy connection could not be established.", msg);

let data_segment = fail!(from this,
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2/src/port/details/subscriber_connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl<Service: service::Service> Connection<Service> {
.receiver_max_borrowed_samples(this.static_config.subscriber_max_borrowed_samples)
.enable_safe_overflow(this.static_config.enable_safe_overflow)
.number_of_samples(number_of_samples)
.create_sender(this.static_config.type_size),
.create_sender(this.static_config.type_details().layout().size()),
"{}.", msg);

Ok(Self {
Expand Down
15 changes: 3 additions & 12 deletions iceoryx2/src/port/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,24 +552,15 @@ impl<Service: service::Service, MessageType: Debug> Publisher<Service, MessageTy
number_of_samples: usize,
static_config: &publish_subscribe::StaticConfig,
) -> Result<Service::SharedMemory, SharedMemoryCreateError> {
let allocator_config = shm_allocator::pool_allocator::Config {
bucket_layout:
// # SAFETY: type_size and type_alignment are acquired via
// core::mem::{size_of|align_of}
unsafe {
Layout::from_size_align_unchecked(
static_config.type_size,
static_config.type_alignment,
)
},
};
let l = static_config.type_details.layout();
let allocator_config = shm_allocator::pool_allocator::Config { bucket_layout: l };

Ok(fail!(from "Publisher::create_data_segment()",
when <<Service::SharedMemory as SharedMemory<PoolAllocator>>::Builder as NamedConceptBuilder<
Service::SharedMemory,
>>::new(&data_segment_name(port_id))
.config(&data_segment_config::<Service>(global_config))
.size(static_config.type_size * number_of_samples + static_config.type_alignment - 1)
.size(l.size() * number_of_samples + l.align() - 1)
.create(&allocator_config),
"Unable to create the data segment."))
}
Expand Down
25 changes: 11 additions & 14 deletions iceoryx2/src/service/builder/publish_subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
//!
use std::marker::PhantomData;

use crate::message::Message;
use crate::service;
use crate::service::dynamic_config::publish_subscribe::DynamicConfigSettings;
use crate::service::header::publish_subscribe::Header;
Expand All @@ -30,6 +29,8 @@ use iceoryx2_cal::dynamic_storage::DynamicStorageCreateError;
use iceoryx2_cal::serialize::Serialize;
use iceoryx2_cal::static_storage::StaticStorageLocked;

use self::static_config::publish_subscribe::TypeDetails;

use super::ServiceState;

/// Errors that can occur when an existing [`MessagingPattern::PublishSubscribe`] [`Service`] shall be opened.
Expand Down Expand Up @@ -165,10 +166,10 @@ impl<ServiceType: service::Service> Builder<ServiceType> {
) -> Result<Option<(StaticConfig, ServiceType::StaticStorage)>, ServiceAvailabilityState> {
match self.base.is_service_available() {
Ok(Some((config, storage))) => {
if config.publish_subscribe().type_name != self.config_details().type_name {
if config.publish_subscribe().type_details != self.config_details().type_details {
fail!(from self, with ServiceAvailabilityState::IncompatibleTypes,
"{} since the service offers the type \"{}\" but the requested type is \"{}\".",
error_msg, &config.publish_subscribe().type_name , self.config_details().type_name);
"{} since the service offers the type \"{:?}\" but the requested type is \"{:?}\".",
error_msg, &config.publish_subscribe().type_details , self.config_details().type_details);
}

Ok(Some((config, storage)))
Expand All @@ -178,13 +179,6 @@ impl<ServiceType: service::Service> Builder<ServiceType> {
}
}

fn finalize_config<MessageType: Debug>(&mut self) {
self.config_details_mut().type_name = std::any::type_name::<MessageType>().to_string();
self.config_details_mut().type_size = core::mem::size_of::<Message<Header, MessageType>>();
self.config_details_mut().type_alignment =
core::mem::align_of::<Message<Header, MessageType>>();
}

/// If the [`Service`] is created, defines the overflow behavior of the service. If an existing
/// [`Service`] is opened it requires the service to have the defined overflow behavior.
pub fn enable_safe_overflow(mut self, value: bool) -> Self {
Expand Down Expand Up @@ -359,7 +353,8 @@ impl<MessageType: Debug, ServiceType: service::Service> TypedBuilder<MessageType
PublishSubscribeOpenOrCreateError,
> {
let msg = "Unable to open or create publish subscribe service";
self.builder.finalize_config::<MessageType>();
self.builder.config_details_mut().type_details =
TypeDetails::from_type::<MessageType, Header>();

match self.builder.is_service_available(msg) {
Ok(Some(_)) => Ok(self.open()?),
Expand Down Expand Up @@ -401,7 +396,8 @@ impl<MessageType: Debug, ServiceType: service::Service> TypedBuilder<MessageType
) -> Result<publish_subscribe::PortFactory<ServiceType, MessageType>, PublishSubscribeOpenError>
{
let msg = "Unable to open publish subscribe service";
self.builder.finalize_config::<MessageType>();
self.builder.config_details_mut().type_details =
TypeDetails::from_type::<MessageType, Header>();

let mut adaptive_wait = fail!(from self, when AdaptiveWaitBuilder::new().create(),
with PublishSubscribeOpenError::InternalFailure,
Expand Down Expand Up @@ -484,7 +480,8 @@ impl<MessageType: Debug, ServiceType: service::Service> TypedBuilder<MessageType
self.builder.adjust_properties_to_meaningful_values();

let msg = "Unable to create publish subscribe service";
self.builder.finalize_config::<MessageType>();
self.builder.config_details_mut().type_details =
TypeDetails::from_type::<MessageType, Header>();

if !self.builder.config_details().enable_safe_overflow
&& (self.builder.config_details().subscriber_max_buffer_size
Expand Down
65 changes: 47 additions & 18 deletions iceoryx2/src/service/static_config/publish_subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
//! # }
//! ```
use crate::config;
use std::alloc::Layout;

use crate::{config, message::Message};
use serde::{Deserialize, Serialize};

/// The static configuration of an
Expand All @@ -49,9 +51,40 @@ pub struct StaticConfig {
pub(crate) subscriber_max_buffer_size: usize,
pub(crate) subscriber_max_borrowed_samples: usize,
pub(crate) enable_safe_overflow: bool,
pub(crate) type_name: String,
pub(crate) type_size: usize,
pub(crate) type_alignment: usize,
pub(crate) type_details: TypeDetails,
}

#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct Typed {
pub type_name: String,
pub type_size: usize,
pub type_alignment: usize,
}

#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum TypeDetails {
Typed { typed: Typed },
}

impl TypeDetails {
pub fn from_type<MessageType, Header>() -> Self {
Self::Typed {
typed: Typed {
type_name: core::any::type_name::<MessageType>().to_string(),
type_size: core::mem::size_of::<Message<Header, MessageType>>(),
type_alignment: core::mem::align_of::<Message<Header, MessageType>>(),
},
}
}

pub fn layout(&self) -> Layout {
match self {
Self::Typed { typed: d } => unsafe {
Layout::from_size_align_unchecked(d.type_size, d.type_alignment)
},
}
}
}

impl StaticConfig {
Expand All @@ -69,9 +102,13 @@ impl StaticConfig {
.publish_subscribe
.subscriber_max_borrowed_samples,
enable_safe_overflow: config.defaults.publish_subscribe.enable_safe_overflow,
type_name: String::new(),
type_size: 0,
type_alignment: 0,
type_details: TypeDetails::Typed {
typed: Typed {
type_name: String::new(),
type_size: 0,
type_alignment: 0,
},
},
}
}

Expand Down Expand Up @@ -109,16 +146,8 @@ impl StaticConfig {
self.enable_safe_overflow
}

/// Returns the type name of the [`crate::service::Service`].
pub fn type_name(&self) -> &str {
&self.type_name
}

pub fn type_size(&self) -> usize {
self.type_size
}

pub fn type_alignment(&self) -> usize {
self.type_alignment
/// Returns the type details of the [`crate::service::Service`].
pub fn type_details(&self) -> &TypeDetails {
&self.type_details
}
}
11 changes: 8 additions & 3 deletions iceoryx2/tests/service_publish_subscribe_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod service_publish_subscribe {
use iceoryx2::service::builder::publish_subscribe::PublishSubscribeCreateError;
use iceoryx2::service::builder::publish_subscribe::PublishSubscribeOpenError;
use iceoryx2::service::port_factory::publisher::UnableToDeliverStrategy;
use iceoryx2::service::static_config::publish_subscribe::TypeDetails;
use iceoryx2::service::static_config::StaticConfig;
use iceoryx2::service::Service;
use iceoryx2_bb_posix::unique_system_id::UniqueSystemId;
Expand Down Expand Up @@ -499,9 +500,13 @@ mod service_publish_subscribe {

type MessageType = Message<iceoryx2::service::header::publish_subscribe::Header, u64>;

assert_that!(sut.static_config().type_name(), eq "u64");
assert_that!(sut.static_config().type_size(), eq std::mem::size_of::<MessageType>());
assert_that!(sut.static_config().type_alignment(), eq std::mem::align_of::<MessageType>());
if let TypeDetails::Typed { typed: d } = sut.static_config().type_details() {
assert_that!(d.type_name, eq "u64");
assert_that!(d.type_size, eq std::mem::size_of::<MessageType>());
assert_that!(d.type_alignment, eq std::mem::align_of::<MessageType>());
} else {
assert_that!(true, eq false);
}
}

#[test]
Expand Down

0 comments on commit d18a304

Please sign in to comment.