Skip to content

Commit 5bb75a7

Browse files
committed
feat/fix: added new function try_find_path which tries 3 times to find path to dest
1 parent 583813f commit 5bb75a7

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

src/routing_handler.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/types.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ impl TextFile {
8383

8484

8585
#[derive(Debug, Clone, Serialize, Deserialize, Hash, PartialEq, Eq)]
86+
8687
pub struct MediaFile {
8788
pub id: Uuid,
8889
pub title: String,
@@ -253,7 +254,7 @@ pub enum WebCommand {
253254
GetTextFiles,
254255
GetTextFile(Uuid),
255256
GetMediaFiles,
256-
GetMediaFile(Uuid),
257+
GetMediaFile { media_id: Uuid, location: NodeId },
257258
AddTextFile(TextFile),
258259
AddTextFileFromPath(String),
259260
AddMediaFile(MediaFile),
@@ -319,4 +320,4 @@ pub enum ServerType {
319320
ChatServer,
320321
TextServer,
321322
MediaServer,
322-
}
323+
}

0 commit comments

Comments
 (0)