@@ -32,7 +32,30 @@ use crate::protocol::{
32
32
/// `tracker` is the node id of the tracker to announce to. It must understand the [TRACKER_ALPN] protocol.
33
33
/// `content` is the content to announce.
34
34
/// `kind` is the kind of the announcement. We can claim to have the complete data or only some of it.
35
- pub async fn announce (
35
+ pub async fn announce_quinn (
36
+ connection : iroh_quinn:: Connection ,
37
+ signed_announce : SignedAnnounce ,
38
+ ) -> anyhow:: Result < ( ) > {
39
+ let ( mut send, mut recv) = connection. open_bi ( ) . await ?;
40
+ tracing:: debug!( "opened bi stream" ) ;
41
+ let request = Request :: Announce ( signed_announce) ;
42
+ let request = postcard:: to_stdvec ( & request) ?;
43
+ tracing:: debug!( "sending announce" ) ;
44
+ send. write_all ( & request) . await ?;
45
+ send. finish ( ) ?;
46
+ let _response = recv. read_to_end ( REQUEST_SIZE_LIMIT ) . await ?;
47
+ Ok ( ( ) )
48
+ }
49
+
50
+ /// Announce to a tracker.
51
+ ///
52
+ /// You can only announce content you yourself claim to have, to avoid spamming other nodes.
53
+ ///
54
+ /// `endpoint` is the iroh endpoint to use for announcing.
55
+ /// `tracker` is the node id of the tracker to announce to. It must understand the [TRACKER_ALPN] protocol.
56
+ /// `content` is the content to announce.
57
+ /// `kind` is the kind of the announcement. We can claim to have the complete data or only some of it.
58
+ pub async fn announce_iroh (
36
59
connection : iroh:: endpoint:: Connection ,
37
60
signed_announce : SignedAnnounce ,
38
61
) -> anyhow:: Result < ( ) > {
@@ -80,7 +103,7 @@ async fn query_socket_one(
80
103
args : Query ,
81
104
) -> anyhow:: Result < Vec < SignedAnnounce > > {
82
105
let connection = endpoint. connect ( addr) . await ?;
83
- let result = query ( connection, args) . await ?;
106
+ let result = query_quinn ( connection, args) . await ?;
84
107
Ok ( result. hosts )
85
108
}
86
109
@@ -90,7 +113,7 @@ async fn query_iroh_one(
90
113
args : Query ,
91
114
) -> anyhow:: Result < Vec < SignedAnnounce > > {
92
115
let connection = endpoint. connect ( node_id, ALPN ) . await ?;
93
- let result = query ( connection, args) . await ?;
116
+ let result = query_iroh ( connection, args) . await ?;
94
117
Ok ( result. hosts )
95
118
}
96
119
@@ -185,9 +208,29 @@ pub fn announce_dht(
185
208
}
186
209
187
210
/// Assume an existing connection to a tracker and query it for peers for some content.
188
- pub async fn query (
211
+ pub async fn query_iroh (
189
212
connection : iroh:: endpoint:: Connection ,
190
213
args : Query ,
214
+ ) -> anyhow:: Result < QueryResponse > {
215
+ tracing:: info!( "connected to {:?}" , connection. remote_node_id( ) ?) ;
216
+ let ( mut send, mut recv) = connection. open_bi ( ) . await ?;
217
+ tracing:: info!( "opened bi stream" ) ;
218
+ let request = Request :: Query ( args) ;
219
+ let request = postcard:: to_stdvec ( & request) ?;
220
+ tracing:: info!( "sending query" ) ;
221
+ send. write_all ( & request) . await ?;
222
+ send. finish ( ) ?;
223
+ let response = recv. read_to_end ( REQUEST_SIZE_LIMIT ) . await ?;
224
+ let response = postcard:: from_bytes :: < Response > ( & response) ?;
225
+ Ok ( match response {
226
+ Response :: QueryResponse ( response) => response,
227
+ } )
228
+ }
229
+
230
+ /// Assume an existing connection to a tracker and query it for peers for some content.
231
+ pub async fn query_quinn (
232
+ connection : iroh_quinn:: Connection ,
233
+ args : Query ,
191
234
) -> anyhow:: Result < QueryResponse > {
192
235
tracing:: info!( "connected to {:?}" , connection. remote_address( ) ) ;
193
236
let ( mut send, mut recv) = connection. open_bi ( ) . await ?;
@@ -283,14 +326,23 @@ pub async fn connect(
283
326
tracker : & TrackerId ,
284
327
local_ipv4_addr : SocketAddrV4 ,
285
328
local_ipv6_addr : SocketAddrV6 ,
286
- ) -> anyhow:: Result < iroh :: endpoint :: Connection > {
329
+ ) -> anyhow:: Result < Connection > {
287
330
match tracker {
288
- TrackerId :: Quinn ( tracker) => connect_socket ( * tracker, local_ipv4_addr. into ( ) ) . await ,
289
- TrackerId :: Iroh ( tracker) => connect_iroh ( * tracker, local_ipv4_addr, local_ipv6_addr) . await ,
331
+ TrackerId :: Quinn ( tracker) => Ok ( Connection :: Quinn (
332
+ connect_socket ( * tracker, local_ipv4_addr. into ( ) ) . await ?,
333
+ ) ) ,
334
+ TrackerId :: Iroh ( tracker) => Ok ( Connection :: Iroh (
335
+ connect_iroh ( * tracker, local_ipv4_addr, local_ipv6_addr) . await ?,
336
+ ) ) ,
290
337
TrackerId :: Udp ( _) => anyhow:: bail!( "can not connect to udp tracker" ) ,
291
338
}
292
339
}
293
340
341
+ pub enum Connection {
342
+ Iroh ( iroh:: endpoint:: Connection ) ,
343
+ Quinn ( iroh_quinn:: Connection ) ,
344
+ }
345
+
294
346
/// Create a iroh endpoint and connect to a tracker using the [crate::protocol::ALPN] protocol.
295
347
async fn connect_iroh (
296
348
tracker : NodeId ,
@@ -307,13 +359,13 @@ async fn connect_iroh(
307
359
Ok ( connection)
308
360
}
309
361
310
- /// Create a quinn endpoint and connect to a tracker using the [crate::protocol::ALPN ] protocol.
362
+ /// Create a quinn endpoint and connect to a tracker using the [crate] protocol.
311
363
async fn connect_socket (
312
364
tracker : SocketAddr ,
313
365
local_addr : SocketAddr ,
314
- ) -> anyhow:: Result < iroh :: endpoint :: Connection > {
366
+ ) -> anyhow:: Result < iroh_quinn :: Connection > {
315
367
let endpoint = create_quinn_client ( local_addr, vec ! [ ALPN . to_vec( ) ] , false ) ?;
316
- tracing:: info!( "trying to connect to tracker at {:?}" , tracker) ;
368
+ tracing:: info!( "trying t?o ) connect to tracker at {:?}" , tracker) ;
317
369
let connection = endpoint. connect ( tracker, "localhost" ) ?. await ?;
318
370
Ok ( connection)
319
371
}
0 commit comments