context.arch.comm
Class CommunicationsObject

java.lang.Object
  |
  +--context.arch.comm.CommunicationsObject

public class CommunicationsObject
extends java.lang.Object

This class handles the network communications for the calling class.

See Also:
MessageHandler

Field Summary
static java.lang.String DEFAULT_CLIENT
          The default client protocol class to use is "context.arch.comm.protocol.HTTPClientSocket".
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)
          Basic constructor for CommunicationsObject using given port and protocol server class, and client 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.Socket socket)
          This method handles an incoming request on the given socket and sends a reply.
 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
 RequestData sendRequest(java.lang.String request, java.lang.String url)
          This method creates a communications client with the default hostname and port and sends the given request using the newly created CommunicationsClient object
 RequestData sendRequest(java.lang.String request, java.lang.String url, java.lang.String host)
          This method creates a communications client with the given hostname and default port and sends the given request using the newly created CommunicationsClient object
 RequestData sendRequest(java.lang.String request, java.lang.String url, java.lang.String host, int port)
          This method creates a communications client with the given hostname and port and sends the given request using the newly created CommunicationsClient object
 RequestData sendRequest(java.lang.String request, java.lang.String url, java.lang.String host, int port, java.lang.String type)
          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 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.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

DEFAULT_PORT

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

DEFAULT_REMOTE_PORT

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

DEFAULT_REMOTE_SERVER

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

DEFAULT_SERVER

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

DEFAULT_CLIENT

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

HTTP_SERVER

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

HTTP_CLIENT

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

CommunicationsObject

public CommunicationsObject(MessageHandler handler,
                            java.lang.String clientClass,
                            java.lang.String serverClass,
                            int serverPort)
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)
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)

sendRequest

public RequestData sendRequest(java.lang.String request,
                               java.lang.String url)
                        throws InvalidProtocolException,
                               ProtocolException
This method creates a communications client with the default hostname and port and sends the given request using the newly created CommunicationsClient object
Parameters:
request - The request to send
url - The request type
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

sendRequest

public RequestData sendRequest(java.lang.String request,
                               java.lang.String url,
                               java.lang.String host)
                        throws InvalidProtocolException,
                               ProtocolException
This method creates a communications client with the given hostname and default port and sends the given request using the newly created CommunicationsClient object
Parameters:
request - The request to send
url - The request type
host - Hostname of the server to connect to
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

sendRequest

public RequestData sendRequest(java.lang.String request,
                               java.lang.String url,
                               java.lang.String host,
                               int port)
                        throws InvalidProtocolException,
                               ProtocolException
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
url - The request type
host - Hostname of the server to connect to
port - Port number of the server to connect to
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

sendRequest

public RequestData sendRequest(java.lang.String request,
                               java.lang.String url,
                               java.lang.String host,
                               int port,
                               java.lang.String type)
                        throws InvalidProtocolException,
                               ProtocolException
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
url - The request type
host - Hostname of the server to connect to
port - Port number of the server to connect to
type - Whether the request is a GET or a POST
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

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).