use core::{fmt::Debug, hash::Hash};
use iceoryx2_bb_derive_macros::ZeroCopySend;
use iceoryx2_bb_elementary_traits::zero_copy_send::ZeroCopySend;
use iceoryx2_log::fail;
use serde::{Deserialize, Serialize};
use crate::{
config::Config,
node::node_name::NodeName,
port::port_name::PortName,
service::{self, service_name::ServiceName},
};
pub mod unique_system_id;
#[repr(C)]
#[derive(
Debug, Eq, PartialEq, Hash, PartialOrd, Ord, Clone, Copy, Serialize, Deserialize, ZeroCopySend,
)]
pub struct UniqueId {
payload_value: u64,
unique_value: u64,
}
impl UniqueId {
pub unsafe fn from_raw_id(value: u128) -> Self {
Self {
payload_value: value as u64,
unique_value: (value >> 64) as u64,
}
}
pub fn value(&self) -> u128 {
(self.unique_value as u128) << 64 | (self.payload_value as u128)
}
pub fn payload_value(&self) -> u64 {
self.payload_value
}
pub fn unique_value(&self) -> u64 {
self.unique_value
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UniqueIdGeneratorGenerateError {
GenerationError,
}
impl core::fmt::Display for UniqueIdGeneratorGenerateError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "UniqueIdGeneratorGenerateError::{self:?}")
}
}
impl core::error::Error for UniqueIdGeneratorGenerateError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum UniqueIdGeneratorDetailsError {
NotImplemented,
}
impl core::fmt::Display for UniqueIdGeneratorDetailsError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "UniqueIdGeneratorDetailsError::{self:?}")
}
}
impl core::error::Error for UniqueIdGeneratorDetailsError {}
pub trait UniqueIdGenerator {
fn generate<Service: service::Service>(
entity: Entity,
config: &Config,
) -> Result<UniqueId, UniqueIdGeneratorGenerateError>;
fn pid(
_id: UniqueId,
) -> Result<iceoryx2_bb_posix::process::ProcessId, UniqueIdGeneratorDetailsError> {
fail!(from "UniqueIdGenerator::pid()", with UniqueIdGeneratorDetailsError::NotImplemented,
"pid() is not implemented");
}
fn creation_time(
_id: UniqueId,
) -> Result<iceoryx2_bb_posix::clock::Time, UniqueIdGeneratorDetailsError> {
fail!(from "UniqueIdGenerator::creation_time()",
with UniqueIdGeneratorDetailsError::NotImplemented, "creation_time() not implemented");
}
}
pub enum Entity {
Node(NodeName),
PubSubService(ServiceName),
ReqResService(ServiceName),
EventService(ServiceName),
BlackboardService(ServiceName),
Publisher(PortName),
Subscriber(PortName),
Client(PortName),
Server(PortName),
Notifier(PortName),
Listener(PortName),
Writer(PortName),
Reader(PortName),
}