sg.edu.nus.peer.request
Class UDPHandler

java.lang.Object
  extended by sg.edu.nus.peer.request.UDPHandler
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
BootstrapPnPHandler, ClientPnPHandler, ServerPnPHandler

public abstract class UDPHandler
extends java.lang.Object
implements java.lang.Runnable

The UDPHandler is responsible for processing UDP packets.

All UDPHandlers will be blocked when there is no UDP packet in the pool; and will be woke up when any UDP request comes in and is inserted into the pool. To stop UDPHandler just set the stop signal as true.

The UDPReceiver is responsible for listening UDP requests from remote users and then passing the UDP packets to the UDPHandler.

To process the UDP packets, UDPListener should be registered to each UDPHandler by invoking the method addUDPListener(UDPListener). The UDPListener defines the callback interface that is used for triggering a proper actions when a desired UDP packet is received. The method handlePacket() is responsible for invoking the callback method of the UDPListener.

To define customized UDPHandler, just create your own handler by extending this class and implement the method handlePacket().

An example to show how to use UDPListener:

 public void handlePacket()
 {
        Object[] list;
        synchronized (this)
        {
                list = listeners.toArray().clone();
        }
        UDPListener listener;
        for (int i = 0; i < list.length; i++)
        {
                listener = (UDPListener) list[i];
                ...
        }
 }
 

Version:
1.0 2006-9-22
Author:
Xu Linhao
See Also:
UDPReceiver, BootstrapPnPHandler

Constructor Summary
UDPHandler(java.net.DatagramSocket ds)
          Construct the UDPHandler.
 
Method Summary
 void addUDPListener(UDPListener l)
          Add an UDPListener to UDPHandler.
static void processRequest(java.net.DatagramPacket request)
          Accepting the UDP packet to be handled from the UDPReceiver and then put the packet into a pool for further processing.
 void removeUDPListener(UDPListener l)
          Remove an UDPListener to UDPHandler.
 void run()
          Start the UDPHandler and wait for any incoming UDP packet.
 void stop()
          Stop the handler by setting the flag as false
static void stopAllHandlers()
          Clear the UDP packets in the pool and notify all UDPHandlers.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UDPHandler

public UDPHandler(java.net.DatagramSocket ds)
Construct the UDPHandler.

Method Detail

addUDPListener

public void addUDPListener(UDPListener l)
Add an UDPListener to UDPHandler.

Parameters:
l - the UDPListener that is responsible for processing UDP message

removeUDPListener

public void removeUDPListener(UDPListener l)
Remove an UDPListener to UDPHandler.

Parameters:
l - the UDPListener that is responsible for processing UDP message

processRequest

public static void processRequest(java.net.DatagramPacket request)
Accepting the UDP packet to be handled from the UDPReceiver and then put the packet into a pool for further processing.

Parameters:
request - the incoming UDP packet to be processed

stopAllHandlers

public static void stopAllHandlers()
Clear the UDP packets in the pool and notify all UDPHandlers.


stop

public void stop()
Stop the handler by setting the flag as false

run

public void run()
Start the UDPHandler and wait for any incoming UDP packet.

Specified by:
run in interface java.lang.Runnable