Skip to content

Commit 791d015

Browse files
committed
fix: send of command and events ---> cannot send Any
1 parent 9d0c556 commit 791d015

4 files changed

Lines changed: 45 additions & 41 deletions

File tree

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7-
tokio = { version = "1", features = ["full"] }
87
crossbeam = "0.8"
98
wg_internal = { git = "https://github.com/WGL-2024/WGL_repo_2024.git", features = ["debug"] }
109
common = { path = "../common" }

src/communication_server.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
use std::any::Any;
21
use std::collections::{HashMap, HashSet};
32
use crossbeam::channel::{Receiver, Sender};
43
use wg_internal::network::NodeId;
54
use wg_internal::packet::{NodeType, Packet};
65
use common::{FragmentAssembler, RoutingHandler};
76
use common::packet_processor::Processor;
8-
use common::types::{ChatCommand, ChatEvent, ChatRequest, ChatResponse, NodeCommand, ServerType};
7+
use common::types::{ChatCommand, ChatEvent, ChatRequest, ChatResponse, Command, Event, NodeCommand, ServerType};
98

109
pub struct ChatServer {
1110
routing_handler: RoutingHandler,
12-
controller_recv: Receiver<Box<dyn Any>>,
13-
controller_send: Sender<Box<dyn Any>>,
11+
controller_recv: Receiver<Box<dyn Command>>,
12+
controller_send: Sender<Box<dyn Event>>,
1413
packet_recv: Receiver<Packet>,
1514
_id: NodeId,
1615
assembler: FragmentAssembler,
1716
registered_clients: HashSet<NodeId>,
1817
}
1918

2019
impl ChatServer {
21-
pub fn new(id: NodeId, neighbors: HashMap<NodeId, Sender<Packet>>, packet_recv: Receiver<Packet>, controller_recv: Receiver<Box<dyn Any>>, controller_send: Sender<Box<dyn Any>>) -> Self {
20+
pub fn new(id: NodeId, neighbors: HashMap<NodeId, Sender<Packet>>, packet_recv: Receiver<Packet>, controller_recv: Receiver<Box<dyn Command>>, controller_send: Sender<Box<dyn Event>>) -> Self {
2221
let router = RoutingHandler::new(id, NodeType::Server, neighbors, controller_send.clone());
2322
Self {
2423
routing_handler: router,
@@ -36,7 +35,7 @@ impl ChatServer {
3635
}
3736

3837
impl Processor for ChatServer {
39-
fn controller_recv(&self) -> &Receiver<Box<dyn Any>> {
38+
fn controller_recv(&self) -> &Receiver<Box<dyn Command>> {
4039
&self.controller_recv
4140
}
4241

@@ -87,7 +86,8 @@ impl Processor for ChatServer {
8786
}
8887
}
8988

90-
fn handle_command(&mut self, cmd: Box<dyn Any>) -> bool {
89+
fn handle_command(&mut self, cmd: Box<dyn Command>) -> bool {
90+
let cmd = cmd.into_any();
9191
if let Some(cmd) = cmd.downcast_ref::<NodeCommand>() {
9292
match cmd {
9393
NodeCommand::AddSender(node_id, sender) => self.routing_handler.add_neighbor(*node_id, sender.clone()),
@@ -113,13 +113,14 @@ mod communication_server_tests {
113113
use super::*;
114114
use crossbeam::channel::unbounded;
115115

116-
fn create_test_chat_server() -> (ChatServer, Receiver<Packet>, Sender<Box<dyn Any>>) {
116+
fn create_test_chat_server() -> (ChatServer, Receiver<Packet>, Sender<Box<dyn Command>>) {
117117
let (controller_send, controller_recv) = unbounded();
118+
let (event_send, _event_recv) = unbounded::<Box<dyn Event>>();
118119
let (packet_send, packet_recv) = unbounded();
119120
let mut neighbors = HashMap::new();
120121
neighbors.insert(2, packet_send);
121122

122-
let server = ChatServer::new(1, neighbors, packet_recv.clone(), controller_recv, controller_send.clone());
123+
let server = ChatServer::new(1, neighbors, packet_recv.clone(), controller_recv, event_send);
123124
(server, packet_recv, controller_send)
124125
}
125126

src/media_server.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
use std::any::Any;
21
use std::collections::HashMap;
32
use crossbeam::channel::{Receiver, Sender};
43
use uuid::Uuid;
54
use wg_internal::network::NodeId;
65
use wg_internal::packet::{NodeType, Packet};
76
use common::{FragmentAssembler, RoutingHandler};
87
use common::packet_processor::Processor;
9-
use common::types::{MediaFile, NodeCommand, ServerType, WebCommand, WebEvent, WebRequest, WebResponse};
8+
use common::types::{Command, Event, MediaFile, NodeCommand, ServerType, WebCommand, WebEvent, WebRequest, WebResponse};
109
use common::file_conversion;
1110

1211
pub struct MediaServer {
1312
routing_handler: RoutingHandler,
14-
controller_recv: Receiver<Box<dyn Any>>,
15-
controller_send: Sender<Box<dyn Any>>,
13+
controller_recv: Receiver<Box<dyn Command>>,
14+
controller_send: Sender<Box<dyn Event>>,
1615
packet_recv: Receiver<Packet>,
1716
_id: NodeId,
1817
assembler: FragmentAssembler,
@@ -24,8 +23,8 @@ impl MediaServer {
2423
id: NodeId,
2524
neighbors: HashMap<NodeId, Sender<Packet>>,
2625
packet_recv: Receiver<Packet>,
27-
controller_recv: Receiver<Box<dyn Any>>,
28-
controller_send: Sender<Box<dyn Any>>
26+
controller_recv: Receiver<Box<dyn Command>>,
27+
controller_send: Sender<Box<dyn Event>>
2928
) -> Self {
3029
let router = RoutingHandler::new(id, NodeType::Server, neighbors, controller_send.clone());
3130
Self {
@@ -64,7 +63,7 @@ impl MediaServer {
6463
}
6564

6665
impl Processor for MediaServer {
67-
fn controller_recv(&self) -> &Receiver<Box<dyn Any>> {
66+
fn controller_recv(&self) -> &Receiver<Box<dyn Command>> {
6867
&self.controller_recv
6968
}
7069

@@ -119,7 +118,8 @@ impl Processor for MediaServer {
119118
}
120119
}
121120

122-
fn handle_command(&mut self, cmd: Box<dyn Any>) -> bool {
121+
fn handle_command(&mut self, cmd: Box<dyn Command>) -> bool {
122+
let cmd = cmd.into_any();
123123
if let Some(cmd) = cmd.downcast_ref::<NodeCommand>() {
124124
match cmd {
125125
NodeCommand::AddSender(node_id, sender) => self.routing_handler.add_neighbor(*node_id, sender.clone()),
@@ -137,7 +137,7 @@ impl Processor for MediaServer {
137137
return true;
138138
}
139139
}
140-
WebCommand::GetMediaFile{media_id, location} => {
140+
WebCommand::GetMediaFile{media_id, location: _location} => {
141141
if let Some(media_file) = self.get_media_by_id(*media_id) {
142142
if self.controller_send
143143
.send(Box::new(WebEvent::MediaFile(media_file.clone())))
@@ -240,20 +240,22 @@ mod media_server_tests {
240240
#[test]
241241
fn test_media_server_creation() {
242242
let (controller_send, controller_recv) = unbounded();
243+
let (event_send, _event_recv) = unbounded::<Box<dyn Event>>();
243244
let (_, packet_recv) = unbounded();
244245

245-
let server = MediaServer::new(1, HashMap::new(), packet_recv, controller_recv, controller_send);
246+
let server = MediaServer::new(1, HashMap::new(), packet_recv, controller_recv, event_send);
246247

247-
assert_eq!(server.id, 1);
248+
assert_eq!(server._id, 1);
248249
assert!(server.stored_media.is_empty());
249250
}
250251

251252
#[test]
252253
fn test_get_media_list() {
253254
let (controller_send, controller_recv) = unbounded();
255+
let (event_send, _event_recv) = unbounded::<Box<dyn Event>>();
254256
let (_, packet_recv) = unbounded();
255257

256-
let mut server = MediaServer::new(1, HashMap::new(), packet_recv, controller_recv, controller_send);
258+
let mut server = MediaServer::new(1, HashMap::new(), packet_recv, controller_recv, event_send);
257259
let test_media = MediaFile::new(
258260
"test_image.png".to_string(),
259261
vec![vec![0x89, 0x50, 0x4E, 0x47]]
@@ -268,9 +270,10 @@ mod media_server_tests {
268270
#[test]
269271
fn test_add_and_retrieve_media() {
270272
let (controller_send, controller_recv) = unbounded();
273+
let (event_send, _event_recv) = unbounded::<Box<dyn Event>>();
271274
let (_, packet_recv) = unbounded();
272275

273-
let mut server = MediaServer::new(1, HashMap::new(), packet_recv, controller_recv, controller_send);
276+
let mut server = MediaServer::new(1, HashMap::new(), packet_recv, controller_recv, event_send);
274277

275278
let test_media = MediaFile::new(
276279
"test_image.png".to_string(),

src/text_server.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
use std::any::Any;
2-
use std::collections::{HashMap};
1+
use std::collections::HashMap;
32
use crossbeam::channel::{Receiver, Sender};
4-
use uuid::{Uuid};
3+
use uuid::Uuid;
54
use wg_internal::network::NodeId;
65
use wg_internal::packet::{NodeType, Packet};
76
use common::{FragmentAssembler, RoutingHandler};
87
use common::packet_processor::Processor;
9-
use common::types::{File, NodeCommand, ServerType, TextFile, WebCommand, WebEvent, WebRequest, WebResponse};
8+
use common::types::{Command, Event, File, NodeCommand, ServerType, TextFile, WebCommand, WebEvent, WebRequest, WebResponse};
109
use common::file_conversion;
1110

1211
pub struct TextServer {
1312
routing_handler: RoutingHandler,
14-
controller_recv: Receiver<Box<dyn Any>>,
15-
controller_send: Sender<Box<dyn Any>>,
13+
controller_recv: Receiver<Box<dyn Command>>,
14+
controller_send: Sender<Box<dyn Event>>,
1615
packet_recv: Receiver<Packet>,
1716
id: NodeId,
1817
assembler: FragmentAssembler,
@@ -24,8 +23,8 @@ impl TextServer {
2423
id: NodeId,
2524
neighbors: HashMap<NodeId, Sender<Packet>>,
2625
packet_recv: Receiver<Packet>,
27-
controller_recv: Receiver<Box<dyn Any>>,
28-
controller_send: Sender<Box<dyn Any>>
26+
controller_recv: Receiver<Box<dyn Command>>,
27+
controller_send: Sender<Box<dyn Event>>
2928
) -> Self {
3029
let router = RoutingHandler::new(id, NodeType::Server, neighbors, controller_send.clone());
3130
Self {
@@ -64,7 +63,7 @@ impl TextServer {
6463
}
6564

6665
impl Processor for TextServer {
67-
fn controller_recv(&self) -> &Receiver<Box<dyn Any>> {
66+
fn controller_recv(&self) -> &Receiver<Box<dyn Command>> {
6867
&self.controller_recv
6968
}
7069

@@ -123,7 +122,8 @@ impl Processor for TextServer {
123122
}
124123
}
125124

126-
fn handle_command(&mut self, cmd: Box<dyn Any>) -> bool {
125+
fn handle_command(&mut self, cmd: Box<dyn Command>) -> bool {
126+
let cmd = cmd.into_any();
127127
if let Some(cmd) = cmd.downcast_ref::<NodeCommand>() {
128128
match cmd {
129129
NodeCommand::AddSender(node_id, sender) => self.routing_handler.add_neighbor(*node_id, sender.clone()),
@@ -183,7 +183,7 @@ impl Processor for TextServer {
183183
return true;
184184
}
185185
}
186-
WebCommand::GetMediaFile{media_id,location} => {
186+
WebCommand::GetMediaFile{media_id,location: _location} => {
187187
if self.controller_send
188188
.send(Box::new(WebEvent::FileNotFound(*media_id)))
189189
.is_err()
@@ -204,7 +204,7 @@ impl Processor for TextServer {
204204
}
205205
}
206206
WebCommand::AddTextFileFromPath(file_path) => {
207-
match file_conversion::file_to_text_file(file_path) {
207+
match file_conversion::file_to_text_file(&file_path) {
208208
Ok(text_file) => {
209209
let file_id = text_file.id;
210210
self.add_text_file(text_file);
@@ -271,8 +271,9 @@ mod text_server_tests {
271271

272272
fn create_test_text_server() -> TextServer {
273273
let (controller_send, controller_recv) = unbounded();
274+
let (event_send, _event_recv) = unbounded::<Box<dyn Event>>();
274275
let (_, packet_recv) = unbounded();
275-
TextServer::new(1, HashMap::new(), packet_recv, controller_recv, controller_send)
276+
TextServer::new(1, HashMap::new(), packet_recv, controller_recv, event_send)
276277
}
277278

278279
#[test]
@@ -415,13 +416,13 @@ mod text_server_tests {
415416
let file_id = test_file.id;
416417

417418
let add_command = WebCommand::AddTextFile(test_file);
418-
let should_continue = server.handle_command(Box::new(add_command));
419-
assert!(!should_continue);
419+
let should_not_continue = server.handle_command(Box::new(add_command));
420+
assert!(should_not_continue);
420421
assert!(server.get_file_by_id(file_id).is_some());
421422

422423
let remove_command = WebCommand::RemoveTextFile(file_id);
423-
let should_continue = server.handle_command(Box::new(remove_command));
424-
assert!(!should_continue);
424+
let should_not_continue = server.handle_command(Box::new(remove_command));
425+
assert!(should_not_continue);
425426
assert!(server.get_file_by_id(file_id).is_none());
426427
}
427-
}
428+
}

0 commit comments

Comments
 (0)