Skip to content

Commit 528fc54

Browse files
committed
feat: added get_servers(), fix: return type of handle_command
1 parent 93089fd commit 528fc54

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

src/network.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ impl Network {
148148
}
149149
}
150150

151+
152+
/// BFS to find path to destination
151153
#[must_use]
152154
pub(crate) fn find_path(&self, destination: NodeId) -> Option<Vec<NodeId>> {
153155
let start = self.nodes[0].id;
@@ -182,6 +184,24 @@ impl Network {
182184
}
183185
None
184186
}
187+
188+
#[must_use]
189+
pub(crate) fn get_servers(&self) -> Option<Vec<NodeId>> {
190+
let servers = self.nodes.iter().filter_map(|n| {
191+
if n.get_node_type() == NodeType::Server {
192+
Some(n.get_id())
193+
}
194+
else {
195+
None
196+
}
197+
}).collect::<Vec<_>>();
198+
199+
if servers.is_empty() {
200+
None
201+
}else {
202+
Some(servers)
203+
}
204+
}
185205
}
186206

187207
#[cfg(test)]
@@ -267,4 +287,4 @@ mod tests {
267287

268288
assert!(result.is_none());
269289
}
270-
}
290+
}

src/packet_processor.rs

Lines changed: 2 additions & 2 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>) -> Result<(), ()>;
14+
fn handle_command(&mut self, cmd: Box<dyn Any>) -> bool;
1515

1616
/// Handles a packet in a standard way
1717
/// # Errors
@@ -53,7 +53,7 @@ pub trait Processor {
5353
select_biased! {
5454
recv(self.controller_recv()) -> cmd => {
5555
if let Ok(cmd) = cmd {
56-
if self.handle_command(cmd).is_err() {
56+
if self.handle_command(cmd) {
5757
return
5858
}
5959
}

src/routing_handler.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,11 @@ impl RoutingHandler {
416416
self.try_send(packet)?;
417417
Ok(())
418418
}
419+
420+
#[must_use]
421+
pub fn get_servers(&self) -> Option<Vec<NodeId>> {
422+
self.network_view.get_servers()
423+
}
419424
}
420425

421426
#[cfg(test)]

src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub struct Message {
5050
}
5151

5252
impl Message {
53+
#[must_use]
5354
pub fn new(from: NodeId, to: NodeId, text: String) -> Self {
5455
Message { from, to, text }
5556
}

0 commit comments

Comments
 (0)