mcast
Class MCastPacket

java.lang.Object
  extended by mcast.MCastPacket
Direct Known Subclasses:
TOMCastPacket

public class MCastPacket
extends java.lang.Object

An MCastPacket is a:

   |-------------------------------------------------------------------|
   |  pkt type    |                user payload                        |
   |   (header)   |                                                    |
   |-------------------------------------------------------------------|
 
The pkt type is an int (4 bytes).

An MCastPacket object contains a DatagramPacket object (which is what is actually carried across the network). The MCastPacket's function is to provide a packet type: see the defined constants below. The DatagramPacket is used to manipulate addresses.

Version:
$Id: MCastPacket.java,v 1.1.1.1 2009/01/13 03:43:28 zahorjan Exp $

Field Summary
protected  byte[] buf
           
static int CLIENT_CLIENT_GOODBYE
          Packet type Sent to all clients in the mcast group when one is done sending and wants to leave the group.
static int CLIENT_MCAST
          Packet type A normal client multicast msg, presumably carrying some data payload.
static int CLIENT_SERVER_HELLO
          Packet type part of the join protocol; the client tells lobby it's here, and waits to hear back before allowing the user thread to continue.
static int HEADER_SIZE
           
static int MAX_PACKET_SIZE
           
static int MAX_PAYLOAD_SIZE
           
private  java.net.DatagramPacket myDGPacket
           
static int SERVER_CLIENT_HELLO
          Packet type Once server has received HELLO msgs from all the clients it is expecting, it replies to each with one of these.
static int SOCKET_CLOSED
          Packet type A pseudo-type used by socket implementations to indicate that this is the last packet that can be received, ever.
 
Constructor Summary
MCastPacket()
          By default, create a packet with type suitable for client sending.
MCastPacket(java.net.DatagramPacket dgPkt)
          Constructor for incoming packets.
MCastPacket(int type)
          Constructor for outgoing packets.
 
Method Summary
 java.net.InetSocketAddress getAddress()
          Returns the address of the packet (useful when receiving a packet).
(package private)  byte[] getBuf()
          Gets a reference to the lowest level (datagram packet) buffer.
(package private)  java.net.DatagramPacket getDGPacket()
          Provides access to the contained DG packet, which is required to set or get packet addresses (or to directly manipulate packet lengths).
 byte[] getPayload()
          Returns the payload - everything past the MCastPacket header.
 int getPayloadLength()
          Returns the number of bytes in the packet past the MCastPacket header.
 int getType()
          Extracts the packet type from the header and returns it.
 void setAddress(java.net.InetSocketAddress addr)
          Sets the address of this packet (so useful when you're going to send the packet).
 void setPayload(byte[] payload)
          Sets the payload, as seen from the perspective of an MCastPacket.
protected  void setPayloadLength(int len)
          Sets the payload length (in the underlying DatagramPacket).
 void setType(int type)
          Set the MCastPacket type.
 java.lang.String toString()
          The obligatory toString.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CLIENT_SERVER_HELLO

public static final int CLIENT_SERVER_HELLO
Packet type part of the join protocol; the client tells lobby it's here, and waits to hear back before allowing the user thread to continue. Has no data payload.

See Also:
Constant Field Values

SERVER_CLIENT_HELLO

public static final int SERVER_CLIENT_HELLO
Packet type Once server has received HELLO msgs from all the clients it is expecting, it replies to each with one of these. Has a data payload that gives the addresses of all clients. (See the code...)

See Also:
Constant Field Values

CLIENT_MCAST

public static final int CLIENT_MCAST
Packet type A normal client multicast msg, presumably carrying some data payload.

See Also:
Constant Field Values

CLIENT_CLIENT_GOODBYE

public static final int CLIENT_CLIENT_GOODBYE
Packet type Sent to all clients in the mcast group when one is done sending and wants to leave the group. This helps each client understand when there will be no more mcast messages to receive.

See Also:
Constant Field Values

SOCKET_CLOSED

public static final int SOCKET_CLOSED
Packet type A pseudo-type used by socket implementations to indicate that this is the last packet that can be received, ever. Never actually sent over the network.

See Also:
Constant Field Values

MAX_PACKET_SIZE

public static final int MAX_PACKET_SIZE
See Also:
Constant Field Values

HEADER_SIZE

public static final int HEADER_SIZE
See Also:
Constant Field Values

MAX_PAYLOAD_SIZE

public static final int MAX_PAYLOAD_SIZE
See Also:
Constant Field Values

buf

protected byte[] buf

myDGPacket

private java.net.DatagramPacket myDGPacket
Constructor Detail

MCastPacket

public MCastPacket()
By default, create a packet with type suitable for client sending.


MCastPacket

public MCastPacket(int type)
Constructor for outgoing packets. Creates packet with MCast header, but no contents. Use this to create a packet for sending.

Parameters:
type - one of the defined constant MCastPacket types

MCastPacket

public MCastPacket(java.net.DatagramPacket dgPkt)
Constructor for incoming packets. Once a DatagramPacket has been read (from a DatagramSocket), use this constructor to provide an MCastPacket view of what was received.

Parameters:
dgPkt - the received datagram packet
Method Detail

getDGPacket

java.net.DatagramPacket getDGPacket()
Provides access to the contained DG packet, which is required to set or get packet addresses (or to directly manipulate packet lengths).


setType

public void setType(int type)
Set the MCastPacket type.

Parameters:
type - The MCastPacket defined constant packet type.

getType

public int getType()
Extracts the packet type from the header and returns it.


setPayload

public void setPayload(byte[] payload)
Sets the payload, as seen from the perspective of an MCastPacket. Sets the underlying DatagramPacket's length field as a side effect.


setPayloadLength

protected void setPayloadLength(int len)
Sets the payload length (in the underlying DatagramPacket).

Parameters:
len - payload length (excludes header)

getPayload

public byte[] getPayload()
Returns the payload - everything past the MCastPacket header.


getPayloadLength

public int getPayloadLength()
Returns the number of bytes in the packet past the MCastPacket header.


setAddress

public void setAddress(java.net.InetSocketAddress addr)
Sets the address of this packet (so useful when you're going to send the packet).


getAddress

public java.net.InetSocketAddress getAddress()
Returns the address of the packet (useful when receiving a packet).


getBuf

byte[] getBuf()
Gets a reference to the lowest level (datagram packet) buffer.


toString

public java.lang.String toString()
The obligatory toString.

Overrides:
toString in class java.lang.Object