use iceoryx2::node::{NodeState as IoxNodeState, NodeView};
use iceoryx2::prelude::*;
use iceoryx2::service::static_config::messaging_pattern::MessagingPattern;
use iceoryx2::service::ServiceDetails;
use crate::labels::impl_labels;
use crate::types::RpcError;
type Iox = iceoryx2::service::ipc_threadsafe::Service;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NodeHealth {
Alive,
Dead,
Inaccessible,
Undefined,
}
impl NodeHealth {}
impl_labels!(NodeHealth {
NodeHealth::Alive => "alive",
NodeHealth::Dead => "dead",
NodeHealth::Inaccessible => "inaccessible",
NodeHealth::Undefined => "undefined",
});
#[derive(Debug, Clone)]
pub struct NodeInfo {
pub pid: u32,
pub health: NodeHealth,
pub executable: Option<String>,
pub name: Option<String>,
}
pub fn list_nodes() -> Option<Vec<NodeInfo>> {
let config = crate::config::build_iceoryx2_config();
let mut nodes = Vec::new();
let result = Node::<Iox>::list(&config, |state| {
let pid = crate::types::node_pid_to_u32(state.node_id().pid::<Iox>());
let (health, details) = match &state {
IoxNodeState::Alive(view) => (NodeHealth::Alive, view.details()),
IoxNodeState::Dead(view) => (NodeHealth::Dead, view.details()),
IoxNodeState::Inaccessible(_) => (NodeHealth::Inaccessible, &None),
IoxNodeState::Undefined(_) => (NodeHealth::Undefined, &None),
};
let (executable, name) = match details {
Some(details) => (
Some(String::from_utf8_lossy(details.executable().as_bytes()).into_owned()),
Some(String::from_utf8_lossy(details.name().as_bytes()).into_owned()),
),
None => (None, None),
};
nodes.push(NodeInfo {
pid,
health,
executable,
name,
});
CallbackProgression::Continue
});
match result {
Ok(()) => Some(nodes),
Err(e) => {
log::debug!("[monitor] Node::list failed: {e:?}");
None
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ServiceRole {
Request,
Response,
RequestNotify,
ResponseNotify,
Other,
}
impl ServiceRole {}
impl_labels!(ServiceRole {
ServiceRole::Request => "req",
ServiceRole::Response => "resp",
ServiceRole::RequestNotify => "req_notify",
ServiceRole::ResponseNotify => "resp_notify",
ServiceRole::Other => "other",
});
#[derive(Debug, Clone)]
pub struct ServiceInfo {
pub name: String,
pub pattern: &'static str,
pub role: ServiceRole,
pub participants: usize,
}
fn role_of(name: &str) -> ServiceRole {
if name.ends_with("_req_notify") {
ServiceRole::RequestNotify
} else if name.ends_with("_resp_notify") {
ServiceRole::ResponseNotify
} else if name.ends_with("_req") {
ServiceRole::Request
} else if name.ends_with("_resp") {
ServiceRole::Response
} else {
ServiceRole::Other
}
}
pub fn list_services() -> Result<Vec<ServiceInfo>, RpcError> {
let config = crate::config::build_iceoryx2_config();
let mut services = Vec::new();
<Iox as Service>::list(&config, |details: ServiceDetails<Iox>| {
let name = details.static_details.name().as_str().to_owned();
let pattern = match details.static_details.messaging_pattern() {
MessagingPattern::PublishSubscribe(_) => "PublishSubscribe",
MessagingPattern::Event(_) => "Event",
MessagingPattern::RequestResponse(_) => "RequestResponse",
MessagingPattern::Blackboard(_) => "Blackboard",
_ => "Other",
};
let participants = details
.dynamic_details
.as_ref()
.map(|details| details.nodes.len())
.unwrap_or(0);
let role = role_of(&name);
services.push(ServiceInfo {
name,
pattern,
role,
participants,
});
CallbackProgression::Continue
})
.map_err(|e| RpcError::TransportError(format!("service inventory: {e:?}")))?;
Ok(services)
}
#[derive(Debug, Clone)]
pub struct Iceoryx2Layout {
pub root_path: String,
pub service_dir: String,
pub node_dir: String,
pub prefix: String,
pub data_segment_suffix: String,
}
pub fn iceoryx2_layout() -> Iceoryx2Layout {
let config = crate::config::build_iceoryx2_config();
let global = &config.global;
Iceoryx2Layout {
root_path: String::from_utf8_lossy(global.root_path().as_bytes()).into_owned(),
service_dir: String::from_utf8_lossy(global.service_dir().as_bytes()).into_owned(),
node_dir: String::from_utf8_lossy(global.node_dir().as_bytes()).into_owned(),
prefix: String::from_utf8_lossy(global.prefix.as_bytes()).into_owned(),
data_segment_suffix: String::from_utf8_lossy(global.service.data_segment_suffix.as_bytes())
.into_owned(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_role_is_deduced_from_the_service_name() {
assert_eq!(role_of("Db_req"), ServiceRole::Request);
assert_eq!(role_of("Db_resp"), ServiceRole::Response);
assert_eq!(role_of("Db_req_notify"), ServiceRole::RequestNotify);
assert_eq!(role_of("Db_resp_notify"), ServiceRole::ResponseNotify);
assert_eq!(role_of("something_else"), ServiceRole::Other);
}
#[test]
fn the_labels_are_stable() {
assert_eq!(NodeHealth::Alive.label(), "alive");
assert_eq!(NodeHealth::Dead.label(), "dead");
assert_eq!(ServiceRole::Request.label(), "req");
assert_eq!(ServiceRole::ResponseNotify.label(), "resp_notify");
}
#[test]
fn the_layout_exposes_the_paths_iceoryx2_uses() {
let layout = iceoryx2_layout();
assert!(!layout.root_path.is_empty());
assert!(!layout.prefix.is_empty());
assert!(layout.data_segment_suffix.starts_with('.'));
}
}