11//! DHT Abstraction
22//!
33
4+ use std:: collections:: HashSet ;
45use std:: time:: Duration ;
56
67use crate :: domolibp2p:: { DomoBehaviour , OutEvent } ;
@@ -28,7 +29,7 @@ pub enum Event {
2829 VolatileData ( String ) ,
2930 Config ( String ) ,
3031 Discovered ( Vec < PeerId > ) ,
31- Ready ( Vec < PeerId > ) ,
32+ Ready ( HashSet < PeerId > ) ,
3233}
3334
3435fn handle_command ( swarm : & mut Swarm < DomoBehaviour > , cmd : Command ) -> bool {
@@ -169,15 +170,15 @@ pub fn dht_channel(
169170 let volatile = Topic :: new ( "domo-volatile-data" ) . hash ( ) ;
170171 let persistent = Topic :: new ( "domo-persistent-data" ) . hash ( ) ;
171172 let config = Topic :: new ( "domo-config" ) . hash ( ) ;
172-
173+ let mut ready_peers = HashSet :: new ( ) ;
173174 // Only peers that subscribed to all the topics are usable
174175 let check_peers = |swarm : & mut Swarm < DomoBehaviour > | {
175176 swarm
176177 . behaviour_mut ( )
177178 . gossipsub
178179 . all_peers ( )
179180 . filter_map ( |( p, topics) | {
180- log:: info !( "{p}, {topics:?}" ) ;
181+ log:: debug !( "{p}, {topics:?}" ) ;
181182 ( topics. contains ( & & volatile)
182183 && topics. contains ( & & persistent)
183184 && topics. contains ( & & config) )
@@ -192,10 +193,12 @@ pub fn dht_channel(
192193 // the mdns event is not enough to ensure we can send messages
193194 _ = interval. tick( ) => {
194195 log:: debug!( "{} Checking for peers" , swarm. local_peer_id( ) ) ;
195- let peers: Vec <_> = check_peers( & mut swarm) ;
196- if !peers. is_empty( ) &&
197- ev_send. send( Event :: Ready ( peers) ) . is_err( ) {
196+ let peers: HashSet <_> = check_peers( & mut swarm) ;
197+ if !peers. is_empty( ) && ready_peers != peers {
198+ ready_peers = peers. clone( ) ;
199+ if ev_send. send( Event :: Ready ( peers) ) . is_err( ) {
198200 return swarm;
201+ }
199202 }
200203 }
201204 cmd = cmd_recv. recv( ) => {
0 commit comments