Skip to content

Commit

Permalink
Make Identifier generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Benestar committed Aug 10, 2018
1 parent 487a665 commit b4cf274
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 91 deletions.
6 changes: 3 additions & 3 deletions src/handler/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use error::MessageError;
use message::api::*;
use message::Message;
use network::{Connection, ServerHandler};
use routing::identifier::{Identify, Identifier};
use routing::identifier::{Identify, IdentifierU256};
use routing::Routing;
use procedures::Procedures;
use std::error::Error;
Expand All @@ -29,13 +29,13 @@ impl ApiHandler {
Self { routing, procedures }
}

fn closest_peer(&self, identifier: Identifier) -> SocketAddr {
fn closest_peer(&self, identifier: IdentifierU256) -> SocketAddr {
let routing = self.routing.lock().unwrap();

**routing.closest_peer(identifier)
}

fn find_peer(&self, identifier: Identifier) -> ::Result<SocketAddr> {
fn find_peer(&self, identifier: IdentifierU256) -> ::Result<SocketAddr> {
let closest_peer = self.closest_peer(identifier);

self.procedures.find_peer(identifier, closest_peer)
Expand Down
6 changes: 3 additions & 3 deletions src/handler/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use error::MessageError;
use message::Message;
use message::p2p::*;
use network::{Connection, ServerHandler};
use routing::identifier::{Identifier, Identify};
use routing::identifier::{IdentifierU256, Identify};
use routing::Routing;
use std::collections::HashMap;
use std::error::Error;
Expand Down Expand Up @@ -31,13 +31,13 @@ impl P2PHandler {
Self { routing, storage }
}

fn responsible_for(&self, identifier: Identifier) -> bool {
fn responsible_for(&self, identifier: IdentifierU256) -> bool {
let routing = self.routing.lock().unwrap();

routing.responsible_for(identifier)
}

fn closest_peer(&self, identifier: Identifier) -> SocketAddr {
fn closest_peer(&self, identifier: IdentifierU256) -> SocketAddr {
let routing = self.routing.lock().unwrap();

**routing.closest_peer(identifier)
Expand Down
16 changes: 8 additions & 8 deletions src/message/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
use routing::identifier::Identifier;
use routing::identifier::IdentifierU256;
use std::io;
use std::io::prelude::*;
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
Expand Down Expand Up @@ -71,7 +71,7 @@ pub struct StorageFailure {
/// This can be implemented using finger tables.
#[derive(Debug, PartialEq)]
pub struct PeerFind {
pub identifier: Identifier
pub identifier: IdentifierU256
}

/// If, after a [`PeerFind`] operation, a node has been found which is closest
Expand All @@ -82,7 +82,7 @@ pub struct PeerFind {
/// [`PeerFind`]: struct.PeerFind.html
#[derive(Debug, PartialEq)]
pub struct PeerFound {
pub identifier: Identifier,
pub identifier: IdentifierU256,
pub socket_addr: SocketAddr
}

Expand Down Expand Up @@ -216,7 +216,7 @@ impl MessagePayload for PeerFind {
fn parse(reader: &mut Read) -> io::Result<Self> {
let mut id_arr = [0; 32];
reader.read_exact(&mut id_arr)?;
let identifier = Identifier::new(&id_arr);
let identifier = IdentifierU256::new(&id_arr);

Ok(PeerFind{ identifier })
}
Expand All @@ -232,7 +232,7 @@ impl MessagePayload for PeerFound {
fn parse(reader: &mut Read) -> io::Result<Self> {
let mut id_arr = [0; 32];
reader.read_exact(&mut id_arr)?;
let identifier = Identifier::new(&id_arr);
let identifier = IdentifierU256::new(&id_arr);

let mut ip_arr = [0; 16];
reader.read_exact(&mut ip_arr)?;
Expand Down Expand Up @@ -432,7 +432,7 @@ mod tests {
];

let msg = PeerFind {
identifier: Identifier::new(&[5; 32]),
identifier: IdentifierU256::new(&[5; 32]),
};

test_message_payload(&buf, msg);
Expand All @@ -451,7 +451,7 @@ mod tests {
];

let msg = PeerFound {
identifier: Identifier::new(&[5; 32]),
identifier: IdentifierU256::new(&[5; 32]),
socket_addr: "127.0.0.1:8080".parse().unwrap(),
};

Expand All @@ -471,7 +471,7 @@ mod tests {
];

let msg = PeerFound {
identifier: Identifier::new(&[5; 32]),
identifier: IdentifierU256::new(&[5; 32]),
socket_addr: "[2001:db8:85a3::8a23:370:7334]:8080".parse().unwrap(),
};

Expand Down
4 changes: 2 additions & 2 deletions src/procedures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use error::MessageError;
use message::Message;
use message::p2p::{PeerFind, PredecessorNotify, StorageGet, StoragePut};
use network::Connection;
use routing::identifier::Identifier;
use routing::identifier::IdentifierU256;
use std::net::SocketAddr;
use storage::Key;

Expand All @@ -21,7 +21,7 @@ impl Procedures {
///
/// This iteratively sends PEER FIND messages to successive peers,
/// beginning with `peer_addr` which could be taken from a finger table.
pub fn find_peer(&self, identifier: Identifier, mut peer_addr: SocketAddr)
pub fn find_peer(&self, identifier: IdentifierU256, mut peer_addr: SocketAddr)
-> ::Result<SocketAddr>
{
debug!("Finding peer for identifier {}", identifier);
Expand Down
Loading

0 comments on commit b4cf274

Please sign in to comment.