peerbase
Class Node

java.lang.Object
  extended by peerbase.Node
Direct Known Subclasses:
FileShareNode

public class Node
extends java.lang.Object

This is the primary class for the PeerBase peer-to-peer development system. It maintains this node's information (id, host, port), a list of known peers, list of message handlers, and a handler for routing data through the P2P network.

Author:
Nadeem Abdul Hamid

Constructor Summary
Node(int port)
          Initialize this node with no limit on the size of the peer list and set up to listen for incoming connections on the specified port.
Node(int maxPeers, PeerInfo info)
          Initialize this node with the given info and the specified limit on the size of the peer list.
 
Method Summary
 void addHandler(java.lang.String msgtype, HandlerInterface handler)
           
 boolean addPeer(PeerInfo pd)
           
 boolean addPeer(java.lang.String key, PeerInfo pd)
          Add new peer information to the peer list, indexed by the given key.
 void addRouter(RouterInterface router)
           
 java.util.List<PeerMessage> connectAndSend(PeerInfo pd, java.lang.String msgtype, java.lang.String msgdata, boolean waitreply)
          Connects to the specified peer and sends a message, optionally waiting and returning any replies.
 java.lang.String getHost()
           
 java.lang.String getId()
           
 int getMaxPeers()
           
 int getNumberOfPeers()
           
 PeerInfo getPeer(java.lang.String key)
           
 java.util.Set<java.lang.String> getPeerKeys()
           
 int getPort()
           
 void mainLoop()
          Starts the loop which is the primary operation of the Node.
 java.net.ServerSocket makeServerSocket(int port)
          Create a socket to listen for incoming connections.
 java.net.ServerSocket makeServerSocket(int port, int backlog)
          Create a socket to listen for incoming connections.
 boolean maxPeersReached()
           
 PeerInfo removePeer(java.lang.String key)
           
 java.util.List<PeerMessage> sendToPeer(java.lang.String peerid, java.lang.String msgtype, java.lang.String msgdata, boolean waitreply)
          Attempts to route and send a message to the specified peer, optionally waiting and returning any replies.
 void startStabilizer(StabilizerInterface st, int delay)
          Starts a "stabilizer" function running at the specified interval.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Node

public Node(int maxPeers,
            PeerInfo info)
Initialize this node with the given info and the specified limit on the size of the peer list.

Parameters:
maxPeers - the maximum size of the peer list (0 means 'no limit')
info - the id and host/port information for this node

Node

public Node(int port)
Initialize this node with no limit on the size of the peer list and set up to listen for incoming connections on the specified port.

Method Detail

makeServerSocket

public java.net.ServerSocket makeServerSocket(int port)
                                       throws java.io.IOException
Create a socket to listen for incoming connections.

Parameters:
port - the port number to listen on, or 0 to use any free port
Returns:
the server socket
Throws:
java.io.IOException - if error occurs

makeServerSocket

public java.net.ServerSocket makeServerSocket(int port,
                                              int backlog)
                                       throws java.io.IOException
Create a socket to listen for incoming connections.

Parameters:
port - the port number to listen on, or 0 to use any free port
backlog - he maximum length of the queue
Returns:
the server socket
Throws:
java.io.IOException - if error occurs

sendToPeer

public java.util.List<PeerMessage> sendToPeer(java.lang.String peerid,
                                              java.lang.String msgtype,
                                              java.lang.String msgdata,
                                              boolean waitreply)
Attempts to route and send a message to the specified peer, optionally waiting and returning any replies. This method using the Node's routing function to decide the next immediate peer to actually send the message to, based on the peer identifier of the final destination. If no router function (object) has been registered, it will not work.

Parameters:
peerid - the destination peer identifier
msgtype - the type of the message being send
msgdata - the message data
waitreply - whether to wait for reply(ies)
Returns:
list of replies (may be empty if error occurred)

connectAndSend

public java.util.List<PeerMessage> connectAndSend(PeerInfo pd,
                                                  java.lang.String msgtype,
                                                  java.lang.String msgdata,
                                                  boolean waitreply)
Connects to the specified peer and sends a message, optionally waiting and returning any replies.

Parameters:
pd - the peer information
msgtype - the type of the message being send
msgdata - the message data
waitreply - whether to wait for reply(ies)
Returns:
list of replies (may be empty if error occurred)

mainLoop

public void mainLoop()
Starts the loop which is the primary operation of the Node. The main loop opens a server socket, listens for incoming connections, and dispatches them to registered handlers appropriately.


startStabilizer

public void startStabilizer(StabilizerInterface st,
                            int delay)
Starts a "stabilizer" function running at the specified interval.

Parameters:
st - the stabilizer function object
delay - the delay (in milliseconds)

addHandler

public void addHandler(java.lang.String msgtype,
                       HandlerInterface handler)

addRouter

public void addRouter(RouterInterface router)

addPeer

public boolean addPeer(PeerInfo pd)

addPeer

public boolean addPeer(java.lang.String key,
                       PeerInfo pd)
Add new peer information to the peer list, indexed by the given key.

Parameters:
key - the key associated with the peer
pd - the peer information
Returns:
true if successful; false if maxPeers is reached, or if the peer list already contains the given key.

getPeer

public PeerInfo getPeer(java.lang.String key)

removePeer

public PeerInfo removePeer(java.lang.String key)

getPeerKeys

public java.util.Set<java.lang.String> getPeerKeys()

getNumberOfPeers

public int getNumberOfPeers()

getMaxPeers

public int getMaxPeers()

maxPeersReached

public boolean maxPeersReached()

getId

public java.lang.String getId()

getHost

public java.lang.String getHost()

getPort

public int getPort()