context.arch.comm
Class CommunicationsObject

java.lang.Object
  extended bycontext.arch.comm.CommunicationsObject

public class CommunicationsObject
extends java.lang.Object

This class handles the network communications for the calling class. It is able to send synchronous message, asynchronous messages (with a threaded client pool) and is connected to a multicast address. Agathe - to do : make of the communication client a thread pool

See Also:
MessageHandler

Field Summary
static boolean DEBUG
          Debug flag.
static java.lang.String DEFAULT_CLIENT
          The default client protocol class to use is "context.arch.comm.protocol.HTTPClientSocket".
static int DEFAULT_POOL_CLIENTS_NUMBER
          The default number of threads in the pool of threads.
static int DEFAULT_PORT
          The default port number to use is port 5555.
static int DEFAULT_REMOTE_PORT
          The default remote port number to use is port 5555.
static java.lang.String DEFAULT_REMOTE_SERVER
          The default remote server is localhost.
static java.lang.String DEFAULT_SERVER
          The default server protocol class to use is "context.arch.comm.protocol.HTTPServerSocket".
static java.lang.String HTTP_CLIENT
          The HTTP client protocol class.
static java.lang.String HTTP_SERVER
          The HTTP server protocol class.
 
Constructor Summary
CommunicationsObject(MessageHandler handler)
          Basic constructor for CommunicationsObject using default port and protocol server class, and default client class,
CommunicationsObject(MessageHandler handler, int port)
          Constructor for CommunicationsObject using given port.
CommunicationsObject(MessageHandler handler, int port, java.lang.String serverClass)
          Constructor for CommunicationsObject using the given port and given protocol.
CommunicationsObject(MessageHandler handler, java.lang.String serverClass)
          Constructor for CommunicationsObject using default port, default client class and given protocol server.
CommunicationsObject(MessageHandler handler, java.lang.String clientClass, java.lang.String serverClass, int serverPort, int poolClientsNumber)
          Basic constructor for CommunicationsObject using given port and protocol server class, and client class.
CommunicationsObject(MessageHandler handler, java.lang.String clientClass, java.lang.String serverClass, int serverPort, java.lang.String multicastClass)
          Basic constructor for CommunicationsObject using given port and protocol server class, and client class, and given multicast protocol class.
 
Method Summary
 java.lang.String addReplyProtocol(java.lang.String reply)
          This stub method adds the communications protocol to the given reply using the CommunicationsServer object
 DataObject decodeData(java.io.Reader message)
          This stub method calls the decodeData method in MessageHandler.
 java.lang.String encodeData(DataObject message)
          This stub method calls the encodeData method in MessageHandler.
 java.lang.String getClientClass()
          Returns the class being used as the communications client.
 java.lang.String getServerClass()
          Returns the class being used as the communications client.
 int getServerPort()
          Returns the port being used for incoming communications (server).
 java.lang.String getServerProtocol()
          This method gets the communications protocol being used by the object that implements the CommunicationsServer interface
 void handleIncomingRequest(java.net.DatagramPacket packet)
          This method handles an incoming datagram packet from the multicast channel It should only be called by the underlying CommunicationsMulticast object
 void handleIncomingRequest(java.net.Socket socket)
          This method handles an incoming request on the given socket and sends a reply.
 void handleIndependentReply(IndependentCommunication independentComm)
           
 void println(java.lang.String obs)
          Print on the frame
 void quit()
          This stub method stops the communications server from receiving more data by using the CommunicationsServer object
 DataObject runMethod(java.lang.String line, DataObject data)
          This stub method runs the specified request using the MessageHandler
 void sendIndependentRequest(IndependentCommunication request)
           
 void sendMulticastRequest(java.lang.String request, java.lang.String url)
          This method sends the given request using the CommunicationsMulticast object.
 RequestData sendRequest(RequestObject request)
          This method creates a communications client with the given hostname and port and sends the given request using the newly created CommunicationsClient object
 void setClientClass(java.lang.String client)
          Sets the class to use as the communications client.
 void setFrame(GenericFrame gf)
          Set the display frame of the BaseObject
 void setServerClass(java.lang.String server)
          Sets the class to use as the communications server.
 void setServerPort(int port)
          Sets the port to use for incoming communications (server).
 void start()
          This method creates and starts an instance of the class that deals with the underlying communications protocol being used.
 RequestData stripRequestProtocol(java.net.DatagramPacket packet)
          This stub method strips the communications protocol from the given request using the CommunicationsMulticast object
 RequestData stripRequestProtocol(java.net.Socket socket)
          This stub method strips the communications protocol from the given request using the CommunicationsServer object
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEBUG

public static boolean DEBUG
Debug flag. Set to true to see debug messages.


DEFAULT_PORT

public static final int DEFAULT_PORT
The default port number to use is port 5555.

See Also:
Constant Field Values

DEFAULT_REMOTE_PORT

public static final int DEFAULT_REMOTE_PORT
The default remote port number to use is port 5555.

See Also:
Constant Field Values

DEFAULT_REMOTE_SERVER

public static final java.lang.String DEFAULT_REMOTE_SERVER
The default remote server is localhost.

See Also:
Constant Field Values

DEFAULT_SERVER

public static final java.lang.String DEFAULT_SERVER
The default server protocol class to use is "context.arch.comm.protocol.HTTPServerSocket".

See Also:
Constant Field Values

DEFAULT_CLIENT

public static final java.lang.String DEFAULT_CLIENT
The default client protocol class to use is "context.arch.comm.protocol.HTTPClientSocket".

See Also:
Constant Field Values

HTTP_SERVER

public static final java.lang.String HTTP_SERVER
The HTTP server protocol class.

See Also:
Constant Field Values

HTTP_CLIENT

public static final java.lang.String HTTP_CLIENT
The HTTP client protocol class.

See Also:
Constant Field Values

DEFAULT_POOL_CLIENTS_NUMBER

public static int DEFAULT_POOL_CLIENTS_NUMBER
The default number of threads in the pool of threads.

Constructor Detail

CommunicationsObject

public CommunicationsObject(MessageHandler handler,
                            java.lang.String clientClass,
                            java.lang.String serverClass,
                            int serverPort,
                            int poolClientsNumber)
Basic constructor for CommunicationsObject using given port and protocol server class, and client class. If either class is null, the default class is used, and if the port < 0, the default port is used.

Parameters:
handler - Basic message handling object
clientClass - Class to use for client communications
serverClass - Class to use for server communications
serverPort - Port the server recieves communications on
See Also:
DEFAULT_CLIENT, DEFAULT_PORT, DEFAULT_SERVER

CommunicationsObject

public CommunicationsObject(MessageHandler handler,
                            java.lang.String clientClass,
                            java.lang.String serverClass,
                            int serverPort,
                            java.lang.String multicastClass)
Basic constructor for CommunicationsObject using given port and protocol server class, and client class, and given multicast protocol class. If either class is null, the default class is used, and if the port < 0, the default port is used.

Parameters:
handler - Basic message handling object
clientClass - Class to use for client communications
serverClass - Class to use for server communications
serverPort - Port the server receives communications on
multicastClass - Class to use for multicast communications
See Also:
DEFAULT_CLIENT, DEFAULT_PORT, DEFAULT_SERVER

CommunicationsObject

public CommunicationsObject(MessageHandler handler)
Basic constructor for CommunicationsObject using default port and protocol server class, and default client class,

Parameters:
handler - Basic message handling object
See Also:
DEFAULT_PORT, DEFAULT_SERVER, DEFAULT_CLIENT

CommunicationsObject

public CommunicationsObject(MessageHandler handler,
                            int port)
Constructor for CommunicationsObject using given port. It uses the default server and client class.

Parameters:
handler - Basic message handling object
port - Port number to communicate on
See Also:
DEFAULT_SERVER, DEFAULT_CLIENT

CommunicationsObject

public CommunicationsObject(MessageHandler handler,
                            java.lang.String serverClass)
Constructor for CommunicationsObject using default port, default client class and given protocol server.

Parameters:
handler - Basic message handling object
serverClass - Name of server class to use for communications
See Also:
DEFAULT_PORT, DEFAULT_CLIENT

CommunicationsObject

public CommunicationsObject(MessageHandler handler,
                            int port,
                            java.lang.String serverClass)
Constructor for CommunicationsObject using the given port and given protocol. It uses the port for both the server object and for the client object. The default client class and default remote hostname is used.

Parameters:
handler - Basic message handling object
port - Port number to communicate on
serverClass - Class to use for server communications
See Also:
DEFAULT_CLIENT
Method Detail

start

public void start()
           throws InvalidProtocolException
This method creates and starts an instance of the class that deals with the underlying communications protocol being used. This new class implements the CommunicationsServer interface. Now it also creates an instance of the client class.

Throws:
InvalidProtocolException - if the class implementing the communications protocol can't be created
See Also:
CommunicationsServer, CommunicationsServer.start()

decodeData

public DataObject decodeData(java.io.Reader message)
                      throws DecodeException,
                             InvalidDecoderException
This stub method calls the decodeData method in MessageHandler. The end result is a decoded message.

Parameters:
message - Message to be decoded
Returns:
decoded message in a DataObject
Throws:
DecodeException - if the message can't be decoded
InvalidDecoderException - if a decoder can't be created
See Also:
MessageHandler.decodeData(Reader)

encodeData

public java.lang.String encodeData(DataObject message)
                            throws EncodeException,
                                   InvalidEncoderException
This stub method calls the encodeData method in MessageHandler. The end result is a encoded message.

Parameters:
message - Message to be encoded in the form of a DataObject
Returns:
encoded message in the form of a DataObject
Throws:
EncodeException - if the message can't be encoded
InvalidEncoderException - if the encoder can't be created
See Also:
MessageHandler.encodeData(DataObject)

getServerProtocol

public java.lang.String getServerProtocol()
This method gets the communications protocol being used by the object that implements the CommunicationsServer interface

Returns:
the communications protocol being used
See Also:
CommunicationsServer.getProtocol()

addReplyProtocol

public java.lang.String addReplyProtocol(java.lang.String reply)
                                  throws ProtocolException
This stub method adds the communications protocol to the given reply using the CommunicationsServer object

Parameters:
reply - The reply that needs the protocol added
Returns:
the reply with added protocol
Throws:
ProtocolException - if protocol can't be added to the given reply
See Also:
CommunicationsServer.addReplyProtocol(String)

stripRequestProtocol

public RequestData stripRequestProtocol(java.net.Socket socket)
                                 throws ProtocolException
This stub method strips the communications protocol from the given request using the CommunicationsServer object

Parameters:
socket - The socket the request is being received on
Returns:
the request with the protocol stripped away
Throws:
ProtocolException - thrown if protocol can't be stripped from the given request
See Also:
CommunicationsServer.stripRequestProtocol(java.net.Socket)

stripRequestProtocol

public RequestData stripRequestProtocol(java.net.DatagramPacket packet)
                                 throws ProtocolException
This stub method strips the communications protocol from the given request using the CommunicationsMulticast object

Parameters:
packet - The multicast datagram the request is being received in
Returns:
the request with the protocol stripped away
Throws:
ProtocolException - thrown if protocol can't be stripped from the given request
See Also:
CommunicationsServer.stripRequestProtocol(java.net.Socket)

sendRequest

public RequestData sendRequest(RequestObject request)
                        throws InvalidProtocolException,
                               ProtocolException,
                               java.io.IOException
This method creates a communications client with the given hostname and port and sends the given request using the newly created CommunicationsClient object

Parameters:
request - The request to send
Returns:
the result of the request
Throws:
InvalidProtocolException - if request can't be sent successfully
ProtocolException - thrown when request fails due to mistake in protocol
java.io.IOException

handleIndependentReply

public void handleIndependentReply(IndependentCommunication independentComm)

sendIndependentRequest

public void sendIndependentRequest(IndependentCommunication request)

sendMulticastRequest

public void sendMulticastRequest(java.lang.String request,
                                 java.lang.String url)
                          throws ProtocolException
This method sends the given request using the CommunicationsMulticast object. It does so by adding the request protocol to the given request and request type, sending the request.

Parameters:
request - The request to send
url - The request type
Throws:
ProtocolException

quit

public void quit()
This stub method stops the communications server from receiving more data by using the CommunicationsServer object

See Also:
CommunicationsServer.quit()

runMethod

public DataObject runMethod(java.lang.String line,
                            DataObject data)
                     throws InvalidMethodException,
                            MethodException
This stub method runs the specified request using the MessageHandler

Parameters:
line - Single line specifying the type of request
data - Data for the specified RPC
Throws:
InvalidMethodException - thrown if specified RPC can't be found
MethodException - thrown if specified RPC can't be successfully executed
See Also:
MessageHandler.runMethod(String, DataObject)

handleIncomingRequest

public void handleIncomingRequest(java.net.Socket socket)
This method handles an incoming request on the given socket and sends a reply. It should only be called by the underlying CommunicationsServer object

Parameters:
socket - Socket on which the request is being received
See Also:
MessageHandler.runMethod(String, DataObject)

setClientClass

public void setClientClass(java.lang.String client)
Sets the class to use as the communications client.

Parameters:
client - Class to use as the communications client.

getClientClass

public java.lang.String getClientClass()
Returns the class being used as the communications client.

Returns:
being used as the communications client.

setServerClass

public void setServerClass(java.lang.String server)
Sets the class to use as the communications server.

Parameters:
server - Class to use as the communications server.

getServerClass

public java.lang.String getServerClass()
Returns the class being used as the communications client.

Returns:
class being used as the communications server.

setServerPort

public void setServerPort(int port)
Sets the port to use for incoming communications (server).

Parameters:
port - Port to use for incoming communications (server).

getServerPort

public int getServerPort()
Returns the port being used for incoming communications (server).

Returns:
port being used for incoming communications (server).

handleIncomingRequest

public void handleIncomingRequest(java.net.DatagramPacket packet)
This method handles an incoming datagram packet from the multicast channel It should only be called by the underlying CommunicationsMulticast object

Parameters:
packet - DatagramPacket which is being received ??@see context.arch.comm.language.MessageHandler#runMethod(String, DataObject)

setFrame

public void setFrame(GenericFrame gf)
Set the display frame of the BaseObject


println

public void println(java.lang.String obs)
Print on the frame