use alloc::format;
use serde::{Deserialize, Serialize};
use crate::port::port_name::PortName;
use crate::service::service_name::ServiceName;
use crate::{config::Config, node::node_name::NodeName, unique_id_generator::*};
use iceoryx2_bb_derive_macros::ZeroCopySend;
use iceoryx2_bb_elementary_traits::zero_copy_send::ZeroCopySend;
use iceoryx2_bb_lock_free::mpmc::robust_unique_index_set::OwnerId;
use iceoryx2_log::fail;
macro_rules! generate_id {
{ $(#[$documentation:meta])*
$id_name:ident
$entity:ident
$name_type:ident} => {
$(#[$documentation])*
#[repr(C)]
#[derive(
Debug,
Eq,
Hash,
PartialEq,
Clone,
Copy,
PartialOrd,
Ord,
ZeroCopySend,
serde::Serialize,
serde::Deserialize,
)]
pub struct $id_name(pub(crate) UniqueId);
impl core::fmt::Display for $id_name {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:x}", self.0.value())
}
}
impl $id_name {
pub(crate) fn new<Service: crate::service::Service>(
name: $name_type,
config: &Config
) -> Result<Self, UniqueIdGeneratorGenerateError> {
let id = fail!(from format!("{}::new()", stringify!($id_name)),
when Service::UniqueId::generate::<Service>(Entity::$entity(name), config),
with UniqueIdGeneratorGenerateError::GenerationError,
": Unable to generate required {}.", stringify!($id_name));
Ok(Self(id))
}
pub fn value(&self) -> u128 {
self.0.value()
}
}
};
}
generate_id! {
UniquePublisherId
Publisher
PortName
}
generate_id! {
UniqueSubscriberId
Subscriber
PortName
}
generate_id! {
UniqueNotifierId
Notifier
PortName
}
generate_id! {
UniqueListenerId
Listener
PortName
}
generate_id! {
UniqueClientId
Client
PortName
}
generate_id! {
UniqueServerId
Server
PortName
}
generate_id! {
UniqueReaderId
Reader
PortName
}
generate_id! {
UniqueWriterId
Writer
PortName
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum UniquePortId {
Publisher(UniquePublisherId),
Subscriber(UniqueSubscriberId),
Notifier(UniqueNotifierId),
Listener(UniqueListenerId),
Client(UniqueClientId),
Server(UniqueServerId),
Reader(UniqueReaderId),
Writer(UniqueWriterId),
}
impl UniquePortId {
pub fn value(&self) -> u128 {
match self {
UniquePortId::Publisher(v) => v.value(),
UniquePortId::Subscriber(v) => v.value(),
UniquePortId::Notifier(v) => v.value(),
UniquePortId::Listener(v) => v.value(),
UniquePortId::Client(v) => v.value(),
UniquePortId::Server(v) => v.value(),
UniquePortId::Reader(v) => v.value(),
UniquePortId::Writer(v) => v.value(),
}
}
}
#[repr(C)]
#[derive(
Debug,
Eq,
Hash,
PartialEq,
Clone,
Copy,
PartialOrd,
Ord,
ZeroCopySend,
serde::Serialize,
serde::Deserialize,
)]
pub struct UniqueServiceId(pub(crate) UniqueId);
impl core::fmt::Display for UniqueServiceId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:x}", self.0.value())
}
}
impl UniqueServiceId {
pub(crate) fn from_publish_subscribe_service<Service: crate::service::Service>(
name: &ServiceName,
config: &Config,
) -> Result<Self, UniqueIdGeneratorGenerateError> {
let id = fail!(
from "UniqueServiceId::from_publish_subscribe_service()",
when Service::UniqueId::generate::<Service>(Entity::PubSubService(*name), config),
with UniqueIdGeneratorGenerateError::GenerationError,
": Unable to generate required UniqueServiceId."
);
Ok(Self(id))
}
pub(crate) fn from_request_response_service<Service: crate::service::Service>(
name: &ServiceName,
config: &Config,
) -> Result<Self, UniqueIdGeneratorGenerateError> {
let id = fail!(
from "UniqueServiceId::from_request_response_service()",
when Service::UniqueId::generate::<Service>(Entity::ReqResService(*name), config),
with UniqueIdGeneratorGenerateError::GenerationError,
": Unable to generate required UniqueServiceId."
);
Ok(Self(id))
}
pub(crate) fn from_event_service<Service: crate::service::Service>(
name: &ServiceName,
config: &Config,
) -> Result<Self, UniqueIdGeneratorGenerateError> {
let id = fail!(
from "UniqueServiceId::from_event_service()",
when Service::UniqueId::generate::<Service>(Entity::EventService(*name), config),
with UniqueIdGeneratorGenerateError::GenerationError,
": Unable to generate required UniqueServiceId."
);
Ok(Self(id))
}
pub(crate) fn from_blackboard_service<Service: crate::service::Service>(
name: &ServiceName,
config: &Config,
) -> Result<Self, UniqueIdGeneratorGenerateError> {
let id = fail!(
from "UniqueServiceId::from_blackboard_service()",
when Service::UniqueId::generate::<Service>(Entity::BlackboardService(*name), config),
with UniqueIdGeneratorGenerateError::GenerationError,
": Unable to generate required UniqueServiceId."
);
Ok(Self(id))
}
pub fn value(&self) -> u128 {
self.0.value()
}
}
#[repr(C)]
#[derive(
Debug,
Eq,
Hash,
PartialEq,
Clone,
Copy,
PartialOrd,
Ord,
ZeroCopySend,
serde::Serialize,
serde::Deserialize,
)]
pub struct UniqueNodeId(pub(crate) UniqueId);
impl core::fmt::Display for UniqueNodeId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:x}", self.0.value())
}
}
impl UniqueNodeId {
pub(crate) fn new<Service: crate::service::Service>(
name: NodeName,
config: &Config,
) -> Result<Self, UniqueIdGeneratorGenerateError> {
let id = fail!(from "UniqueNodeId::new()",
when Service::UniqueId::generate::<Service>(Entity::Node(name), config),
with UniqueIdGeneratorGenerateError::GenerationError,
"Unable to generate required UniqueNodeId.");
Ok(Self(id))
}
pub fn value(&self) -> u128 {
self.0.value()
}
pub fn pid<Service: crate::service::Service>(
&self,
) -> Result<iceoryx2_bb_posix::process::ProcessId, UniqueIdGeneratorDetailsError> {
Service::UniqueId::pid(self.0)
}
pub fn creation_time<Service: crate::service::Service>(
&self,
) -> Result<iceoryx2_bb_posix::clock::Time, UniqueIdGeneratorDetailsError> {
Service::UniqueId::creation_time(self.0)
}
pub(crate) fn owner_id(&self) -> OwnerId {
OwnerId::new(self.0.unique_value()).expect("The unique node id is never u64::MAX.")
}
}