rice.pastry.wire
Class WireNodeHandle

java.lang.Object
  |
  +--java.util.Observable
        |
        +--rice.pastry.NodeHandle
              |
              +--rice.pastry.dist.DistNodeHandle
                    |
                    +--rice.pastry.wire.WireNodeHandle
All Implemented Interfaces:
rice.pastry.LocalNodeI, rice.pastry.messaging.MessageReceiver, rice.p2p.commonapi.NodeHandle, java.util.Observer, rice.pastry.wire.SelectionKeyHandler, java.io.Serializable
Direct Known Subclasses:
TcpNodeHandle, UdpNodeHandle

public class WireNodeHandle
extends rice.pastry.dist.DistNodeHandle
implements rice.pastry.wire.SelectionKeyHandler

Class which represents a node handle in the socket-based pastry protocol. Initially, all of the messages are sent over UDP. If a message is too large to be sent over the UDP protocol (as determined by the MAX_UDP_MESSAGE_SIZE), then a socket connection is opened to the remote node. Modified by glchen to make some variable as protected instead of private.

Version:
$Id: WireNodeHandle.java,v 1.2 2003/09/22 19:53:07 glchen Exp $
Author:
Guanling Chen, Alan Mislove
See Also:
Serialized Form

Field Summary
protected  java.nio.channels.SelectionKey key
           
private  long lastpingtime
           
static int MAX_UDP_MESSAGE_SIZE
           
static int PING_THROTTLE
           
protected  rice.pastry.wire.SocketChannelReader reader
           
static int SOCKET_BUFFER_SIZE
           
protected  int state
           
static int STATE_USING_TCP
           
static int STATE_USING_UDP
           
static int STATE_USING_UDP_WAITING_FOR_TCP_DISCONNECT
           
static int STATE_USING_UDP_WAITING_TO_DISCONNECT
           
protected  SocketChannelWriter writer
           
 
Fields inherited from class rice.pastry.dist.DistNodeHandle
address, alive, isInPool, isLocal, nodeId
 
Fields inherited from class rice.pastry.NodeHandle
DECLARED_DEAD, DECLARED_LIVE, PROXIMITY_CHANGED
 
Fields inherited from class java.util.Observable
 
Fields inherited from interface rice.pastry.LocalNodeI
pending
 
Constructor Summary
WireNodeHandle(java.net.InetSocketAddress address, rice.pastry.NodeId nid)
          Constructor.
WireNodeHandle(java.net.InetSocketAddress address, rice.pastry.NodeId nid, rice.pastry.PastryNode pn)
          Alternate constructor with local Pastry node.
 
Method Summary
 void accept(java.nio.channels.SelectionKey key)
          Requeired by the SelectionKeyHandler interface.
private  void close(java.util.LinkedList messages)
          Private method used for closing the socket (if there is one present).
 void connect(java.nio.channels.SelectionKey key)
          Called by the socket manager whnever this node handle needs to complete it's connection to it's remote node.
 void connectToRemoteNode(java.util.LinkedList messages)
          Method which initiates a connection to a remote node.
 void disconnect()
          Method that is designed to be called by the SocketManager when it wishes for this node handle to disconnect.
private  int getAddress(java.net.InetAddress address)
          Utility method for converting an InetAddress to an int (for comparison purposes).
 int getState()
          Returns the state of this WireNodeHandle
protected  int messageSize(java.lang.Object obj)
          Method which returns the size of an object about to be sent over the wire.
 boolean pingImpl()
          Ping the remote node now, and update the proximity metric.
 void pingResponse()
          Method which is called by the SocketPingManager when a ping response comes back for this node.
 void pingStarted()
          Method which is called by the PingMessage right before it is going to be sent across the wire.
 void read(java.nio.channels.SelectionKey key)
          Called by the socket manager whenever there is data to be read from this node handle's remote node.
private  void readObject(java.io.ObjectInputStream ois)
          Overridden in order to specify the default state (using UDP)
 void receiveMessageImpl(rice.pastry.messaging.Message msg)
          Called to send a message to the node corresponding to this handle.
 void receiveSocketMessage(rice.pastry.wire.messaging.socket.SocketCommandMessage message)
          Method which is called when a SocketCommandMessage comes across an open socket for this node handle.
 void sendGreetingResponse(rice.pastry.wire.messaging.socket.HelloMessage hm)
           
 void setKey(java.nio.channels.SelectionKey key, rice.pastry.wire.messaging.socket.SocketCommandMessage scm)
          Method which sets the SelectionKey of this node handle.
 java.lang.String toStringImpl()
           
 void wakeup()
          Is called by the SelectorManager every time the manager is awakened.
 void write(java.nio.channels.SelectionKey key)
          Called by the socket manager whenever this node handle has registered interest in writing to it's remote node, and the socket is ready for writing.
 
Methods inherited from class rice.pastry.dist.DistNodeHandle
addObserver, afterSetLocalNode, clearChanged, countObservers, debug, deleteObserver, deleteObservers, equals, getAddress, getIsInPool, getNodeId, hasChanged, hashCode, isAlive, markAlive, markDead, notifyObservers, notifyObservers, ping, proximity, receiveMessage, setChanged, setIsInPool, setProximity, toString, update
 
Methods inherited from class rice.pastry.NodeHandle
assertLocalNode, getId, getLocalNode, setLocalNode
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

STATE_USING_UDP

public static int STATE_USING_UDP

STATE_USING_TCP

public static int STATE_USING_TCP

STATE_USING_UDP_WAITING_FOR_TCP_DISCONNECT

public static int STATE_USING_UDP_WAITING_FOR_TCP_DISCONNECT

STATE_USING_UDP_WAITING_TO_DISCONNECT

public static int STATE_USING_UDP_WAITING_TO_DISCONNECT

MAX_UDP_MESSAGE_SIZE

public static int MAX_UDP_MESSAGE_SIZE

SOCKET_BUFFER_SIZE

public static int SOCKET_BUFFER_SIZE

PING_THROTTLE

public static int PING_THROTTLE

lastpingtime

private transient long lastpingtime

reader

protected transient rice.pastry.wire.SocketChannelReader reader

writer

protected transient SocketChannelWriter writer

key

protected transient java.nio.channels.SelectionKey key

state

protected transient int state
Constructor Detail

WireNodeHandle

public WireNodeHandle(java.net.InetSocketAddress address,
                      rice.pastry.NodeId nid)
Constructor.

Parameters:
address - The address of the host on which this node resides
nid - The NodeId of this host

WireNodeHandle

public WireNodeHandle(java.net.InetSocketAddress address,
                      rice.pastry.NodeId nid,
                      rice.pastry.PastryNode pn)
Alternate constructor with local Pastry node.

Parameters:
address - The address of the host on which this node resides
nid - The NodeId of this host
pn - The local Pastry node
Method Detail

getState

public int getState()
Returns the state of this WireNodeHandle

Returns:
The state of this handle

receiveSocketMessage

public void receiveSocketMessage(rice.pastry.wire.messaging.socket.SocketCommandMessage message)
Method which is called when a SocketCommandMessage comes across an open socket for this node handle.

Parameters:
message - The message coming across the wire.

receiveMessageImpl

public void receiveMessageImpl(rice.pastry.messaging.Message msg)
Called to send a message to the node corresponding to this handle.

Specified by:
receiveMessageImpl in class rice.pastry.dist.DistNodeHandle
Parameters:
msg - Message to be delivered, may or may not be routeMessage.

messageSize

protected int messageSize(java.lang.Object obj)
                   throws java.io.IOException
Method which returns the size of an object about to be sent over the wire. This size includes all of the wrapper messages (such as the Socket Transport Message).

Parameters:
obj - The object
Returns:
The total size the object and wrappers will occupy.
java.io.IOException

connectToRemoteNode

public void connectToRemoteNode(java.util.LinkedList messages)
Method which initiates a connection to a remote node. This is done by connecting to the server socket on the remote node. This can be called by the receiveMessageImpl, if there is a too-big message waiting to be sent, or by the TransmissionManager if there are too many messages in the queue.


setKey

public void setKey(java.nio.channels.SelectionKey key,
                   rice.pastry.wire.messaging.socket.SocketCommandMessage scm)
Method which sets the SelectionKey of this node handle. Is designed to be called whenever a TCP connection has been established. All pending message will now be sent over TCP using the socket attached to this SelectionKey. If a socket has already been established, there is a protocol for determining which socket to close. If the address:port of the local is less than that of the remote, this node ignores the incoming key. Otherwise, it will kill it's own socket and use the new key as the "real" socket. NOTE: There are known problems with this implementation, especially under high stress.

Parameters:
key - The new SelectionKey

getAddress

private int getAddress(java.net.InetAddress address)
Utility method for converting an InetAddress to an int (for comparison purposes).

Parameters:
address - The address to convert
Returns:
An int representation of the address

sendGreetingResponse

public void sendGreetingResponse(rice.pastry.wire.messaging.socket.HelloMessage hm)

disconnect

public void disconnect()
Method that is designed to be called by the SocketManager when it wishes for this node handle to disconnect. Once this is called, the node handle will finish writing out any pending objects in the queue, and then send a DisconnectMessage to the remote node. Upon receiving this DisconnectMessage, the remote node will finish writing out any pending objects, and then will actually disconnect the socket.


accept

public void accept(java.nio.channels.SelectionKey key)
Requeired by the SelectionKeyHandler interface. Should never be called (because we never accept connections).

Specified by:
accept in interface rice.pastry.wire.SelectionKeyHandler

connect

public void connect(java.nio.channels.SelectionKey key)
Called by the socket manager whnever this node handle needs to complete it's connection to it's remote node. Is specified by the SelectionKeyHandler interface.

Specified by:
connect in interface rice.pastry.wire.SelectionKeyHandler

write

public void write(java.nio.channels.SelectionKey key)
Called by the socket manager whenever this node handle has registered interest in writing to it's remote node, and the socket is ready for writing. Is specified by the SelectionKeyHandler interface.

Specified by:
write in interface rice.pastry.wire.SelectionKeyHandler

read

public void read(java.nio.channels.SelectionKey key)
Called by the socket manager whenever there is data to be read from this node handle's remote node. Is specified from the SelectionKeyHandler interface.

Specified by:
read in interface rice.pastry.wire.SelectionKeyHandler

wakeup

public void wakeup()
Is called by the SelectorManager every time the manager is awakened. Checks to make sure that if we are waiting to write data, we are registered as being interested in writing.

Specified by:
wakeup in interface rice.pastry.wire.SelectionKeyHandler

close

private void close(java.util.LinkedList messages)
Private method used for closing the socket (if there is one present). It also cancels the SelectionKey so that it is never called again.

Parameters:
messages - The messages that need to be rerouted (or null)

pingImpl

public boolean pingImpl()
Ping the remote node now, and update the proximity metric. This method ALWAYS uses UDP, even if there already is a TCP socket open.

Specified by:
pingImpl in class rice.pastry.dist.DistNodeHandle
Returns:
liveness of remote node.

pingStarted

public void pingStarted()
Method which is called by the PingMessage right before it is going to be sent across the wire. Marks the beginning of a ping as now.


pingResponse

public void pingResponse()
Method which is called by the SocketPingManager when a ping response comes back for this node.


readObject

private void readObject(java.io.ObjectInputStream ois)
                 throws java.io.IOException,
                        java.lang.ClassNotFoundException
Overridden in order to specify the default state (using UDP)

java.io.IOException
java.lang.ClassNotFoundException

toStringImpl

public java.lang.String toStringImpl()
Specified by:
toStringImpl in class rice.pastry.dist.DistNodeHandle