|
| 1 | +use std::{env::Args, time::Instant, usize}; |
| 2 | + |
1 | 3 | use anyhow::Result;
|
2 |
| -use iroh::{protocol::Router, Endpoint, Watcher}; |
| 4 | +use clap::Parser; |
| 5 | +use iroh::{protocol::Router, Endpoint, NodeAddr, Watcher}; |
| 6 | +use iroh_base::ticket::NodeTicket; |
3 | 7 | use ping::EchoApi;
|
4 | 8 |
|
5 | 9 | #[tokio::main]
|
6 | 10 | async fn main() -> Result<()> {
|
7 |
| - // tracing_subscriber::fmt().init(); |
8 |
| - println!("Local use"); |
9 |
| - local().await?; |
10 |
| - println!("Remote use"); |
11 |
| - remote().await?; |
12 |
| - Ok(()) |
13 |
| -} |
14 |
| - |
15 |
| -async fn local() -> Result<()> { |
16 |
| - let api = EchoApi::spawn(); |
17 |
| - let res = api.echo(b"hello".to_vec()).await?; |
18 |
| - println!("value = {}", String::from_utf8_lossy(&res)); |
| 11 | + tracing_subscriber::fmt().init(); |
| 12 | + let args = cli::Args::parse(); |
| 13 | + match args { |
| 14 | + cli::Args::Listen { use_0rtt } => { |
| 15 | + let (server_router, server_addr) = { |
| 16 | + let endpoint = Endpoint::builder().bind().await?; |
| 17 | + endpoint.home_relay().initialized().await; |
| 18 | + let addr = endpoint.node_addr().initialized().await; |
| 19 | + let api = EchoApi::spawn(); |
| 20 | + let router = Router::builder(endpoint.clone()); |
| 21 | + let router = if use_0rtt { |
| 22 | + router.accept(EchoApi::ALPN, api.expose_0rtt()?) |
| 23 | + } else { |
| 24 | + router.accept(EchoApi::ALPN, api.expose()?) |
| 25 | + }; |
| 26 | + let router = router.spawn(); |
| 27 | + (router, addr) |
| 28 | + }; |
| 29 | + println!("NodeId: {}", server_addr.node_id); |
| 30 | + println!("Accepting 0rtt connections: {}", use_0rtt); |
| 31 | + let ticket = NodeTicket::from(server_addr); |
| 32 | + println!("Connect using:\n\ncargo run --example 0rtt connect {ticket}\n"); |
| 33 | + println!("Control-C to stop"); |
| 34 | + tokio::signal::ctrl_c() |
| 35 | + .await |
| 36 | + .expect("failed to listen for ctrl_c"); |
| 37 | + server_router.shutdown().await?; |
| 38 | + } |
| 39 | + cli::Args::Connect { |
| 40 | + ticket, |
| 41 | + n, |
| 42 | + delay_ms, |
| 43 | + use_0rtt, |
| 44 | + } => { |
| 45 | + let n = n |
| 46 | + .iter() |
| 47 | + .filter_map(|x| u64::try_from(*x).ok()) |
| 48 | + .next() |
| 49 | + .unwrap_or(u64::MAX); |
| 50 | + let delay = std::time::Duration::from_millis(delay_ms); |
| 51 | + let endpoint = Endpoint::builder().bind().await?; |
| 52 | + let addr: NodeAddr = ticket.into(); |
| 53 | + for i in 0..n { |
| 54 | + if use_0rtt { |
| 55 | + let api = EchoApi::connect_0rtt(endpoint.clone(), addr.clone()).await?; |
| 56 | + let msg = i.to_be_bytes(); |
| 57 | + let t0 = Instant::now(); |
| 58 | + let res = api.echo_0rtt(msg.to_vec()).await; |
| 59 | + drop(api); |
| 60 | + match res { |
| 61 | + Ok(data) => { |
| 62 | + let elapsed = t0.elapsed(); |
| 63 | + assert!(data == msg); |
| 64 | + println!("{}ms", elapsed.as_micros() as f64 / 1000.0); |
| 65 | + } |
| 66 | + Err(err) => { |
| 67 | + eprintln!("RPC error: {err}"); |
| 68 | + } |
| 69 | + } |
| 70 | + tokio::time::sleep(delay).await; |
| 71 | + } else { |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
19 | 76 | Ok(())
|
20 | 77 | }
|
21 | 78 |
|
22 |
| -async fn remote() -> Result<()> { |
23 |
| - let (server_router, server_addr) = { |
24 |
| - let endpoint = Endpoint::builder().discovery_n0().bind().await?; |
25 |
| - let api = EchoApi::spawn(); |
26 |
| - let router = Router::builder(endpoint.clone()) |
27 |
| - .accept(EchoApi::ALPN, api.expose_0rtt()?) |
28 |
| - .spawn(); |
29 |
| - let addr = endpoint.node_addr().initialized().await; |
30 |
| - (router, addr) |
31 |
| - }; |
| 79 | +mod cli { |
| 80 | + use anyhow::Result; |
| 81 | + use clap::Parser; |
| 82 | + use iroh::NodeId; |
| 83 | + use iroh_base::ticket::NodeTicket; |
32 | 84 |
|
33 |
| - let client_endpoint = Endpoint::builder().bind().await?; |
34 |
| - for i in 0..10 { |
35 |
| - let api = EchoApi::connect(client_endpoint.clone(), server_addr.clone()).await?; |
36 |
| - let res = api.echo_0rtt(b"hello".to_vec()).await?; |
37 |
| - println!("value = {}", String::from_utf8_lossy(&res)); |
| 85 | + #[derive(Debug, Parser)] |
| 86 | + pub enum Args { |
| 87 | + Listen { |
| 88 | + #[clap(long, default_value = "true")] |
| 89 | + use_0rtt: bool, |
| 90 | + }, |
| 91 | + Connect { |
| 92 | + ticket: NodeTicket, |
| 93 | + #[clap(short)] |
| 94 | + n: Option<usize>, |
| 95 | + #[clap(long, default_value = "true")] |
| 96 | + use_0rtt: bool, |
| 97 | + #[clap(long, default_value = "1000")] |
| 98 | + delay_ms: u64, |
| 99 | + }, |
38 | 100 | }
|
39 |
| - drop(server_router); |
40 |
| - Ok(()) |
41 | 101 | }
|
42 | 102 |
|
43 | 103 | mod ping {
|
44 | 104 | use anyhow::{Context, Result};
|
45 | 105 | use futures_util::FutureExt;
|
46 | 106 | use iroh::{
|
47 |
| - endpoint::{Connection, RecvStream, SendStream, ZeroRttAccepted}, |
| 107 | + endpoint::{Connection, RecvStream, SendStream}, |
48 | 108 | Endpoint,
|
49 | 109 | };
|
50 | 110 | use irpc::{channel::oneshot, rpc::RemoteService, rpc_requests, Client, WithChannels};
|
51 |
| - use irpc_iroh::{Iroh0RttProtocol, IrohProtocol, IrohRemoteConnection}; |
| 111 | + use irpc_iroh::{Iroh0RttProtocol, IrohProtocol}; |
52 | 112 | use n0_future::future;
|
53 | 113 | use serde::{Deserialize, Serialize};
|
54 | 114 | use tracing::info;
|
@@ -95,7 +155,22 @@ mod ping {
|
95 | 155 | Ok(IrohProtocol::new(EchoProtocol::remote_handler(local)))
|
96 | 156 | }
|
97 | 157 |
|
98 |
| - pub async fn connect( |
| 158 | + // pub async fn connect( |
| 159 | + // endpoint: Endpoint, |
| 160 | + // addr: impl Into<iroh::NodeAddr>, |
| 161 | + // ) -> Result<EchoApi> { |
| 162 | + // let conn = endpoint |
| 163 | + // .connect(addr, Self::ALPN) |
| 164 | + // .await |
| 165 | + // .context("failed to connect to remote service")?; |
| 166 | + // let fut: future::Boxed<bool> = Box::pin(async { true }); |
| 167 | + // Ok(EchoApi { |
| 168 | + // inner: Client::boxed(IrohConnection(conn)), |
| 169 | + // zero_rtt_accepted: fut.shared(), |
| 170 | + // }) |
| 171 | + // } |
| 172 | + |
| 173 | + pub async fn connect_0rtt( |
99 | 174 | endpoint: Endpoint,
|
100 | 175 | addr: impl Into<iroh::NodeAddr>,
|
101 | 176 | ) -> Result<EchoApi> {
|
|
0 commit comments