rice.pastry.wire
Class WireNodeHandle
- 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.
- Version:
- $Id: WireNodeHandle.java,v 1.16 2002/09/13 15:58:43 amislove Exp $
- Author:
- Alan Mislove
WireNodeHandle
public WireNodeHandle(java.net.InetSocketAddress address,
rice.pastry.NodeId nid)
- Constructor.
- Parameters:
address
- The address of the host on which this node resides
port
- The port number of this node on the host
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
getAddress
public java.net.InetSocketAddress getAddress()
- Returns the IP address and port of the remote node.
- Returns:
- The InetSocketAddress of the remote node.
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.
- Parameters:
msg
- Message to be delivered, may or may not be routeMessage.
messageSize
private int messageSize(java.lang.Object obj)
- 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.
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
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).
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.
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.
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.
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.
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.
- 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.
- Parameters:
starttime
- The time at which this ping was initiated.
readObject
private void readObject(java.io.ObjectInputStream ois)
- Overridden in order to specify the default state (using UDP)