Skip to content

Commit 4dad1ed

Browse files
authored
Merge pull request #4 from Rustastic/network_caching
Network caching
2 parents 0b90c9b + 95e77c1 commit 4dad1ed

File tree

6 files changed

+52
-77
lines changed

6 files changed

+52
-77
lines changed

Cargo.lock

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77
wg_2024 = { git = "https://github.com/WGL-2024/WGL_repo_2024.git", features = ["serialize", "debug"] }
88
messages = { git = "https://github.com/Rustastic/Messages.git"}
99
assembler = { git = "https://github.com/Rustastic/Assembler.git"}
10-
source_routing = { git = "https://github.com/Rustastic/source_routing.git"}
10+
source_routing = { git = "https://github.com/Rustastic/source_routing.git", branch = "network_caching"}
1111
crossbeam-channel = "0.5.13"
1212
toml = "0.8.19"
1313
rand = "0.8.0"

src/chat_client/handle_command/flooding.rs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/chat_client/handle_command/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use messages::{
44
client_commands::{ChatClientCommand, ChatClientEvent},
55
high_level_messages::{ClientMessage, MessageContent},
66
};
7+
use std::{thread, time::Duration};
78

89
use super::ChatClient;
910

10-
mod flooding;
1111
mod send_message;
1212

1313
impl ChatClient {
@@ -25,6 +25,7 @@ impl ChatClient {
2525
self.id,
2626
);
2727
e.insert(sender);
28+
self.router.add_neighbour(node_id);
2829
} else {
2930
warn!(
3031
"{} [ ChatClient {} ] is already connected to [ Drone {} ]",
@@ -43,6 +44,7 @@ impl ChatClient {
4344
self.id
4445
);
4546
self.packet_send.remove(&node_id);
47+
self.router.remove_neighbour(node_id);
4648
} else {
4749
warn!(
4850
"{} [ ChatClient {} ] is already disconnected from [ Drone {} ]",
@@ -58,7 +60,17 @@ impl ChatClient {
5860
"ℹ".blue(),
5961
self.id
6062
);
61-
self.start_flooding();
63+
let requests = self.router.get_flood_requests(self.packet_send.len());
64+
for (sender, request) in self.packet_send.values().zip(requests) {
65+
if sender.send(request).is_err() {
66+
error!(
67+
"{} [ ChatClient {} ]: Failed to send floodrequest",
68+
"✓".green(),
69+
self.id
70+
);
71+
}
72+
}
73+
thread::sleep(Duration::from_secs(2));
6274
}
6375
ChatClientCommand::StartChatClient => {
6476
self.running = true;

src/chat_client/handle_packet/mod.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::{thread, time::Duration};
2+
13
use super::ChatClient;
24
use colored::Colorize;
35
use log::{error, info, warn};
@@ -391,14 +393,13 @@ impl ChatClient {
391393

392394
self.router.dropped_fragment(unreachable_node);
393395

394-
if let Some(incorrect_packet) =
395-
self
396-
.msgfactory
397-
.take_packet(packet.session_id, nack.fragment_index)
396+
if let Some(incorrect_packet) = self
397+
.msgfactory
398+
.take_packet(packet.session_id, nack.fragment_index)
398399
{
399400
let dest = incorrect_packet.routing_header.destination().unwrap();
400401

401-
let _ = self.router.drone_crashed(unreachable_node);
402+
self.router.drone_crashed(unreachable_node);
402403

403404
if let Ok(new_routing_header) = self.router.get_source_routing_header(dest) {
404405
let new_packet = Packet {
@@ -424,7 +425,6 @@ impl ChatClient {
424425
self.id,
425426
dest
426427
);
427-
self.reinit_network();
428428
}
429429
}
430430
}
@@ -438,32 +438,37 @@ impl ChatClient {
438438
);
439439
}
440440
NackType::Dropped => {
441-
442-
443441
self.router.dropped_fragment(nack_src);
444442

445443
if let Some((dropped_packet, requests)) = self
446444
.msgfactory
447445
.get_packet(packet.session_id, nack.fragment_index)
448446
{
447+
if requests > 100 {
448+
info!(
449+
"{} [ ChatClient {} ]: Reinitializing network due to excessive dropped requests",
450+
"ℹ".blue(),
451+
self.id
452+
);
453+
let requests = self.router.get_flood_requests(self.packet_send.len());
454+
for (sender, request) in self.packet_send.values().zip(requests) {
455+
if sender.send(request).is_err() {
456+
error!(
457+
"{} [ ChatClient {} ]: Failed to send floodrequest",
458+
"✓".green(),
459+
self.id
460+
);
461+
}
462+
}
463+
thread::sleep(Duration::from_secs(2));
464+
}
449465
error!(
450466
"{} [ ChatClient {} ]: Packet with session_id: {} and fragment_index: {} has been dropped",
451467
"✗".red(),
452468
self.id,
453469
dropped_packet.session_id,
454470
nack.fragment_index
455471
);
456-
457-
if requests > 100 {
458-
error!(
459-
"{} [ ChatClient {} ]: Packet with session_id: {} and fragment_index: {} has been dropped more than 100 times. Dropping permanently.",
460-
"✗".red(),
461-
self.id,
462-
dropped_packet.session_id,
463-
nack.fragment_index
464-
);
465-
return;
466-
}
467472

468473
let destination = dropped_packet.routing_header.destination().unwrap();
469474

@@ -494,7 +499,6 @@ impl ChatClient {
494499
self.id,
495500
destination
496501
);
497-
self.reinit_network();
498502
}
499503
}
500504
}

src/chat_client/mod.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
use assembler::HighLevelMessageFactory;
2-
use colored::Colorize;
32
use crossbeam_channel::{select_biased, Receiver, Sender};
4-
use log::info;
53
use messages::{
64
client_commands::{ChatClientCommand, ChatClientEvent},
75
high_level_messages::Message,
86
};
97
use source_routing::Router;
10-
use std::thread;
11-
use std::{collections::HashMap, time::Duration};
8+
use std::collections::HashMap;
129
use wg_2024::{
1310
network::NodeId,
1411
packet::{NodeType, Packet},
@@ -107,14 +104,4 @@ impl ChatClient {
107104
}
108105
}
109106
}
110-
111-
fn reinit_network(&mut self) {
112-
info!(
113-
"{} [ ChatClient {} ]: reinitializing the network...",
114-
"✓".green(),
115-
self.id
116-
);
117-
self.start_flooding();
118-
thread::sleep(Duration::from_secs(2));
119-
}
120107
}

0 commit comments

Comments
 (0)