copyright The Portico Project 2008.

org.portico.binding.jsop.rti
Class JSOPActiveConnection

java.lang.Object
  extended by java.lang.Thread
      extended by org.portico.binding.jsop.rti.JSOPActiveConnection
All Implemented Interfaces:
Runnable

public class JSOPActiveConnection
extends Thread

This class represents an active JSOP socket connection between an LRC and the RTI. It waits for messages to be received from the LRC and routes them as required.

When a JSOP client connects to the RTI, a new instance of this class is created and started in a new Thread. The run() method goes into an infinite loop waiting for incoming messages from the client. When a message is received, it is routed either to the appropriate federation, or to the BootstrapHandler (if it is one of the bootstrap methods - create, destroy, join). When the response is obtained from that process, a ResponseMessage is created and sent back down the socket connection to the client. If an exception occurs, it is wrapped in an ExceptionMessage (commons class).

Callbacks sent to the LRC *from* the RTI are done so unsolicited. These are handled by subclasses of the RTIConnection class which is implemented here as the private inner-class JSOPServerConnection. The RTIConnection.sendCallback(MessageContext) method handles that process, and the sending is done in the thread of the caller, *NOT* the running thread of this class. Access to the underlying socket is synchronized so as to avoid any race conditions or corruption problems.


Nested Class Summary
 class JSOPActiveConnection.JSOPServerConnection
          This class provides a link to the connection for the RTI.
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
private  boolean active
           
private  JSOPBootstrap bootstrap
           
private  com.lbf.commons.messaging.IMessageHandler bootstrapHandler
           
private static int COUNTER
           
private  ObjectInputStream inStream
           
private  org.apache.log4j.Logger logger
           
private  ObjectOutputStream outStream
           
private  JSOPActiveConnection.JSOPServerConnection rtiConnection
           
private  Socket socket
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
JSOPActiveConnection(Socket socket, JSOPBootstrap bootstrap)
          Creates a new active connection for the client that is connecting over the given socket.
 
Method Summary
private  void closeSocket()
          Closes off the socket that this connection was attached to.
private  com.lbf.commons.messaging.ResponseMessage fireJoinRequest(RTI_JoinFederation request)
          Process a join request.
private  com.lbf.commons.messaging.ResponseMessage fireResignRequest(RTI_ResignFederation request)
          This class will process a resign request.
private  com.lbf.commons.messaging.ResponseMessage fireToBootstrap(RTI_RequestMessage request)
          This method will take the given request message and pass it on to the the bootstrap handler, returning the response.
private  com.lbf.commons.messaging.ResponseMessage fireToFederation(RTI_RequestMessage request)
          Pass a message to the rti-request sink of the federation for processing, returning the given response.
 Socket getSocket()
          Get the socket that this connection implementation is using to send/receive messages on.
private  void killConnection()
          This will manually kill off and clean up the socket connection.
 void run()
          This method will gain access to the incoming and outgoing streams of the socket used to create this instance.
 void shutdown()
          Interrupts the running thread and gets it to close the socket connection and shutdown.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

COUNTER

private static int COUNTER

socket

private Socket socket

bootstrap

private JSOPBootstrap bootstrap

bootstrapHandler

private com.lbf.commons.messaging.IMessageHandler bootstrapHandler

logger

private org.apache.log4j.Logger logger

active

private volatile boolean active

inStream

private ObjectInputStream inStream

outStream

private ObjectOutputStream outStream

rtiConnection

private JSOPActiveConnection.JSOPServerConnection rtiConnection
Constructor Detail

JSOPActiveConnection

public JSOPActiveConnection(Socket socket,
                            JSOPBootstrap bootstrap)
Creates a new active connection for the client that is connecting over the given socket. The bootstrap is used to gain access to the bootstrap handler to which all of the "big" incoming requests (create/destroy/join) are passed. A reference is also kept so that when the connection shuts down for any reason it can be removed from the list of active connections.

Method Detail

getSocket

public Socket getSocket()
Get the socket that this connection implementation is using to send/receive messages on.


run

public void run()
This method will gain access to the incoming and outgoing streams of the socket used to create this instance. It will then enter a loop listening for incoming messages from the connected LRC. This process will block until data is ready to be received. When a full message instance has been received, it will be routed as required. Bootstrap messages (such as create,destory,join) are routed to the bootstrap handler. If it is not one of these messages, it is routed directly to the federation through the JSOPServerConnection inner class. This will continue until the connection is shutdown or disconnected.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

killConnection

private void killConnection()
This will manually kill off and clean up the socket connection. It will make sure that the socket is closed and that the active connection is removed from the daemon's list of currently running connections.


closeSocket

private void closeSocket()
Closes off the socket that this connection was attached to.


shutdown

public void shutdown()
Interrupts the running thread and gets it to close the socket connection and shutdown.


fireToBootstrap

private com.lbf.commons.messaging.ResponseMessage fireToBootstrap(RTI_RequestMessage request)
This method will take the given request message and pass it on to the the bootstrap handler, returning the response. If the requests causes an exception, it is wrapped in a commons ExceptionResponse class and returned.


fireJoinRequest

private com.lbf.commons.messaging.ResponseMessage fireJoinRequest(RTI_JoinFederation request)
Process a join request. An LRC can only be connected to a single federate at a time. Thus, if this active connection is already connected, an error response will be returned and the request won't be passed on to the federation.


fireResignRequest

private com.lbf.commons.messaging.ResponseMessage fireResignRequest(RTI_ResignFederation request)
This class will process a resign request. If the request is successful, it will remove any connection between this instance and a federation. Note that this does not close the actual socket connection, but rather, releases the callback handler by setting it to null.


fireToFederation

private com.lbf.commons.messaging.ResponseMessage fireToFederation(RTI_RequestMessage request)
Pass a message to the rti-request sink of the federation for processing, returning the given response.


copyright The Portico Project 2008.