pywatt_sdk 0.5.3

Standardized SDK for building PyWatt modules in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! Module registration protocol for PyWatt modules.
//!
//! This module provides the functionality for modules to register with the orchestrator,
//! advertise their capabilities, and maintain their lifecycle.

pub mod models;

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;

use tokio::sync::Mutex;
use tokio::time::timeout;
use tracing::{debug, info, warn};
use uuid::Uuid;

use crate::message::{Message, MessageMetadata};
use crate::communication::{MessageChannel, TcpChannel};
use crate::tcp_types::ConnectionConfig;

pub use models::*;

/// Result type for registration operations.
pub type Result<T> = std::result::Result<T, RegistrationError>;

// Default timeout for registration operations
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// Shared module registry to maintain connection to registered modules.
struct Registry {
    /// Map of module ID to channel
    modules: HashMap<Uuid, Arc<TcpChannel>>,
}

// Singleton registry for all registered modules
static REGISTRY: once_cell::sync::Lazy<Mutex<Registry>> = once_cell::sync::Lazy::new(|| {
    Mutex::new(Registry {
        modules: HashMap::new(),
    })
});

/// Register a module with the orchestrator.
///
/// # Arguments
/// * `config` - Connection configuration for the orchestrator
/// * `info` - Information about the module to register
///
/// # Returns
/// A registered module if successful
pub async fn register_module(config: ConnectionConfig, info: ModuleInfo) -> Result<RegisteredModule> {
    // Connect to the orchestrator
    let channel = TcpChannel::connect(config.clone()).await
        .map_err(|e| RegistrationError::ConnectionError(e.to_string()))?;
    
    // Create registration request
    let request = RegistrationRequest {
        request_type: "register".to_string(),
        module_info: info.clone(),
    };
    
    // Create message with request ID
    let mut metadata = MessageMetadata::new();
    metadata.id = Some(Uuid::new_v4().to_string());
    
    let message = Message::with_metadata(request, metadata);
    let encoded = message.encode()
        .map_err(|e| RegistrationError::SerializationError(e.to_string()))?;
    
    // Send the request
    debug!("Sending registration request to orchestrator");
    channel.send(encoded).await
        .map_err(|e| RegistrationError::ChannelError(e.to_string()))?;
    
    // Wait for response with timeout
    debug!("Waiting for registration response");
    let response_encoded = timeout(DEFAULT_TIMEOUT, channel.receive()).await
        .map_err(|_| RegistrationError::Timeout(DEFAULT_TIMEOUT))?
        .map_err(|e| RegistrationError::ChannelError(e.to_string()))?;
    
    // Decode response
    let response_data: RegistrationResponse = serde_json::from_slice(response_encoded.data())
        .map_err(|e| RegistrationError::SerializationError(e.to_string()))?;
    
    // Check response
    if !response_data.success {
        return Err(RegistrationError::Rejected(
            response_data.error.clone().unwrap_or_else(|| "Unknown error".to_string())
        ));
    }
    
    // Get registered module from response
    let registered_module = response_data.module.clone()
        .ok_or_else(|| RegistrationError::Rejected("No module information in response".to_string()))?;
    
    // Store channel in registry for later use
    let mut registry = REGISTRY.lock().await;
    registry.modules.insert(registered_module.id, Arc::new(channel));
    
    info!("Successfully registered module {}", registered_module.id);
    Ok(registered_module)
}

/// Unregister a module from the orchestrator.
///
/// # Arguments
/// * `module` - The registered module to unregister
///
/// # Returns
/// `()` if successful
pub async fn unregister_module(module: &RegisteredModule) -> Result<()> {
    // Get channel from registry
    let channel = {
        let registry = REGISTRY.lock().await;
        registry.modules.get(&module.id).cloned()
    };
    
    let channel = match channel {
        Some(channel) => channel,
        None => {
            // Connect to the orchestrator
            let config = ConnectionConfig::new(
                module.orchestrator_host.clone(),
                module.orchestrator_port,
            );
            
            Arc::new(TcpChannel::connect(config).await
                .map_err(|e| RegistrationError::ConnectionError(e.to_string()))?)
        }
    };
    
    // Create unregistration request
    let request = UnregistrationRequest {
        request_type: "unregister".to_string(),
        module_id: module.id,
        token: module.token.clone(),
    };
    
    // Create message with request ID
    let mut metadata = MessageMetadata::new();
    metadata.id = Some(Uuid::new_v4().to_string());
    
    let message = Message::with_metadata(request, metadata);
    let encoded = message.encode()
        .map_err(|e| RegistrationError::SerializationError(e.to_string()))?;
    
    // Send the request
    debug!("Sending unregistration request to orchestrator");
    channel.send(encoded).await
        .map_err(|e| RegistrationError::ChannelError(e.to_string()))?;
    
    // Wait for response with timeout
    debug!("Waiting for unregistration response");
    let response_encoded = timeout(DEFAULT_TIMEOUT, channel.receive()).await
        .map_err(|_| RegistrationError::Timeout(DEFAULT_TIMEOUT))?
        .map_err(|e| RegistrationError::ChannelError(e.to_string()))?;
    
    // Decode response
    let response_data: UnregistrationResponse = serde_json::from_slice(response_encoded.data())
        .map_err(|e| RegistrationError::SerializationError(e.to_string()))?;
    
    // Check response
    if !response_data.success {
        return Err(RegistrationError::Rejected(
            response_data.error.clone().unwrap_or_else(|| "Unknown error".to_string())
        ));
    }
    
    // Remove channel from registry
    let mut registry = REGISTRY.lock().await;
    registry.modules.remove(&module.id);
    
    info!("Successfully unregistered module {}", module.id);
    Ok(())
}

/// Send a heartbeat to the orchestrator to report health status.
///
/// # Arguments
/// * `module` - The registered module
/// * `status` - Current health status
/// * `details` - Optional additional health details
///
/// # Returns
/// Current health status from orchestrator's perspective
pub async fn heartbeat(
    module: &RegisteredModule,
    status: HealthStatus,
    details: Option<HashMap<String, String>>,
) -> Result<HealthStatus> {
    // Get channel from registry
    let channel = {
        let registry = REGISTRY.lock().await;
        registry.modules.get(&module.id).cloned()
    };
    
    let channel = match channel {
        Some(channel) => channel,
        None => {
            // Connect to the orchestrator
            let config = ConnectionConfig::new(
                module.orchestrator_host.clone(),
                module.orchestrator_port,
            );
            
            Arc::new(TcpChannel::connect(config).await
                .map_err(|e| RegistrationError::ConnectionError(e.to_string()))?)
        }
    };
    
    // Create heartbeat request
    let request = HeartbeatRequest {
        request_type: "heartbeat".to_string(),
        module_id: module.id,
        token: module.token.clone(),
        status,
        details,
    };
    
    // Create message with request ID
    let mut metadata = MessageMetadata::new();
    metadata.id = Some(Uuid::new_v4().to_string());
    
    let message = Message::with_metadata(request, metadata);
    let encoded = message.encode()
        .map_err(|e| RegistrationError::SerializationError(e.to_string()))?;
    
    // Send the request
    debug!("Sending heartbeat request to orchestrator");
    channel.send(encoded).await
        .map_err(|e| RegistrationError::ChannelError(e.to_string()))?;
    
    // Wait for response with timeout
    debug!("Waiting for heartbeat response");
    let response_encoded = timeout(DEFAULT_TIMEOUT, channel.receive()).await
        .map_err(|_| RegistrationError::Timeout(DEFAULT_TIMEOUT))?
        .map_err(|e| RegistrationError::ChannelError(e.to_string()))?;
    
    // Decode response
    let response_data: HeartbeatResponse = serde_json::from_slice(response_encoded.data())
        .map_err(|e| RegistrationError::SerializationError(e.to_string()))?;
    
    // Check response
    if !response_data.success {
        return Err(RegistrationError::Rejected(
            response_data.error.clone().unwrap_or_else(|| "Unknown error".to_string())
        ));
    }
    
    // Return health status
    let status = response_data.status;
    debug!("Heartbeat successful, status: {:?}", status);
    Ok(status)
}

/// Advertise capabilities to the orchestrator.
///
/// # Arguments
/// * `module` - The registered module
/// * `capabilities` - Capabilities to advertise
///
/// # Returns
/// `()` if successful
pub async fn advertise_capabilities(module: &RegisteredModule, capabilities: Capabilities) -> Result<()> {
    // Get channel from registry
    let channel = {
        let registry = REGISTRY.lock().await;
        registry.modules.get(&module.id).cloned()
    };
    
    let channel = match channel {
        Some(channel) => channel,
        None => {
            // Connect to the orchestrator
            let config = ConnectionConfig::new(
                module.orchestrator_host.clone(),
                module.orchestrator_port,
            );
            
            Arc::new(TcpChannel::connect(config).await
                .map_err(|e| RegistrationError::ConnectionError(e.to_string()))?)
        }
    };
    
    // Create capabilities request
    let request = CapabilitiesRequest {
        request_type: "capabilities".to_string(),
        module_id: module.id,
        token: module.token.clone(),
        capabilities,
    };
    
    // Create message with request ID
    let mut metadata = MessageMetadata::new();
    metadata.id = Some(Uuid::new_v4().to_string());
    
    let message = Message::with_metadata(request, metadata);
    let encoded = message.encode()
        .map_err(|e| RegistrationError::SerializationError(e.to_string()))?;
    
    // Send the request
    debug!("Sending capabilities request to orchestrator");
    channel.send(encoded).await
        .map_err(|e| RegistrationError::ChannelError(e.to_string()))?;
    
    // Wait for response with timeout
    debug!("Waiting for capabilities response");
    let response_encoded = timeout(DEFAULT_TIMEOUT, channel.receive()).await
        .map_err(|_| RegistrationError::Timeout(DEFAULT_TIMEOUT))?
        .map_err(|e| RegistrationError::ChannelError(e.to_string()))?;
    
    // Decode response
    let response_data: CapabilitiesResponse = serde_json::from_slice(response_encoded.data())
        .map_err(|e| RegistrationError::SerializationError(e.to_string()))?;
    
    // Check response
    if !response_data.success {
        return Err(RegistrationError::Rejected(
            response_data.error.clone().unwrap_or_else(|| "Unknown error".to_string())
        ));
    }
    
    info!("Successfully advertised capabilities for module {}", module.id);
    Ok(())
}

/// Start a heartbeat loop in a background task.
///
/// # Arguments
/// * `module` - The registered module
/// * `interval` - Interval between heartbeats
/// * `status_provider` - Function that returns the current health status
///
/// # Returns
/// A handle to the heartbeat task
pub fn start_heartbeat_loop<F>(
    module: RegisteredModule,
    interval: Duration,
    mut status_provider: F,
) -> tokio::task::JoinHandle<()>
where
    F: FnMut() -> (HealthStatus, Option<HashMap<String, String>>) + Send + 'static,
{
    tokio::spawn(async move {
        loop {
            // Get current status
            let (status, details) = status_provider();
            
            // Send heartbeat
            match heartbeat(&module, status, details).await {
                Ok(_) => {
                    debug!("Heartbeat sent successfully");
                }
                Err(e) => {
                    warn!("Failed to send heartbeat: {}", e);
                }
            }
            
            // Wait for next interval
            tokio::time::sleep(interval).await;
        }
    })
}

/// A simple wrapper for the heartbeat function that doesn't require additional details.
///
/// # Arguments
/// * `module` - The registered module
///
/// # Returns
/// Current health status from orchestrator's perspective
pub async fn simple_heartbeat(module: &RegisteredModule) -> Result<HealthStatus> {
    heartbeat(module, HealthStatus::Healthy, None).await
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokio::net::TcpListener;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    
    // Mock orchestrator for testing
    async fn mock_orchestrator(port: u16) -> std::io::Result<tokio::task::JoinHandle<()>> {
        let listener = TcpListener::bind(format!("127.0.0.1:{}", port)).await?;
        
        let handle = tokio::spawn(async move {
            if let Ok((mut socket, _)) = listener.accept().await {
                loop {
                    // Read the length header (4 bytes)
                    let mut len_bytes = [0u8; 4];
                    if socket.read_exact(&mut len_bytes).await.is_err() {
                        break;
                    }
                    let len = u32::from_be_bytes(len_bytes) as usize;
                    
                    // Read the format byte (1 byte)
                    let mut format_byte = [0u8; 1];
                    if socket.read_exact(&mut format_byte).await.is_err() {
                        break;
                    }
                    
                    // Read the message data
                    let mut data = vec![0u8; len];
                    if socket.read_exact(&mut data).await.is_err() {
                        break;
                    }
                    
                    // Parse the message
                    let message_str = String::from_utf8_lossy(&data);
                    
                    // Construct response based on request
                    let response = if message_str.contains("\"request_type\":\"register\"") {
                        // Registration response
                        let module = RegisteredModule {
                            info: ModuleInfo::new("test", "1.0.0", "Test module"),
                            id: Uuid::new_v4(),
                            token: "test-token".to_string(),
                            orchestrator_host: "127.0.0.1".to_string(),
                            orchestrator_port: port,
                            env: None,
                            additional_data: None,
                        };
                        
                        // Create a direct RegistrationResponse structure
                        let reg_response = RegistrationResponse {
                            response_type: "register".to_string(),
                            success: true,
                            error: None,
                            module: Some(module),
                        };
                        
                        serde_json::to_value(reg_response).unwrap()
                    } else if message_str.contains("\"request_type\":\"heartbeat\"") {
                        // Heartbeat response
                        let heartbeat_response = HeartbeatResponse {
                            response_type: "heartbeat".to_string(),
                            success: true,
                            error: None,
                            status: HealthStatus::Healthy,
                            context: None,
                        };
                        
                        serde_json::to_value(heartbeat_response).unwrap()
                    } else if message_str.contains("\"request_type\":\"capabilities\"") {
                        // Capabilities response
                        let capabilities_response = CapabilitiesResponse {
                            response_type: "capabilities".to_string(),
                            success: true,
                            error: None,
                        };
                        
                        serde_json::to_value(capabilities_response).unwrap()
                    } else if message_str.contains("\"request_type\":\"unregister\"") {
                        // Unregistration response
                        let unregister_response = UnregistrationResponse {
                            response_type: "unregister".to_string(),
                            success: true,
                            error: None,
                        };
                        
                        serde_json::to_value(unregister_response).unwrap()
                    } else {
                        // Unknown request - doesn't need content/metadata wrapper
                        serde_json::json!({
                            "response_type": "unknown",
                            "success": false,
                            "error": "Unknown request type"
                        })
                    };
                    
                    // Serialize response
                    let response_json = serde_json::to_string(&response).unwrap();
                    let response_bytes = response_json.as_bytes();
                    
                    // Write the length header (4 bytes)
                    let length = response_bytes.len() as u32;
                    let len_bytes = length.to_be_bytes();
                    let _ = socket.write_all(&len_bytes).await;
                    
                    // Write the format byte (1 byte) - JSON
                    let _ = socket.write_all(&[1u8]).await;
                    
                    // Write the response data
                    let _ = socket.write_all(response_bytes).await;
                    let _ = socket.flush().await;
                }
            }
        });
        
        // Give the server a moment to start
        tokio::time::sleep(Duration::from_millis(100)).await;
        
        Ok(handle)
    }
    
    #[tokio::test]
    async fn test_register_module() -> Result<()> {
        // Start mock orchestrator
        let port = 9901;
        let _orchestrator = mock_orchestrator(port).await.unwrap();
        
        // Create module info
        let info = ModuleInfo::new("test-module", "1.0.0", "Test module");
        
        // Create connection config
        let config = ConnectionConfig::new("127.0.0.1", port);
        
        // Register module
        let module = register_module(config, info).await?;
        
        assert_eq!(module.info.name, "test");
        assert_eq!(module.info.version, "1.0.0");
        assert_eq!(module.info.description, "Test module");
        assert_eq!(module.token, "test-token");
        assert_eq!(module.orchestrator_host, "127.0.0.1");
        assert_eq!(module.orchestrator_port, port);
        
        Ok(())
    }
    
    #[tokio::test]
    async fn test_heartbeat() -> Result<()> {
        // Start mock orchestrator
        let port = 9902;
        let _orchestrator = mock_orchestrator(port).await.unwrap();
        
        // Create registered module
        let module = RegisteredModule {
            info: ModuleInfo::new("test", "1.0.0", "Test module"),
            id: Uuid::new_v4(),
            token: "test-token".to_string(),
            orchestrator_host: "127.0.0.1".to_string(),
            orchestrator_port: port,
            env: None,
            additional_data: None,
        };
        
        // Send heartbeat
        let status = heartbeat(&module, HealthStatus::Healthy, None).await?;
        
        assert_eq!(status, HealthStatus::Healthy);
        
        Ok(())
    }
    
    #[tokio::test]
    async fn test_advertise_capabilities() -> Result<()> {
        // Start mock orchestrator
        let port = 9903;
        let _orchestrator = mock_orchestrator(port).await.unwrap();
        
        // Create registered module
        let module = RegisteredModule {
            info: ModuleInfo::new("test", "1.0.0", "Test module"),
            id: Uuid::new_v4(),
            token: "test-token".to_string(),
            orchestrator_host: "127.0.0.1".to_string(),
            orchestrator_port: port,
            env: None,
            additional_data: None,
        };
        
        // Create capabilities
        let capabilities = Capabilities::new()
            .with_http_endpoint("/api/test", vec!["GET", "POST"])
            .with_message_type("test_message");
        
        // Advertise capabilities
        advertise_capabilities(&module, capabilities).await?;
        
        Ok(())
    }
    
    #[tokio::test]
    async fn test_unregister_module() -> Result<()> {
        // Start mock orchestrator
        let port = 9904;
        let _orchestrator = mock_orchestrator(port).await.unwrap();
        
        // Create registered module
        let module = RegisteredModule {
            info: ModuleInfo::new("test", "1.0.0", "Test module"),
            id: Uuid::new_v4(),
            token: "test-token".to_string(),
            orchestrator_host: "127.0.0.1".to_string(),
            orchestrator_port: port,
            env: None,
            additional_data: None,
        };
        
        // Unregister module
        unregister_module(&module).await?;
        
        Ok(())
    }
}