extern crate alloc;
use alloc::sync::Arc;
use super::nodes;
use super::{publisher::PortFactoryPublisher, subscriber::PortFactorySubscriber};
use crate::identifiers::UniqueServiceId;
use crate::node::NodeListFailure;
use crate::service::attribute::AttributeSet;
use crate::service::port_factory::blocking_cleanup_dead_nodes_in_service;
use crate::service::service_hash::ServiceHash;
use crate::service::service_name::ServiceName;
use crate::service::{
self, NoResource, ServiceState, SharedServiceState, dynamic_config, static_config,
};
use core::ptr::NonNull;
use core::{fmt::Debug, marker::PhantomData};
use iceoryx2_bb_elementary::CallbackProgression;
use iceoryx2_bb_elementary_traits::non_null::NonNullCompat;
use iceoryx2_bb_elementary_traits::testing::abandonable::Abandonable;
use iceoryx2_bb_elementary_traits::zero_copy_send::ZeroCopySend;
use iceoryx2_cal::dynamic_storage::DynamicStorage;
#[derive(Debug)]
pub struct PortFactory<
Service: service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: Debug + ZeroCopySend,
> {
pub(crate) service: SharedServiceState<Service, NoResource>,
_payload: PhantomData<Payload>,
_user_header: PhantomData<UserHeader>,
}
unsafe impl<
Service: service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: Debug + ZeroCopySend,
> Send for PortFactory<Service, Payload, UserHeader>
{
}
unsafe impl<
Service: service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: Debug + ZeroCopySend,
> Sync for PortFactory<Service, Payload, UserHeader>
{
}
impl<
Service: service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: Debug + ZeroCopySend,
> Abandonable for PortFactory<Service, Payload, UserHeader>
{
unsafe fn abandon_in_place(mut this: NonNull<Self>) {
let this = unsafe { this.as_mut() };
unsafe { SharedServiceState::abandon_in_place(NonNull::iox2_from_mut(&mut this.service)) };
}
}
impl<
Service: service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: Debug + ZeroCopySend,
> crate::service::port_factory::PortFactory for PortFactory<Service, Payload, UserHeader>
{
type Service = Service;
type StaticConfig = static_config::publish_subscribe::StaticConfig;
type DynamicConfig = dynamic_config::publish_subscribe::DynamicConfig;
fn name(&self) -> &ServiceName {
self.service.static_config().name()
}
fn unique_service_id(&self) -> UniqueServiceId {
self.service.static_config().unique_service_id()
}
fn service_hash(&self) -> &ServiceHash {
self.service.static_config().service_hash()
}
fn attributes(&self) -> &AttributeSet {
self.service.static_config().attributes()
}
fn static_config(&self) -> &static_config::publish_subscribe::StaticConfig {
self.service.static_config().publish_subscribe()
}
fn dynamic_config(&self) -> &dynamic_config::publish_subscribe::DynamicConfig {
self.service.dynamic_storage().get().publish_subscribe()
}
fn nodes<F: FnMut(crate::node::NodeState<Service>) -> CallbackProgression>(
&self,
callback: F,
) -> Result<(), NodeListFailure> {
nodes(
self.service.dynamic_storage().get(),
self.service.shared_node().config(),
callback,
)
}
}
impl<
Service: service::Service,
Payload: Debug + ZeroCopySend + ?Sized,
UserHeader: Debug + ZeroCopySend,
> PortFactory<Service, Payload, UserHeader>
{
pub(crate) fn new(service: ServiceState<Service, NoResource>) -> Self {
let shared_node = service.shared_node.clone();
let new_self = Self {
service: SharedServiceState {
state: Arc::new(service),
},
_payload: PhantomData,
_user_header: PhantomData,
};
if shared_node
.config()
.global
.service
.cleanup_dead_nodes_on_open
{
blocking_cleanup_dead_nodes_in_service(
&new_self,
shared_node.config().global.creation_timeout,
);
}
new_self
}
pub fn subscriber_builder(&self) -> PortFactorySubscriber<'_, Service, Payload, UserHeader> {
PortFactorySubscriber::new(self)
}
pub fn publisher_builder(&self) -> PortFactoryPublisher<'_, Service, Payload, UserHeader> {
PortFactoryPublisher::new(self)
}
}