Skip to content

Commit

Permalink
Fixed #10 by adding new error enum
Browse files Browse the repository at this point in the history
  • Loading branch information
solanav committed Jan 29, 2020
1 parent 1fb53bd commit e96df6c
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fs::File;
use std::io::{BufWriter, BufReader};
use crate::types::node::Node;
use crate::node::Node;

pub fn save(path: &str, node_list: &Vec<Node>) {
let file = match File::create(&path) {
Expand Down
9 changes: 5 additions & 4 deletions src/types/bucket_list.rs → src/bucket/bucket_list.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::types::bucket::Bucket;
use crate::types::id::Id;
use crate::types::node::Node;
use crate::bucket::Bucket;
use crate::id::Id;
use crate::node::Node;
use std::fmt;
use serde::{Deserialize, Serialize};
use crate::bucket::error::BucketError;

#[derive(Serialize, Deserialize)]
pub struct BucketList {
Expand Down Expand Up @@ -56,7 +57,7 @@ impl BucketList {
panic!("Bucket list is not well built");
}

pub fn add_node(&mut self, node: &Node) -> Result<(), &'static str> {
pub fn add_node(&mut self, node: &Node) -> Result<(), BucketError> {
let i = self.find_bucket(&node.id());
self.buckets[i].add_node(node)
}
Expand Down
26 changes: 26 additions & 0 deletions src/bucket/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BucketError {
BucketFull,
IncorrectBucket,
RepeatedLNode,
LNodeNotFound,
NodeNotFound,
IndexError,
}

impl std::error::Error for BucketError {}

impl fmt::Display for BucketError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
BucketError::BucketFull => write!(f, "This bucket is full."),
BucketError::IncorrectBucket => write!(f, "The node does not fit."),
BucketError::RepeatedLNode => write!(f, "There already is a local node."),
BucketError::LNodeNotFound => write!(f, "Could not find the local node."),
BucketError::NodeNotFound => write!(f, "Could not find the node."),
BucketError::IndexError => write!(f, "Index is out of bounds."),
}
}
}
28 changes: 16 additions & 12 deletions src/types/bucket.rs → src/bucket/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use crate::types::id::Id;
use crate::types::node::Node;
pub mod bucket_list;
pub mod error;

use crate::id::Id;
use crate::node::Node;
use std::fmt;
use serde::{Deserialize, Serialize};
use crate::bucket::error::BucketError;

#[derive(Serialize, Deserialize)]
pub struct Bucket {
Expand All @@ -21,17 +25,17 @@ impl Bucket {
}
}

pub fn add_node(&mut self, node: &Node) -> Result<(), &'static str> {
pub fn add_node(&mut self, node: &Node) -> Result<(), BucketError> {
if self.node_list.len() >= self.max_nodes {
return Err("This bucket is already full");
return Err(BucketError::BucketFull);
}

if self.start_id > node.id() || node.id() > self.end_id {
return Err("This bucket should not contain that node");
return Err(BucketError::IncorrectBucket);
}

if self.local().is_ok() && node.is_local() {
return Err("There already is a local node inside the bucket")
return Err(BucketError::RepeatedLNode);
}

self.node_list.push(*node);
Expand All @@ -44,14 +48,14 @@ impl Bucket {
}
}

pub fn local(&self) -> Result<Node, &'static str> {
pub fn local(&self) -> Result<Node, BucketError> {
for node in self.node_list.iter() {
if node.is_local() {
return Ok(*node);
}
}

Err("Local node not found in this bucket")
Err(BucketError::LNodeNotFound)
}

pub fn divide(&mut self) -> Option<Self> {
Expand Down Expand Up @@ -82,22 +86,22 @@ impl Bucket {
Some(new_bucket)
}

pub fn get(&self, i: usize) -> Result<Node, &'static str> {
pub fn get(&self, i: usize) -> Result<Node, BucketError> {
if i >= self.node_list.len() {
return Err("Index out of range");
return Err(BucketError::IndexError);
}

Ok(self.node_list[i])
}

pub fn get_by_id(&self, id: &Id) -> Result<Node, &'static str> {
pub fn get_by_id(&self, id: &Id) -> Result<Node, BucketError> {
for node in self.node_list.iter() {
if node.id() == *id {
return Ok(*node);
}
}

Err("Node not found on bucket")
Err(BucketError::NodeNotFound)
}

pub fn fits(&self, id: &Id) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/client/active.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::net::TcpStream;
use crate::types::id::{Id, ID_BYTES};
use crate::types::packet::{Packet, *};
use crate::id::{Id, ID_BYTES};
use crate::packet::{Packet, *};
use std::io::Write;

fn send_packet(stream: &mut TcpStream, packet: Packet) {
Expand Down
File renamed without changes.
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
pub mod types; // Data structures shared by other modules
pub mod bucket; // Data structures shared by other modules
pub mod server; // Server that listens for packets
pub mod client; // Functions used to send messages to other peers
pub mod bootstrap; // Functions used to get into the network when restarted
pub mod id; // Id of a node in the network
pub mod node; // Structure representing a node in the network
pub mod packet; // Packet structure of the protocol
2 changes: 1 addition & 1 deletion src/types/node.rs → src/node.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::id::Id;
use crate::id::Id;
use std::fmt;
use std::net::SocketAddr;
use serde::{Deserialize, Serialize};
Expand Down
7 changes: 1 addition & 6 deletions src/node_test_0.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
use samurai::server::passive::Server;
use std::time::Duration;
use std::thread;
use samurai::client::active::{send_node, ping};
use samurai::server::threadpool::ThreadPool;
use std::net::{TcpStream, Ipv4Addr, IpAddr, SocketAddr};

fn main() {
let server = Server::new();
let _server = Server::new();
}
File renamed without changes.
6 changes: 3 additions & 3 deletions src/server/handler.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::types::packet::{self, Packet, DATA_SIZE, TOTAL_SIZE};
use crate::packet::{self, Packet, DATA_SIZE, TOTAL_SIZE};
use std::net::TcpStream;
use crate::types::id::{Id, ID_BYTES};
use crate::types::bucket_list::BucketList;
use crate::id::{Id, ID_BYTES};
use crate::bucket::bucket_list::BucketList;
use std::sync::{Arc, Mutex};
use std::io::Read;
use crate::client::active;
Expand Down
3 changes: 1 addition & 2 deletions src/server/passive.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::server::handler::Handler;
use crate::types::bucket_list::BucketList;
use crate::bucket::bucket_list::BucketList;
use crate::bootstrap::file::{save, load};

use std::net::{TcpListener, TcpStream};
Expand All @@ -12,7 +12,6 @@ use crate::server::threadpool::ThreadPool;

const MAX_BUCKETS: usize = 10;
const BUCKET_SIZE: usize = 10;
const BTST_FILE: &str = "peers.json";

pub struct Server {
thread_pool: Arc<Mutex<ThreadPool>>, // Worker pool for handlers
Expand Down
5 changes: 0 additions & 5 deletions src/types/mod.rs

This file was deleted.

37 changes: 0 additions & 37 deletions src/types/request_list.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/id_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use samurai::types::id::Id;
use samurai::id::Id;

#[test]
fn test_add() {
Expand Down
8 changes: 4 additions & 4 deletions tests/kbucket_tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use samurai::types::bucket::Bucket;
use samurai::types::bucket_list::BucketList;
use samurai::types::node::Node;
use samurai::types::id::Id;
use samurai::bucket::Bucket;
use samurai::bucket_list::BucketList;
use samurai::node::Node;
use samurai::id::Id;
use std::net::{Ipv4Addr, SocketAddr, IpAddr};

static MAX_BUCKETS: usize = 10;
Expand Down

0 comments on commit e96df6c

Please sign in to comment.