j_waste.network
Class HeadedPacket

java.lang.Object
  extended byj_waste.network.Packet
      extended byj_waste.network.HeadedPacket
All Implemented Interfaces:
NetworkConstants
Direct Known Subclasses:
ChatPacket, ChatReplyPacket, FileRequestPacket, FileRequestReplyPacket, LocalCapsPacket, PingPacket, SearchPacket, SearchReplyPacket

public class HeadedPacket
extends Packet
implements NetworkConstants

This class is the base class for headed packets used in J-WASTE.

Version:
1.1
Author:
Jan Lindblom (cl0wn@users.sourceforge.net)

Field Summary
protected  boolean iscomplete
           
protected  java.nio.ByteBuffer packetData
           
protected  int padBytes
           
 
Fields inherited from class j_waste.network.Packet
packet
 
Fields inherited from interface j_waste.network.NetworkConstants
ABORT, CAP_BANDWIDTH, CAP_REMOTE_IP, CAP_SATURATION, GENERAL_TYPE_BROADCAST, GENERAL_TYPE_LOCAL, GENERAL_TYPE_ROUTED, HEADER_SIZE, MAX_BROADCAST_SIZE, MAX_ROUTED_SIZE, NO_ABORT, PREMATURE_ABORT, PRIO_CHAT, PRIO_CHAT_REPLY, PRIO_FILE_REQ_REPLY, PRIO_FILE_REQUEST, PRIO_KEYDIST, PRIO_KEYDIST_REPLY, PRIO_LCAPS, PRIO_LOCAL_SATURATE, PRIO_PING, PRIO_SEARCH, PRIO_SEARCH_REPLY, PRIO_SEARCH_USERLIST, PRIO_UPLOAD, TYPE_CHAT, TYPE_CHAT_REPLY, TYPE_FILE_REQ_REPLY, TYPE_FILE_REQUEST, TYPE_KEYDIST, TYPE_KEYDIST_REPLY, TYPE_LCAPS, TYPE_LOCAL_SATURATE, TYPE_PING, TYPE_SEARCH, TYPE_SEARCH_REPLY, TYPE_SEARCH_USERLIST, TYPE_UPLOAD
 
Constructor Summary
HeadedPacket(byte[] pkg)
          Constructs a HeadedPacket given a byte array representation of the Packet.
HeadedPacket(int type, byte[] guid)
          Constructs a HeadedPacket of the type specified and with the specified GUID.
 
Method Summary
static HeadedPacket createPacket(byte[] pkg)
          Creates a HeadedPacket of the correct subtype as defined in the header embedded in the byte array.
 void finalizePacket()
          Finalize the packet.
 byte[] getData()
          Returns the data stored in this packet, null if empty.
 int getGeneralType()
          Returns the general type of this headed packet.
 byte[] getGUID()
          Returns the GUID of this HeadedPacket.
 byte[] getHeader()
          Returns the header of this HeadedPacket, null if incomplete.
 int getIntPrio()
          Returns the Priority of this HeadedPacket as an integer.
 int getIntType()
          Returns the type if this HeadedPacket as an integer.
 int getLength()
          Returns the length of this HeadedPacket.
 byte[] getMD5()
          Returns the MD5 checksum of this HeadedPacket.
 byte getPrio()
          Returns the Priority of this HeadedPacket.
 int getTTL()
          Returns the TTL of this HeadedPacket.
 byte[] getType()
          Returns the type of this HeadedPacket.
protected  void setLength(int l)
          Sets the length of this HeadedPacket.
protected  void setTTL(int ttl)
          Sets the TTL of this HeadedPacket.
 byte[] toByteArray()
          Returns a byte array representation of this HeadedPacket.
 
Methods inherited from class j_waste.network.Packet
padCD, reverseBytes, reverseInt, reverseShort, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

packetData

protected java.nio.ByteBuffer packetData

iscomplete

protected boolean iscomplete

padBytes

protected int padBytes
Constructor Detail

HeadedPacket

public HeadedPacket(int type,
                    byte[] guid)
Constructs a HeadedPacket of the type specified and with the specified GUID.

Parameters:
type - the type of this HeadedPacket.
guid - the GUID of this HeadedPacket.

HeadedPacket

public HeadedPacket(byte[] pkg)
             throws InvalidPacketException
Constructs a HeadedPacket given a byte array representation of the Packet.

Parameters:
pkg - the Packet.
Throws:
InvalidPacketException - upon failure to create the HeadedPacket.
Method Detail

createPacket

public static HeadedPacket createPacket(byte[] pkg)
                                 throws InvalidPacketException
Creates a HeadedPacket of the correct subtype as defined in the header embedded in the byte array.

Parameters:
pkg - the byte array representation of the HeadedPacket.
Returns:
a headed packet of the correct subtype. Return value should be compared to the desired type using instanceof.
Throws:
InvalidPacketException - upon failure to create the packet.

setTTL

protected void setTTL(int ttl)
Sets the TTL of this HeadedPacket.

Parameters:
ttl - the TTL of this HeadedPacket.

getTTL

public int getTTL()
Returns the TTL of this HeadedPacket.

Returns:
the TTL of this HeadedPacket.

setLength

protected void setLength(int l)
Sets the length of this HeadedPacket.

Parameters:
l - the length of this HeadedPacket.

getLength

public int getLength()
Returns the length of this HeadedPacket.

Returns:
the length of this HeadedPacket.

getType

public byte[] getType()
Returns the type of this HeadedPacket.

Returns:
the type of this HeadedPacket.

getIntType

public int getIntType()
Returns the type if this HeadedPacket as an integer.

Returns:
the integer type of this HeadedPacket.

getPrio

public byte getPrio()
Returns the Priority of this HeadedPacket.

Returns:
the priority of this HeadedPacket.

getIntPrio

public int getIntPrio()
Returns the Priority of this HeadedPacket as an integer.

Returns:
the integer priority of this HeadedPacket.

getHeader

public byte[] getHeader()
Returns the header of this HeadedPacket, null if incomplete.

Returns:
the header of this HeadedPacket.

getGUID

public byte[] getGUID()
Returns the GUID of this HeadedPacket.

Returns:
the GUID of this HeadedPacket.

getMD5

public byte[] getMD5()
Returns the MD5 checksum of this HeadedPacket.

Returns:
the MD5 checksum of this HeadedPacket.

finalizePacket

public void finalizePacket()
Finalize the packet. This method finalizes the packet by creating a byte array with the Packet and calculates the MD5 checksum.

Specified by:
finalizePacket in class Packet

getData

public byte[] getData()
Returns the data stored in this packet, null if empty.

Returns:
the data stored in this packet.

toByteArray

public byte[] toByteArray()
Returns a byte array representation of this HeadedPacket.

Specified by:
toByteArray in class Packet
Returns:
a byte array representation of this HeadedPacket.

getGeneralType

public int getGeneralType()
Returns the general type of this headed packet. General type can be one of the following: Broadcasted, Local or Routed.

Returns:
the general packet type if packet is finalized, -1 if not.