@@ -6,6 +6,7 @@ use iroh::{
66 Endpoint , NodeAddr , NodeId ,
77} ;
88use iroh_blobs:: { ticket:: BlobTicket , BlobFormat , Hash } ;
9+ use iroh_gossip:: proto:: TopicId ;
910use n0_future:: { task:: AbortOnDropHandle , SinkExt , StreamExt } ;
1011use rand:: Rng ;
1112use rcan:: Rcan ;
@@ -192,6 +193,36 @@ impl Client {
192193 let res = r. await ??;
193194 Ok ( res)
194195 }
196+
197+ /// Create a gossip topic.
198+ pub async fn put_gossip_topic (
199+ & mut self ,
200+ topic : TopicId ,
201+ label : String ,
202+ bootstrap : Vec < NodeId > ,
203+ ) -> Result < ( ) > {
204+ let ( s, r) = oneshot:: channel ( ) ;
205+ self . sender
206+ . send ( ActorMessage :: PutTopic {
207+ topic,
208+ label,
209+ bootstrap,
210+ s,
211+ } )
212+ . await ?;
213+ let res = r. await ??;
214+ Ok ( res)
215+ }
216+
217+ /// Delete a gossip topic.
218+ pub async fn delete_gossip_topic ( & mut self , topic : TopicId ) -> Result < ( ) > {
219+ let ( s, r) = oneshot:: channel ( ) ;
220+ self . sender
221+ . send ( ActorMessage :: DeleteTopic { topic, s } )
222+ . await ?;
223+ let res = r. await ??;
224+ Ok ( res)
225+ }
195226}
196227
197228struct Actor {
@@ -237,6 +268,16 @@ enum ActorMessage {
237268 name : String ,
238269 s : oneshot:: Sender < anyhow:: Result < Hash > > ,
239270 } ,
271+ PutTopic {
272+ topic : iroh_gossip:: proto:: TopicId ,
273+ label : String ,
274+ bootstrap : Vec < NodeId > ,
275+ s : oneshot:: Sender < anyhow:: Result < ( ) > > ,
276+ } ,
277+ DeleteTopic {
278+ topic : iroh_gossip:: proto:: TopicId ,
279+ s : oneshot:: Sender < anyhow:: Result < ( ) > > ,
280+ } ,
240281}
241282
242283impl Actor {
@@ -378,6 +419,61 @@ impl Actor {
378419 } ;
379420 s. send ( response) . ok ( ) ;
380421 }
422+ ActorMessage :: PutTopic {
423+ topic,
424+ label,
425+ bootstrap,
426+ s,
427+ } => {
428+ if let Err ( err) = self
429+ . writer
430+ . send ( ServerMessage :: PutTopic {
431+ topic : * topic. as_bytes ( ) ,
432+ label,
433+ bootstrap,
434+ } )
435+ . await
436+ {
437+ s. send ( Err ( err. into ( ) ) ) . ok ( ) ;
438+ return ;
439+ }
440+ let response = match self . reader . next ( ) . await {
441+ Some ( Ok ( msg) ) => match msg {
442+ ClientMessage :: PutTopicResponse ( None ) => Ok ( ( ) ) ,
443+ ClientMessage :: PutTopicResponse ( Some ( err) ) => {
444+ Err ( anyhow ! ( "put topic failed: {}" , err) )
445+ }
446+ _ => Err ( anyhow ! ( "unexpected message from server: {:?}" , msg) ) ,
447+ } ,
448+ Some ( Err ( err) ) => Err ( anyhow ! ( "failed to receive response: {:?}" , err) ) ,
449+ None => Err ( anyhow ! ( "connection closed" ) ) ,
450+ } ;
451+ s. send ( response) . ok ( ) ;
452+ }
453+ ActorMessage :: DeleteTopic { topic, s } => {
454+ if let Err ( err) = self
455+ . writer
456+ . send ( ServerMessage :: DeleteTopic {
457+ topic : * topic. as_bytes ( ) ,
458+ } )
459+ . await
460+ {
461+ s. send ( Err ( err. into ( ) ) ) . ok ( ) ;
462+ return ;
463+ }
464+ let response = match self . reader . next ( ) . await {
465+ Some ( Ok ( msg) ) => match msg {
466+ ClientMessage :: DeleteTopicResponse ( None ) => Ok ( ( ) ) ,
467+ ClientMessage :: DeleteTopicResponse ( Some ( err) ) => {
468+ Err ( anyhow ! ( "delete topic failed: {}" , err) )
469+ }
470+ _ => Err ( anyhow ! ( "unexpected message from server: {:?}" , msg) ) ,
471+ } ,
472+ Some ( Err ( err) ) => Err ( anyhow ! ( "failed to receive response: {:?}" , err) ) ,
473+ None => Err ( anyhow ! ( "connection closed" ) ) ,
474+ } ;
475+ s. send ( response) . ok ( ) ;
476+ }
381477 }
382478 }
383479
0 commit comments