Skip to content

Commit b514611

Browse files
committed
fix: send of command and events ---> cannot send Any
1 parent 9621d41 commit b514611

3 files changed

Lines changed: 44 additions & 13 deletions

File tree

src/packet_processor.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
use crate::{network::NetworkError, FragmentAssembler, RoutingHandler};
2-
use std::any::Any;
1+
use crate::{network::NetworkError, types::Command, FragmentAssembler, RoutingHandler};
32

43
use crossbeam_channel::{select_biased, Receiver};
54
use wg_internal::{network::NodeId, packet::{Packet, PacketType}};
65

7-
pub trait Processor {
8-
fn controller_recv(&self) -> &Receiver<Box<dyn Any>>;
6+
pub trait Processor: Send {
7+
fn controller_recv(&self) -> &Receiver<Box<dyn Command>>;
98
fn packet_recv(&self) -> &Receiver<Packet>;
109
fn assembler(&mut self) -> &mut FragmentAssembler;
1110
fn routing_handler(&mut self) -> &mut RoutingHandler;
1211

1312
fn handle_msg(&mut self, msg: Vec<u8>, from: NodeId, session_id: u64);
14-
fn handle_command(&mut self, cmd: Box<dyn Any>) -> bool;
13+
fn handle_command(&mut self, cmd: Box<dyn Command>) -> bool;
1514

1615
/// Handles a packet in a standard way
1716
/// # Errors

src/routing_handler.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use crate::{
22
network::{Network, NetworkError, Node},
3-
types::NodeEvent,
3+
types::{Event, NodeEvent},
44
};
55
use crossbeam_channel::Sender;
6-
use std::{
7-
any::Any,
8-
collections::{HashMap, HashSet},
9-
};
6+
use std::collections::{HashMap, HashSet};
107
use wg_internal::{
118
network::{NodeId, SourceRoutingHeader},
129
packet::{Ack, FloodRequest, FloodResponse, Fragment, Nack, NackType, NodeType, Packet},
@@ -85,7 +82,7 @@ pub struct RoutingHandler {
8582
flood_seen: HashSet<(u64, NodeId)>,
8683
session_counter: u64,
8784
flood_counter: u64,
88-
controller_send: Sender<Box<dyn Any>>,
85+
controller_send: Sender<Box<dyn Event>>,
8986
buffer: Buffer,
9087
}
9188

@@ -95,7 +92,7 @@ impl RoutingHandler {
9592
id: NodeId,
9693
node_type: NodeType,
9794
neighbors: HashMap<NodeId, Sender<Packet>>,
98-
controller_send: Sender<Box<dyn Any>>,
95+
controller_send: Sender<Box<dyn Event>>,
9996
) -> Self {
10097
Self {
10198
id,
@@ -506,6 +503,7 @@ mod routing_handler_tests {
506503
handler.start_flood().unwrap();
507504

508505
let packet = receiver.try_recv().unwrap();
506+
let packet = packet.into_any();
509507
if let Ok(cmd) = packet.downcast::<NodeEvent>() {
510508
assert!(matches!(*cmd, NodeEvent::FloodStarted(_, _)));
511509
}
@@ -566,7 +564,7 @@ mod routing_handler_tests {
566564
handler.handle_ack(&ack, 1, 2);
567565
}
568566

569-
fn create_test_routing_handler() -> (RoutingHandler, Receiver<Box<dyn Any>>) {
567+
fn create_test_routing_handler() -> (RoutingHandler, Receiver<Box<dyn Event>>) {
570568
let (controller_send, controller_recv) = unbounded();
571569
let (neighbor_send, _) = unbounded();
572570
let mut neighbors = HashMap::new();

src/types.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::any::Any;
12
use std::{collections::HashMap, str::FromStr};
23
use std::fmt::Display;
34
use anyhow::anyhow;
@@ -243,13 +244,44 @@ impl Message {
243244
}
244245
}
245246

247+
pub trait Command: Send {
248+
fn as_any(&self) -> &dyn Any;
249+
fn into_any(self: Box<Self>) -> Box<dyn Any>;
250+
251+
}
252+
253+
impl<T: 'static + Send> Command for T {
254+
fn as_any(&self) -> &dyn Any {
255+
self
256+
}
257+
fn into_any(self: Box<Self>) -> Box<dyn Any> {
258+
self
259+
}
260+
}
261+
262+
263+
pub trait Event: Send {
264+
fn as_any(&self) -> &dyn Any;
265+
fn into_any(self: Box<Self>) -> Box<dyn Any>;
266+
}
267+
268+
impl<T: 'static + Send> Event for T {
269+
fn as_any(&self) -> &dyn Any {
270+
self
271+
}
272+
fn into_any(self: Box<Self>) -> Box<dyn Any> {
273+
self
274+
}
275+
}
276+
246277
#[derive(Debug, Clone)]
247278
pub enum ChatCommand {
248279
GetChatsHistory,
249280
GetRegisteredClients,
250281
SendMessage(Message)
251282
}
252283

284+
253285
#[derive(Debug, Clone)]
254286
pub enum ChatEvent {
255287
ChatHistory(HashMap<NodeId, Vec<Message>>),
@@ -274,6 +306,7 @@ pub enum WebCommand {
274306
RemoveMediaFile(Uuid),
275307
}
276308

309+
277310
#[derive(Debug, Clone)]
278311
pub enum WebEvent {
279312
CachedFiles(Vec<File>),
@@ -305,6 +338,7 @@ pub enum NodeCommand {
305338
Shutdown,
306339
}
307340

341+
308342
impl NodeCommand {
309343
#[must_use]
310344
pub fn as_add_sender(self) -> Option<(NodeId, Sender<Packet>)> {

0 commit comments

Comments
 (0)