sg.edu.nus.peer.request
Class UDPServer

java.lang.Object
  extended by sg.edu.nus.peer.request.UDPServer
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
BootstrapPnPServer, ServerPnPServer

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

The UDPServer is composed of a UDPSender and a UDPReceiver, where the sender is responsible for sending UDP packets to remote peers and the receiver for receiving UDP packets from remote peers.

Each concrete UDPServer should extend this class by using customized UDPSender, UDPReceiver and UDPHandler.

The following example demonstrate how to use UDPSender and UDPReceiver:

 public void run()
 {
        scheduler = new Timer("UDP Sender");
        sender = new UDPSender();
        scheduler.scheduleAtFixedRate(sender, 100, 10000); // 10 seconds
        receiver = new UDPReceiver(sender.getSocket(), 10);
        Thread receiverThread = new Thread("UDP Receiver", receiver);
        receiverThread.start();
 }
 

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

Constructor Summary
UDPServer(AbstractMainFrame gui, int port, int maxConn, long period)
          Construct UDPServer by specifying the maximum number of UDP packets that can be handled simultaneously.
 
Method Summary
 java.net.DatagramSocket getDatagramSocket()
          Returns the datagram socket.
 int getLocalPort()
          Returns the port used for running DatagramSocket.
 boolean isAlive()
          Returns true if UDPServer is alive by checking the status of both UDPReceiver and UDPSender.
 void scheduleUDPSender(long period)
          Reschedule the UDP Sender with a new time interval.
 void stop()
          Stop both UDPSender and UDPReceiver.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.lang.Runnable
run
 

Constructor Detail

UDPServer

public UDPServer(AbstractMainFrame gui,
                 int port,
                 int maxConn,
                 long period)
Construct UDPServer by specifying the maximum number of UDP packets that can be handled simultaneously.

Parameters:
gui - the main frame that has the control of UDPServer
port - the port used for initializing UDPsender and UDPReceiver
maxConn - the maximum number of UDP packets that can be handled simultaneously
period - the sitting up time used for initiating next round of UDP packets dissemination
Method Detail

getDatagramSocket

public java.net.DatagramSocket getDatagramSocket()
Returns the datagram socket.

Returns:
the datagram socket

getLocalPort

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

Returns:
if the instance of UDPSender is not null and alive, return the port used for running DatagramSocket; otherwise, return -1

isAlive

public boolean isAlive()
Returns true if UDPServer is alive by checking the status of both UDPReceiver and UDPSender.

Returns:
true if alive; otherwise, return false

scheduleUDPSender

public void scheduleUDPSender(long period)
Reschedule the UDP Sender with a new time interval.

Parameters:
period - the new delay

stop

public void stop()
Stop both UDPSender and UDPReceiver.