@@ -286,6 +286,22 @@ impl RoutingHandler {
286286 Ok ( ( ) )
287287 }
288288
289+ fn try_find_path ( & mut self , destination : NodeId ) -> Result < SourceRoutingHeader , NetworkError > {
290+ if destination == self . id {
291+ return Ok ( SourceRoutingHeader :: empty_route ( ) ) ;
292+ }
293+ let tries: u8 = 4 ;
294+ for _ in 0 ..tries {
295+ if let Some ( path) = self . network_view . find_path ( destination) {
296+ return Ok ( SourceRoutingHeader :: new ( path, 1 ) . without_loops ( ) ) ;
297+ }
298+ self . start_flood ( ) ?;
299+ // sleep for a short time to allow the flood to propagate
300+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
301+ }
302+ Err ( NetworkError :: PathNotFound ( destination) )
303+ }
304+
289305 /// Tries to send a packet to next hop until it succeeds or there are no more neighbors.
290306 /// If sending fails, it removes the neighbor, finds a new route and tries again.
291307 /// # Errors
@@ -308,11 +324,7 @@ impl RoutingHandler {
308324 if let Some ( first_hop) = packet. routing_header . hops . get ( 1 ) {
309325 self . remove_neighbor ( * first_hop) ;
310326 // remove neighbor and start flood
311- let route = self
312- . network_view
313- . find_path ( destination)
314- . ok_or ( NetworkError :: PathNotFound ( destination) ) ?;
315- packet. routing_header = SourceRoutingHeader :: new ( route, 1 ) . without_loops ( ) ;
327+ packet. routing_header = self . try_find_path ( destination) ?;
316328
317329 }
318330 }
@@ -342,13 +354,8 @@ impl RoutingHandler {
342354 if session_id. is_none ( ) {
343355 self . session_counter += 1 ;
344356 }
345- let shr = SourceRoutingHeader :: new (
346- self . network_view
347- . find_path ( destination)
348- . ok_or ( NetworkError :: PathNotFound ( destination) ) ?,
349- 1 ,
350- )
351- . without_loops ( ) ;
357+
358+ let shr = self . try_find_path ( destination) ?;
352359
353360 for ( i, chunk) in chunks. into_iter ( ) . enumerate ( ) {
354361 // Pad/truncate to exactly 128 bytes
0 commit comments