datex_core/network/com_hub/
metadata.rs1use log::info;
2
3use crate::{
4 collections::HashMap,
5 network::{
6 com_hub::{
7 ComHub, managers::socket_manager::DynamicEndpointProperties,
8 },
9 com_interfaces::com_interface::ComInterfaceUUID,
10 },
11};
12
13use crate::{
14 network::com_interfaces::com_interface::properties::{
15 ComInterfaceProperties, InterfaceDirection,
16 },
17 values::core_values::endpoint::Endpoint,
18};
19
20use crate::{
21 network::com_hub::managers::com_interface_manager::InterfaceInfo,
22 prelude::*,
23};
24use core::fmt::Display;
25use datex_macros_internal::Datex;
26use itertools::Itertools;
27use serde::{Deserialize, Serialize};
28
29#[derive(Datex, Debug, Clone)]
30
31pub struct ComHubMetadataInterfaceSocket {
32 pub uuid: String,
33 pub direction: InterfaceDirection,
34 pub endpoint: Option<Endpoint>,
35 pub properties: Option<DynamicEndpointProperties>,
36}
37
38#[derive(Serialize, Deserialize, Clone)]
39
40pub struct ComHubMetadataInterfaceSocketWithoutEndpoint {
41 pub uuid: String,
42 pub direction: InterfaceDirection,
43}
44
45#[derive(Debug, Datex, Clone)]
46pub struct ComHubMetadataInterface {
47 pub uuid: String,
48 pub properties: ComInterfaceProperties,
49 pub sockets: Vec<ComHubMetadataInterfaceSocket>,
50 pub is_waiting_for_socket_connections: bool,
51}
52
53#[derive(Debug, Datex, Clone)]
54pub struct ComHubMetadata {
55 pub endpoint: Endpoint,
56 pub interfaces: Vec<ComHubMetadataInterface>,
57}
58
59impl Display for ComHubMetadata {
60 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
61 writeln!(f, "ComHub ({})", self.endpoint)?;
62
63 for interface in &self.interfaces {
65 writeln!(
66 f,
67 " {}/{}{}{}:",
68 interface.properties.interface_type,
69 interface.properties.channel,
70 if interface.is_waiting_for_socket_connections {
71 " [accepting connections]"
72 } else {
73 ""
74 },
75 interface
76 .properties
77 .name
78 .clone()
79 .map(|n| format!(" ({n})"))
80 .unwrap_or("".to_string()),
81 )?;
82
83 let sorted_sockets = interface.sockets.iter().sorted_by_key(|s| {
85 match &s.properties {
86 Some(properties) => properties.distance,
87 None => i8::MAX,
88 }
89 });
90
91 for socket in sorted_sockets {
92 writeln!(
93 f,
94 " {} {}{} (distance: {}, uuid: {})",
95 match socket.direction {
96 InterfaceDirection::In => "──▶".to_string(),
97 InterfaceDirection::Out => "◀──".to_string(),
98 InterfaceDirection::InOut => "◀──▶".to_string(),
99 },
100 match &socket.properties {
101 Some(properties) => match properties.is_direct {
102 true => "".to_string(),
103 false => "[INDIRECT] ".to_string(),
104 },
105 None => "".to_string(),
106 },
107 match &socket.endpoint {
108 Some(endpoint) => endpoint.to_string(),
109 None => "unknown".to_string(),
110 },
111 match &socket.properties {
112 Some(properties) => properties.distance.to_string(),
113 None => "unknown".to_string(),
114 },
115 socket.uuid
116 )?;
117 }
118 }
119
120 Ok(())
121 }
122}
123
124impl ComHub {
125 pub fn get_metadata(&self) -> ComHubMetadata {
127 let mut metadata = ComHubMetadata {
128 endpoint: self.endpoint.clone(),
129 interfaces: Vec::new(),
130 };
131
132 let mut sockets_by_com_interface_uuid: HashMap<
133 ComInterfaceUUID,
134 Vec<ComHubMetadataInterfaceSocket>,
135 > = HashMap::new();
136
137 for (endpoint, sockets) in
138 self.socket_manager.endpoint_sockets.borrow().iter()
139 {
140 for (socket_uuid, properties) in sockets {
141 let socket = self
142 .socket_manager
143 .get_socket_by_uuid(socket_uuid)
144 .unwrap();
145 let com_interface_uuid = socket.interface_uuid.clone();
146 if !sockets_by_com_interface_uuid
147 .contains_key(&com_interface_uuid)
148 {
149 sockets_by_com_interface_uuid
150 .insert(com_interface_uuid.clone(), Vec::new());
151 }
152 sockets_by_com_interface_uuid
153 .get_mut(&com_interface_uuid)
154 .unwrap()
155 .push(ComHubMetadataInterfaceSocket {
156 uuid: socket_uuid.to_string(),
157 endpoint: Some(endpoint.clone()),
158 direction: socket.socket_properties.direction.clone(),
159 properties: Some(properties.clone()),
160 });
161 }
162 }
163
164 for (socket_uuid, socket) in self.socket_manager.sockets.borrow().iter()
165 {
166 if socket.endpoints.is_empty() {
168 let com_interface_uuid = socket.interface_uuid.clone();
169 if !sockets_by_com_interface_uuid
170 .contains_key(&com_interface_uuid)
171 {
172 sockets_by_com_interface_uuid
173 .insert(com_interface_uuid.clone(), Vec::new());
174 }
175 sockets_by_com_interface_uuid
176 .get_mut(&com_interface_uuid)
177 .unwrap()
178 .push(ComHubMetadataInterfaceSocket {
179 uuid: socket_uuid.to_string(),
180 direction: socket.socket_properties.direction.clone(),
181 endpoint: None,
182 properties: None,
183 });
184 continue;
185 }
186 }
187
188 for (
189 uuid,
190 InterfaceInfo {
191 properties,
192 is_waiting_for_socket_connections,
193 ..
194 },
195 ) in self.interfaces_manager.interfaces.borrow().iter()
196 {
197 metadata.interfaces.push(ComHubMetadataInterface {
198 uuid: uuid.to_string(),
199 properties: properties.as_ref().clone(),
200 sockets: sockets_by_com_interface_uuid
201 .remove(uuid)
202 .unwrap_or_default(),
203 is_waiting_for_socket_connections:
204 *is_waiting_for_socket_connections,
205 });
206 }
207
208 metadata
209 }
210
211 pub fn print_metadata(&self) {
213 let metadata = self.get_metadata();
214 info!("ComHub Metadata:\n{metadata}");
215 }
216}