use sdtn::consts::BUNDLES_ADVANCED_DIR;
use sdtn::{convenience, BundleStatus, DtnNode};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
println!("๐ SpaceArth DTN Advanced Usage Example");
println!("\n๐ Method 1: Using DtnNode API (default settings)");
let node = DtnNode::new()?;
let messages = [
"First message",
"Second message with special chars: ๐๐",
"Third message with numbers: 12345",
"Fourth message - very long message that demonstrates how the system handles longer content",
];
for (i, msg) in messages.iter().enumerate() {
println!(" Inserting bundle {}: {}", i + 1, msg);
node.insert_bundle(msg.to_string()).await?;
}
println!("\n๐ Method 1b: Using DtnNode API (custom store path)");
let custom_node = DtnNode::with_store_path(BUNDLES_ADVANCED_DIR)?;
custom_node
.insert_bundle("Message in custom store".to_string())
.await?;
let bundles = node.list_bundles()?;
if let Some(first_id) = bundles.first() {
println!("\n๐ Detailed status for bundle: {}", first_id);
let status = node.get_bundle_status(Some(first_id))?;
match status {
BundleStatus::Single { id, bundle } => {
println!(" ID: {}", id);
println!(" Source: {}", bundle.primary.source);
println!(" Destination: {}", bundle.primary.destination);
println!(" Creation Time: {}", bundle.primary.creation_timestamp);
println!(" Lifetime: {} seconds", bundle.primary.lifetime);
println!(" Expired: {}", bundle.is_expired());
println!(" Message: {}", String::from_utf8_lossy(&bundle.payload));
}
_ => unreachable!(),
}
}
println!("\n๐ Method 2: Using convenience functions");
convenience::insert_bundle_quick("Quick message from convenience function").await?;
let quick_bundles = convenience::list_bundles_quick()?;
println!(
" Found {} bundles using convenience function",
quick_bundles.len()
);
if let Some(bundle_id) = quick_bundles.first() {
let partial_id = &bundle_id[..8]; match convenience::show_bundle_quick(partial_id) {
Ok(bundle) => {
println!(
" Quick show for {}: {}",
partial_id,
String::from_utf8_lossy(&bundle.payload)
);
}
Err(e) => {
println!(" Quick show failed: {}", e);
}
}
}
println!("\n๐ Method 3: Error handling demonstration");
match node.show_bundle("nonexistent") {
Ok(_) => println!(" Unexpected: Found non-existent bundle"),
Err(e) => println!(" Expected error for non-existent bundle: {}", e),
}
println!("\n๐ Method 4: Status summary");
let summary = node.get_bundle_status(None)?;
match summary {
BundleStatus::Summary {
active,
expired,
total,
} => {
println!(" ๐ Bundle Summary:");
println!(" โ
Active: {}", active);
println!(" โฐ Expired: {}", expired);
println!(" ๐ฆ Total: {}", total);
if expired > 0 {
println!(" ๐งน Cleaning up expired bundles...");
node.cleanup_expired()?;
let after_cleanup = node.get_bundle_status(None)?;
if let BundleStatus::Summary {
active: new_active,
expired: new_expired,
total: new_total,
} = after_cleanup
{
println!(" ๐ After cleanup:");
println!(" โ
Active: {}", new_active);
println!(" โฐ Expired: {}", new_expired);
println!(" ๐ฆ Total: {}", new_total);
}
}
}
_ => unreachable!(),
}
println!("\n๐ Method 5: Using Default trait");
let default_node = DtnNode::default();
default_node
.insert_bundle("Message from default instance".to_string())
.await?;
println!("\nโ
Advanced usage example completed successfully!");
Ok(())
}