-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmulti_agent_demo.rs
More file actions
118 lines (100 loc) · 3.44 KB
/
multi_agent_demo.rs
File metadata and controls
118 lines (100 loc) · 3.44 KB
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
//! Extended demo with three agents synchronizing state via in‑memory transport.
use agent_core::Agent;
use common::types::AgentId;
use mesh_transport::MeshTransportConfig;
use serde_json::json;
use std::time::Duration;
use tokio::time::sleep;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Initialize logging
tracing_subscriber::fmt::init();
println!("Starting multi‑agent demo with in‑memory transport...");
// Create three agents with in‑memory backend
let mut agent1 = Agent::new(
AgentId(1),
MeshTransportConfig {
local_agent_id: AgentId(1),
static_peers: vec![],
use_mdns: false,
listen_addr: "/ip4/0.0.0.0/tcp/0".to_string(),
use_in_memory: true,
},
)?;
let mut agent2 = Agent::new(
AgentId(2),
MeshTransportConfig {
local_agent_id: AgentId(2),
static_peers: vec![],
use_mdns: false,
listen_addr: "/ip4/0.0.0.0/tcp/0".to_string(),
use_in_memory: true,
},
)?;
let mut agent3 = Agent::new(
AgentId(3),
MeshTransportConfig {
local_agent_id: AgentId(3),
static_peers: vec![],
use_mdns: false,
listen_addr: "/ip4/0.0.0.0/tcp/0".to_string(),
use_in_memory: true,
},
)?;
// Start all agents
agent1.start()?;
agent2.start()?;
agent3.start()?;
println!("Agents started. Waiting for discovery...");
sleep(Duration::from_secs(1)).await;
// Agent1 sets a value
println!("Agent 1 setting counter = 100");
agent1.set_value("counter", json!(100))?;
agent1.broadcast_changes().await?;
println!("Agent 1 broadcast changes");
// Wait for propagation
sleep(Duration::from_millis(500)).await;
// Agent2 should have received the update
println!("Agent 2 checking counter...");
if let Some(value) = agent2.get_value::<serde_json::Value>("counter") {
println!("Agent 2 counter value: {}", value);
assert_eq!(value, json!(100));
} else {
println!("Agent 2 did not receive the update");
}
// Agent3 also should have it
println!("Agent 3 checking counter...");
if let Some(value) = agent3.get_value::<serde_json::Value>("counter") {
println!("Agent 3 counter value: {}", value);
assert_eq!(value, json!(100));
} else {
println!("Agent 3 did not receive the update");
}
// Now agent2 updates the counter
println!("Agent 2 incrementing counter by 5");
if let Some(mut value) = agent2.get_value::<i64>("counter") {
value += 5;
agent2.set_value("counter", json!(value))?;
agent2.broadcast_changes().await?;
println!("Agent 2 broadcast changes");
}
sleep(Duration::from_millis(500)).await;
// Check final values
println!("Final values:");
let v1 = agent1.get_value::<i64>("counter").unwrap_or(-1);
let v2 = agent2.get_value::<i64>("counter").unwrap_or(-1);
let v3 = agent3.get_value::<i64>("counter").unwrap_or(-1);
println!(" Agent 1: {}", v1);
println!(" Agent 2: {}", v2);
println!(" Agent 3: {}", v3);
// All should converge to 105
assert_eq!(v1, 105);
assert_eq!(v2, 105);
assert_eq!(v3, 105);
// Stop agents
agent1.stop().await?;
agent2.stop().await?;
agent3.stop().await?;
println!("Demo completed successfully.");
Ok(())
}