copyright The Portico Project 2008.

org.portico.binding.jsop.lrc
Class JSOPClientConnection

java.lang.Object
  extended by org.portico.lrc.LRCConnection
      extended by org.portico.binding.jsop.lrc.JSOPClientConnection
All Implemented Interfaces:
Runnable

public class JSOPClientConnection
extends LRCConnection
implements Runnable

This class is responsible for creating and managing a connection between the LRC and RTI using the JSOP "protocol".

The JSOP protocol isn't anything special. It stands for "Java Serialized Object Protocol" and basically just consists of throwing serialized objects back-and-forth over a socket connection. When the connection is asked to send a message, it extracts the request portion and writes it down the socket (see sendMessage(MessageContext)). The entire context isn't written for efficiency reasons.

This connection will also start a listener thread, that will sit and wait for any communication the RTI has to give. See the run() method for more information on this process.

Information about the location of the RTI can be either automatically discovered, or provided through the PROP_HOST system property.


Field Summary
private  Socket daemonSocket
           
private static String DEFAULT_HOST
           
private  ObjectInputStream fromRTI
           
private  BlockingQueue<com.lbf.commons.messaging.ResponseMessage> handoffQueue
           
private  String host
           
private  Thread listenerThread
           
private static String PROP_HOST
          The sys property that will be used to find the RTI-host to connect to.
private  ObjectOutputStream toRTI
           
 
Fields inherited from class org.portico.lrc.LRCConnection
logger, lrc
 
Constructor Summary
JSOPClientConnection()
           
 
Method Summary
 void close()
          This method will close the connection to the RTI and kill the active thread that is processing incoming messages.
 void configure(com.lbf.commons.utils.Bag<String,?> properties)
          Extracts the host it should connect to from the PROP_HOST key in the properties (with a default of DEFAULT_HOST).
private  Socket connectToHost(String theHost)
          This method will attempt to open a JSOP connection to an RTI using the information in the given parameter.
private  Socket directConnect(String host, int port)
          Create a direct socket connection to the appropriate bootstrap running on the RTI-side.
private  Socket discoverRTI(int port)
          Discovers the location and port of the RTI using the MulticastLookup class.
 void run()
          As messages could come in from the RTI at any point, this method should be started in a new Thread to sit and listen for any communications.
 void sendMessage(com.lbf.commons.messaging.MessageContext message)
          Sends the message to the RTI by writing the request to the outgoing stream.
 void startup()
          This method will connect to the RTI and set up the input and output streams that communication will flow over.
 
Methods inherited from class org.portico.lrc.LRCConnection
receiveCallback, receiveCallback
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROP_HOST

private static final String PROP_HOST
The sys property that will be used to find the RTI-host to connect to. See class-level javadoc for more information on how this is used.

See Also:
Constant Field Values

DEFAULT_HOST

private static final String DEFAULT_HOST

host

private String host

daemonSocket

private Socket daemonSocket

toRTI

private ObjectOutputStream toRTI

fromRTI

private ObjectInputStream fromRTI

listenerThread

private Thread listenerThread

handoffQueue

private BlockingQueue<com.lbf.commons.messaging.ResponseMessage> handoffQueue
Constructor Detail

JSOPClientConnection

public JSOPClientConnection()
Method Detail

configure

public void configure(com.lbf.commons.utils.Bag<String,?> properties)
               throws com.lbf.commons.config.ConfigurationException
Extracts the host it should connect to from the PROP_HOST key in the properties (with a default of DEFAULT_HOST).

Overrides:
configure in class LRCConnection
Throws:
com.lbf.commons.config.ConfigurationException

startup

public void startup()
             throws JRTIinternalError
This method will connect to the RTI and set up the input and output streams that communication will flow over. It will also start a separate thread that will sit and listen for all incoming messages and route them appropriately. That thread wraps around the same instance that is being invoked on. See the run() method for information on what happens inside it.

Specified by:
startup in class LRCConnection
Throws:
JRTIinternalError

close

public void close()
           throws JRTIinternalError
This method will close the connection to the RTI and kill the active thread that is processing incoming messages. If there is a problem doing any of this, an exception will be thrown.

Specified by:
close in class LRCConnection
Throws:
JRTIinternalError

run

public void run()
As messages could come in from the RTI at any point, this method should be started in a new Thread to sit and listen for any communications. We are expecting two types of messages: If the message is a callback, it is sent directly to the RTI. If the message is a response, it is dropped onto the "hand-off" queue. The hand-off queue is SynchronousQueue, so it only ever handles 1 message. It is expected that the main LRC thread (the user thread) will be blocked on that queue, awaiting the response to come through.

Specified by:
run in interface Runnable

sendMessage

public void sendMessage(com.lbf.commons.messaging.MessageContext message)
                 throws JRTIinternalError
Sends the message to the RTI by writing the request to the outgoing stream. It then attempts to grab the response to this request off the hand-off queue, blocking until something is there (see run() for more info). When it has the response, it throws it into the context and returns.

Specified by:
sendMessage in class LRCConnection
Throws:
JRTIinternalError

connectToHost

private Socket connectToHost(String theHost)
                      throws JRTIinternalError
This method will attempt to open a JSOP connection to an RTI using the information in the given parameter. Depending on the value of the given parameter, the process involved will differ. If the value is:

Throws:
JRTIinternalError

discoverRTI

private Socket discoverRTI(int port)
                    throws JRTIinternalError
Discovers the location and port of the RTI using the MulticastLookup class. It will then connect to the RTI and return a socket ready for use. The actual connection is made by the directConnect(String, int) method once the relevant location has been found. Discovers the location and port of the RTI using the JSOP multicast discovery mechanism.

Throws:
JRTIinternalError

directConnect

private Socket directConnect(String host,
                             int port)
                      throws JRTIinternalError
Create a direct socket connection to the appropriate bootstrap running on the RTI-side. The RTI is located on the given host at the given port.

Throws:
JRTIinternalError

copyright The Portico Project 2008.