-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_sync.rs
More file actions
69 lines (57 loc) Β· 1.87 KB
/
simple_sync.rs
File metadata and controls
69 lines (57 loc) Β· 1.87 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
//! Simple example of two agents synchronizing a counter.
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();
// Create two agents with different IDs
let mut agent1 = Agent::new(
AgentId(1),
MeshTransportConfig {
local_agent_id: AgentId(1),
static_peers: vec![],
use_mdns: true,
listen_addr: "/ip4/0.0.0.0/tcp/0".to_string(),
},
)?;
let mut agent2 = Agent::new(
AgentId(2),
MeshTransportConfig {
local_agent_id: AgentId(2),
static_peers: vec![],
use_mdns: true,
listen_addr: "/ip4/0.0.0.0/tcp/0".to_string(),
},
)?;
// Start both agents
agent1.start()?;
agent2.start()?;
println!("Agents started. Waiting for discovery...");
sleep(Duration::from_secs(2)).await;
// Agent1 sets a value in its CRDT map
println!("Agent 1 setting counter = 42");
agent1.set_value("counter", json!(42))?;
// Broadcast changes
agent1.broadcast_changes().await?;
println!("Agent 1 broadcast changes");
// Wait a bit for synchronization
sleep(Duration::from_secs(1)).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!(42));
} else {
println!("Agent 2 did not receive the update (transport not yet functional)");
}
// Stop agents
agent1.stop().await?;
agent2.stop().await?;
println!("Example completed.");
Ok(())
}