FileDocCategorySizeDatePackage
ChannelData.javaAPI DocApache Tomcat 6.0.1412845Fri Jul 20 04:20:36 BST 2007org.apache.catalina.tribes.io

ChannelData

public class ChannelData extends Object implements org.apache.catalina.tribes.ChannelMessage
The ChannelData object is used to transfer a message through the channel interceptor stack and eventually out on a transport to be sent to another node. While the message is being processed by the different interceptors, the message data can be manipulated as each interceptor seems appropriate.
author
Peter Rossbach
author
Filip Hanik
version
$Revision: 538977 $ $Date: 2007-05-17 17:43:49 +0200 (jeu., 17 mai 2007) $

Fields Summary
public static ChannelData[]
EMPTY_DATA_ARRAY
public static boolean
USE_SECURE_RANDOM_FOR_UUID
private int
options
The options this message was sent with
private XByteBuffer
message
The message data, stored in a dynamic buffer
private long
timestamp
The timestamp that goes with this message
private byte[]
uniqueId
A unique message id
private org.apache.catalina.tribes.Member
address
The source or reply-to address for this message
Constructors Summary
public ChannelData()
Creates an empty channel data with a new unique Id

see
#ChannelData(boolean)


                     
      
        this(true);
    
public ChannelData(boolean generateUUID)
Create an empty channel data object

param
generateUUID boolean - if true, a unique Id will be generated

        if ( generateUUID ) generateUUID();
    
public ChannelData(byte[] uniqueId, XByteBuffer message, long timestamp)
Creates a new channel data object with data

param
uniqueId - unique message id
param
message - message data
param
timestamp - message timestamp

        this.uniqueId = uniqueId;
        this.message = message;
        this.timestamp = timestamp;
    
Methods Summary
public static java.lang.StringbToS(byte[] data)

        StringBuffer buf = new StringBuffer(4*16);
        buf.append("{");
        for (int i=0; data!=null && i<data.length; i++ ) buf.append(String.valueOf(data[i])).append(" ");
        buf.append("}");
        return buf.toString();
    
public java.lang.Objectclone()
Create a shallow clone, only the data gets recreated

return
ClusterData

//        byte[] d = this.getDataPackage();
//        return ClusterData.getDataFromPackage(d);
        ChannelData clone = new ChannelData(false);
        clone.options = this.options;
        clone.message = new XByteBuffer(this.message.getBytesDirect(),false);
        clone.timestamp = this.timestamp;
        clone.uniqueId = this.uniqueId;
        clone.address = this.address;
        return clone;
    
public java.lang.Objectdeepclone()
Complete clone

return
ClusterData

        byte[] d = this.getDataPackage();
        return ChannelData.getDataFromPackage(d);
    
public booleanequals(java.lang.Object o)
Compares to ChannelData objects, only compares on getUniqueId().equals(o.getUniqueId())

param
o Object
return
boolean

        if ( o instanceof ChannelData ) {
            return Arrays.equals(getUniqueId(),((ChannelData)o).getUniqueId());
        } else return false;
    
public voidgenerateUUID()
Generates a UUID and invokes setUniqueId

        byte[] data = new byte[16];
        UUIDGenerator.randomUUID(USE_SECURE_RANDOM_FOR_UUID,data,0);
        setUniqueId(data);
    
public org.apache.catalina.tribes.MembergetAddress()
Returns the source or reply-to address

return
Member

        return address;
    
public static org.apache.catalina.tribes.io.ChannelDatagetDataFromPackage(XByteBuffer xbuf)
Deserializes a ChannelData object from a byte array

param
b byte[]
return
ChannelData

        ChannelData data = new ChannelData(false);
        int offset = 0;
        data.setOptions(XByteBuffer.toInt(xbuf.getBytesDirect(),offset));
        offset += 4; //options
        data.setTimestamp(XByteBuffer.toLong(xbuf.getBytesDirect(),offset));
        offset += 8; //timestamp
        data.uniqueId = new byte[XByteBuffer.toInt(xbuf.getBytesDirect(),offset)];
        offset += 4; //uniqueId length
        System.arraycopy(xbuf.getBytesDirect(),offset,data.uniqueId,0,data.uniqueId.length);
        offset += data.uniqueId.length; //uniqueId data
        //byte[] addr = new byte[XByteBuffer.toInt(xbuf.getBytesDirect(),offset)];
        int addrlen = XByteBuffer.toInt(xbuf.getBytesDirect(),offset);
        offset += 4; //addr length
        //System.arraycopy(xbuf.getBytesDirect(),offset,addr,0,addr.length);
        data.setAddress(MemberImpl.getMember(xbuf.getBytesDirect(),offset,addrlen));
        //offset += addr.length; //addr data
        offset += addrlen;
        int xsize = XByteBuffer.toInt(xbuf.getBytesDirect(),offset);
        offset += 4; //xsize length
        System.arraycopy(xbuf.getBytesDirect(),offset,xbuf.getBytesDirect(),0,xsize);
        xbuf.setLength(xsize);
        data.message = xbuf;
        return data;

    
public static org.apache.catalina.tribes.io.ChannelDatagetDataFromPackage(byte[] b)

        ChannelData data = new ChannelData(false);
        int offset = 0;
        data.setOptions(XByteBuffer.toInt(b,offset));
        offset += 4; //options
        data.setTimestamp(XByteBuffer.toLong(b,offset));
        offset += 8; //timestamp
        data.uniqueId = new byte[XByteBuffer.toInt(b,offset)];
        offset += 4; //uniqueId length
        System.arraycopy(b,offset,data.uniqueId,0,data.uniqueId.length);
        offset += data.uniqueId.length; //uniqueId data
        byte[] addr = new byte[XByteBuffer.toInt(b,offset)];
        offset += 4; //addr length
        System.arraycopy(b,offset,addr,0,addr.length);
        data.setAddress(MemberImpl.getMember(addr));
        offset += addr.length; //addr data
        int xsize = XByteBuffer.toInt(b,offset);
        //data.message = new XByteBuffer(new byte[xsize],false);
        data.message = BufferPool.getBufferPool().getBuffer(xsize,false);
        offset += 4; //message length
        System.arraycopy(b,offset,data.message.getBytesDirect(),0,xsize);
        data.message.append(b,offset,xsize);
        offset += xsize; //message data
        return data;
    
public byte[]getDataPackage()
Serializes the ChannelData object into a byte[] array

return
byte[]

        int length = getDataPackageLength();
        byte[] data = new byte[length];
        int offset = 0;
        return getDataPackage(data,offset);
    
public byte[]getDataPackage(byte[] data, int offset)

        byte[] addr = ((MemberImpl)address).getData(false);
        XByteBuffer.toBytes(options,data,offset);
        offset += 4; //options
        XByteBuffer.toBytes(timestamp,data,offset);
        offset += 8; //timestamp
        XByteBuffer.toBytes(uniqueId.length,data,offset);
        offset += 4; //uniqueId.length
        System.arraycopy(uniqueId,0,data,offset,uniqueId.length);
        offset += uniqueId.length; //uniqueId data
        XByteBuffer.toBytes(addr.length,data,offset);
        offset += 4; //addr.length
        System.arraycopy(addr,0,data,offset,addr.length);
        offset += addr.length; //addr data
        XByteBuffer.toBytes(message.getLength(),data,offset);
        offset += 4; //message.length
        System.arraycopy(message.getBytesDirect(),0,data,offset,message.getLength());
        offset += message.getLength(); //message data
        return data;
    
public intgetDataPackageLength()

        int length = 
            4 + //options
            8 + //timestamp  off=4
            4 + //unique id length off=12
            uniqueId.length+ //id data off=12+uniqueId.length
            4 + //addr length off=12+uniqueId.length+4
            ((MemberImpl)address).getDataLength()+ //member data off=12+uniqueId.length+4+add.length
            4 + //message length off=12+uniqueId.length+4+add.length+4
            message.getLength();
        return length;

    
public XByteBuffergetMessage()

return
Returns the message byte buffer

        return message;
    
public intgetOptions()

return
returns the message options see org.apache.catalina.tribes.Channel#sendMessage(org.apache.catalina.tribes.Member[], java.io.Serializable, int)

        return options;
    
public longgetTimestamp()

return
Returns the timestamp.

        return timestamp;
    
public byte[]getUniqueId()

return
Returns the uniqueId.

        return uniqueId;
    
public inthashCode()

        return XByteBuffer.toInt(getUniqueId(),0);
    
public static booleansendAckAsync(int options)
Utility method, returns true if the options flag indicates that an ack is to be sent after the message has been received but not yet processed

param
options int - the options for the message
return
boolean
see
org.apache.catalina.tribes.Channel#SEND_OPTIONS_USE_ACK
see
org.apache.catalina.tribes.Channel#SEND_OPTIONS_SYNCHRONIZED_ACK

        return ( (Channel.SEND_OPTIONS_USE_ACK & options) == Channel.SEND_OPTIONS_USE_ACK) &&
            ( (Channel.SEND_OPTIONS_SYNCHRONIZED_ACK & options) != Channel.SEND_OPTIONS_SYNCHRONIZED_ACK);
    
public static booleansendAckSync(int options)
Utility method, returns true if the options flag indicates that an ack is to be sent after the message has been received and processed

param
options int - the options for the message
return
boolean
see
org.apache.catalina.tribes.Channel#SEND_OPTIONS_USE_ACK
see
org.apache.catalina.tribes.Channel#SEND_OPTIONS_SYNCHRONIZED_ACK

        return ( (Channel.SEND_OPTIONS_USE_ACK & options) == Channel.SEND_OPTIONS_USE_ACK) &&
            ( (Channel.SEND_OPTIONS_SYNCHRONIZED_ACK & options) == Channel.SEND_OPTIONS_SYNCHRONIZED_ACK);
    
public voidsetAddress(org.apache.catalina.tribes.Member address)
Sets the source or reply-to address

param
address Member

        this.address = address;
    
public voidsetMessage(XByteBuffer message)

param
message The message to send.

        this.message = message;
    
public voidsetOptions(int options)

param
sets the message options

        this.options = options;
    
public voidsetTimestamp(long timestamp)

param
timestamp The timestamp to send

        this.timestamp = timestamp;
    
public voidsetUniqueId(byte[] uniqueId)

param
uniqueId The uniqueId to send.

        this.uniqueId = uniqueId;
    
public java.lang.StringtoString()

        StringBuffer buf = new StringBuffer();
        buf.append("ClusterData[src=");
        buf.append(getAddress()).append("; id=");
        buf.append(bToS(getUniqueId())).append("; sent=");
        buf.append(new Timestamp(this.getTimestamp()).toString()).append("]");
        return buf.toString();