use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;
use tokio::time::sleep;
use xlink::channels::memory::MemoryChannel;
use xlink::core::types::{ChannelType, DeviceCapabilities, DeviceId, DeviceType, MessagePayload};
use xlink::XLink;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let alice_id = DeviceId::new();
let alice_caps = DeviceCapabilities {
device_id: alice_id,
device_type: DeviceType::Smartphone,
device_name: "Alice Phone".to_string(),
supported_channels: HashSet::from([ChannelType::Lan]),
battery_level: Some(80),
is_charging: false,
data_cost_sensitive: false,
};
struct DemoHandler;
#[async_trait::async_trait]
impl xlink::core::traits::MessageHandler for DemoHandler {
async fn handle_message(
&self,
_msg: xlink::core::types::Message,
) -> xlink::core::error::Result<()> {
Ok(())
}
}
let mem_channel = Arc::new(MemoryChannel::new(Arc::new(DemoHandler), 50));
let sdk = XLink::new(alice_caps, vec![mem_channel.clone()]).await?;
sdk.start().await?;
log::info!("Alice SDK started. ID: {}", alice_id);
let bob_id = DeviceId::new();
let carol_id = DeviceId::new();
sdk.capability_manager()
.register_remote_device(DeviceCapabilities {
device_id: bob_id,
device_type: DeviceType::Smartphone,
device_name: "Bob Phone".to_string(),
supported_channels: HashSet::from([ChannelType::Lan]),
battery_level: Some(90),
is_charging: false,
data_cost_sensitive: false,
});
sdk.capability_manager()
.register_remote_device(DeviceCapabilities {
device_id: carol_id,
device_type: DeviceType::Smartphone,
device_name: "Carol Phone".to_string(),
supported_channels: HashSet::from([ChannelType::Lan]),
battery_level: Some(70),
is_charging: false,
data_cost_sensitive: false,
});
log::info!("Creating group with Bob and Carol...");
let group_members = vec![bob_id, carol_id];
let group_id = sdk
.create_group("Project Team".to_string(), group_members)
.await?;
log::info!("Group created with ID: {}", group_id);
log::info!("Sending group message: 'Hello Team!'");
sdk.send_to_group(group_id, MessagePayload::Text("Hello Team!".to_string()))
.await?;
sleep(Duration::from_millis(200)).await;
log::info!("Rotating group key for enhanced security...");
sdk.rotate_group_key(group_id).await?;
log::info!("Group key rotated.");
let dave_id = DeviceId::new();
log::info!("Adding Dave to the group...");
sdk.group_manager().add_member(group_id, dave_id).await?;
log::info!("Dave added.");
log::info!("Sending group message: 'Welcome Dave!'");
sdk.send_to_group(group_id, MessagePayload::Text("Welcome Dave!".to_string()))
.await?;
sleep(Duration::from_millis(200)).await;
log::info!("Group chat demo completed.");
Ok(())
}