mirror of
https://github.com/logos-messaging/libchat.git
synced 2026-04-01 17:13:13 +00:00
Implement a `client` crate that wraps the `libchat` context behind a simple `ChatClient<D>` API. The delivery strategy is pluggable via a `DeliveryService` trait, with two implementations provided: - `InProcessDelivery` — shared `MessageBus` for single-process tests - `CDelivery` — C function-pointer callback for the FFI layer Add a `client-ffi` crate that exposes the client as a C API via `safer-ffi`. A `generate-headers` binary produces the companion C header. Include two runnable examples: - `examples/in-process` — Alice/Bob exchange using in-process delivery - `examples/c-ffi` — same exchange written entirely in C; smoketested under valgrind (to catch memory leaks) in CI iterates: #71
35 lines
1.0 KiB
Rust
35 lines
1.0 KiB
Rust
use client::{ChatClient, ConversationIdOwned, InProcessDelivery};
|
|
use std::sync::Arc;
|
|
|
|
fn main() {
|
|
let (delivery, bus) = InProcessDelivery::new();
|
|
let mut cursor = bus.subscribe_tail("delivery_address");
|
|
|
|
let mut alice = ChatClient::new("alice", delivery.clone());
|
|
let mut bob = ChatClient::new("bob", delivery);
|
|
|
|
let bob_bundle = bob.create_intro_bundle().unwrap();
|
|
alice
|
|
.create_conversation(&bob_bundle, b"hello bob")
|
|
.unwrap();
|
|
|
|
let raw = cursor.next().unwrap();
|
|
let content = bob.receive(&raw).unwrap().unwrap();
|
|
println!(
|
|
"Bob received: {:?}",
|
|
std::str::from_utf8(&content.data).unwrap()
|
|
);
|
|
|
|
let bob_convo_id: ConversationIdOwned = Arc::from(content.conversation_id.as_str());
|
|
bob.send_message(&bob_convo_id, b"hi alice").unwrap();
|
|
|
|
let raw = cursor.next().unwrap();
|
|
let content = alice.receive(&raw).unwrap().unwrap();
|
|
println!(
|
|
"Alice received: {:?}",
|
|
std::str::from_utf8(&content.data).unwrap()
|
|
);
|
|
|
|
println!("Message exchange complete.");
|
|
}
|