sg.edu.nus.peer.request
Class UDPReceiver

java.lang.Object
  extended by sg.edu.nus.peer.request.UDPReceiver
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
BootstrapPnPReceiver, ClientPnPReceiver, ServerPnPReceiver

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

The UDPReceiver is responsible for listening and receiving UDP packets from remote sites by wrapping DatagramSocket.

All concrete receivers should extend the UDPReceiver with a set of UDPHandler, each of which is responsible for processing the received UDP packets.

To handle UDP packets, a callback pattern is used by UDPHandler. To pass UDPListeners to UDPHandler, the UDPReceiver provides the methods addUDPListener(UDPListener) and removeUDPListener(UDPListener) to register and remove a UDPListener to its UDPHandlers.

Version:
1.0 2006-9-22
Author:
Xu Linhao
See Also:
BootstrapPnPReceiver, UDPSender

Constructor Summary
UDPReceiver(AbstractMainFrame gui, java.net.DatagramSocket ds, int maxConn)
          Construct the UDPReceiver with specified DatagramSocket and the maximum number of UDP packets that can be handled simultaneously.
 
Method Summary
 void addUDPListener(UDPListener l)
          Add an UDPListener to UDPHandler.
 java.net.DatagramSocket getDatagramSocket()
          Returns the instance of the DatagramSocket.
 int getLocalPort()
          Returns the port used for running DatagramSocket.
 boolean isAlive()
          Returns true if the UDPReceiver is still alive.
 void removeUDPListener(UDPListener l)
          Remove an UDPListener to UDPHandler.
 void run()
          Setup all UDPHandlers and then be prepared for receiving the incoming UDP packets.
abstract  void setupHandlers()
          Initiate a set of UDPHandlers and the maximum number of handlers is specified by the parameter maxConn.
abstract  void stop()
          Stop all UDPHandlers and do necessary clearup job.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UDPReceiver

public UDPReceiver(AbstractMainFrame gui,
                   java.net.DatagramSocket ds,
                   int maxConn)
            throws java.net.SocketException
Construct the UDPReceiver with specified DatagramSocket and the maximum number of UDP packets that can be handled simultaneously.

Parameters:
gui - the main frame
ds - the instance of a DatagramSocket
maxConn - the maximum number of UDP packets that can be handled simultaneously
Throws:
java.net.SocketException
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

getDatagramSocket

public java.net.DatagramSocket getDatagramSocket()
Returns the instance of the DatagramSocket.

Returns:
the instance of the DatagramSocket

getLocalPort

public int getLocalPort()
Returns the port used for running DatagramSocket.

Returns:
the port used for running DatagramSocket

setupHandlers

public abstract void setupHandlers()
Initiate a set of UDPHandlers and the maximum number of handlers is specified by the parameter maxConn.


isAlive

public boolean isAlive()
Returns true if the UDPReceiver is still alive.

Returns:
true if alive; otherwise, return false

stop

public abstract void stop()
Stop all UDPHandlers and do necessary clearup job.


run

public void run()
Setup all UDPHandlers and then be prepared for receiving the incoming UDP packets.

Specified by:
run in interface java.lang.Runnable