sg.edu.nus.peer.request
Class UDPPacket

java.lang.Object
  extended by sg.edu.nus.peer.request.UDPPacket

public final class UDPPacket
extends java.lang.Object

This class is used to generate various UDP packets used by UDPSender and UDPReceiver.

Version:
1.0 2006-10-11
Author:
Xu Linhao

Constructor Summary
UDPPacket()
           
 
Method Summary
static PeerInfo parsePong(byte[] buf, int start)
          Parse the content of the packet whose type is PONG and wrap all parsed data into a PeerInfo instance.
static PeerInfo parseTroubleshoot(byte[] buf, int start)
          Parse the content of the packet whose type is TROUBLESHOOT and wrap all parsed data into a PeerInfo instance.
static int parseType(byte[] buf, int start)
          Returns the message type of the packet.
static java.net.DatagramPacket ping()
          Returns a ping packet that only includes a 4-bytes message type without specifying the IP address and port of the destination peer.
static java.net.DatagramPacket ping(java.net.InetAddress ip, int port)
          Returns a ping packet that only includes a 4-bytes message type with specified IP address and port of the destination peer.
static java.net.DatagramPacket pong(PeerType type, java.lang.String pid)
          Returns a pong packet that includes a 4-bytes message type, the peer type and the peer identifier, without specifying the IP address and port of the destination peer.
static java.net.DatagramPacket pong(PeerType type, java.lang.String pid, java.net.InetAddress ip, int port)
          Returns a pong packet that includes a 4-bytes message type, the peer type and the peer identifier, with specified IP address and port of the destination peer.
static java.net.DatagramPacket troubleshoot(PeerType type, java.lang.String pid)
          Returns a troubleshoot packet to indicate the peer who sends the packet will leave the system due to hardware or system errors.
static java.net.DatagramPacket troubleshoot(PeerType type, java.lang.String pid, java.net.InetAddress ip, int port)
          Returns a troubleshoot packet to indicate the peer who sends the packet will leave the system due to hardware or system errors.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UDPPacket

public UDPPacket()
Method Detail

ping

public static java.net.DatagramPacket ping()
Returns a ping packet that only includes a 4-bytes message type without specifying the IP address and port of the destination peer.

The packet format is:

 0-3: the int value of PING  
 

Returns:
a DatagramPacket with ping message type

ping

public static java.net.DatagramPacket ping(java.net.InetAddress ip,
                                           int port)
Returns a ping packet that only includes a 4-bytes message type with specified IP address and port of the destination peer.

The packet format is:

 0-3: the int value of PING  
 

Parameters:
ip - the IP address of the destination peer
port - the port that the datagram socket is running
Returns:
a DatagramPacket with ping message type

pong

public static java.net.DatagramPacket pong(PeerType type,
                                           java.lang.String pid)
Returns a pong packet that includes a 4-bytes message type, the peer type and the peer identifier, without specifying the IP address and port of the destination peer.

The packet format is:

 0-3: the int value of PING;
 4-7: the number of bytes used for peer type, denoted as "tsize"
 8-(tsize+7): the bytes array representing peer type 
 (tsize+8)-(tsize+11): the number of bytes used for peer identifier, denoted as "psize"
 (tsize+12)-(tsize+psize+11): the bytes array representing peer identifier   
 

Parameters:
type - the peer type
pid - the peer identifier
Returns:
a DatagramPacket with pong message type and data object

pong

public static java.net.DatagramPacket pong(PeerType type,
                                           java.lang.String pid,
                                           java.net.InetAddress ip,
                                           int port)
Returns a pong packet that includes a 4-bytes message type, the peer type and the peer identifier, with specified IP address and port of the destination peer.

The packet format is:

 0-3: the int value of PING;
 4-7: the number of bytes used for peer type, denoted as "tsize"
 8-(tsize+7): the bytes array representing peer type 
 (tsize+8)-(tsize+11): the number of bytes used for peer identifier, denoted as "psize"
 (tsize+12)-(tsize+psize+11): the bytes array representing peer identifier   
 

Parameters:
type - the peer type
pid - the peer identifier
ip - the IP address of the destination peer
port - the port that the datagram socket is running
Returns:
a DatagramPacket with pong message type and data object

troubleshoot

public static java.net.DatagramPacket troubleshoot(PeerType type,
                                                   java.lang.String pid)
Returns a troubleshoot packet to indicate the peer who sends the packet will leave the system due to hardware or system errors.

The packet format is:

 0-3: the int value of TROUBLESHOOT
 4-7: the number of bytes used for peer type, denoted as "tsize"
 8-(tsize+7): the bytes array representing peer type 
 (tsize+8)-(tsize+11): the number of bytes used for peer identifier, denoted as "psize"
 (tsize+12)-(tsize+psize+11): the bytes array representing peer identifier   
 

Parameters:
type - the peer type
pid - the peer identifier
Returns:
a DatagramPacket with troubleshoot message type

troubleshoot

public static java.net.DatagramPacket troubleshoot(PeerType type,
                                                   java.lang.String pid,
                                                   java.net.InetAddress ip,
                                                   int port)
Returns a troubleshoot packet to indicate the peer who sends the packet will leave the system due to hardware or system errors.

The packet format is:

 0-3: the int value of TROUBLESHOOT
 4-7: the number of bytes used for peer type, denoted as "tsize"
 8-(tsize+7): the bytes array representing peer type 
 (tsize+8)-(tsize+11): the number of bytes used for peer identifier, denoted as "psize"
 (tsize+12)-(tsize+psize+11): the bytes array representing peer identifier   
 

Parameters:
type - the peer type
pid - the peer identifier
ip - the IP address of the destination peer
port - the port that the datagram socket is running
Returns:
a DatagramPacket with troubleshoot message type

parsePong

public static PeerInfo parsePong(byte[] buf,
                                 int start)
Parse the content of the packet whose type is PONG and wrap all parsed data into a PeerInfo instance.

Parameters:
buf - the byte array of the packet content
start - the start position of the byte array to be parsed
Returns:
a PeerInfo instance
See Also:
pong(PeerType, String)

parseTroubleshoot

public static PeerInfo parseTroubleshoot(byte[] buf,
                                         int start)
Parse the content of the packet whose type is TROUBLESHOOT and wrap all parsed data into a PeerInfo instance.

Parameters:
buf - the byte array of the packet content
start - the start position of the byte array to be parsed
Returns:
a PeerInfo instance
See Also:
troubleshoot(PeerType, String)

parseType

public static int parseType(byte[] buf,
                            int start)
Returns the message type of the packet.

Parameters:
buf - the byte array to be parsed
start - the start position of the byte array to be parsed
Returns:
the message type of the packet