use anyhow::Result;
use futures::prelude::*;
use structopt::StructOpt;
use tokio::time::{self, Duration};
use tsclientlib::prelude::*;
use tsclientlib::sync::SyncConnection;
use tsclientlib::{Connection, DisconnectOptions, Identity, StreamItem};
#[derive(StructOpt, Debug)]
#[structopt(author, about)]
struct Args {
#[structopt(short = "a", long, default_value = "localhost")]
address: String,
#[structopt(short = "v", long, parse(from_occurrences))]
verbose: u8,
}
#[tokio::main]
async fn main() -> Result<()> { real_main().await }
async fn real_main() -> Result<()> {
let args = Args::from_args();
let con_config = Connection::build(args.address)
.log_commands(args.verbose >= 1)
.log_packets(args.verbose >= 2)
.log_udp_packets(args.verbose >= 3);
let id = Identity::new_from_str(
"MG0DAgeAAgEgAiAIXJBlj1hQbaH0Eq0DuLlCmH8bl+veTAO2+\
k9EQjEYSgIgNnImcmKo7ls5mExb6skfK2Tw+u54aeDr0OP1ITs\
C/50CIA8M5nmDBnmDM/gZ//4AAAAAAAAAAAAAAAAAAAAZRzOI").unwrap();
let con_config = con_config.identity(id);
let mut con = con_config.connect()?;
let r = con
.events()
.try_filter(|e| future::ready(matches!(e, StreamItem::BookEvents(_))))
.next()
.await;
if let Some(r) = r {
r?;
}
let sync_con: SyncConnection = con.into();
let mut con = sync_con.get_handle();
tokio::spawn(sync_con.for_each(|_| future::ready(())));
con.with_connection(move |mut con| {
let state = con.get_state()?;
state.server.send_textmessage("Hello there").send(&mut con)?;
Result::<_>::Ok(())
})
.await??;
time::sleep(Duration::from_secs(1)).await;
con.disconnect(DisconnectOptions::new()).await?;
Ok(())
}