use torrent::session::{Session, SessionConfig};
use tracing_subscriber::EnvFilter;
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
let download_dir = tempfile::tempdir()?;
let config = SessionConfig::default();
let session = Session::new(config).await?;
println!(
"Session created (port: {})",
SessionConfig::default().listen_port
);
let data = include_bytes!("data/debian-13.5.0-amd64-netinst.iso.torrent");
let info_hash = session
.add_torrent_bytes(data)?
.download_dir(download_dir.path())
.start()
.await?;
println!("\nTorrent added:");
println!(" info_hash: {:02x?}", info_hash);
let status = session.torrent_status(&info_hash).await?;
println!(" name: {}", status.name);
println!(" progress: {:.1}%", status.progress * 100.0);
println!(" state: {:?}", status.state);
println!(" peers: {}", status.num_peers);
let active = session.active_torrents();
println!("\nActive torrents: {}", active.len());
for ih in &active {
let s = session.torrent_status(ih).await?;
println!(" - {} ({:.1}%)", s.name, s.progress * 100.0);
}
session.remove_torrent(&info_hash).await?;
println!("\nTorrent removed.");
println!("Active torrents: {}", session.active_torrents().len());
Ok(())
}