1use core::ops::Deref;
14
15use iceoryx2::node::NodeDetails as IceoryxNodeDetails;
16use iceoryx2::node::NodeId as IceoryxNodeId;
17use iceoryx2::node::NodeState as IceoryxNodeState;
18use iceoryx2::node::NodeView as IceoryxNodeView;
19use iceoryx2::service::attribute::AttributeSet as IceoryxAttributeSet;
20use iceoryx2::service::static_config::messaging_pattern::MessagingPattern as IceoryxMessagingPattern;
21use iceoryx2::service::Service as IceoryxService;
22use iceoryx2::service::ServiceDetails as IceoryxServiceDetails;
23use iceoryx2::service::ServiceDynamicDetails as IceoryxServiceDynamicDetails;
24use iceoryx2_pal_posix::posix::pid_t;
25
26#[derive(serde::Serialize, Debug, Eq, PartialEq, Ord, PartialOrd)]
27pub enum ServiceDescriptor {
28 PublishSubscribe(String),
29 Event(String),
30 RequestResponse(String),
31 Undefined(String),
32}
33
34impl<T> From<&IceoryxServiceDetails<T>> for ServiceDescriptor
35where
36 T: IceoryxService,
37{
38 fn from(service: &IceoryxServiceDetails<T>) -> Self {
39 match service.static_details.messaging_pattern() {
40 IceoryxMessagingPattern::PublishSubscribe(_) => {
41 ServiceDescriptor::PublishSubscribe(service.static_details.name().to_string())
42 }
43 IceoryxMessagingPattern::Event(_) => {
44 ServiceDescriptor::Event(service.static_details.name().to_string())
45 }
46 IceoryxMessagingPattern::RequestResponse(_) => {
47 ServiceDescriptor::RequestResponse(service.static_details.name().to_string())
48 }
49 _ => ServiceDescriptor::Undefined("Undefined".to_string()),
50 }
51 }
52}
53
54#[derive(serde::Serialize)]
55pub struct ServiceDescription {
56 pub service_id: String,
57 pub service_name: String,
58 pub attributes: IceoryxAttributeSet,
59 pub pattern: IceoryxMessagingPattern,
60 pub nodes: Option<NodeList>,
61}
62
63impl<T> From<&IceoryxServiceDetails<T>> for ServiceDescription
64where
65 T: IceoryxService,
66{
67 fn from(service: &IceoryxServiceDetails<T>) -> Self {
68 let config = &service.static_details;
69
70 ServiceDescription {
71 service_id: config.service_id().as_str().to_string(),
72 service_name: config.name().as_str().to_string(),
73 attributes: config.attributes().clone(),
74 pattern: config.messaging_pattern().clone(),
75 nodes: service.dynamic_details.as_ref().map(NodeList::from),
76 }
77 }
78}
79
80#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize)]
81pub struct NodeIdString(String);
82
83impl From<&IceoryxNodeId> for NodeIdString {
84 fn from(id: &IceoryxNodeId) -> Self {
85 NodeIdString(format!("{:032x}", id.value()))
86 }
87}
88
89impl AsRef<str> for NodeIdString {
90 fn as_ref(&self) -> &str {
91 &self.0
92 }
93}
94
95impl Deref for NodeIdString {
96 type Target = str;
97
98 fn deref(&self) -> &Self::Target {
99 &self.0
100 }
101}
102
103impl PartialEq<str> for NodeIdString {
104 fn eq(&self, other: &str) -> bool {
105 self.0 == other
106 }
107}
108
109impl PartialEq<&str> for NodeIdString {
110 fn eq(&self, other: &&str) -> bool {
111 self.0 == *other
112 }
113}
114
115#[derive(serde::Serialize)]
116pub enum NodeState {
117 Alive,
118 Dead,
119 Inaccessible,
120 Undefined,
121}
122
123#[derive(serde::Serialize)]
124pub struct NodeDescriptor {
125 state: NodeState,
126 id: NodeIdString,
127 pid: pid_t,
128 executable: Option<String>,
129 name: Option<String>,
130}
131
132impl<T> From<&IceoryxNodeState<T>> for NodeDescriptor
133where
134 T: IceoryxService,
135{
136 fn from(node: &IceoryxNodeState<T>) -> Self {
137 match node {
138 IceoryxNodeState::Alive(view) => NodeDescriptor {
139 state: NodeState::Alive,
140 id: NodeIdString::from(view.id()),
141 pid: view.id().pid().value(),
142 executable: view
143 .details()
144 .as_ref()
145 .map(|details| details.executable().to_string()),
146 name: view
147 .details()
148 .as_ref()
149 .map(|details| details.name().as_str().to_string()),
150 },
151 IceoryxNodeState::Dead(view) => NodeDescriptor {
152 state: NodeState::Dead,
153 id: NodeIdString::from(view.id()),
154 pid: view.id().pid().value(),
155 executable: view
156 .details()
157 .as_ref()
158 .map(|details| details.executable().to_string()),
159 name: view
160 .details()
161 .as_ref()
162 .map(|details| details.name().as_str().to_string()),
163 },
164 IceoryxNodeState::Inaccessible(node_id) => NodeDescriptor {
165 state: NodeState::Inaccessible,
166 id: NodeIdString::from(node_id),
167 pid: node_id.pid().value(),
168 executable: None,
169 name: None,
170 },
171 IceoryxNodeState::Undefined(node_id) => NodeDescriptor {
172 state: NodeState::Undefined,
173 id: NodeIdString::from(node_id),
174 pid: node_id.pid().value(),
175 executable: None,
176 name: None,
177 },
178 }
179 }
180}
181
182#[derive(serde::Serialize)]
183pub struct NodeDescription {
184 state: NodeState,
185 id: NodeIdString,
186 pid: pid_t,
187 #[serde(flatten)]
188 details: Option<IceoryxNodeDetails>,
189}
190
191impl<T> From<&IceoryxNodeState<T>> for NodeDescription
192where
193 T: IceoryxService,
194{
195 fn from(node: &IceoryxNodeState<T>) -> Self {
196 match node {
197 IceoryxNodeState::Alive(view) => NodeDescription {
198 state: NodeState::Alive,
199 id: NodeIdString::from(view.id()),
200 pid: view.id().pid().value(),
201 details: view.details().clone(),
202 },
203 IceoryxNodeState::Dead(view) => NodeDescription {
204 state: NodeState::Dead,
205 id: NodeIdString::from(view.id()),
206 pid: view.id().pid().value(),
207 details: view.details().clone(),
208 },
209 IceoryxNodeState::Inaccessible(node_id) => NodeDescription {
210 state: NodeState::Inaccessible,
211 id: NodeIdString::from(node_id),
212 pid: node_id.pid().value(),
213 details: None,
214 },
215 IceoryxNodeState::Undefined(node_id) => NodeDescription {
216 state: NodeState::Undefined,
217 id: NodeIdString::from(node_id),
218 pid: node_id.pid().value(),
219 details: None,
220 },
221 }
222 }
223}
224
225#[derive(serde::Serialize)]
226pub struct NodeList {
227 pub num: usize,
228 pub details: Vec<NodeDescriptor>,
229}
230
231impl<T> From<&IceoryxServiceDynamicDetails<T>> for NodeList
232where
233 T: IceoryxService,
234{
235 fn from(details: &IceoryxServiceDynamicDetails<T>) -> Self {
236 NodeList {
237 num: details.nodes.len(),
238 details: details.nodes.iter().map(NodeDescriptor::from).collect(),
239 }
240 }
241}