96 lines
3.3 KiB
Rust
Raw Normal View History

2026-02-05 12:20:36 +08:00
//! Example: Ping-Pong Chat
//!
2026-02-05 14:12:21 +08:00
//! This example demonstrates a back-and-forth conversation between two users
2026-02-05 16:25:28 +08:00
//! using in-memory storage.
2026-02-05 12:20:36 +08:00
//!
//! Run with: cargo run -p logos-chat --example ping_pong
2026-02-05 16:25:28 +08:00
use logos_chat::ChatManager;
2026-02-05 12:20:36 +08:00
fn main() {
println!("=== Ping-Pong Chat Example ===\n");
2026-02-05 16:25:28 +08:00
// Create two chat participants with in-memory storage
// Each ChatManager has its own shared in-memory SQLite database
let mut alice = ChatManager::in_memory("alice").expect("Failed to create Alice's chat manager");
let mut bob = ChatManager::in_memory("bob").expect("Failed to create Bob's chat manager");
2026-02-05 12:20:36 +08:00
println!("Created participants:");
2026-02-05 14:12:21 +08:00
println!(" Alice: {}", alice.local_address());
println!(" Bob: {}", bob.local_address());
2026-02-05 12:20:36 +08:00
println!();
// Bob shares his intro bundle with Alice
let bob_intro = bob.create_intro_bundle().unwrap();
println!("Bob shared his introduction bundle with Alice\n");
// Alice initiates the conversation
let (alice_chat_id, initial_envelopes) = alice.start_private_chat(&bob_intro, "Ping!").unwrap();
println!("Alice -> Bob: \"Ping!\"");
println!(" Chat ID: {}", &alice_chat_id);
println!(" Envelopes: {}", initial_envelopes.len());
2026-02-05 16:02:02 +08:00
// Bob receives the message
let envelope = initial_envelopes.first().unwrap();
let content = bob.handle_incoming(&envelope.data).unwrap();
println!(
" Bob received: \"{}\"",
String::from_utf8_lossy(&content.data)
);
// Get Bob's chat ID (same as Alice's due to shared conversation_hint)
let bob_chat_id = bob.list_chats().unwrap().first().unwrap().clone();
println!(" Bob's chat ID: {}", &bob_chat_id);
println!();
// Bob replies
let bob_envelopes = bob.send_message(&bob_chat_id, b"Pong!").unwrap();
println!("Bob -> Alice: \"Pong!\"");
println!(" Envelopes: {}", bob_envelopes.len());
let bob_reply = bob_envelopes.first().unwrap();
let alice_received = alice.handle_incoming(&bob_reply.data).unwrap();
println!(
" Alice received: \"{}\"",
String::from_utf8_lossy(&alice_received.data)
);
2026-02-05 12:20:36 +08:00
println!();
// Continue the conversation
2026-02-05 16:02:02 +08:00
let alice_messages = ["How's it going?", "Are you there?"];
let bob_replies = ["Pretty good!", "Yes, I'm here!"];
2026-02-05 12:20:36 +08:00
2026-02-05 16:02:02 +08:00
for (msg, reply) in alice_messages.iter().zip(bob_replies.iter()) {
// Alice sends
2026-02-05 12:20:36 +08:00
let envelopes = alice.send_message(&alice_chat_id, msg.as_bytes()).unwrap();
2026-02-05 16:02:02 +08:00
println!("Alice -> Bob: \"{}\"", msg);
let env = envelopes.first().unwrap();
let bob_received = bob.handle_incoming(&env.data).unwrap();
println!(
" Bob received: \"{}\"",
String::from_utf8_lossy(&bob_received.data)
);
2026-02-05 12:20:36 +08:00
2026-02-05 16:02:02 +08:00
// Bob replies
let bob_envs = bob.send_message(&bob_chat_id, reply.as_bytes()).unwrap();
println!("Bob -> Alice: \"{}\"", reply);
2026-02-05 12:20:36 +08:00
2026-02-05 16:02:02 +08:00
let bob_env = bob_envs.first().unwrap();
let alice_got = alice.handle_incoming(&bob_env.data).unwrap();
println!(
" Alice received: \"{}\"",
String::from_utf8_lossy(&alice_got.data)
);
println!();
2026-02-05 12:20:36 +08:00
}
println!("Chat statistics:");
2026-02-05 16:02:02 +08:00
println!(" Alice's chats: {:?}", alice.list_chats().unwrap());
println!(" Bob's chats: {:?}", bob.list_chats().unwrap());
2026-02-05 12:20:36 +08:00
println!();
println!("=== Example Complete ===");
}