Skip to content

Commit

Permalink
Merge pull request #384 from elfenpiff/iox2-264-update-connections
Browse files Browse the repository at this point in the history
[#264] update connections
  • Loading branch information
elBoberido authored Sep 19, 2024
2 parents 089661f + d37104a commit f974a6b
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 4 deletions.
8 changes: 7 additions & 1 deletion iceoryx2-ffi/cxx/include/iox2/publisher.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,13 @@ inline auto Publisher<S, Payload, UserHeader>::loan_slice_uninit(const uint64_t

template <ServiceType S, typename Payload, typename UserHeader>
inline auto Publisher<S, Payload, UserHeader>::update_connections() -> iox::expected<void, ConnectionFailure> {
IOX_TODO();
auto* ref_handle = iox2_cast_publisher_ref_h(m_handle);
auto result = iox2_publisher_update_connections(ref_handle);
if (result != IOX2_OK) {
return iox::err(iox::into<ConnectionFailure>(result));
}

return iox::ok();
}
} // namespace iox2

Expand Down
9 changes: 8 additions & 1 deletion iceoryx2-ffi/cxx/include/iox2/subscriber.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "iox2/service_type.hpp"
#include "iox2/subscriber_error.hpp"
#include "iox2/unique_port_id.hpp"
#include <cinttypes>

namespace iox2 {
/// The receiving endpoint of a publish-subscribe communication.
Expand Down Expand Up @@ -149,7 +150,13 @@ inline auto Subscriber<S, Payload, UserHeader>::receive() const

template <ServiceType S, typename Payload, typename UserHeader>
inline auto Subscriber<S, Payload, UserHeader>::update_connections() const -> iox::expected<void, ConnectionFailure> {
IOX_TODO();
auto* ref_handle = iox2_cast_subscriber_ref_h(m_handle);
auto result = iox2_subscriber_update_connections(ref_handle);
if (result != IOX2_OK) {
return iox::err(iox::into<ConnectionFailure>(result));
}

return iox::ok();
}

} // namespace iox2
Expand Down
25 changes: 25 additions & 0 deletions iceoryx2-ffi/cxx/tests/src/service_publish_subscribe_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,31 @@ TYPED_TEST(ServicePublishSubscribeTest, loan_send_receive_works) {
ASSERT_THAT(**recv_sample, Eq(payload));
}

TYPED_TEST(ServicePublishSubscribeTest, update_connections_delivers_history) {
constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE;

const auto* name_value = "Whoop here it is - the publishers historyyyy!";
const auto service_name = ServiceName::create(name_value).expect("");

auto node = NodeBuilder().create<SERVICE_TYPE>().expect("");
auto service = node.service_builder(service_name).template publish_subscribe<uint64_t>().create().expect("");

auto sut_publisher = service.publisher_builder().create().expect("");
const uint64_t payload = 123;
sut_publisher.send_copy(payload).expect("");

auto sut_subscriber = service.subscriber_builder().create().expect("");
auto sample = sut_subscriber.receive().expect("");

ASSERT_FALSE(sample.has_value());

ASSERT_TRUE(sut_publisher.update_connections().has_value());
sample = sut_subscriber.receive().expect("");

ASSERT_TRUE(sample.has_value());
ASSERT_THAT(**sample, Eq(payload));
}

TYPED_TEST(ServicePublishSubscribeTest, setting_service_properties_works) {
constexpr ServiceType SERVICE_TYPE = TestFixture::TYPE;
constexpr uint64_t NUMBER_OF_NODES = 10;
Expand Down
33 changes: 33 additions & 0 deletions iceoryx2-ffi/ffi/src/api/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
};

use iceoryx2::port::publisher::{Publisher, PublisherLoanError, PublisherSendError};
use iceoryx2::port::update_connections::UpdateConnections;
use iceoryx2::prelude::*;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_ffi_macros::iceoryx2_ffi;
Expand Down Expand Up @@ -419,6 +420,38 @@ pub unsafe extern "C" fn iox2_publisher_loan(
}
}

/// Updates all connections to new and obsolete subscriber ports and automatically delivery the history if
/// requested.
///
/// # Arguments
///
/// * `publisher_handle` - Must be a valid [`iox2_publisher_ref_h`]
/// obtained by [`iox2_port_factory_publisher_builder_create`](crate::iox2_port_factory_publisher_builder_create) and
/// casted by [`iox2_cast_publisher_ref_h`].
///
/// # Safety
///
/// * The `publisher_handle` is still valid after the return of this function and can be use in another function call.
#[no_mangle]
pub unsafe extern "C" fn iox2_publisher_update_connections(
publisher_handle: iox2_publisher_ref_h,
) -> c_int {
debug_assert!(!publisher_handle.is_null());

let publisher = &mut *publisher_handle.as_type();

match publisher.service_type {
iox2_service_type_e::IPC => match publisher.value.as_ref().ipc.update_connections() {
Ok(()) => IOX2_OK,
Err(error) => error.into_c_int(),
},
iox2_service_type_e::LOCAL => match publisher.value.as_ref().local.update_connections() {
Ok(()) => IOX2_OK,
Err(error) => error.into_c_int(),
},
}
}

/// This function needs to be called to destroy the publisher!
///
/// # Arguments
Expand Down
33 changes: 32 additions & 1 deletion iceoryx2-ffi/ffi/src/api/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::api::{
use crate::{iox2_unique_subscriber_id_h, iox2_unique_subscriber_id_t};

use iceoryx2::port::subscriber::{Subscriber, SubscriberReceiveError};
use iceoryx2::port::update_connections::ConnectionFailure;
use iceoryx2::port::update_connections::{ConnectionFailure, UpdateConnections};
use iceoryx2::prelude::*;
use iceoryx2_bb_elementary::static_assert::*;
use iceoryx2_ffi_macros::iceoryx2_ffi;
Expand Down Expand Up @@ -358,6 +358,37 @@ pub unsafe extern "C" fn iox2_subscriber_has_samples(
}
}

/// Updates all connections to new and obsolete publisher ports
///
/// # Arguments
///
/// * `subscriber_handle` - Must be a valid [`iox2_subscriber_ref_h`]
/// obtained by [`iox2_port_factory_subscriber_builder_create`](crate::iox2_port_factory_subscriber_builder_create) and
/// casted by [`iox2_cast_subscriber_ref_h`].
///
/// # Safety
///
/// * The `subscriber_handle` is still valid after the return of this function and can be use in another function call.
#[no_mangle]
pub unsafe extern "C" fn iox2_subscriber_update_connections(
subscriber_handle: iox2_subscriber_ref_h,
) -> c_int {
debug_assert!(!subscriber_handle.is_null());

let subscriber = &mut *subscriber_handle.as_type();

match subscriber.service_type {
iox2_service_type_e::IPC => match subscriber.value.as_ref().ipc.update_connections() {
Ok(()) => IOX2_OK,
Err(error) => error.into_c_int(),
},
iox2_service_type_e::LOCAL => match subscriber.value.as_ref().local.update_connections() {
Ok(()) => IOX2_OK,
Err(error) => error.into_c_int(),
},
}
}

/// This function needs to be called to destroy the subscriber!
///
/// # Arguments
Expand Down
2 changes: 1 addition & 1 deletion iceoryx2/src/port/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ impl<Service: service::Service, Payload: Debug, UserHeader: Debug>
// END: sliced API
////////////////////////

impl<Service: service::Service, Payload: Debug, UserHeader: Debug> UpdateConnections
impl<Service: service::Service, Payload: Debug + ?Sized, UserHeader: Debug> UpdateConnections
for Publisher<Service, Payload, UserHeader>
{
fn update_connections(&self) -> Result<(), ConnectionFailure> {
Expand Down

0 comments on commit f974a6b

Please sign in to comment.