net.tomp2p.peers
Class PeerMapKadImpl

java.lang.Object
  extended by net.tomp2p.peers.PeerMapKadImpl
All Implemented Interfaces:
PeerMap

public class PeerMapKadImpl
extends Object
implements PeerMap

This routing implementation uses is based on Kademlia. However, many changes have been applied to make it faster and more flexible. This class is partially thread-safe.

Author:
Thomas Bocek

Constructor Summary
PeerMapKadImpl(Number160 self, int bagSize, int cacheTimeoutMillis, int maxNrBeforeExclude, int[] waitingTimeBetweenNodeMaintenenceSeconds, int cachSize, boolean isBehindFirewall)
          Creates the bag for the peers.
 
Method Summary
 void addAddressFilter(InetAddress address)
           
 void addPeerMapChangeListener(PeerMapChangeListener peerMapChangeListener)
           
 void addPeerOfflineListener(PeerStatusListener peerListener)
           
 SortedSet<PeerAddress> closePeers(Number160 id, int atLeast)
          Returns close peer from the set to a given key.
 boolean contains(PeerAddress peerAddress)
           
static Comparator<PeerAddress> createComparator(Number160 id)
           
 Comparator<PeerAddress> createPeerComparator()
           
 Comparator<PeerAddress> createPeerComparator(Number160 id)
          Creates a comparator that orders to peers according to their distance to the specified id.
 Collection<PeerAddress> getAll()
          Return all addresses from the neighbor list.
 Statistics getStatistics()
           
 int isCloser(Number160 id, Number160 rn, Number160 rn2)
          Returns -1 if the first key is closer to the key, if the second is closer, then 1 is returned.
 int isCloser(Number160 id, PeerAddress rn, PeerAddress rn2)
          Returns -1 if the first remote node is closer to the key, if the second is closer, then 1 is returned.
 boolean isPeerRemovedTemporarly(PeerAddress remotePeer)
          Checks if this peer has been removed.
 boolean peerFound(PeerAddress remotePeer, PeerAddress referrer)
          Adds a neighbor to the neighbor list.
 boolean peerOffline(PeerAddress remotePeer, boolean force)
          Remove a peer from the list.
 Collection<PeerAddress> peersForMaintenance()
           
 void removePeerMapChangeListener(PeerMapChangeListener peerMapChangeListener)
           
 void removePeerOfflineListener(PeerStatusListener peerListener)
           
 Number160 self()
          Each node that has a bag has an ID itself to define what is close.
 int size()
          The peerCount keeps track of the total number of peer in the system.
 String toString()
           
 void updateExistingPeerAddress(PeerAddress peerAddress)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PeerMapKadImpl

public PeerMapKadImpl(Number160 self,
                      int bagSize,
                      int cacheTimeoutMillis,
                      int maxNrBeforeExclude,
                      int[] waitingTimeBetweenNodeMaintenenceSeconds,
                      int cachSize,
                      boolean isBehindFirewall)
Creates the bag for the peers. This peer knows a lot about close peers and the further away the peers are, the less known they are. Distance is measured with XOR of the peer ID. The distance of peer with ID 0x12 and peer with Id 0x28 is 0x3a.

Parameters:
self - The peer ID of this peer
configuration - Configuration settings for this map
Method Detail

addPeerMapChangeListener

public void addPeerMapChangeListener(PeerMapChangeListener peerMapChangeListener)
Specified by:
addPeerMapChangeListener in interface PeerMap

removePeerMapChangeListener

public void removePeerMapChangeListener(PeerMapChangeListener peerMapChangeListener)
Specified by:
removePeerMapChangeListener in interface PeerMap

addPeerOfflineListener

public void addPeerOfflineListener(PeerStatusListener peerListener)
Specified by:
addPeerOfflineListener in interface PeerMap

removePeerOfflineListener

public void removePeerOfflineListener(PeerStatusListener peerListener)
Specified by:
removePeerOfflineListener in interface PeerMap

getStatistics

public Statistics getStatistics()
Specified by:
getStatistics in interface PeerMap

size

public int size()
The peerCount keeps track of the total number of peer in the system.

Specified by:
size in interface PeerMap
Returns:
the total number of peers

self

public Number160 self()
Description copied from interface: PeerMap
Each node that has a bag has an ID itself to define what is close. This method returns this ID.

Specified by:
self in interface PeerMap
Returns:
The id of this node

peerFound

public boolean peerFound(PeerAddress remotePeer,
                         PeerAddress referrer)
Description copied from interface: PeerMap
Adds a neighbor to the neighbor list. If the bag is full, the id zero or the same as our id, the neighbor is not added. This method is tread-safe

Specified by:
peerFound in interface PeerMap
Parameters:
remotePeer - The node that should be added
Returns:
True if the neighbor could be added, otherwise false.

peerOffline

public boolean peerOffline(PeerAddress remotePeer,
                           boolean force)
Description copied from interface: PeerMap
Remove a peer from the list. In order to not reappear, the node is put for a certain time in a cache list to keep the node removed. This method is thread-safe.

Specified by:
peerOffline in interface PeerMap
Parameters:
remotePeer - The node that should be removed
Returns:
True if the neighbor was removed and added to a cache list. False if peer has not been removed or is already in the peer removed temporarly list.

peersForMaintenance

public Collection<PeerAddress> peersForMaintenance()
Specified by:
peersForMaintenance in interface PeerMap

isPeerRemovedTemporarly

public boolean isPeerRemovedTemporarly(PeerAddress remotePeer)
Description copied from interface: PeerMap
Checks if this peer has been removed. A peer that has been removed will be stored in a cache list for a certain time. This method is tread-safe

Specified by:
isPeerRemovedTemporarly in interface PeerMap
Parameters:
remotePeer - The node to check
Returns:
True if the peer has been removed recently

updateExistingPeerAddress

public void updateExistingPeerAddress(PeerAddress peerAddress)

contains

public boolean contains(PeerAddress peerAddress)
Specified by:
contains in interface PeerMap

closePeers

public SortedSet<PeerAddress> closePeers(Number160 id,
                                         int atLeast)
Description copied from interface: PeerMap
Returns close peer from the set to a given key. This method is tread-safe. You can use the returned set as its a copy of the actual PeerMap and changes in the return set do not affect PeerMap.

Specified by:
closePeers in interface PeerMap
Parameters:
id - The key that should be close to the keys in the map
atLeast - The number we want to find at least
Returns:
A navigable set with close peers first in this set.

isCloser

public int isCloser(Number160 id,
                    PeerAddress rn,
                    PeerAddress rn2)
Description copied from interface: PeerMap
Returns -1 if the first remote node is closer to the key, if the second is closer, then 1 is returned. If both are equal, 0 is returned

Specified by:
isCloser in interface PeerMap
Parameters:
id - The key to search for
rn - The remote node on the routing path to node close to key
rn2 - An other remote node on the routing path to node close to key
Returns:
-1 if nodeAddress1 is closer to the key than nodeAddress2, otherwise 1. 0 is returned if both are equal.

isCloser

public int isCloser(Number160 id,
                    Number160 rn,
                    Number160 rn2)
Description copied from interface: PeerMap
Returns -1 if the first key is closer to the key, if the second is closer, then 1 is returned. If both are equal, 0 is returned

Specified by:
isCloser in interface PeerMap
Parameters:
id - The key to search for
rn - The first key
rn2 - The second key
Returns:
-1 if key1 is closer to key, otherwise 1. 0 is returned if both are equal.

toString

public String toString()
Overrides:
toString in class Object

createComparator

public static Comparator<PeerAddress> createComparator(Number160 id)

createPeerComparator

public Comparator<PeerAddress> createPeerComparator(Number160 id)
Description copied from interface: PeerMap
Creates a comparator that orders to peers according to their distance to the specified id.

Specified by:
createPeerComparator in interface PeerMap
Parameters:
id - The id that defines the metric
Returns:
The comparator to be used with the collection framework

createPeerComparator

public Comparator<PeerAddress> createPeerComparator()
Specified by:
createPeerComparator in interface PeerMap

getAll

public Collection<PeerAddress> getAll()
Description copied from interface: PeerMap
Return all addresses from the neighbor list. The collection is partially sorted.

Specified by:
getAll in interface PeerMap
Returns:
All neighbors

addAddressFilter

public void addAddressFilter(InetAddress address)
Specified by:
addAddressFilter in interface PeerMap


Copyright © 2012. All Rights Reserved.