Class Packet

java.lang.Object
  extended by Packet

public class Packet
extends java.lang.Object

   
 Packet defines the Fishnet packet headers and some constants.
 


Field Summary
static int BROADCAST_ADDRESS
           
static int HEADER_SIZE
           
static int MAX_ADDRESS
           
static int MAX_PACKET_SIZE
           
static int MAX_PAYLOAD_SIZE
           
static int MAX_TTL
           
 
Constructor Summary
Packet(int dest, int src, int ttl, int protocol, int seq, byte[] payload)
          Constructing a new packet.
 
Method Summary
 int getDest()
           
 byte[] getPayload()
           
 int getProtocol()
           
 int getSeq()
           
 int getSrc()
           
 int getTTL()
           
 boolean isValid()
          Tests if this Packet is valid or not
 boolean isValidToSend()
          Tests if this Packet is valid to send A valid packet may have TTL = 0, but a "valid to send" packet cannot have TTL = 0
 byte[] pack()
          Convert the Packet object into a byte array for sending over the wire.
 void setTTL(int ttl)
          Sets the TTL of this packet
 java.lang.String toString()
          Provides a string representation of the packet.
static Packet unpack(byte[] packedPacket)
          Unpacks a byte array to create a Packet object Assumes the array has been formatted using pack method in Packet
static boolean validAddress(int addr)
          Tests if the address is a valid one
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

BROADCAST_ADDRESS

public static final int BROADCAST_ADDRESS
See Also:
Constant Field Values

MAX_ADDRESS

public static final int MAX_ADDRESS
See Also:
Constant Field Values

HEADER_SIZE

public static final int HEADER_SIZE
See Also:
Constant Field Values

MAX_PACKET_SIZE

public static final int MAX_PACKET_SIZE
See Also:
Constant Field Values

MAX_PAYLOAD_SIZE

public static final int MAX_PAYLOAD_SIZE
See Also:
Constant Field Values

MAX_TTL

public static final int MAX_TTL
See Also:
Constant Field Values
Constructor Detail

Packet

public Packet(int dest,
              int src,
              int ttl,
              int protocol,
              int seq,
              byte[] payload)
       throws java.lang.IllegalArgumentException
Constructing a new packet.

Parameters:
dest - The destination fishnet address.
src - The source fishnet address.
ttl - The time-to-live value for this packet.
protocol - What type of packet this is.
seq - The sequence number of the packet.
payload - The payload of the packet.
Throws:
java.lang.IllegalArgumentException - If the given arguments are invalid
Method Detail

toString

public java.lang.String toString()
Provides a string representation of the packet.

Overrides:
toString in class java.lang.Object
Returns:
A string representation of the packet.

getDest

public int getDest()
Returns:
The address of the destination node

getSrc

public int getSrc()
Returns:
The address of the src node

getTTL

public int getTTL()
Returns:
The TTL of the packet

setTTL

public void setTTL(int ttl)
Sets the TTL of this packet

Parameters:
ttl - TTL to set

getProtocol

public int getProtocol()
Returns:
The protocol used for this packet

getSeq

public int getSeq()
Returns:
The sequence number of this packet

getPayload

public byte[] getPayload()
Returns:
The payload of this packet

pack

public byte[] pack()
Convert the Packet object into a byte array for sending over the wire. Format: destination address: 1 byte source address: 1 byte ttl (time to live): 1 byte protocol: 1 byte packet length: 1 byte packet sequence num: 4 bytes payload: <= MAX_PAYLOAD_SIZE bytes

Returns:
A byte[] for transporting over the wire. Null if failed to pack for some reason

unpack

public static Packet unpack(byte[] packedPacket)
Unpacks a byte array to create a Packet object Assumes the array has been formatted using pack method in Packet

Parameters:
packedPacket - String representation of the packet
Returns:
Packet object created or null if the byte[] representation was corrupted

validAddress

public static boolean validAddress(int addr)
Tests if the address is a valid one

Parameters:
addr - Address to check
Returns:
True is address is valid, else false

isValid

public boolean isValid()
Tests if this Packet is valid or not

Returns:
True if packet is valid, else false

isValidToSend

public boolean isValidToSend()
Tests if this Packet is valid to send A valid packet may have TTL = 0, but a "valid to send" packet cannot have TTL = 0

Returns:
Trie if the packet is valid to send, else false