|
| 1 | +use aimdb_core::{buffer::BufferCfg, AimDbBuilder}; |
| 2 | +use aimdb_sync::AimDbBuilderSyncExt; |
| 3 | +use aimdb_tokio_adapter::{TokioAdapter, TokioRecordRegistrarExt}; |
| 4 | +use std::sync::Arc; |
| 5 | +use std::thread; |
| 6 | +use std::time::Duration; |
| 7 | + |
| 8 | +// Enum of colors for the LED |
| 9 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 10 | +enum Color { |
| 11 | + Red, |
| 12 | + Green, |
| 13 | + Blue, |
| 14 | +} |
| 15 | + |
| 16 | +// Struct representing the LED state |
| 17 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 18 | +struct Led { |
| 19 | + color: Color, |
| 20 | +} |
| 21 | + |
| 22 | +// Main function |
| 23 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 24 | + println!("=== hello-mailbox: Mailbox buffer demo ===\n"); |
| 25 | + |
| 26 | + // configuration |
| 27 | + let adapter = Arc::new(TokioAdapter); |
| 28 | + let mut builder = AimDbBuilder::new().runtime(adapter); |
| 29 | + |
| 30 | + builder.configure::<Led>("actuator.led", |reg| { |
| 31 | + reg.buffer(BufferCfg::Mailbox); |
| 32 | + }); |
| 33 | + |
| 34 | + let handle = builder.attach()?; |
| 35 | + let producer = handle.producer::<Led>("actuator.led")?; |
| 36 | + |
| 37 | + { |
| 38 | + // Produce quickly BEFORE creating the consumer |
| 39 | + println!(" Round 1 "); |
| 40 | + println!("1. Firing three rapid commands BEFORE consumer exists: Red → Green → Blue"); |
| 41 | + producer.set(Led { color: Color::Red })?; |
| 42 | + producer.set(Led { |
| 43 | + color: Color::Green, |
| 44 | + })?; |
| 45 | + producer.set(Led { color: Color::Blue })?; |
| 46 | + thread::sleep(Duration::from_millis(100)); |
| 47 | + |
| 48 | + // Now we create the consumer — it will only see the last value in the Mailbox |
| 49 | + println!("2. Consumer created AFTER the burst — reads once:"); |
| 50 | + let consumer = handle.consumer::<Led>("actuator.led")?; |
| 51 | + thread::sleep(Duration::from_millis(100)); |
| 52 | + |
| 53 | + match consumer.try_get() { |
| 54 | + Ok(msg) => println!(" ✓ Got: {:?} ← only the latest survived", msg.color), |
| 55 | + Err(_) => println!(" (mailbox was already empty)"), |
| 56 | + } |
| 57 | + |
| 58 | + println!(" (Red and Green were overwritten before anyone could read them)\n"); |
| 59 | + } |
| 60 | + |
| 61 | + println!(" Round 2 "); |
| 62 | + println!("1. Firing two rapid commands BEFORE consumer exists: Red → Green"); |
| 63 | + // Create the color burst again |
| 64 | + producer.set(Led { color: Color::Red })?; |
| 65 | + producer.set(Led { |
| 66 | + color: Color::Green, |
| 67 | + })?; |
| 68 | + thread::sleep(Duration::from_millis(100)); |
| 69 | + |
| 70 | + println!("2. Consumer created AFTER the burst — reads once:"); |
| 71 | + let consumer2 = handle.consumer::<Led>("actuator.led")?; |
| 72 | + thread::sleep(Duration::from_millis(100)); |
| 73 | + |
| 74 | + match consumer2.try_get() { |
| 75 | + Ok(msg) => println!( |
| 76 | + " ✓ Got: {:?} ← only the latest survived (Green)", |
| 77 | + msg.color |
| 78 | + ), |
| 79 | + Err(_) => println!(" (mailbox was already empty)"), |
| 80 | + } |
| 81 | + |
| 82 | + println!("3. Shutting down..."); |
| 83 | + handle.detach()?; |
| 84 | + println!(" ✓ Done."); |
| 85 | + Ok(()) |
| 86 | +} |
0 commit comments