Skip to content

Commit 8323a27

Browse files
committed
fix: handling shutdown
1 parent 7252b5a commit 8323a27

2 files changed

Lines changed: 34 additions & 7 deletions

File tree

src/packet_processor.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub trait Processor {
1111
fn routing_header(&mut self) -> &mut RoutingHandler;
1212

1313
fn handle_msg(&mut self, msg: Vec<u8>, from: NodeId, session_id: u64);
14-
fn handle_command(&mut self, cmd: Box<dyn Any>);
14+
fn handle_command(&mut self, cmd: Box<dyn Any>) -> Result<(), ()>;
1515

1616
/// Handles a packet in a standard way
1717
/// # Errors
@@ -20,11 +20,15 @@ pub trait Processor {
2020
let router = self.routing_header();
2121
match pkt.pack_type {
2222
PacketType::MsgFragment(fragment) => {
23+
let idx = fragment.fragment_index;
2324
if let Some(msg) = self.assembler().add_fragment(
2425
fragment,
2526
pkt.session_id,
2627
pkt.routing_header.hops[0],
2728
) {
29+
let mut shr = pkt.routing_header.clone();
30+
shr.reverse();
31+
self.routing_header().send_ack(shr, pkt.session_id, idx)?;
2832
self.handle_msg(msg, pkt.routing_header.hops[0], pkt.session_id);
2933
}
3034
}
@@ -49,15 +53,16 @@ pub trait Processor {
4953
select_biased! {
5054
recv(self.controller_recv()) -> cmd => {
5155
if let Ok(cmd) = cmd {
52-
self.handle_command(cmd);
56+
if self.handle_command(cmd).is_err() {
57+
return
58+
}
5359
}
5460
}
5561

5662
recv(self.packet_recv()) -> pkt => {
5763
if let Ok(pkt) = pkt {
58-
match self.handle_packet(pkt) {
59-
Ok(()) => {},
60-
Err(_) => return
64+
if self.handle_packet(pkt).is_err() {
65+
return
6166
}
6267
}
6368
}

src/types.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashMap;
2+
13
use serde::{Deserialize, Serialize};
24
use wg_internal::{network::NodeId, packet::Packet};
35
use crossbeam_channel::Sender;
@@ -40,9 +42,29 @@ pub enum ChatResponse {
4042
RegistrationSuccess,
4143
}
4244

45+
#[derive(Serialize, Deserialize, Debug, Clone)]
46+
pub struct Message {
47+
from: NodeId,
48+
to: NodeId,
49+
text: String,
50+
}
51+
52+
impl Message {
53+
pub fn new(from: NodeId, to: NodeId, text: String) -> Self {
54+
Message { from, to, text }
55+
}
56+
}
4357

44-
pub enum ChatClientCommand {
45-
NodeCommand(NodeCommand)
58+
#[derive(Debug, Clone)]
59+
pub enum ChatCommand {
60+
GetChatsHistories,
61+
GetConnectedClients,
62+
}
63+
64+
#[derive(Debug, Clone)]
65+
pub enum ChatEvent {
66+
ClientHistory(HashMap<NodeId, Vec<Message>>),
67+
ConnectedClients(Vec<NodeId>),
4668
}
4769

4870

0 commit comments

Comments
 (0)