1use crate::{DeviceError, DeviceResult};
8use serde::{Deserialize, Serialize};
9use std::collections::HashMap;
10use std::time::{Duration, SystemTime};
11
12#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub enum ProtocolType {
15 Http,
17 WebSocket,
19 Tcp,
21 Serial,
23 Custom(String),
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
29pub struct ProtocolConfig {
30 pub protocol_type: ProtocolType,
32 pub connection_params: ConnectionParams,
34 pub authentication: AuthenticationConfig,
36 pub timeouts: TimeoutConfig,
38 pub retry_config: RetryConfig,
40}
41
42#[derive(Debug, Clone, Serialize, Deserialize)]
44pub struct ConnectionParams {
45 pub host: String,
47 pub port: u16,
49 pub secure: bool,
51 pub endpoint: String,
53 pub parameters: HashMap<String, String>,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct AuthenticationConfig {
60 pub auth_type: AuthenticationType,
62 pub username: Option<String>,
64 pub credentials: Option<String>,
66 pub api_key: Option<String>,
68 pub certificate_path: Option<String>,
70}
71
72#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
74pub enum AuthenticationType {
75 None,
77 Basic,
79 Bearer,
81 ApiKey,
83 ClientCertificate,
85 Custom(String),
87}
88
89#[derive(Debug, Clone, Serialize, Deserialize)]
91pub struct TimeoutConfig {
92 pub connection_timeout: Duration,
94 pub request_timeout: Duration,
96 pub response_timeout: Duration,
98 pub keepalive_timeout: Duration,
100}
101
102#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct RetryConfig {
105 pub max_retries: usize,
107 pub initial_delay: Duration,
109 pub max_delay: Duration,
111 pub backoff_multiplier: f64,
113 pub jitter_factor: f64,
115}
116
117#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
119pub enum MessageType {
120 Discovery,
122 Status,
124 Execute,
126 Configure,
128 Calibrate,
130 EmergencyStop,
132 HealthCheck,
134 Custom(String),
136}
137
138#[derive(Debug, Clone, Serialize, Deserialize)]
140pub struct ProtocolMessage {
141 pub message_id: String,
143 pub message_type: MessageType,
145 pub timestamp: SystemTime,
147 pub sender: String,
149 pub recipient: String,
151 pub payload: HashMap<String, serde_json::Value>,
153 pub priority: MessagePriority,
155}
156
157#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
159pub enum MessagePriority {
160 Low,
162 Normal,
164 High,
166 Critical,
168}
169
170#[derive(Debug, Clone, Serialize, Deserialize)]
172pub struct ProtocolResponse {
173 pub response_id: String,
175 pub timestamp: SystemTime,
177 pub success: bool,
179 pub status_code: u16,
181 pub status_message: String,
183 pub data: HashMap<String, serde_json::Value>,
185 pub execution_time: Duration,
187}
188
189pub struct DeviceDiscoveryProtocol {
191 config: ProtocolConfig,
192 discovered_devices: Vec<DiscoveredDevice>,
193}
194
195#[derive(Debug, Clone, Serialize, Deserialize)]
197pub struct DiscoveredDevice {
198 pub device_id: String,
200 pub device_name: String,
202 pub device_type: String,
204 pub address: String,
206 pub port: u16,
208 pub capabilities: Vec<String>,
210 pub status: DeviceStatus,
212 pub last_seen: SystemTime,
214}
215
216#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
218pub enum DeviceStatus {
219 Online,
221 Busy,
223 Offline,
225 Maintenance,
227 Error,
229 Unknown,
231}
232
233impl DeviceDiscoveryProtocol {
234 pub const fn new(config: ProtocolConfig) -> Self {
236 Self {
237 config,
238 discovered_devices: Vec::new(),
239 }
240 }
241
242 pub async fn discover_devices(&mut self) -> DeviceResult<Vec<DiscoveredDevice>> {
244 let mock_device = DiscoveredDevice {
247 device_id: "neutral_atom_1".to_string(),
248 device_name: "Neutral Atom Device 1".to_string(),
249 device_type: "NeutralAtom".to_string(),
250 address: "192.168.1.100".to_string(),
251 port: 8080,
252 capabilities: vec![
253 "rydberg_gates".to_string(),
254 "optical_tweezers".to_string(),
255 "hyperfine_manipulation".to_string(),
256 ],
257 status: DeviceStatus::Online,
258 last_seen: SystemTime::now(),
259 };
260
261 self.discovered_devices = vec![mock_device.clone()];
262 Ok(vec![mock_device])
263 }
264
265 pub fn get_discovered_devices(&self) -> &[DiscoveredDevice] {
267 &self.discovered_devices
268 }
269
270 pub async fn refresh_device_status(&mut self, device_id: &str) -> DeviceResult<DeviceStatus> {
272 for device in &mut self.discovered_devices {
274 if device.device_id == device_id {
275 device.last_seen = SystemTime::now();
277 return Ok(device.status.clone());
278 }
279 }
280
281 Err(DeviceError::DeviceNotFound(format!(
282 "Device {device_id} not found"
283 )))
284 }
285}
286
287pub struct CommandExecutionProtocol {
289 config: ProtocolConfig,
290 pending_commands: HashMap<String, PendingCommand>,
291}
292
293#[derive(Debug, Clone, Serialize, Deserialize)]
295pub struct PendingCommand {
296 pub command_id: String,
298 pub command_type: String,
300 pub target_device: String,
302 pub parameters: HashMap<String, serde_json::Value>,
304 pub submitted_at: SystemTime,
306 pub expected_completion: Option<SystemTime>,
308 pub status: CommandStatus,
310}
311
312#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
314pub enum CommandStatus {
315 Queued,
317 Executing,
319 Completed,
321 Failed,
323 Cancelled,
325 TimedOut,
327}
328
329impl CommandExecutionProtocol {
330 pub fn new(config: ProtocolConfig) -> Self {
332 Self {
333 config,
334 pending_commands: HashMap::new(),
335 }
336 }
337
338 pub async fn submit_command(
340 &mut self,
341 command_type: &str,
342 target_device: &str,
343 parameters: HashMap<String, serde_json::Value>,
344 ) -> DeviceResult<String> {
345 let command_id = format!("cmd_{}", uuid::Uuid::new_v4());
346
347 let pending_command = PendingCommand {
348 command_id: command_id.clone(),
349 command_type: command_type.to_string(),
350 target_device: target_device.to_string(),
351 parameters,
352 submitted_at: SystemTime::now(),
353 expected_completion: None,
354 status: CommandStatus::Queued,
355 };
356
357 self.pending_commands
358 .insert(command_id.clone(), pending_command);
359 Ok(command_id)
360 }
361
362 pub fn get_command_status(&self, command_id: &str) -> DeviceResult<CommandStatus> {
364 self.pending_commands
365 .get(command_id)
366 .map(|cmd| cmd.status.clone())
367 .ok_or_else(|| DeviceError::InvalidInput(format!("Command {command_id} not found")))
368 }
369
370 pub async fn cancel_command(&mut self, command_id: &str) -> DeviceResult<()> {
372 if let Some(command) = self.pending_commands.get_mut(command_id) {
373 command.status = CommandStatus::Cancelled;
374 Ok(())
375 } else {
376 Err(DeviceError::InvalidInput(format!(
377 "Command {command_id} not found"
378 )))
379 }
380 }
381
382 pub fn get_pending_commands(&self) -> Vec<&PendingCommand> {
384 self.pending_commands.values().collect()
385 }
386}
387
388pub struct StatusMonitoringProtocol {
390 config: ProtocolConfig,
391 monitored_devices: HashMap<String, DeviceMonitoringInfo>,
392}
393
394#[derive(Debug, Clone, Serialize, Deserialize)]
396pub struct DeviceMonitoringInfo {
397 pub device_id: String,
399 pub last_update: SystemTime,
401 pub current_status: DeviceStatus,
403 pub status_history: Vec<StatusHistoryEntry>,
405 pub metrics: HashMap<String, f64>,
407}
408
409#[derive(Debug, Clone, Serialize, Deserialize)]
411pub struct StatusHistoryEntry {
412 pub timestamp: SystemTime,
414 pub status: DeviceStatus,
416 pub info: Option<String>,
418}
419
420impl StatusMonitoringProtocol {
421 pub fn new(config: ProtocolConfig) -> Self {
423 Self {
424 config,
425 monitored_devices: HashMap::new(),
426 }
427 }
428
429 pub fn start_monitoring(&mut self, device_id: &str) -> DeviceResult<()> {
431 let monitoring_info = DeviceMonitoringInfo {
432 device_id: device_id.to_string(),
433 last_update: SystemTime::now(),
434 current_status: DeviceStatus::Unknown,
435 status_history: Vec::new(),
436 metrics: HashMap::new(),
437 };
438
439 self.monitored_devices
440 .insert(device_id.to_string(), monitoring_info);
441 Ok(())
442 }
443
444 pub fn stop_monitoring(&mut self, device_id: &str) -> DeviceResult<()> {
446 self.monitored_devices.remove(device_id);
447 Ok(())
448 }
449
450 pub fn update_device_status(
452 &mut self,
453 device_id: &str,
454 status: DeviceStatus,
455 info: Option<String>,
456 ) -> DeviceResult<()> {
457 if let Some(monitoring_info) = self.monitored_devices.get_mut(device_id) {
458 let history_entry = StatusHistoryEntry {
459 timestamp: SystemTime::now(),
460 status: status.clone(),
461 info,
462 };
463
464 monitoring_info.current_status = status;
465 monitoring_info.last_update = SystemTime::now();
466 monitoring_info.status_history.push(history_entry);
467
468 if monitoring_info.status_history.len() > 1000 {
470 monitoring_info.status_history.drain(0..500);
471 }
472
473 Ok(())
474 } else {
475 Err(DeviceError::DeviceNotFound(format!(
476 "Device {device_id} not found in monitoring list"
477 )))
478 }
479 }
480
481 pub fn get_device_status(&self, device_id: &str) -> DeviceResult<&DeviceMonitoringInfo> {
483 self.monitored_devices.get(device_id).ok_or_else(|| {
484 DeviceError::DeviceNotFound(format!("Device {device_id} not found in monitoring list"))
485 })
486 }
487
488 pub fn get_monitored_devices(&self) -> Vec<&DeviceMonitoringInfo> {
490 self.monitored_devices.values().collect()
491 }
492}
493
494impl Default for ProtocolConfig {
495 fn default() -> Self {
496 Self {
497 protocol_type: ProtocolType::Http,
498 connection_params: ConnectionParams::default(),
499 authentication: AuthenticationConfig::default(),
500 timeouts: TimeoutConfig::default(),
501 retry_config: RetryConfig::default(),
502 }
503 }
504}
505
506impl Default for ConnectionParams {
507 fn default() -> Self {
508 Self {
509 host: "localhost".to_string(),
510 port: 8080,
511 secure: false,
512 endpoint: "/api/v1".to_string(),
513 parameters: HashMap::new(),
514 }
515 }
516}
517
518impl Default for AuthenticationConfig {
519 fn default() -> Self {
520 Self {
521 auth_type: AuthenticationType::None,
522 username: None,
523 credentials: None,
524 api_key: None,
525 certificate_path: None,
526 }
527 }
528}
529
530impl Default for TimeoutConfig {
531 fn default() -> Self {
532 Self {
533 connection_timeout: Duration::from_secs(30),
534 request_timeout: Duration::from_secs(60),
535 response_timeout: Duration::from_secs(120),
536 keepalive_timeout: Duration::from_secs(300),
537 }
538 }
539}
540
541impl Default for RetryConfig {
542 fn default() -> Self {
543 Self {
544 max_retries: 3,
545 initial_delay: Duration::from_millis(100),
546 max_delay: Duration::from_secs(30),
547 backoff_multiplier: 2.0,
548 jitter_factor: 0.1,
549 }
550 }
551}
552
553pub fn create_protocol_message(
555 message_type: MessageType,
556 sender: &str,
557 recipient: &str,
558 payload: HashMap<String, serde_json::Value>,
559 priority: MessagePriority,
560) -> ProtocolMessage {
561 ProtocolMessage {
562 message_id: uuid::Uuid::new_v4().to_string(),
563 message_type,
564 timestamp: SystemTime::now(),
565 sender: sender.to_string(),
566 recipient: recipient.to_string(),
567 payload,
568 priority,
569 }
570}
571
572pub fn create_protocol_response(
574 request_id: &str,
575 success: bool,
576 status_code: u16,
577 status_message: &str,
578 data: HashMap<String, serde_json::Value>,
579 execution_time: Duration,
580) -> ProtocolResponse {
581 ProtocolResponse {
582 response_id: request_id.to_string(),
583 timestamp: SystemTime::now(),
584 success,
585 status_code,
586 status_message: status_message.to_string(),
587 data,
588 execution_time,
589 }
590}
591
592#[cfg(test)]
593mod tests {
594 use super::*;
595
596 #[test]
597 fn test_protocol_config_creation() {
598 let config = ProtocolConfig::default();
599 assert_eq!(config.protocol_type, ProtocolType::Http);
600 assert_eq!(config.connection_params.host, "localhost");
601 assert_eq!(config.connection_params.port, 8080);
602 }
603
604 #[test]
605 fn test_protocol_message_creation() {
606 let payload = HashMap::new();
607 let message = create_protocol_message(
608 MessageType::Status,
609 "client",
610 "device_1",
611 payload,
612 MessagePriority::Normal,
613 );
614
615 assert_eq!(message.message_type, MessageType::Status);
616 assert_eq!(message.sender, "client");
617 assert_eq!(message.recipient, "device_1");
618 assert_eq!(message.priority, MessagePriority::Normal);
619 }
620
621 #[test]
622 fn test_device_discovery() {
623 let config = ProtocolConfig::default();
624 let discovery = DeviceDiscoveryProtocol::new(config);
625 assert_eq!(discovery.get_discovered_devices().len(), 0);
626 }
627}
628
629mod uuid {
631 use std::fmt;
632
633 pub struct Uuid([u8; 16]);
634
635 impl Uuid {
636 pub fn new_v4() -> Self {
637 use std::collections::hash_map::DefaultHasher;
639 use std::hash::{Hash, Hasher};
640 use std::time::SystemTime;
641
642 let mut hasher = DefaultHasher::new();
643 SystemTime::now().hash(&mut hasher);
644 let hash = hasher.finish();
645
646 let mut bytes = [0u8; 16];
647 bytes[0..8].copy_from_slice(&hash.to_le_bytes());
648 bytes[8..16].copy_from_slice(&hash.to_be_bytes());
649
650 Self(bytes)
651 }
652 }
653
654 impl fmt::Display for Uuid {
655 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
656 write!(f, "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
657 self.0[0], self.0[1], self.0[2], self.0[3],
658 self.0[4], self.0[5],
659 self.0[6], self.0[7],
660 self.0[8], self.0[9],
661 self.0[10], self.0[11], self.0[12], self.0[13], self.0[14], self.0[15])
662 }
663 }
664}