amber.type
Class Packet

java.lang.Object
  |
  +--amber.type.Packet
All Implemented Interfaces:
java.io.Serializable

public class Packet
extends java.lang.Object
implements java.io.Serializable

This class handles the actual packet processing required to message between the client and server parts of the amber system.

See Also:
Serialized Form

Field Summary
static int Command
          The type of command to perform or event to process.
static int CommandNone
          This is the basic command.
protected  byte[] data
          This is the actual data held in the packet.
static int DataLengthStart
          This is the length of the data following the header if any.
static int DestinationIdHigh
          This is the id to send the packet to.
static int DestinationIdLow
          This is the id to send the packet to.
static int EventFlag
          This is a flag indicating that the packet is an event rather than a command.
static int Flags
          These are various flags which affect the processing of the header and data.
protected  byte[] header
          This is the actual header information of the packet.
static int IncompleteFlag
          This is a flag indicating that there are more packets to follow before the sequence is complete.
static int MagicMarker
          This is the magic marker start of the packet.
static int PacketHeaderLength
          This is the length of the header in the packet.
static byte PacketMagicMarker
          This is magic character which flags the beginning of the packet.
static short ServerId
          This is the ID given to the remote server.
static int SourceIdHigh
          This is the id of the applet which sent the packet.
static int SourceIdLow
          This is the id of the applet which sent the packet.
static int Version
          This is the version of the packet.
 
Constructor Summary
Packet()
          Default Constructor.
Packet(byte version, byte flags, byte command, short destinationId, short sourceId, int dataLength, byte[] data, boolean event)
          Initialising Constructor.
Packet(byte command, short destinationId, short sourceId, boolean event)
          Initialising Constructor.
Packet(byte command, short destinationId, short sourceId, int dataLength, byte[] data, boolean event)
          Initialising Constructor.
Packet(java.io.InputStream input)
          Constructor used when filling the packet from an input stream.
 
Method Summary
static int bytesToInt(byte[] value, int offset)
           
static short bytesToShort(byte[] data, int offset)
           
 boolean compareTo(byte[] otherHeader)
          This function compares the packets header to the input byte array.
 byte getCommand()
          Gets the data in the following variable: The type of command to perform or event to process.
 byte[] getData()
          Gets the data in the following variable: This is the actual data itself following the header if any.
 int getDataLength()
          Gets the data in the following variable: This is the length of the data following the header if any.
 short getDestinationId()
          Gets the data in the following variable: This is the id to send the packet to.
 byte getFlags()
          Gets the data in the following variable: These are various flags which affect the processing of the header and data.
 byte[] getHeader()
          Returns the header array.
 short getSourceId()
          Gets the data in the following variable: This is the id of the applet which sent the packet.
 byte getVersion()
          Gets the data in the following variable: This is the version of the packet.
 boolean headerOnlyComparesTo(byte[] otherHeader)
          This function compares the packets header to the input byte array.
static void intToBytes(int value, byte[] data, int offset)
           
 boolean isEvent()
          Returns true if this packet is an event.
 boolean isIncomplete()
          Returns true if this packet is part of an incomplete packet sequence.
 void readPacket(java.io.InputStream input)
          Reads in a packet from the specified input stream.
 void sendPacket(java.io.OutputStream output)
          Writes a packet to the specified output stream.
 void setCommand(byte data)
          Sets the data in the following variable: The type of command to perform or event to process.
 void setData(byte[] data)
          Sets the data in the following variable: This is the actual data in the packet if any.
 void setDataLength(int data)
          Sets the data in the following variable: This is the length of the data following the header if any.
 void setDestinationId(short data)
          Sets the data in the following variable: This is the id to send the packet to.
 void setEvent(boolean eventFlag)
          Sets the event flag in the packet.
 void setFlags(byte data)
          Sets the data in the following variable: These are various flags which affect the processing of the header and data.
 void setIncomplete(boolean incompleteFlag)
          Sets the incomplete packet sequence flag in the packet.
 void setSourceId(short data)
          Sets the data in the following variable: This is the id of the applet which sent the packet.
 void setVersion(byte data)
          Sets the data in the following variable: This is the version of the packet.
static byte[] shortToBytes(short value)
           
static void shortToBytes(short value, byte[] data, int offset)
           
 java.lang.String toString()
          This function returns the String form of the data.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PacketHeaderLength

public static final int PacketHeaderLength
This is the length of the header in the packet.

PacketMagicMarker

public static final byte PacketMagicMarker
This is magic character which flags the beginning of the packet.

CommandNone

public static final int CommandNone
This is the basic command.

ServerId

public static final short ServerId
This is the ID given to the remote server.

MagicMarker

public static final int MagicMarker
This is the magic marker start of the packet.

Version

public static final int Version
This is the version of the packet.

Flags

public static final int Flags
These are various flags which affect the processing of the header and data.

Command

public static final int Command
The type of command to perform or event to process.

DestinationIdHigh

public static final int DestinationIdHigh
This is the id to send the packet to.

DestinationIdLow

public static final int DestinationIdLow
This is the id to send the packet to.

SourceIdHigh

public static final int SourceIdHigh
This is the id of the applet which sent the packet.

SourceIdLow

public static final int SourceIdLow
This is the id of the applet which sent the packet.

DataLengthStart

public static final int DataLengthStart
This is the length of the data following the header if any.

EventFlag

public static final int EventFlag
This is a flag indicating that the packet is an event rather than a command.

IncompleteFlag

public static final int IncompleteFlag
This is a flag indicating that there are more packets to follow before the sequence is complete.

header

protected byte[] header
This is the actual header information of the packet.

data

protected byte[] data
This is the actual data held in the packet.
Constructor Detail

Packet

public Packet()
Default Constructor.

Packet

public Packet(byte command,
              short destinationId,
              short sourceId,
              boolean event)
Initialising Constructor.
Parameters:
command - The type of command to perform or event to process.
destinationId - This is the id to send the packet to.
sourceId - This is the id of the applet which sent the packet.
event - boolean true when this packet is an event.

Packet

public Packet(byte command,
              short destinationId,
              short sourceId,
              int dataLength,
              byte[] data,
              boolean event)
Initialising Constructor.
Parameters:
command - The type of command to perform or event to process.
destinationId - This is the id to send the packet to.
sourceId - This is the id of the applet which sent the packet.
dataLength - This is the length of the data following the header if any.
data - This is the data for the packet stored in a byte array.
event - boolean true when this packet is an event.

Packet

public Packet(byte version,
              byte flags,
              byte command,
              short destinationId,
              short sourceId,
              int dataLength,
              byte[] data,
              boolean event)
Initialising Constructor.
Parameters:
version - This is the version of the packet.
flags - These are various flags which affect the processing of the header and data.
command - The type of command to perform or event to process.
destinationId - This is the id to send the packet to.
sourceId - This is the id of the applet which sent the packet.
dataLength - This is the length of the data following the header if any.
data - This is the data for the packet stored in a byte array.

Packet

public Packet(java.io.InputStream input)
       throws java.io.IOException,
              java.net.ProtocolException
Constructor used when filling the packet from an input stream.
Method Detail

getVersion

public byte getVersion()
Gets the data in the following variable: This is the version of the packet.
Returns:
byte containing the required information.

getFlags

public byte getFlags()
Gets the data in the following variable: These are various flags which affect the processing of the header and data.
Returns:
byte containing the required information.

getCommand

public byte getCommand()
Gets the data in the following variable: The type of command to perform or event to process.
Returns:
byte containing the required information.

getDestinationId

public short getDestinationId()
Gets the data in the following variable: This is the id to send the packet to.
Returns:
byte containing the required information.

getSourceId

public short getSourceId()
Gets the data in the following variable: This is the id of the applet which sent the packet.
Returns:
byte containing the required information.

getDataLength

public int getDataLength()
Gets the data in the following variable: This is the length of the data following the header if any.
Returns:
int containing the required information.

getData

public byte[] getData()
Gets the data in the following variable: This is the actual data itself following the header if any.
Returns:
byte [] which is null if no data.

setVersion

public void setVersion(byte data)
Sets the data in the following variable: This is the version of the packet.
Parameters:
data - byte containing the data to set the variable to.

setFlags

public void setFlags(byte data)
Sets the data in the following variable: These are various flags which affect the processing of the header and data.
Parameters:
data - byte containing the data to set the variable to.

setCommand

public void setCommand(byte data)
Sets the data in the following variable: The type of command to perform or event to process.
Parameters:
data - byte containing the data to set the variable to.

setDestinationId

public void setDestinationId(short data)
Sets the data in the following variable: This is the id to send the packet to.
Parameters:
data - byte containing the data to set the variable to.

setSourceId

public void setSourceId(short data)
Sets the data in the following variable: This is the id of the applet which sent the packet.
Parameters:
data - byte containing the data to set the variable to.

setDataLength

public void setDataLength(int data)
Sets the data in the following variable: This is the length of the data following the header if any.
Parameters:
data - int containing the data to set the variable to.

setData

public void setData(byte[] data)
Sets the data in the following variable: This is the actual data in the packet if any. Also sets the data length variable.
Parameters:
data - byte [] containing the data to set the variable to.

readPacket

public void readPacket(java.io.InputStream input)
                throws java.io.IOException,
                       java.net.ProtocolException
Reads in a packet from the specified input stream.
Parameters:
input - InputStream from which the packet will be read.
Throws:
java.net.ProtocolException. - This exception contains the error in the packet.
java.io.IOException. - This exception contains the error in the IO stream.

sendPacket

public void sendPacket(java.io.OutputStream output)
                throws java.io.IOException,
                       java.net.ProtocolException
Writes a packet to the specified output stream.
Parameters:
output - OutputStream to which the packet will be written.
Throws:
java.net.ProtocolException. - This exception contains the error in the packet.
java.io.IOException. - This exception contains the error in the IO stream.

toString

public java.lang.String toString()
This function returns the String form of the data.
Overrides:
toString in class java.lang.Object
Returns:
String containing the string version of this class.

intToBytes

public static void intToBytes(int value,
                              byte[] data,
                              int offset)

bytesToInt

public static int bytesToInt(byte[] value,
                             int offset)

bytesToShort

public static short bytesToShort(byte[] data,
                                 int offset)

shortToBytes

public static void shortToBytes(short value,
                                byte[] data,
                                int offset)

shortToBytes

public static byte[] shortToBytes(short value)

setEvent

public void setEvent(boolean eventFlag)
Sets the event flag in the packet.
Parameters:
eventFlag - boolean true if this packet is an event.

isEvent

public boolean isEvent()
Returns true if this packet is an event.
Returns:
boolean true if this packet is an event.

setIncomplete

public void setIncomplete(boolean incompleteFlag)
Sets the incomplete packet sequence flag in the packet.
Parameters:
incompleteFlag - boolean true if this packet is an incomplete sequence.

isIncomplete

public boolean isIncomplete()
Returns true if this packet is part of an incomplete packet sequence.
Returns:
boolean true if this packet sequence is currently incomplete.

getHeader

public byte[] getHeader()
Returns the header array.
Returns:
byte [] containing the header.

compareTo

public boolean compareTo(byte[] otherHeader)
This function compares the packets header to the input byte array.
Parameters:
otherHeader - byte [] containing the other packet header to match.
Returns:
boolean true if the headers match.

headerOnlyComparesTo

public boolean headerOnlyComparesTo(byte[] otherHeader)
This function compares the packets header to the input byte array. It ignores the data and the data length.
Parameters:
otherHeader - byte [] containing the other packet header to match.
Returns:
boolean true if the headers match.


Copyright © 2002 Clearfield Research Ltd. All Rights Reserved.