Skip to content

Commit

Permalink
Make Identifier generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Benestar committed May 24, 2019
1 parent 3dffd36 commit 08453cc
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 89 deletions.
6 changes: 3 additions & 3 deletions src/handler/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::message::api::*;
use crate::message::Message;
use crate::network::{Connection, ServerHandler};
use crate::procedures::Procedures;
use crate::routing::identifier::{Identifier, Identify};
use crate::routing::identifier::{IdentifierU256, Identify};
use crate::routing::Routing;
use crate::storage::Key;
use std::error::Error;
Expand Down Expand Up @@ -31,13 +31,13 @@ impl ApiHandler {
}
}

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) -> crate::Result<SocketAddr> {
fn find_peer(&self, identifier: IdentifierU256) -> crate::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 crate::error::MessageError;
use crate::message::p2p::*;
use crate::message::Message;
use crate::network::{Connection, ServerHandler};
use crate::routing::identifier::{Identifier, Identify};
use crate::routing::identifier::{IdentifierU256, Identify};
use crate::routing::Routing;
use crate::storage::Key;
use std::collections::HashMap;
Expand Down Expand Up @@ -30,13 +30,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 super::MessagePayload;
use crate::routing::identifier::Identifier;
use crate::routing::identifier::IdentifierU256;
use byteorder::{NetworkEndian, ReadBytesExt, WriteBytesExt};
use std::io;
use std::io::prelude::*;
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 @@ -224,7 +224,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 @@ -240,7 +240,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 @@ -445,7 +445,7 @@ mod tests {
];

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

test_message_payload(&buf, msg);
Expand All @@ -465,7 +465,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 @@ -486,7 +486,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 crate::error::MessageError;
use crate::message::p2p::{PeerFind, PredecessorNotify, StorageGet, StoragePut};
use crate::message::Message;
use crate::network::Connection;
use crate::routing::identifier::Identifier;
use crate::routing::identifier::IdentifierU256;
use crate::storage::Key;
use std::net::SocketAddr;

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

0 comments on commit 08453cc

Please sign in to comment.