uk.ac.rdg.resc.jstyx.messages
Class StyxMessage

java.lang.Object
  extended by uk.ac.rdg.resc.jstyx.messages.StyxMessage
Direct Known Subclasses:
RattachMessage, RauthMessage, RclunkMessage, RcreateMessage, RerrorMessage, RflushMessage, RopenMessage, RreadMessage, RremoveMessage, RstatMessage, RversionMessage, RwalkMessage, RwriteMessage, RwstatMessage, TattachMessage, TauthMessage, TclunkMessage, TcreateMessage, TflushMessage, TopenMessage, TreadMessage, TremoveMessage, TstatMessage, TversionMessage, TwalkMessage, TwriteMessage, TwstatMessage

public abstract class StyxMessage
extends java.lang.Object

Abstract superclass for all Styx messages.

Author:
Jon Blower $Revision: 507 $ $Date: 2005-12-01 08:21:56 +0000 (Thu, 01 Dec 2005) $ $Log$ Revision 1.14 2005/12/01 08:21:56 jonblower Fixed javadoc comments Revision 1.13 2005/11/03 17:09:27 jonblower Created more efficient RreadMessage that involves less copying of buffers (still reliable) Revision 1.12 2005/11/03 07:46:55 jonblower Trying to fix bug with sending RreadMessages Revision 1.11 2005/05/10 19:17:54 jonblower Added dispose() method Revision 1.10 2005/03/22 17:48:27 jonblower Removed debug code that tracked ByteBuffer allocation Revision 1.9 2005/03/21 17:57:11 jonblower Trying to fix ByteBuffer leak in SGS server Revision 1.8 2005/03/18 13:56:00 jonblower Improved freeing of ByteBuffers, and bug fixes Revision 1.7 2005/03/16 17:56:22 jonblower Replaced use of java.nio.ByteBuffer with MINA's ByteBuffer to minimise copying of buffers Revision 1.6 2005/03/15 16:56:19 jonblower Changed to allow re-use of ByteBuffers once message is finished with Revision 1.5 2005/03/15 09:01:48 jonblower Message type now stored as short, not int Revision 1.4 2005/03/11 14:02:15 jonblower Merged MINA-Test_20059309 into main line of development Revision 1.3.2.3 2005/03/11 12:30:46 jonblower Changed so that message payloads are always ints, not longs Revision 1.3.2.2 2005/03/10 14:05:26 jonblower Reinstated getFid() and getName() methods Revision 1.3.2.1 2005/03/10 11:50:59 jonblower Changed to fit with MINA framework

Field Summary
protected  org.apache.mina.common.ByteBuffer buf
           
protected  int length
           
protected static java.lang.String lock
           
protected  java.lang.String name
           
protected  int tag
           
protected  short type
           
 
Constructor Summary
protected StyxMessage(int length, short type, int tag)
          Creates a new instance of StyxMessage.
 
Method Summary
static StyxMessage createStyxMessage(int length, short type, int tag)
          Static factory method for creating a StyxMessage.
protected abstract  void decodeBody(StyxBuffer styxBuf)
          Called when a complete message has arrived; signals that we are ready to interpret the raw bytes in the buffer and turn them into meaningful information.
 void dispose()
          This is called after the message has been sent (in StyxServerProtocolHandler.messageSent()) and is a signal to free any resources associated with the message (e.g.
 void encode()
          Called by StyxMessageEncoder when we are about to send a message.
protected abstract  void encodeBody(StyxBuffer styxBuf)
          Encode the body of the message into bytes in the underlying buffer
 org.apache.mina.common.ByteBuffer getBuffer()
           
protected abstract  java.lang.String getElements()
           
 long getFid()
           
 int getLength()
           
 java.lang.String getName()
           
 int getTag()
           
 short getType()
           
 boolean readBytesFrom(org.apache.mina.common.ByteBuffer in)
          Read bytes from the given ByteBuffer into this Message.
 void setTag(int newTag)
          Sets the tag of the message
 java.lang.String toFriendlyString()
           
 java.lang.String toString()
           
 void write(org.apache.mina.protocol.ProtocolEncoderOutput out)
          Called by StyxMessageEncoder to send a message to the output
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

lock

protected static final java.lang.String lock

length

protected int length

type

protected short type

tag

protected int tag

name

protected java.lang.String name

buf

protected org.apache.mina.common.ByteBuffer buf
Constructor Detail

StyxMessage

protected StyxMessage(int length,
                      short type,
                      int tag)
Creates a new instance of StyxMessage.

Method Detail

getName

public final java.lang.String getName()
Returns:
The name of this message (e.g. "Tread", "Rwalk", etc)

getLength

public final int getLength()
Returns:
The total length of the StyxMessage in bytes

getType

public final short getType()
Returns:
The type of the message

getTag

public final int getTag()
Returns:
The tag of the message

setTag

public final void setTag(int newTag)
Sets the tag of the message


getBuffer

public final org.apache.mina.common.ByteBuffer getBuffer()
Returns:
the buffer containing the body of this message

getFid

public long getFid()
Returns:
the fid associated with this message. This default implementation returns StyxUtils.NOFID; subclasses should override this. This method only exists in this superclass as a convenience for the StyxMon application.

readBytesFrom

public final boolean readBytesFrom(org.apache.mina.common.ByteBuffer in)
                            throws org.apache.mina.protocol.ProtocolViolationException
Read bytes from the given ByteBuffer into this Message. There may still be bytes remaining in the input buffer after this method has been called.

Parameters:
in - The org.apache.mina.common.ByteBuffer that contains the data.
Returns:
true if we now have a complete StyxMessage, false otherwise
Throws:
org.apache.mina.protocol.ProtocolViolationException - if the bytes do not represent a valid StyxMessage

decodeBody

protected abstract void decodeBody(StyxBuffer styxBuf)
                            throws org.apache.mina.protocol.ProtocolViolationException
Called when a complete message has arrived; signals that we are ready to interpret the raw bytes in the buffer and turn them into meaningful information. Subclasses should make sure that the buffer is no longer needed once this method has finished, as the underlying buffer will be reused.

Throws:
org.apache.mina.protocol.ProtocolViolationException - if the buffer doesn't contain a valid StyxMessage body

write

public void write(org.apache.mina.protocol.ProtocolEncoderOutput out)
           throws org.apache.mina.protocol.ProtocolViolationException
Called by StyxMessageEncoder to send a message to the output

Throws:
org.apache.mina.protocol.ProtocolViolationException

encode

public void encode()
            throws org.apache.mina.protocol.ProtocolViolationException
Called by StyxMessageEncoder when we are about to send a message. Creates the underlying ByteBuffer, wraps it as a StyxBuffer, writes the header information, calls encodeBody() to write the body information, then flips the buffer so that it is ready for writing to the output stream.

Throws:
org.apache.mina.protocol.ProtocolViolationException - if a problem occurred encoding the message (shouldn't happen)

encodeBody

protected abstract void encodeBody(StyxBuffer styxBuf)
                            throws org.apache.mina.protocol.ProtocolViolationException
Encode the body of the message into bytes in the underlying buffer

Throws:
org.apache.mina.protocol.ProtocolViolationException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
Returns:
String representation of this StyxMessage

getElements

protected abstract java.lang.String getElements()
Returns:
the body elements of this message as a string

toFriendlyString

public java.lang.String toFriendlyString()
Returns:
a human-readable string that displays the contents of the message, without the header info. This default implementation simply calls this.getElements(): subclasses should override this behaviour

dispose

public void dispose()
This is called after the message has been sent (in StyxServerProtocolHandler.messageSent()) and is a signal to free any resources associated with the message (e.g. an RreadMessage can release the ByteBuffer holding the payload). This default implementation does nothing: subclasses should override if necessary.


createStyxMessage

public static StyxMessage createStyxMessage(int length,
                                            short type,
                                            int tag)
                                     throws org.apache.mina.protocol.ProtocolViolationException
Static factory method for creating a StyxMessage. Called by StyxMessageDecoder when the header of a message has been decoded. Returns the appropriate subclass of StyxMessage, depending on the provided type.

Parameters:
length - The total length of the message (header and body)
type - The numeric code representing the message type
tag - The message tag
Returns:
A StyxMessage of the appropriate type, depending on the tag
Throws:
org.apache.mina.protocol.ProtocolViolationException - if the message is of an unknown type


Copyright © 2004-2006 Reading e-Science Centre. All Rights Reserved.