diff --git a/src/bootstrap/file.rs b/src/bootstrap/file.rs index 96762c1..f2b4932 100644 --- a/src/bootstrap/file.rs +++ b/src/bootstrap/file.rs @@ -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) { let file = match File::create(&path) { diff --git a/src/types/bucket_list.rs b/src/bucket/bucket_list.rs similarity index 92% rename from src/types/bucket_list.rs rename to src/bucket/bucket_list.rs index a6e9701..0524043 100644 --- a/src/types/bucket_list.rs +++ b/src/bucket/bucket_list.rs @@ -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 { @@ -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) } diff --git a/src/bucket/error.rs b/src/bucket/error.rs new file mode 100644 index 0000000..41ab7a1 --- /dev/null +++ b/src/bucket/error.rs @@ -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."), + } + } +} \ No newline at end of file diff --git a/src/types/bucket.rs b/src/bucket/mod.rs similarity index 79% rename from src/types/bucket.rs rename to src/bucket/mod.rs index 85cb28f..dee21c6 100644 --- a/src/types/bucket.rs +++ b/src/bucket/mod.rs @@ -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 { @@ -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); @@ -44,14 +48,14 @@ impl Bucket { } } - pub fn local(&self) -> Result { + pub fn local(&self) -> Result { 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 { @@ -82,22 +86,22 @@ impl Bucket { Some(new_bucket) } - pub fn get(&self, i: usize) -> Result { + pub fn get(&self, i: usize) -> Result { 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 { + pub fn get_by_id(&self, id: &Id) -> Result { 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 { diff --git a/src/client/active.rs b/src/client/active.rs index 58f580d..06dc420 100644 --- a/src/client/active.rs +++ b/src/client/active.rs @@ -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) { diff --git a/src/types/id.rs b/src/id.rs similarity index 100% rename from src/types/id.rs rename to src/id.rs diff --git a/src/lib.rs b/src/lib.rs index 1176793..94b8f2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 \ No newline at end of file diff --git a/src/types/node.rs b/src/node.rs similarity index 95% rename from src/types/node.rs rename to src/node.rs index b734392..94cb15d 100644 --- a/src/types/node.rs +++ b/src/node.rs @@ -1,4 +1,4 @@ -use crate::types::id::Id; +use crate::id::Id; use std::fmt; use std::net::SocketAddr; use serde::{Deserialize, Serialize}; diff --git a/src/node_test_0.rs b/src/node_test_0.rs index e07edd4..96567a4 100644 --- a/src/node_test_0.rs +++ b/src/node_test_0.rs @@ -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(); } diff --git a/src/types/packet.rs b/src/packet.rs similarity index 100% rename from src/types/packet.rs rename to src/packet.rs diff --git a/src/server/handler.rs b/src/server/handler.rs index 6fb3602..a3d677a 100644 --- a/src/server/handler.rs +++ b/src/server/handler.rs @@ -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; diff --git a/src/server/passive.rs b/src/server/passive.rs index e421826..28a08ac 100644 --- a/src/server/passive.rs +++ b/src/server/passive.rs @@ -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}; @@ -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>, // Worker pool for handlers diff --git a/src/types/mod.rs b/src/types/mod.rs deleted file mode 100644 index 5d20c15..0000000 --- a/src/types/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub mod id; -pub mod node; -pub mod bucket; -pub mod bucket_list; -pub mod packet; \ No newline at end of file diff --git a/src/types/request_list.rs b/src/types/request_list.rs deleted file mode 100644 index c281a95..0000000 --- a/src/types/request_list.rs +++ /dev/null @@ -1,37 +0,0 @@ -use crate::network::packet::Packet; - -pub struct RequestList { - requests: Vec, -} - -impl RequestList { - pub fn new() -> Self { - RequestList { - requests: Vec::new(), - } - } - - pub fn add(&mut self, packet: Packet) { - self.requests.push(packet); - } - - pub fn rm(&mut self, i: usize) { - if i > self.requests.len() { - return; - } - - self.requests.remove(i); - } - - pub fn find_cookie(&self, cookie: u32) -> Option { - let mut i = 0; - for p in self.requests.iter() { - if p.cookie() == cookie { - return Some(i); - } - i += 1; - } - - None - } -} \ No newline at end of file diff --git a/tests/id_tests.rs b/tests/id_tests.rs index 4c9ff9d..6fed9b7 100644 --- a/tests/id_tests.rs +++ b/tests/id_tests.rs @@ -1,4 +1,4 @@ -use samurai::types::id::Id; +use samurai::id::Id; #[test] fn test_add() { diff --git a/tests/kbucket_tests.rs b/tests/kbucket_tests.rs index 710e5b3..769f6b3 100644 --- a/tests/kbucket_tests.rs +++ b/tests/kbucket_tests.rs @@ -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;