From 0f6414e4254ea22a59626537c88e8268a4387a66 Mon Sep 17 00:00:00 2001 From: kaichaosun Date: Thu, 5 Feb 2026 12:20:36 +0800 Subject: [PATCH] chore: ping pong example in rust --- conversations/Cargo.toml | 2 +- conversations/examples/ping_pong.rs | 65 +++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 conversations/examples/ping_pong.rs diff --git a/conversations/Cargo.toml b/conversations/Cargo.toml index 2355137..8536f8b 100644 --- a/conversations/Cargo.toml +++ b/conversations/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2024" [lib] -crate-type = ["staticlib","dylib"] +crate-type = ["staticlib", "dylib", "rlib"] [dependencies] blake2.workspace = true diff --git a/conversations/examples/ping_pong.rs b/conversations/examples/ping_pong.rs new file mode 100644 index 0000000..5f72559 --- /dev/null +++ b/conversations/examples/ping_pong.rs @@ -0,0 +1,65 @@ +//! Example: Ping-Pong Chat +//! +//! This example demonstrates a back-and-forth conversation between two users. +//! Note: The handle_incoming implementation is currently stubbed, so this +//! demonstrates the API flow rather than full encryption roundtrip. +//! +//! Run with: cargo run -p logos-chat --example ping_pong + +use logos_chat::chat::ChatManager; + +fn main() { + println!("=== Ping-Pong Chat Example ===\n"); + + // Create two chat participants + let mut alice = ChatManager::new(); + let mut bob = ChatManager::new(); + + println!("Created participants:"); + println!(" Alice: {}", &alice.local_address()); + println!(" Bob: {}", &bob.local_address()); + 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()); + + // Simulate delivering to Bob (stub) + for env in &initial_envelopes { + let _ = bob.handle_incoming(&env.data); + } + println!(); + + // Continue the conversation + let messages = [("Alice", "How's it going?"), ("Alice", "Are you there?")]; + + for (sender, msg) in &messages { + let envelopes = alice.send_message(&alice_chat_id, msg.as_bytes()).unwrap(); + + println!("{} -> Bob: \"{}\"", sender, msg); + println!(" Envelopes: {}", envelopes.len()); + + // Simulate delivery + for env in &envelopes { + let _ = bob.handle_incoming(&env.data); + } + } + + println!(); + println!("Chat statistics:"); + println!(" Alice's active chats: {}", alice.list_chats().len()); + println!(" Bob's active chats: {}", bob.list_chats().len()); + println!(); + + println!("=== Example Complete ==="); + println!(); + println!("Note: Full message roundtrip requires implementing handle_incoming()"); + println!("to properly decrypt messages and establish the chat on the receiver side."); +}