net.tomp2p.peers
Class PeerMap

java.lang.Object
  extended by net.tomp2p.peers.PeerMap

public class PeerMap
extends Object

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
PeerMap(Number160 self, int bagSize, int cacheTimeoutMillis, int maxNrBeforeExclude, int[] waitingTimeBetweenNodeMaintenenceSeconds, int cachSize, boolean acceptFirstClassOnly)
          Deprecated. 
PeerMap(Number160 self, int bagSize, int cacheTimeoutMillis, int maxNrBeforeExclude, int[] waitingTimeBetweenNodeMaintenenceSeconds, int cachSize, MapAcceptHandler mapHandler)
          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.
 List<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.
static int isKadCloser(Number160 id, PeerAddress rn, PeerAddress rn2)
          Returns -1 if the first remote node is closer to the key, if the secondBITS 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()
           
 boolean updateExistingPeerAddress(PeerAddress peerAddress)
          Checks if a peer already existis in this map and if it does, it will update the entry becaues the peer address (e.g.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

PeerMap

@Deprecated
public PeerMap(Number160 self,
                          int bagSize,
                          int cacheTimeoutMillis,
                          int maxNrBeforeExclude,
                          int[] waitingTimeBetweenNodeMaintenenceSeconds,
                          int cachSize,
                          boolean acceptFirstClassOnly)
Deprecated. 


PeerMap

public PeerMap(Number160 self,
               int bagSize,
               int cacheTimeoutMillis,
               int maxNrBeforeExclude,
               int[] waitingTimeBetweenNodeMaintenenceSeconds,
               int cachSize,
               MapAcceptHandler mapHandler)
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)

removePeerMapChangeListener

public void removePeerMapChangeListener(PeerMapChangeListener peerMapChangeListener)

addPeerOfflineListener

public void addPeerOfflineListener(PeerStatusListener peerListener)

removePeerOfflineListener

public void removePeerOfflineListener(PeerStatusListener peerListener)

getStatistics

public Statistics getStatistics()

size

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

Returns:
the total number of peers

self

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

Returns:
The id of this node

peerFound

public boolean peerFound(PeerAddress remotePeer,
                         PeerAddress referrer)
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

Parameters:
node - The node that should be added
firstHand - If we had direct contact and we know for sure that this node is online, we set firsthand to true. Information from 3rd party peers are always second hand and treated as such
Returns:
True if the neighbor could be added or updated, otherwise false.

peerOffline

public boolean peerOffline(PeerAddress remotePeer,
                           boolean force)
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.

Parameters:
node - 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()

isPeerRemovedTemporarly

public boolean isPeerRemovedTemporarly(PeerAddress remotePeer)
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

Parameters:
node - The node to check
Returns:
True if the peer has been removed recently

updateExistingPeerAddress

public boolean updateExistingPeerAddress(PeerAddress peerAddress)
Checks if a peer already existis in this map and if it does, it will update the entry becaues the peer address (e.g. port) may have changed.

Parameters:
peerAddress - The address of the peer that may have been changed.
Returns:
True if we have updated the peer, false otherwise

contains

public boolean contains(PeerAddress peerAddress)

closePeers

public SortedSet<PeerAddress> closePeers(Number160 id,
                                         int atLeast)
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.

Parameters:
key - The key that should be close to the keys in the map
number - 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)
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

Parameters:
key - The key to search for
nodeAddress1 - The remote node on the routing path to node close to key
nodeAddress2 - 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)
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

Parameters:
key - The key to search for
key1 - The first key
key2 - The second key
Returns:
-1 if key1 is closer to key, otherwise 1. 0 is returned if both are equal.

isKadCloser

public static int isKadCloser(Number160 id,
                              PeerAddress rn,
                              PeerAddress rn2)
Returns -1 if the first remote node is closer to the key, if the secondBITS is closer, then 1 is returned. If both are equal, 0 is returned

Parameters:
id - The id as a distance reference
rn - The peer to test if closer to the id
rn2 - The other peer to test if closer to the id
Returns:
-1 if first peer is closer, 1 otherwise, 0 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)
Creates a comparator that orders to peers according to their distance to the specified id.

Parameters:
id - The id that defines the metric
Returns:
The comparator to be used with the collection framework

createPeerComparator

public Comparator<PeerAddress> createPeerComparator()

getAll

public List<PeerAddress> getAll()
Return all addresses from the neighbor list. The collection is partially sorted.

Returns:
All neighbors

addAddressFilter

public void addAddressFilter(InetAddress address)


Copyright © 2013. All Rights Reserved.