Skip to content

Commit

Permalink
Merge pull request #55 from elfenpiff/iox2-16-same-name-different-mes…
Browse files Browse the repository at this point in the history
…saging-pattern-allowed

[#16] same name different messaging pattern allowed
  • Loading branch information
elfenpiff authored Dec 27, 2023
2 parents 03a4ec2 + ba8b1bf commit b11a41d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 9 deletions.
1 change: 1 addition & 0 deletions doc/release-notes/iceoryx2-unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### Features

* MacOS Platform support [#51](https://github.com/eclipse-iceoryx/iceoryx2/issues/51)
* Services with the same name for different messaging patterns are supported [#16](https://github.com/eclipse-iceoryx/iceoryx2/issues/16)

### Bugfixes

Expand Down
4 changes: 2 additions & 2 deletions iceoryx2/src/port/event_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ pub struct EventId(u64);

impl EventId {
/// Creates a new [`EventId`] from a given integer value.
pub fn new(value: u64) -> Self {
pub const fn new(value: u64) -> Self {
EventId(value)
}

/// Returns the underlying integer value of the [`EventId`].
pub fn as_u64(&self) -> u64 {
pub const fn as_u64(&self) -> u64 {
self.0
}
}
Expand Down
9 changes: 9 additions & 0 deletions iceoryx2/src/service/messaging_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ pub enum MessagingPattern {
Event(event::StaticConfig),
}

impl From<MessagingPattern> for u32 {
fn from(value: MessagingPattern) -> Self {
match value {
MessagingPattern::Event(_) => 0,
MessagingPattern::PublishSubscribe(_) => 1,
}
}
}

impl MessagingPattern {
pub(crate) fn is_same_pattern(&self, rhs: &MessagingPattern) -> bool {
match self {
Expand Down
4 changes: 3 additions & 1 deletion iceoryx2/src/service/service_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct ServiceName {
}

impl ServiceName {
/// Creates a new [`ServiceName`]. The name is not allowed to be empty.
pub fn new(name: &str) -> Result<Self, SemanticStringError> {
if name.is_empty() {
return Err(SemanticStringError::InvalidName);
Expand All @@ -45,7 +46,8 @@ impl ServiceName {
})
}

fn as_str(&self) -> &str {
/// Returns a str reference to the [`ServiceName`]
pub fn as_str(&self) -> &str {
// SAFETY: `ServieName` was created from a `&str` and therefore this conversion is safe
unsafe { std::str::from_utf8_unchecked(self.value.as_bytes()) }
}
Expand Down
23 changes: 17 additions & 6 deletions iceoryx2/src/service/static_config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,39 @@ pub struct StaticConfig {
pub(crate) messaging_pattern: MessagingPattern,
}

fn create_uuid<Hasher: Hash>(
service_name: &ServiceName,
messaging_pattern: &MessagingPattern,
) -> Hasher {
let pattern_and_service = (<MessagingPattern as Into<u32>>::into(messaging_pattern.clone()))
.to_string()
+ service_name.as_str();
Hasher::new(pattern_and_service.as_bytes())
}

impl StaticConfig {
pub(crate) fn new_event<Hasher: Hash>(
service_name: &ServiceName,
config: &config::Config,
) -> Self {
let messaging_pattern = MessagingPattern::Event(event::StaticConfig::new(config));
Self {
uuid: Hasher::new(service_name.as_bytes()).as_hex_string(),
uuid: create_uuid::<Hasher>(service_name, &messaging_pattern).as_hex_string(),
service_name: *service_name,
messaging_pattern: MessagingPattern::Event(event::StaticConfig::new(config)),
messaging_pattern,
}
}

pub(crate) fn new_publish_subscribe<Hasher: Hash>(
service_name: &ServiceName,
config: &config::Config,
) -> Self {
let messaging_pattern =
MessagingPattern::PublishSubscribe(publish_subscribe::StaticConfig::new(config));
Self {
uuid: Hasher::new(service_name.as_bytes()).as_hex_string(),
uuid: create_uuid::<Hasher>(service_name, &messaging_pattern).as_hex_string(),
service_name: *service_name,
messaging_pattern: MessagingPattern::PublishSubscribe(
publish_subscribe::StaticConfig::new(config),
),
messaging_pattern,
}
}

Expand Down
62 changes: 62 additions & 0 deletions iceoryx2/tests/service_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) 2023 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache Software License 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
// which is available at https://opensource.org/licenses/MIT.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT

#[generic_tests::define]
mod service {
use iceoryx2::port::event_id::EventId;
use iceoryx2::service::{service_name::ServiceName, Service};
use iceoryx2_bb_posix::unique_system_id::UniqueSystemId;
use iceoryx2_bb_testing::assert_that;

fn generate_name() -> ServiceName {
ServiceName::new(&format!(
"service_tests_{}",
UniqueSystemId::new().unwrap().value()
))
.unwrap()
}

#[test]
fn same_name_with_different_messaging_pattern_is_allowed<Sut: Service>() {
let service_name = generate_name();
let sut_pub_sub = Sut::new(&service_name).publish_subscribe().create::<u64>();
assert_that!(sut_pub_sub, is_ok);
let sut_pub_sub = sut_pub_sub.unwrap();

let sut_event = Sut::new(&service_name).event().create();
assert_that!(sut_event, is_ok);
let sut_event = sut_event.unwrap();

let sut_subscriber = sut_pub_sub.subscriber().create().unwrap();
let sut_publisher = sut_pub_sub.publisher().create().unwrap();

let mut sut_listener = sut_event.listener().create().unwrap();
let sut_notifier = sut_event.notifier().create().unwrap();

const SAMPLE_VALUE: u64 = 891231211;
sut_publisher.send_copy(SAMPLE_VALUE).unwrap();
let received_sample = sut_subscriber.receive().unwrap().unwrap();
assert_that!(*received_sample, eq SAMPLE_VALUE);

const EVENT_ID: EventId = EventId::new(10123101301);
sut_notifier.notify_with_custom_event_id(EVENT_ID).unwrap();
let received_event = sut_listener.try_wait().unwrap();
assert_that!(received_event, len 1);
assert_that!(received_event[0], eq EVENT_ID);
}

#[instantiate_tests(<iceoryx2::service::zero_copy::Service>)]
mod zero_copy {}

#[instantiate_tests(<iceoryx2::service::process_local::Service>)]
mod process_local {}
}

0 comments on commit b11a41d

Please sign in to comment.