acton-reactive 8.2.0

Acton Reactive is the main crate of the Acton framework, designed for building reactive, event-driven, and asynchronous systems. It provides intuitive abstractions to make working with distributed actors seamless and efficient.
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
/*
 * Copyright (c) 2024. Govcraft
 *
 * Licensed under either of
 *   * Apache License, Version 2.0 (the "License");
 *     you may not use this file except in compliance with the License.
 *     You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *   * MIT license: http://opensource.org/licenses/MIT
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the applicable License for the specific language governing permissions and
 * limitations under that License.
 */

//! IPC (Inter-Process Communication) Example
//!
//! This example demonstrates the complete IPC infrastructure for acton-reactive:
//!
//! 1. **Phase 1 - Serialization Foundation**: Type registration, message envelopes,
//!    and actor exposure via logical names.
//!
//! 2. **Phase 2 - UDS Listener**: Unix Domain Socket communication with external
//!    processes using length-prefixed wire protocol.
//!
//! # Key Concepts Demonstrated
//!
//! - **Type Registration**: Registering message types with `IpcTypeRegistry`
//! - **Actor Exposure**: Exposing actors via logical names for IPC routing
//! - **UDS Listener**: Starting the IPC listener with `runtime.start_ipc_listener()`
//! - **Wire Protocol**: Using `protocol::write_envelope` and `protocol::read_response`
//!   for client-side communication
//! - **Socket Utilities**: Using `socket_exists` and `socket_is_alive` for
//!   socket state checking
//!
//! # Running This Example
//!
//! ```bash
//! cargo run --example ipc_basic --features ipc
//! ```

use std::sync::Arc;

use acton_macro::{acton_actor, acton_message};
use acton_reactive::ipc::protocol::{read_response, write_envelope};
use acton_reactive::prelude::*;
use tokio::net::UnixStream;

// ============================================================================
// Message Definitions
// ============================================================================

/// A price update message that can be sent via IPC.
#[acton_message(ipc)]
struct PriceUpdate {
    symbol: String,
    price: f64,
    timestamp: u64,
}

/// A request to get the current price of a symbol.
#[acton_message(ipc)]
struct GetPrice {
    symbol: String,
}

/// Response containing the current price.
#[acton_message(ipc)]
struct PriceResponse {
    symbol: String,
    price: f64,
    found: bool,
}

/// A message to print text to the console.
#[acton_message]
struct Print(String);

/// A message to print a section header.
#[acton_message]
struct PrintSection(String);

/// Message to initialize the price service with a printer handle.
#[acton_message]
struct InitPrinter(ActorHandle);

// ============================================================================
// Actor States
// ============================================================================

/// State for the console printer actor.
/// All console output goes through this actor to ensure proper ordering.
#[acton_actor]
struct PrinterState;

/// State for our price tracking actor.
#[acton_actor]
struct PriceServiceState {
    prices: std::collections::HashMap<String, f64>,
    update_count: usize,
    printer: Option<ActorHandle>,
}

// ============================================================================
// Helper Functions
// ============================================================================

/// Creates and starts the printer actor for coordinated console output.
async fn create_printer_actor(runtime: &mut ActorRuntime) -> ActorHandle {
    let mut printer_actor = runtime.new_actor::<PrinterState>();
    printer_actor
        .act_on::<Print>(|_actor, envelope| {
            println!("{}", envelope.message().0);
            Reply::ready()
        })
        .act_on::<PrintSection>(|_actor, envelope| {
            println!("\n--- {} ---\n", envelope.message().0);
            Reply::ready()
        });
    printer_actor.start().await
}

/// Creates and starts the price service actor.
async fn create_price_service(runtime: &mut ActorRuntime, printer: &ActorHandle) -> ActorHandle {
    let mut price_service =
        runtime.new_actor_with_name::<PriceServiceState>("price_service".to_string());

    price_service
        .mutate_on::<InitPrinter>(|actor, envelope| {
            actor.model.printer = Some(envelope.message().0.clone());
            Reply::ready()
        })
        .mutate_on::<PriceUpdate>(|actor, envelope| {
            let msg = envelope.message().clone();
            let printer = actor.model.printer.clone();
            actor.model.prices.insert(msg.symbol.clone(), msg.price);
            actor.model.update_count += 1;

            Reply::pending(async move {
                if let Some(p) = printer {
                    p.send(Print(format!(
                        "[PriceService] Received update: {} = ${:.2} (ts: {})",
                        msg.symbol, msg.price, msg.timestamp
                    )))
                    .await;
                }
            })
        })
        .mutate_on::<GetPrice>(|actor, envelope| {
            let msg = envelope.message().clone();
            let printer = actor.model.printer.clone();
            let price = actor.model.prices.get(&msg.symbol).copied();
            let reply_envelope = envelope.reply_envelope();

            Reply::pending(async move {
                if let Some(p) = &printer {
                    p.send(Print(format!("[PriceService] Query for: {}", msg.symbol)))
                        .await;
                }
                let response = PriceResponse {
                    symbol: msg.symbol.clone(),
                    price: price.unwrap_or(0.0),
                    found: price.is_some(),
                };
                reply_envelope.send(response).await;
            })
        })
        .after_stop(|actor| {
            let printer = actor.model.printer.clone();
            let update_count = actor.model.update_count;
            let prices = actor.model.prices.clone();

            Reply::pending(async move {
                if let Some(p) = printer {
                    p.send(Print(format!(
                        "\n[PriceService] Shutting down. Processed {update_count} updates."
                    )))
                    .await;
                    p.send(Print(format!("Final prices: {prices:?}"))).await;
                }
            })
        });

    let handle = price_service.start().await;
    handle.send(InitPrinter(printer.clone())).await;
    handle
}

/// Processes an incoming IPC envelope and routes it to the target actor.
async fn process_ipc_envelope(
    runtime: &ActorRuntime,
    registry: &Arc<IpcTypeRegistry>,
    printer: &ActorHandle,
    incoming_json: &str,
) {
    printer
        .send(Print(format!("Received IPC envelope:\n{incoming_json}")))
        .await;

    let envelope: IpcEnvelope = serde_json::from_str(incoming_json).expect("Invalid JSON");
    printer
        .send(Print(format!(
            "\nParsed envelope: correlation_id={}, target={}, type={}",
            envelope.correlation_id, envelope.target, envelope.message_type
        )))
        .await;

    if let Some(target_handle) = runtime.ipc_lookup(&envelope.target) {
        printer
            .send(Print(format!("Found target actor: {}", target_handle.id())))
            .await;

        match registry.deserialize_value(&envelope.message_type, &envelope.payload) {
            Ok(message) => {
                let price_update: &PriceUpdate =
                    (*message).as_any().downcast_ref().expect("Type mismatch");
                target_handle.send(price_update.clone()).await;

                let response = IpcResponse::success(
                    &envelope.correlation_id,
                    Some(serde_json::json!({"status": "delivered"})),
                );
                printer
                    .send(Print(format!(
                        "\nResponse: {}",
                        serde_json::to_string_pretty(&response).unwrap()
                    )))
                    .await;
            }
            Err(e) => {
                let response = IpcResponse::error(&envelope.correlation_id, &e);
                printer
                    .send(Print(format!(
                        "\nError response: {}",
                        serde_json::to_string_pretty(&response).unwrap()
                    )))
                    .await;
            }
        }
    } else {
        let response = IpcResponse::error(
            &envelope.correlation_id,
            &IpcError::ActorNotFound(envelope.target.clone()),
        );
        printer
            .send(Print(format!(
                "\nError: {}",
                serde_json::to_string_pretty(&response).unwrap()
            )))
            .await;
    }
}

/// Demonstrates programmatic envelope creation with MTI-generated correlation ID.
async fn demonstrate_envelope_creation(printer: &ActorHandle) {
    let new_envelope = IpcEnvelope::new(
        "prices",
        "PriceUpdate",
        serde_json::json!({
            "symbol": "AMZN",
            "price": 178.25,
            "timestamp": 1_700_000_004
        }),
    );

    printer
        .send(Print(
            "Created envelope with auto-generated correlation_id:".to_string(),
        ))
        .await;
    printer
        .send(Print(serde_json::to_string_pretty(&new_envelope).unwrap()))
        .await;
}

/// Demonstrates error handling for various IPC failure scenarios.
async fn demonstrate_error_handling(
    runtime: &ActorRuntime,
    registry: &Arc<IpcTypeRegistry>,
    printer: &ActorHandle,
) {
    // Unknown message type
    let unknown_result = registry.deserialize("UnknownType", b"{}");
    if let Err(e) = unknown_result {
        printer
            .send(Print(format!("Error deserializing unknown type: {e}")))
            .await;
        let response = IpcResponse::error("req_err_1", &e);
        printer
            .send(Print(format!(
                "Response: {}",
                serde_json::to_string_pretty(&response).unwrap()
            )))
            .await;
    }

    // Actor not found
    if runtime.ipc_lookup("nonexistent").is_none() {
        let err = IpcError::ActorNotFound("nonexistent".to_string());
        printer
            .send(Print(format!("\nError looking up actor: {err}")))
            .await;
        let response = IpcResponse::error("req_err_2", &err);
        printer
            .send(Print(format!(
                "Response: {}",
                serde_json::to_string_pretty(&response).unwrap()
            )))
            .await;
    }
}

/// Demonstrates the UDS listener and client-side protocol functions.
async fn demonstrate_uds_communication(
    runtime: &ActorRuntime,
    printer: &ActorHandle,
) -> Result<(), Box<dyn std::error::Error>> {
    use acton_reactive::ipc::{socket_exists, socket_is_alive};

    // Create a custom IPC config with a unique socket path for this example
    let config = IpcConfig::load();
    let socket_path = config.socket_path();

    printer
        .send(Print(format!("Socket path: {}", socket_path.display())))
        .await;

    // Check socket state before starting
    printer
        .send(Print(format!(
            "Socket exists before start: {}",
            socket_exists(&socket_path)
        )))
        .await;

    // Start the IPC listener
    let listener_handle = runtime.start_ipc_listener_with_config(config).await?;

    printer
        .send(Print("IPC listener started successfully!".to_string()))
        .await;

    // Small delay to ensure listener is ready
    tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

    // Check socket state after starting
    printer
        .send(Print(format!(
            "Socket exists after start: {}",
            socket_exists(&socket_path)
        )))
        .await;
    printer
        .send(Print(format!(
            "Socket is alive: {}",
            socket_is_alive(&socket_path).await
        )))
        .await;

    // Connect as a client and send a message
    printer
        .send(Print("\nConnecting as IPC client...".to_string()))
        .await;

    let stream = UnixStream::connect(&socket_path).await?;
    let (mut reader, mut writer) = stream.into_split();

    // Create and send an envelope using the wire protocol
    let envelope = IpcEnvelope::new(
        "prices",
        "PriceUpdate",
        serde_json::json!({
            "symbol": "NVDA",
            "price": 875.50,
            "timestamp": 1_700_000_010
        }),
    );

    printer
        .send(Print(format!(
            "Sending envelope via UDS: correlation_id={}",
            envelope.correlation_id
        )))
        .await;

    // Use the wire protocol to send the envelope
    write_envelope(&mut writer, &envelope).await?;

    // Read the response using the wire protocol
    let response = read_response(&mut reader, 1024 * 1024).await?;

    printer
        .send(Print(format!(
            "Received response: success={}, correlation_id={}",
            response.success, response.correlation_id
        )))
        .await;
    printer
        .send(Print(format!(
            "Response payload: {}",
            serde_json::to_string_pretty(&response.payload).unwrap_or_default()
        )))
        .await;

    // Show listener statistics
    let stats = &listener_handle.stats;
    printer
        .send(Print(format!(
            "\nListener stats:\n  Connections accepted: {}\n  Messages received: {}\n  Messages routed: {}",
            stats.connections_accepted(),
            stats.messages_received(),
            stats.messages_routed()
        )))
        .await;

    // Stop the listener
    listener_handle.stop();
    printer
        .send(Print("IPC listener stopped.".to_string()))
        .await;

    // Small delay for cleanup
    tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;

    // Verify socket is cleaned up
    printer
        .send(Print(format!(
            "Socket exists after stop: {}",
            socket_exists(&socket_path)
        )))
        .await;

    Ok(())
}

/// Demonstrates hiding an actor from IPC access.
async fn demonstrate_ipc_hiding(runtime: &ActorRuntime, printer: &ActorHandle) {
    let hidden = runtime.ipc_hide("prices");
    printer
        .send(Print(format!(
            "Removed 'prices' from IPC: {}",
            hidden.map_or_else(|| "not found".to_string(), |h| h.id().to_string())
        )))
        .await;
    printer
        .send(Print(format!(
            "Exposed actors remaining: {}",
            runtime.ipc_actor_count()
        )))
        .await;
}

// ============================================================================
// Main
// ============================================================================

#[acton_main]
async fn main() {
    let mut runtime = ActonApp::launch_async().await;

    // Create printer actor for coordinated console output
    let printer = create_printer_actor(&mut runtime).await;
    printer
        .send(Print(
            "=== IPC Example (Phase 1 + Phase 2) ===\n".to_string(),
        ))
        .await;

    // Register IPC message types
    let registry = runtime.ipc_registry();
    registry.register::<PriceUpdate>("PriceUpdate");
    registry.register::<GetPrice>("GetPrice");
    registry.register::<PriceResponse>("PriceResponse");

    printer
        .send(Print(format!(
            "Registered {} IPC message types:",
            registry.len()
        )))
        .await;
    for type_name in registry.type_names() {
        printer.send(Print(format!("  - {type_name}"))).await;
    }

    // Create and expose price service
    let price_handle = create_price_service(&mut runtime, &printer).await;
    runtime.ipc_expose("prices", price_handle.clone());

    printer
        .send(Print(format!(
            "\nExposed {} actor(s) for IPC:",
            runtime.ipc_actor_count()
        )))
        .await;
    printer
        .send(Print(format!("  - 'prices' -> {}", price_handle.id())))
        .await;

    // Phase 1: Simulate IPC message flow (in-process)
    printer
        .send(PrintSection(
            "Phase 1: In-Process IPC Simulation".to_string(),
        ))
        .await;
    let incoming_json = r#"{
        "correlation_id": "req_001",
        "target": "prices",
        "message_type": "PriceUpdate",
        "payload": { "symbol": "AAPL", "price": 178.25, "timestamp": 1700000000 }
    }"#;
    process_ipc_envelope(&runtime, &registry, &printer, incoming_json).await;

    // Send more updates directly
    printer
        .send(PrintSection("Direct Actor Updates".to_string()))
        .await;
    for (symbol, price, ts) in [
        ("GOOGL", 141.80, 1_700_000_001),
        ("MSFT", 378.91, 1_700_000_002),
        ("AAPL", 179.50, 1_700_000_003),
    ] {
        price_handle
            .send(PriceUpdate {
                symbol: symbol.to_string(),
                price,
                timestamp: ts,
            })
            .await;
    }

    // Demonstrate envelope creation
    printer
        .send(PrintSection(
            "Creating IpcEnvelope Programmatically".to_string(),
        ))
        .await;
    demonstrate_envelope_creation(&printer).await;

    // Demonstrate error handling
    printer
        .send(PrintSection("Error Handling Examples".to_string()))
        .await;
    demonstrate_error_handling(&runtime, &registry, &printer).await;

    // Phase 2: UDS listener demonstration
    printer
        .send(PrintSection(
            "Phase 2: Unix Domain Socket Communication".to_string(),
        ))
        .await;
    if let Err(e) = demonstrate_uds_communication(&runtime, &printer).await {
        printer
            .send(Print(format!("UDS demonstration error: {e}")))
            .await;
    }

    // Allow time for async message processing
    tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;

    // Demonstrate hiding actors
    printer
        .send(PrintSection("Hiding Actor from IPC".to_string()))
        .await;
    demonstrate_ipc_hiding(&runtime, &printer).await;

    // Shutdown - actor framework ensures all messages are processed
    runtime
        .shutdown_all()
        .await
        .expect("Failed to shut down system");
    println!("\n=== Example Complete ===");
}