hmi.bml.bridge
Class TCPIPRealizerBridgeClient

java.lang.Object
  extended by hmi.bml.bridge.TCPIPRealizerBridgeClient
All Implemented Interfaces:
RealizerBridge, Runnable

public final class TCPIPRealizerBridgeClient
extends Object
implements RealizerBridge, Runnable

A RealizerBridge that uses a tcp/ip connection to provide transparent access to a BML Realizer running on a remote machine. The connection is "self-healing". Detailed documentation can be found in the project report.

Author:
Dennis Reidsma

Nested Class Summary
private  class TCPIPRealizerBridgeClient.FeedbackReader
          The process that reads feedback from the network connection, and puts it in the feedback queue.
private  class TCPIPRealizerBridgeClient.FeedbackRedirector
          The process that reads feedback from the feedback queue, and sends it to the BML feedback listeners.
 
Field Summary
private  ConcurrentLinkedQueue<String> bmlRequestQ
          Incoming BML Requests from the application are stored here.
private  Socket bmlSendSocket
           
private  InetSocketAddress bmlSendSocketAddress
           
private  PrintWriter bmlSendWriter
           
private static int CONNECT_RETRY_WAIT
           
private  Object connectionLock
          Locking object for the states of the main networking loop.
private  BMLFeedbackManager fbManager
           
private static long FEEDBACK_REDIRECT_WAIT_MILLI
          Sleeping time of the FeedbackRedirector if the feedback queue is empty
private  Object feedbackLock
          Locking object for the feedbacklisteners -- needed to avoid a situation where feedback is sent out simultaneously with setting the feedbacklisteners.
private  ConcurrentLinkedQueue<BMLFeedback> feedbackQ
          Incoming feedback from the server are stored here.
private  TCPIPRealizerBridgeClient.FeedbackReader feedbackReader
           
private  Thread feedbackReaderThread
           
private  BufferedReader feedbackReadReader
           
private  ServerSocket feedbackReadServerSocket
           
private  Socket feedbackReadSocket
           
private  TCPIPRealizerBridgeClient.FeedbackRedirector feedbackRedirector
          redirects feedback that has come in over the socket to the listeners
private  Thread feedbackRedirectorThread
          the thread under which the feedbackRedirector runs.
private  boolean isconnected
          Internal state var: true if a conenction to a server is active
private static org.slf4j.Logger logger
           
private  boolean mustconnect
          Internal state var: true if a connect request has been given
private  boolean mustdisconnect
          Internal state var: true if a disconnect request has been given
private  boolean mustshutdown
          Internal state var: true if a shutdown request has been given
private  long nextMainLoopWait
          Waiting time for next run() loop.
private  ServerInfo serverInfo
          Internal state var: The ServerInfo for the connection that must be set up (see #mustconnect).
private static int SOCKET_TIMEOUT
           
 
Constructor Summary
TCPIPRealizerBridgeClient()
          Set the state variables to appropriate values, start the main processing loop, and start the processing loop that will deliver feedback messages to the BML Feedback listeners.
 
Method Summary
 void addListeners(BMLListener... bmlListeners)
          Listeners must be stored; they will get updates from the feedbackRedirectionThread
 void connect(ServerInfo serverInfo)
          May return before connection is actually made.
 void disconnect()
          Instigates disconnection procedure.
private  void doconnect()
          Attempt to connect to server, given ServerInfo.
private  void dodisconnect()
          Disconnect from server (if connected); close feedback reading thread; return when processes finished.
private  void dosendBML(String bmlRequest)
          Send given BML request.
private  void doshutdown()
          Disconnect.
private  void failConnect(String msg)
          Fail and don't try again
protected  ServerInfo getServerInfo()
           
 boolean isConnected()
          Returns true iff there is a connection to a server active (and has not been lost).
 void performBML(String bmlString)
          Add BML request to the queue and return.
private  void retryConnect(String msg)
          Error connecting, prepare to retry
 void run()
          The main loop!
 void shutdown()
          Instigates total shutdown.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fbManager

private BMLFeedbackManager fbManager

logger

private static org.slf4j.Logger logger

FEEDBACK_REDIRECT_WAIT_MILLI

private static final long FEEDBACK_REDIRECT_WAIT_MILLI
Sleeping time of the FeedbackRedirector if the feedback queue is empty

See Also:
Constant Field Values

SOCKET_TIMEOUT

private static final int SOCKET_TIMEOUT
See Also:
Constant Field Values

CONNECT_RETRY_WAIT

private static final int CONNECT_RETRY_WAIT
See Also:
Constant Field Values

bmlRequestQ

private ConcurrentLinkedQueue<String> bmlRequestQ
Incoming BML Requests from the application are stored here. The main networking loop will get these requests and send them to the server if a connection is available.


connectionLock

private Object connectionLock
Locking object for the states of the main networking loop.


isconnected

private boolean isconnected
Internal state var: true if a conenction to a server is active


mustshutdown

private boolean mustshutdown
Internal state var: true if a shutdown request has been given


mustconnect

private boolean mustconnect
Internal state var: true if a connect request has been given


serverInfo

private ServerInfo serverInfo
Internal state var: The ServerInfo for the connection that must be set up (see #mustconnect).


mustdisconnect

private boolean mustdisconnect
Internal state var: true if a disconnect request has been given


nextMainLoopWait

private long nextMainLoopWait
Waiting time for next run() loop.


bmlSendSocketAddress

private InetSocketAddress bmlSendSocketAddress

bmlSendSocket

private Socket bmlSendSocket

bmlSendWriter

private PrintWriter bmlSendWriter

feedbackReadServerSocket

private ServerSocket feedbackReadServerSocket

feedbackReadSocket

private Socket feedbackReadSocket

feedbackReadReader

private BufferedReader feedbackReadReader

feedbackReader

private TCPIPRealizerBridgeClient.FeedbackReader feedbackReader

feedbackReaderThread

private Thread feedbackReaderThread

feedbackLock

private Object feedbackLock
Locking object for the feedbacklisteners -- needed to avoid a situation where feedback is sent out simultaneously with setting the feedbacklisteners.


feedbackRedirector

private TCPIPRealizerBridgeClient.FeedbackRedirector feedbackRedirector
redirects feedback that has come in over the socket to the listeners


feedbackRedirectorThread

private Thread feedbackRedirectorThread
the thread under which the feedbackRedirector runs.


feedbackQ

private ConcurrentLinkedQueue<BMLFeedback> feedbackQ
Incoming feedback from the server are stored here. The feedbackredirector loop will get tehm and send them to the BML FeedbackListeners.

Constructor Detail

TCPIPRealizerBridgeClient

public TCPIPRealizerBridgeClient()
Set the state variables to appropriate values, start the main processing loop, and start the processing loop that will deliver feedback messages to the BML Feedback listeners. The last loop, the one reading feedback from the socket, is not started until a connection is made.

Method Detail

disconnect

public void disconnect()
Instigates disconnection procedure. After disconnecting, client goes into a waiting state, and may be asked to connect to a server again at a later time.


connect

public void connect(ServerInfo serverInfo)
May return before connection is actually made. However, implementation guarantees that performBML request made AFTER connect request, will be sent over that connection (if the connection is successful)


getServerInfo

protected ServerInfo getServerInfo()

shutdown

public void shutdown()
Instigates total shutdown. May return from this method before shutdown is completed. Shutdown process will terminate all connections and threads for this client.


isConnected

public boolean isConnected()
Returns true iff there is a connection to a server active (and has not been lost).


addListeners

public void addListeners(BMLListener... bmlListeners)
Listeners must be stored; they will get updates from the feedbackRedirectionThread

Specified by:
addListeners in interface RealizerBridge

performBML

public void performBML(String bmlString)
Add BML request to the queue and return. Sending will happen in the main networking loop.

Specified by:
performBML in interface RealizerBridge

run

public void run()
The main loop!

Specified by:
run in interface Runnable

dodisconnect

private void dodisconnect()
Disconnect from server (if connected); close feedback reading thread; return when processes finished. Called from the run() loop.


doconnect

private void doconnect()
Attempt to connect to server, given ServerInfo. Start feedback reading loop. Log results. If successful, isconnected is true; else, clean up everything, try again enxt round with wait = 1000. Called from the run() loop.


failConnect

private void failConnect(String msg)
Fail and don't try again


retryConnect

private void retryConnect(String msg)
Error connecting, prepare to retry


doshutdown

private void doshutdown()
Disconnect. Clean up feedbackredirectionloop. Called from the run() loop.


dosendBML

private void dosendBML(String bmlRequest)
Send given BML request. If fail: drop request, fire off feedback, disconnect.