sg.edu.nus.peer
Class AbstractPeer

java.lang.Object
  extended by sg.edu.nus.peer.AbstractPeer
Direct Known Subclasses:
Bootstrap, ServerPeer

public abstract class AbstractPeer
extends java.lang.Object

An abstract class that defines all necessary interfaces for communicating between peers.

The philosophy is twofolds: (1) wrap all necessary functionalities that has no relationship with GUI in a separated class; (2) provide the interfaces to construct a peer in a simulation environment.

Version:
1.0 2006-1-27
Author:
Xu Linhao

Field Summary
static int CAPACITY
          The maximal number of threads used for handling network events simultaneously.
static int LOCAL_SERVER_PORT
          The local port used for monitoring network events, i.e., tcp and udp services will be initialized on it at the same time.
 
Constructor Summary
AbstractPeer(AbstractMainFrame gui, java.lang.String peerType)
          Construct an empty peer with PeerType only.
 
Method Summary
static boolean checkInet(java.lang.String ip)
          Check if there exists an IP address in the BOOTSTRAP_SERVER_LIST whose value is equal to the current IP address of the peer.
 void clearSession()
          Clear the user's session.
 void createLogStore()
          Create the directory for storing system logs.
abstract  void forceOut(java.lang.String ip, int port)
          Force a peer out the network with its IP address and port.
abstract  java.lang.String getLogStore()
          Returns the directory of the system logs.
 AbstractMainFrame getMainFrame()
          Get the handler of the AbstractMainFrame.
 java.lang.String getPeerType()
          Get the peer type.
 PhysicalInfo getPhysicalInfo()
          Get the instance of PhysicalInfo, which includes the physical information of the IP address and the port used for monitoring network events.
 void initSession(java.lang.String user, java.lang.String pwd)
          Initialize the user's session.
 boolean isEventManagerAlive()
          Determine whether the instance of EventManager is null.
 boolean isUDPServerAlive()
          Returns true if the server is alive.
 void scheduleUDPSender(long period)
          Re-schedule the UDP Sender with a new time interval.
 void sendMessage(PhysicalInfo dest, Message message)
          Send a message to a peer.
abstract  boolean startEventManager(int port, int capacity)
          Start the network monitoring service.
abstract  boolean startUDPServer(int port, int capacity, long period)
          Starts the UDP service by specify the port and the number of threads that can handle incoming and outgoing UDP packets at the same time.
 boolean stopEventManager()
          Stop the network monitoring server.
 boolean stopUDPServer()
          Stops the UDP service.
abstract  void troubleshoot(boolean toBoot, boolean toServer, boolean toClient)
          When the peer exits the system for some reason, it will broadcast a TROUBLESHOOT message to all relevant peers for its leave.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCAL_SERVER_PORT

public static int LOCAL_SERVER_PORT
The local port used for monitoring network events, i.e., tcp and udp services will be initialized on it at the same time.


CAPACITY

public static int CAPACITY
The maximal number of threads used for handling network events simultaneously.

Constructor Detail

AbstractPeer

public AbstractPeer(AbstractMainFrame gui,
                    java.lang.String peerType)
Construct an empty peer with PeerType only.

Parameters:
peerType - the type of the peer
Method Detail

getMainFrame

public AbstractMainFrame getMainFrame()
Get the handler of the AbstractMainFrame.

Returns:
the handler of the AbstractMainFrame

getPeerType

public java.lang.String getPeerType()
Get the peer type.

Returns:
the type of the peer

isEventManagerAlive

public boolean isEventManagerAlive()
Determine whether the instance of EventManager is null.

Returns:
if null, return false; otherwise, return true

startEventManager

public abstract boolean startEventManager(int port,
                                          int capacity)
Start the network monitoring service.

Parameters:
port - the port used for monitoring network events
capacity - the maximum capacity that serves the incoming socket connections at the same time
Returns:
if success, return true; otherwise, return false

stopEventManager

public boolean stopEventManager()
Stop the network monitoring server.

Returns:
if success, return true; otherwise, return false

isUDPServerAlive

public boolean isUDPServerAlive()
Returns true if the server is alive.

Returns:
true if alive; otherwise, return false

startUDPServer

public abstract boolean startUDPServer(int port,
                                       int capacity,
                                       long period)
Starts the UDP service by specify the port and the number of threads that can handle incoming and outgoing UDP packets at the same time.

Parameters:
port - the port to be used for starting the UDP service
capacity - the number of threads that can handle incoming and outgoing UDP packets at the same time
Returns:
true if the UDP service is started; otherwise, return false

scheduleUDPSender

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

Parameters:
period - the new period for disseminating UDP packets

stopUDPServer

public boolean stopUDPServer()
Stops the UDP service.

Returns:
true if the UDP service is stoped; otherwise, return false

forceOut

public abstract void forceOut(java.lang.String ip,
                              int port)
Force a peer out the network with its IP address and port.

Parameters:
ip - the IP address of the peer to be forced out the network
port - the port where the network service is running

troubleshoot

public abstract void troubleshoot(boolean toBoot,
                                  boolean toServer,
                                  boolean toClient)
When the peer exits the system for some reason, it will broadcast a TROUBLESHOOT message to all relevant peers for its leave.

Parameters:
toBoot - if true, send messages to bootstrapper
toServer - if true, send messages to server peers
toClient - if true, send messages to client peers

checkInet

public static boolean checkInet(java.lang.String ip)
Check if there exists an IP address in the BOOTSTRAP_SERVER_LIST whose value is equal to the current IP address of the peer.

Parameters:
ip - the IP address list
Returns:
if the current IP address is in the BOOTSTRAP_SERVER_LIST, return true; otherwise, return false

getPhysicalInfo

public PhysicalInfo getPhysicalInfo()
                             throws java.net.UnknownHostException
Get the instance of PhysicalInfo, which includes the physical information of the IP address and the port used for monitoring network events.

Returns:
an instance of PhysicalInfo
Throws:
java.net.UnknownHostException - if cannot obtain a valid IP address

sendMessage

public void sendMessage(PhysicalInfo dest,
                        Message message)
                 throws java.net.UnknownHostException,
                        java.io.IOException
Send a message to a peer.

Parameters:
dest - the peer to which the message will be sent
message - the message to be sent
Throws:
UnknownHostException, - IOException
java.net.UnknownHostException
java.io.IOException

initSession

public void initSession(java.lang.String user,
                        java.lang.String pwd)
                 throws java.lang.RuntimeException
Initialize the user's session.

Parameters:
user - the user to be sign in the system
pwd - the password
Throws:
java.lang.RuntimeException

clearSession

public void clearSession()
Clear the user's session.


getLogStore

public abstract java.lang.String getLogStore()
Returns the directory of the system logs.

Returns:
returns the directory of the system logs

createLogStore

public void createLogStore()
Create the directory for storing system logs. If the directory cannot be created, then exit system.