Skip to content

Commit

Permalink
Rename identifier to identify in Identify trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Benestar committed Aug 10, 2018
1 parent dcd4f21 commit 487a665
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/handler/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ApiHandler {
for i in 0..u8::MAX {
let key = Key { raw_key: dht_get.key, replication_index: i };

let peer_addr = self.find_peer(key.identifier())?;
let peer_addr = self.find_peer(key.identify())?;

if let Some(value) = self.procedures.get_value(peer_addr, key)? {
let dht_success = DhtSuccess { key: dht_get.key, value };
Expand All @@ -68,7 +68,7 @@ impl ApiHandler {
for i in 0..dht_put.replication + 1 {
let key = Key { raw_key: dht_put.key, replication_index: i };

let peer_addr = self.find_peer(key.identifier())?;
let peer_addr = self.find_peer(key.identify())?;

self.procedures.put_value(peer_addr, key, dht_put.ttl, dht_put.value.clone())?;
}
Expand Down
6 changes: 3 additions & 3 deletions src/handler/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl P2PHandler {
let old_predecessor_addr = *routing.predecessor;

// 1. check if the predecessor is closer than the previous predecessor
if routing.responsible_for(predecessor_addr.identifier()) {
if routing.responsible_for(predecessor_addr.identify()) {
// 2. update the predecessor if necessary
routing.set_predecessor(predecessor_addr);

Expand Down Expand Up @@ -103,7 +103,7 @@ impl P2PHandler {
info!("Received STORAGE GET request for key {}", key);

// 1. check if given key falls into range
if self.responsible_for(key.identifier()) {
if self.responsible_for(key.identify()) {
// 2. find value for given key
let value_opt = self.get_from_storage(key);

Expand Down Expand Up @@ -133,7 +133,7 @@ impl P2PHandler {
info!("Received STORAGE PUT request for key {}", key);

// 1. check if given key falls into range
if self.responsible_for(key.identifier()) {
if self.responsible_for(key.identify()) {
// 2. save value for given key
let msg = if self.put_to_storage(key, storage_put.value) {
info!("Stored value for key {} and replying with STORAGE PUT SUCCESS", key);
Expand Down
23 changes: 15 additions & 8 deletions src/routing/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,43 +164,50 @@ impl fmt::Debug for Identifier {
/// Trait to obtain an identifier from a data structure
pub trait Identify {
/// Generates an identifier for this object.
fn identifier(&self) -> Identifier;
fn identify(&self) -> Identifier;
}

/// Obtains an identifier by hashing the four octets of the ip address.
impl Identify for SocketAddrV4 {
fn identifier(&self) -> Identifier {
fn identify(&self) -> Identifier {
Identifier::generate(self.ip().octets().as_ref())
}
}

/// Obtains an identifier by hashing the first eight octets of the ip address.
impl Identify for SocketAddrV6 {
fn identifier(&self) -> Identifier {
fn identify(&self) -> Identifier {
Identifier::generate(self.ip().octets()[..8].as_ref())
}
}

/// Get the identifier for a V4 or V6 socket address.
impl Identify for SocketAddr {
fn identifier(&self) -> Identifier {
fn identify(&self) -> Identifier {
match self {
SocketAddr::V4(v4) => v4.identifier(),
SocketAddr::V6(v6) => v6.identifier()
SocketAddr::V4(v4) => v4.identify(),
SocketAddr::V6(v6) => v6.identify()
}
}
}

/// Hashes the raw key and its replication index.
impl Identify for Key {
fn identifier(&self) -> Identifier {
fn identify(&self) -> Identifier {
let mut bytes = [0; 33];
bytes[..32].copy_from_slice(&self.raw_key);
bytes[32] = self.replication_index;
Identifier::generate(&bytes)
}
}

/// Returns the identifier itself.
impl Identify for Identifier {
fn identify(&self) -> Identifier {
*self
}
}

/// Container for a value and its identifier
#[derive(Clone, Copy, Debug)]
pub struct IdentifierValue<T> {
Expand All @@ -212,7 +219,7 @@ impl<T: Identify> IdentifierValue<T> {
/// Obtains the identifier for the given `value` and stores it along with
/// the value in an `IdentifierValue` object.
pub fn new(value: T) -> Self {
let identifier = value.identifier();
let identifier = value.identify();

Self { value, identifier }
}
Expand Down
4 changes: 2 additions & 2 deletions src/stabilization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Bootstrap {
/// scucessor peer. Finally, we initialize the finger table with our own address.
pub fn bootstrap(&self, timeout: u64) -> ::Result<Routing<SocketAddr>> {
let procedures = Procedures::new(timeout);
let current_id = self.current_addr.identifier();
let current_id = self.current_addr.identify();

let successor = procedures.find_peer(current_id, self.boot_addr)?;
let predecessor = procedures.notify_predecessor(self.current_addr, self.boot_addr)?;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl Stabilization {
let current_id = current.identifier();
let successor_id = successor.identifier();

if new_successor.identifier().is_between(&current_id, &successor_id) {
if new_successor.identify().is_between(&current_id, &successor_id) {
info!("Updating successor to address {}", new_successor);

let mut routing = self.routing.lock().unwrap();
Expand Down

0 comments on commit 487a665

Please sign in to comment.