use iroh::{Endpoint, protocol::{RouterBuilder, Router}, SecretKey};
use crate::protocol::{Evergreen, ALPN};
pub struct Client {
evergreen: Evergreen,
router: Router,
}
impl Client {
pub async fn new(identity: SecretKey) -> Result<Self, iroh::endpoint::BindError> {
let endpoint = Endpoint::builder(iroh::endpoint::presets::N0)
.alpns(vec![ALPN.into()])
.secret_key(identity)
.bind().await?;
let (evergreen, handler) = crate::protocol::new(endpoint.clone());
let router = RouterBuilder::new(endpoint.clone())
.accept(crate::protocol::ALPN, handler)
.spawn();
endpoint.online().await;
Ok(Client{
evergreen,
router,
})
}
pub async fn stop_client(self) {
let _ = self.router.shutdown().await;
}
}