uk.ac.ic.doc.automed.p2p
Class AutoMedMessagingHelper

java.lang.Object
  extended by uk.ac.ic.doc.automed.p2p.AutoMedMessagingHelper

public class AutoMedMessagingHelper
extends java.lang.Object


Field Summary
private static java.util.Map _clients
           
private  AutoMedPeer _mainPeer
           
private  AbstractServer _peerDaemon
           
private  TimedThread _peerDaemonThread
          The companion peer daemon attached to every peer for handling other peer's request
private static java.util.Map _servers
           
private  SimpleMessageHandler _simpleHandler
           
private static java.util.Map _threads
           
private static java.lang.String TEMP_CLIENT
           
 
Constructor Summary
AutoMedMessagingHelper(AutoMedPeer mainPeer)
          Creates a new instance of AutoMedMessagingHelper
 
Method Summary
 void clear()
           
static TimedThread createDaemon(java.lang.String id, java.lang.String protocol, int port, RequestListener requestHandler, boolean reuse)
          Initiates a network daemon for an AutoMedPeer to listen to other peers' requests
.
 void doCustomRequest(java.lang.String requestType, java.lang.String protocol, java.lang.String remoteAddress, int remotePort, int sleepTime, boolean asynchronous)
          Create a custom keep-alive messaging helper for an AutoMedPeer.
 void doRequest(java.lang.String requestType)
          Utility method for performing specific P2P requests
See Constants for a list of OPT_* request types
static void doRequest(java.lang.String id, java.lang.String type, AbstractMessage requestMessage, java.lang.String protocol, java.lang.String remoteAddress, int remotePort, RequestListener responseHandler, int sleepTime, boolean reuse, boolean asynchronous, boolean ackExpected)
          Perform a network request on behalf of the client.
static void doRequest(java.lang.String id, java.lang.String type, AbstractMessage requestMessage, java.lang.String protocol, java.lang.String remoteAddress, int remotePort, RequestListener responseHandler, int sleepTime, boolean reuse, boolean asynchronous, boolean ackExpected, java.util.Properties props)
          Perform a network request on behalf of the client.
 AbstractMessage doSimpleAnonymousRequest(AbstractMessage requestMessage, PeerProcessDescription peerDesc)
          Overloading method that accepts a PeerDescription object and finds the destination port of this peer
 AbstractMessage doSimpleAnonymousRequest(AbstractMessage requestMessage, java.lang.String remoteAddress, int remotePort)
          This overiding method works the same as {@link doSimpleAnonymousRequest( AbstractMessage requestMessage, String protocol, String remoteAddress, int remotePort) in that it uses the default protocol in the peer's configuration file
 AbstractMessage doSimpleAnonymousRequest(AbstractMessage requestMessage, java.lang.String protocol, java.lang.String remoteAddress, int remotePort)
          This overiding method is used for executing switched, synchronousrequests
that expects a response.
 void doSimpleRequest(AbstractMessage requestMessage, java.lang.String protocol, java.lang.String remoteAddress, int remotePort, boolean asynchronous)
          This overiding method is used for executing switched, synchronous/asynchronousrequests
that does NOT expect a response
.
 void doSimpleRequest(AbstractMessage requestMessage, java.lang.String protocol, java.lang.String remoteAddress, int remotePort, RequestListener responseHandler, boolean asynchronous)
          This overiding method is used for executing switched, synchronous/asynchronousrequests that expects a response
.
static void doSimpleRequest(java.lang.String id, AbstractMessage requestMessage, java.lang.String protocol, java.lang.String remoteAddress, int remotePort, boolean reuse, boolean asynchronous)
          This overiding method is used for executing switched, synchronous/asynchronousrequests
that does NOT expect a response
.
 int getDaemonPort()
          Get the daemon port as created by the system
 int getKeepAliveStatus()
          Check keep-alive status
 void initMulticastDaemon(RequestListener handler)
          Initiate a multicast daemon
private  void initPeerDaemon()
          Initiate the main peer daemon
 void stopAll()
           
 void terminateMsgProcess(java.lang.String id, java.lang.String type)
          Terminate an active messaging process
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_peerDaemonThread

private TimedThread _peerDaemonThread
The companion peer daemon attached to every peer for handling other peer's request


_peerDaemon

private AbstractServer _peerDaemon

_clients

private static java.util.Map _clients

_threads

private static java.util.Map _threads

_servers

private static java.util.Map _servers

_mainPeer

private AutoMedPeer _mainPeer

TEMP_CLIENT

private static final java.lang.String TEMP_CLIENT
See Also:
Constant Field Values

_simpleHandler

private SimpleMessageHandler _simpleHandler
Constructor Detail

AutoMedMessagingHelper

public AutoMedMessagingHelper(AutoMedPeer mainPeer)
                       throws AutoMedException
Creates a new instance of AutoMedMessagingHelper

Throws:
AutoMedException
Method Detail

initPeerDaemon

private void initPeerDaemon()
                     throws CommunicationException,
                            AutoMedException
Initiate the main peer daemon

Throws:
CommunicationException
AutoMedException

initMulticastDaemon

public void initMulticastDaemon(RequestListener handler)
                         throws CommunicationException,
                                AutoMedException
Initiate a multicast daemon

Throws:
CommunicationException
AutoMedException

getDaemonPort

public int getDaemonPort()
                  throws CommunicationException
Get the daemon port as created by the system

Throws:
CommunicationException

doSimpleAnonymousRequest

public AbstractMessage doSimpleAnonymousRequest(AbstractMessage requestMessage,
                                                PeerProcessDescription peerDesc)
                                         throws CommunicationException,
                                                AutoMedException
Overloading method that accepts a PeerDescription object and finds the destination port of this peer

Throws:
CommunicationException
AutoMedException

doSimpleAnonymousRequest

public AbstractMessage doSimpleAnonymousRequest(AbstractMessage requestMessage,
                                                java.lang.String remoteAddress,
                                                int remotePort)
                                         throws CommunicationException,
                                                AutoMedException
This overiding method works the same as {@link doSimpleAnonymousRequest( AbstractMessage requestMessage, String protocol, String remoteAddress, int remotePort) in that it uses the default protocol in the peer's configuration file

Throws:
CommunicationException
AutoMedException

doSimpleAnonymousRequest

public AbstractMessage doSimpleAnonymousRequest(AbstractMessage requestMessage,
                                                java.lang.String protocol,
                                                java.lang.String remoteAddress,
                                                int remotePort)
                                         throws CommunicationException,
                                                AutoMedException
This overiding method is used for executing switched, synchronousrequests
that expects a response. But it differs from #doSimpleRequest() in that
the calling method does not handle the response itself (no RequestListener object is passed
in as argument).
All the calling method cares about is an AbstractMessage
object that contains the content of response
.

Throws:
CommunicationException
AutoMedException

doSimpleRequest

public void doSimpleRequest(AbstractMessage requestMessage,
                            java.lang.String protocol,
                            java.lang.String remoteAddress,
                            int remotePort,
                            RequestListener responseHandler,
                            boolean asynchronous)
                     throws CommunicationException,
                            AutoMedException
This overiding method is used for executing switched, synchronous/asynchronousrequests that expects a response
. The client thread is not reused.

Throws:
CommunicationException
AutoMedException

doSimpleRequest

public void doSimpleRequest(AbstractMessage requestMessage,
                            java.lang.String protocol,
                            java.lang.String remoteAddress,
                            int remotePort,
                            boolean asynchronous)
                     throws CommunicationException,
                            AutoMedException
This overiding method is used for executing switched, synchronous/asynchronousrequests
that does NOT expect a response
. The client thread is not reused.

Throws:
CommunicationException
AutoMedException

doSimpleRequest

public static void doSimpleRequest(java.lang.String id,
                                   AbstractMessage requestMessage,
                                   java.lang.String protocol,
                                   java.lang.String remoteAddress,
                                   int remotePort,
                                   boolean reuse,
                                   boolean asynchronous)
                            throws CommunicationException,
                                   AutoMedException
This overiding method is used for executing switched, synchronous/asynchronousrequests
that does NOT expect a response
. The id and reuse of the client thread can be set.

Throws:
CommunicationException
AutoMedException

doRequest

public static void doRequest(java.lang.String id,
                             java.lang.String type,
                             AbstractMessage requestMessage,
                             java.lang.String protocol,
                             java.lang.String remoteAddress,
                             int remotePort,
                             RequestListener responseHandler,
                             int sleepTime,
                             boolean reuse,
                             boolean asynchronous,
                             boolean ackExpected)
                      throws CommunicationException,
                             AutoMedException
Perform a network request on behalf of the client. This method acts as an interface
between AutoMedPeer and the network. It requires an instance of RequestListener
as a parameter to handle call-backs for any responses that are received

A request client can use either TCP or UDP protocol as specified by protocol
. See Constants for a list of network protocols.
There are two types of client as specified by type (see
Constants):

This method through the reuse parameter also offers
the flexibility of reusing an existing switched client if one is available.
Not only does this save resources but also allow a degree of parallelism which allows multiple
requests to be sent and processed at the same time

Parameters:
id - a unique name/id for the operation
type - type of network client used for this operation (see note above)
requestMessage - an AbstractMessage object representing the message to send
protocol - network protocol used (see note above)
remoteAddress - destination address to send to
remotePort - destination port to send to
responseHandler - a RequestListener object to handle responses
sleepTime - sleep time between two successive runs for dedicated client request
reuse - whether reuse an existing network client if one exists? (see note)
asynchronous - whether to perform an asynchronous communication (see note)
ackExpected - whether or not to expect an acknowledgement response
this helps speed up the operation
Throws:
CommunicationException
AutoMedException

doRequest

public static void doRequest(java.lang.String id,
                             java.lang.String type,
                             AbstractMessage requestMessage,
                             java.lang.String protocol,
                             java.lang.String remoteAddress,
                             int remotePort,
                             RequestListener responseHandler,
                             int sleepTime,
                             boolean reuse,
                             boolean asynchronous,
                             boolean ackExpected,
                             java.util.Properties props)
                      throws CommunicationException,
                             AutoMedException
Perform a network request on behalf of the client. This method acts as an interface
between AutoMedPeer and the network. It requires an instance of RequestListener
as a parameter to handle call-backs for any responses that are received

A request client can use either TCP or UDP protocol as specified by protocol
. See Constants for a list of network protocols.
There are two types of client as specified by type (see
Constants):

This method through the reuse parameter also offers
the flexibility of reusing an existing switched client if one is available.
Not only does this save resources but also allow a degree of parallelism which allows multiple
requests to be sent and processed at the same time

Parameters:
id - a unique name/id for the operation
type - type of network client used for this operation (see note above)
requestMessage - an AbstractMessage object representing the message to send
protocol - network protocol used (see note above)
remoteAddress - destination address to send to
remotePort - destination port to send to
responseHandler - a RequestListener object to handle responses
sleepTime - sleep time between two successive runs for dedicated client request
reuse - whether reuse an existing network client if one exists? (see note)
asynchronous - whether to perform an asynchronous communication (see note)
ackExpected - whether or not to expect an acknowledgement response
this helps speed up the operation
Throws:
CommunicationException
AutoMedException

terminateMsgProcess

public void terminateMsgProcess(java.lang.String id,
                                java.lang.String type)
Terminate an active messaging process


createDaemon

public static TimedThread createDaemon(java.lang.String id,
                                       java.lang.String protocol,
                                       int port,
                                       RequestListener requestHandler,
                                       boolean reuse)
                                throws CommunicationException,
                                       AutoMedException
Initiates a network daemon for an AutoMedPeer to listen to other peers' requests
. Once a request is received, it is passed on to the RequestListener object
for processing. An AutoMedPeer is typically associated with
one such network daemon which could be using either TCP or UDP as the transport protocol

Throws:
CommunicationException
AutoMedException

doRequest

public void doRequest(java.lang.String requestType)
               throws CommunicationException,
                      AutoMedException
Utility method for performing specific P2P requests
See Constants for a list of OPT_* request types

Throws:
CommunicationException
AutoMedException

doCustomRequest

public void doCustomRequest(java.lang.String requestType,
                            java.lang.String protocol,
                            java.lang.String remoteAddress,
                            int remotePort,
                            int sleepTime,
                            boolean asynchronous)
                     throws CommunicationException,
                            AutoMedException
Create a custom keep-alive messaging helper for an AutoMedPeer.
To create standard messaging helpers, please use doRequest(String requestType) instead.

Throws:
CommunicationException
AutoMedException

getKeepAliveStatus

public int getKeepAliveStatus()
Check keep-alive status

Returns:
int indicating on/off status of remote. Applicable values are:
  • -1 remote is of (no responses received for keep-alive probes)
  • 1 remote is on

stopAll

public void stopAll()

clear

public void clear()