rice.pastry.wire
Class SocketChannelWriter

java.lang.Object
  extended byrice.pastry.wire.SocketChannelWriter

public class SocketChannelWriter
extends java.lang.Object

Class which serves as an "writer" for all of the messages sent across the wire in Pastry. This class serializes and properly formats all messages, and then waits to be called with an available SocketChannel in order to write the message out. If the messagae could not be written in one go, subsequent calls to the write() method will finish writing out the message. This class also maintains an internal queue of messages waiting to be sent across the wire. Calling isEmpty() will tell clients if it is safe to mark the SelectionKey as not being interested in writing.


Field Summary
protected  WireNodeHandle handle
           
protected static byte[] MAGIC_NUMBER
          the magic number array which is written first
static int MAXIMUM_QUEUE_LENGTH
          the maximum length of the queue
protected  java.lang.Object pendingMsg
          a message that has already been serialized and pulled off of the queue but hasn't been sent
 
Constructor Summary
SocketChannelWriter(WirePastryNode spn, SocketCommandMessage msg, java.nio.channels.SelectionKey key)
          Constructor which creates this SocketChannelWriter with a pastry node and an object to write out.
SocketChannelWriter(WirePastryNode spn, SocketCommandMessage msg, java.nio.channels.SelectionKey key, WireNodeHandle wnh)
          Constructor which creates this SocketChannelWriter with a pastry node and an object to write out.
 
Method Summary
 void enqueue(java.lang.Object o)
          Adds an object to this SocketChannelWriter's queue of pending objects to write.
 java.util.Iterator getQueue()
          Returns the queue of writes for the remote address We are only returning the iterator of the queue because we don't actually want to return the queue, as we need to control the SelectionKey state based on the queue.
 void greetingReceived()
          called when the greeting has been received, setting that the socket is ready to be used to write data over
 boolean isEmpty()
          Returns whether or not there are objects in the queue on in writing.
 boolean isInterestedInWriting()
          Gets the InterestedInWriting attribute of the SocketChannelWriter object
 void notifyKilled()
          prints out any messages still in queue
 int queueSize()
          Accessor for the queue size.
 void reset(SocketCommandMessage msg)
          Resets the SocketChannelWriter, by clearing both the buffer and the queue.
static java.nio.ByteBuffer serialize(java.lang.Object o, java.nio.ByteBuffer oldBuf)
          Method which serializes a given object into a ByteBuffer, in order to prepare it for writing.
 void setKey(java.nio.channels.SelectionKey key)
          method to re-assign the key if it changes
 boolean write(java.nio.channels.SocketChannel sc)
          Method which is designed to be called when this writer should write out its data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXIMUM_QUEUE_LENGTH

public static int MAXIMUM_QUEUE_LENGTH
the maximum length of the queue


MAGIC_NUMBER

protected static byte[] MAGIC_NUMBER
the magic number array which is written first


handle

protected WireNodeHandle handle

pendingMsg

protected java.lang.Object pendingMsg
a message that has already been serialized and pulled off of the queue but hasn't been sent

Constructor Detail

SocketChannelWriter

public SocketChannelWriter(WirePastryNode spn,
                           SocketCommandMessage msg,
                           java.nio.channels.SelectionKey key)
Constructor which creates this SocketChannelWriter with a pastry node and an object to write out. But is not associated with a WireNodeHandle.

Parameters:
spn - The PastryNode the SocketChannelWriter servers
msg - first message to send
key - the key that this writer should maintain

SocketChannelWriter

public SocketChannelWriter(WirePastryNode spn,
                           SocketCommandMessage msg,
                           java.nio.channels.SelectionKey key,
                           WireNodeHandle wnh)
Constructor which creates this SocketChannelWriter with a pastry node and an object to write out.

Parameters:
spn - The PastryNode the SocketChannelWriter servers
msg - first message to send
key - the key that this writer should maintain
wnh - the WireNodeHandle that this object services
Method Detail

isInterestedInWriting

public boolean isInterestedInWriting()
Gets the InterestedInWriting attribute of the SocketChannelWriter object

Returns:
The InterestedInWriting value

isEmpty

public boolean isEmpty()
Returns whether or not there are objects in the queue on in writing. If the result is true, it the safe to mark the SelectionKey as not being interested in writing.

Returns:
Whether or not there are objects still to be written.

getQueue

public java.util.Iterator getQueue()
Returns the queue of writes for the remote address We are only returning the iterator of the queue because we don't actually want to return the queue, as we need to control the SelectionKey state based on the queue. If we give up the queue, we don't know if someone is adding/removing items to it without properly adjusting the selection key state.

Returns:
the queue of writes for the remote address

enqueue

public void enqueue(java.lang.Object o)
Adds an object to this SocketChannelWriter's queue of pending objects to write. This methos is synchronized and therefore safe for use by multiple threads.

Parameters:
o - The object to be written.

setKey

public void setKey(java.nio.channels.SelectionKey key)
method to re-assign the key if it changes

Parameters:
key - the new key

queueSize

public int queueSize()
Accessor for the queue size.

Returns:
the size of the queue

reset

public void reset(SocketCommandMessage msg)
Resets the SocketChannelWriter, by clearing both the buffer and the queue. Should not be used except when a socket has just been opened.

Parameters:
msg - The greeting message that should be enqueued first

greetingReceived

public void greetingReceived()
called when the greeting has been received, setting that the socket is ready to be used to write data over


write

public boolean write(java.nio.channels.SocketChannel sc)
              throws java.io.IOException
Method which is designed to be called when this writer should write out its data. Returns whether or not the message was completely written. If false is returns, write() will need to be called again when the SocketChannel is ready for data to be written.

Parameters:
sc - The SocketChannel to write to
Returns:
true if this output stream is done, false otherwise
Throws:
java.io.IOException - DESCRIBE THE EXCEPTION

serialize

public static java.nio.ByteBuffer serialize(java.lang.Object o,
                                            java.nio.ByteBuffer oldBuf)
                                     throws java.io.IOException
Method which serializes a given object into a ByteBuffer, in order to prepare it for writing. This is necessary because the size of the object must be prepended to the to the front of the buffer in order to tell the reciever how long the object is.

Parameters:
o - The object to serialize
Returns:
A ByteBuffer containing the object prepended with its size.
Throws:
java.io.IOException - if there is an error

notifyKilled

public void notifyKilled()
prints out any messages still in queue






Imprint-Dataprotection