rice.pastry.wire
Class SocketManager

java.lang.Object
  extended byrice.pastry.wire.SocketManager
All Implemented Interfaces:
SelectionKeyHandler
Direct Known Subclasses:
GlobalSocketManager

public class SocketManager
extends java.lang.Object
implements SelectionKeyHandler

Class which maintains all outgoing open sockets. It is responsible for keeping only MAX_OPEN_SOCKETS number of client sockets open at once. It also binds a ServerSocketChannel to the specified port and listens for incoming connections. Once a connections is established, it uses the interal SocketConnector to read the greeting message (HelloMessage) off of the stream, and hands the connection off to the appropriate node handle.


Field Summary
static int MAX_OPEN_SOCKETS
          the number of sockets where we start closing other sockets
protected  java.util.LinkedList openSockets
          the linked list of open sockets
 
Constructor Summary
SocketManager(WirePastryNode node, int port, java.nio.channels.Selector selector)
          Constructs a new SocketManager.
 
Method Summary
 void accept(java.nio.channels.SelectionKey key)
          Specified by the SelectionKeyHandler interface.
 void closeSocket(WireNodeHandle handle)
          Method which is designed to be called by node handles which have been disconnected by the remote node (i.e.
 void connect(java.nio.channels.SelectionKey key)
          Specified by the SelectionKeyHandler interface - should NEVER be called!
protected  java.util.LinkedList generateOpenSockets()
          Can be overloaded.
protected  boolean needToDisconnectSockets()
          called to determine if a socket should be closed
 void openSocket(WireNodeHandle handle)
          Method which is designed to be called by node handles when they wish to open a socket to their remote node.
 void read(java.nio.channels.SelectionKey key)
          Specified by the SelectionKeyHandler interface - is called whenever a key has data available.
protected  void removeOpenSocketsIfNeeded()
          called routinely to recycle sockets that haven't recently been used
 void update(WireNodeHandle handle)
          Method which is designed to be called whenever a node has network activity.
 void wakeup()
          Specified by the SelectionKeyHandler interface - does nothing.
 void write(java.nio.channels.SelectionKey key)
          Specified by the SelectionKeyHandler interface - should NEVER be called!
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

openSockets

protected java.util.LinkedList openSockets
the linked list of open sockets


MAX_OPEN_SOCKETS

public static int MAX_OPEN_SOCKETS
the number of sockets where we start closing other sockets

Constructor Detail

SocketManager

public SocketManager(WirePastryNode node,
                     int port,
                     java.nio.channels.Selector selector)
Constructs a new SocketManager.

Parameters:
node - The pastry node this manager is serving
port - The port number which this manager is listening on
selector - The Selector this manager should register with
Method Detail

generateOpenSockets

protected java.util.LinkedList generateOpenSockets()
Can be overloaded. This returns the list of open sockets that this socketmanager is to act upon.

Returns:
a LinkedList to remember what sockets are open

openSocket

public void openSocket(WireNodeHandle handle)
Method which is designed to be called by node handles when they wish to open a socket to their remote node. This method will determine if another node handle needs to disconnect, and will disconnect the ejected node handle if necessary.

Parameters:
handle - The handle which wishes to open a connection

removeOpenSocketsIfNeeded

protected void removeOpenSocketsIfNeeded()
called routinely to recycle sockets that haven't recently been used


needToDisconnectSockets

protected boolean needToDisconnectSockets()
called to determine if a socket should be closed

Returns:
wether to close a socket

closeSocket

public void closeSocket(WireNodeHandle handle)
Method which is designed to be called by node handles which have been disconnected by the remote node (i.e. they received a DisconnectMessage).

Parameters:
handle - The handle which has been disconnected.

update

public void update(WireNodeHandle handle)
Method which is designed to be called whenever a node has network activity. This is used to determine which nodes should be disconnected, should it be necessary (implementation of a LRU stack).

Parameters:
handle - The node handle which has activity.

accept

public void accept(java.nio.channels.SelectionKey key)
Specified by the SelectionKeyHandler interface. Is called whenever a key has become acceptable, representing an incoming connection. This method will accept the connection, and attach a SocketConnector in order to read the greeting off of the channel. Once the greeting has been read, the connector will hand the channel off to the appropriate node handle.

Specified by:
accept in interface SelectionKeyHandler
Parameters:
key - The key which is acceptable.

read

public void read(java.nio.channels.SelectionKey key)
Specified by the SelectionKeyHandler interface - is called whenever a key has data available. The appropriate SocketConnecter is informed, and is told to read the data.

Specified by:
read in interface SelectionKeyHandler
Parameters:
key - The key which is readable.

write

public void write(java.nio.channels.SelectionKey key)
Specified by the SelectionKeyHandler interface - should NEVER be called!

Specified by:
write in interface SelectionKeyHandler
Parameters:
key - The key which is writable.

connect

public void connect(java.nio.channels.SelectionKey key)
Specified by the SelectionKeyHandler interface - should NEVER be called!

Specified by:
connect in interface SelectionKeyHandler
Parameters:
key - The key which is connectable.

wakeup

public void wakeup()
Specified by the SelectionKeyHandler interface - does nothing.






Imprint-Dataprotection