hmi.bml.bridge
Class TCPIPRealizerBridgeServer

java.lang.Object
  extended by hmi.bml.bridge.TCPIPRealizerBridgeServer
All Implemented Interfaces:
BMLTPlanningListener, BMLExceptionListener, BMLFeedbackListener, BMLListener, BMLWarningListener, Runnable

public final class TCPIPRealizerBridgeServer
extends Object
implements Runnable, BMLExceptionListener, BMLWarningListener, BMLFeedbackListener, BMLTPlanningListener

Takes a RealizerBridge, and exposes access to it through a tcp/ip connection. The connection is "self-healing". Detailed documentation can be found in the project report.

Author:
Dennis Reidsma

Nested Class Summary
private  class TCPIPRealizerBridgeServer.BMLReader
          The process that reads bml from the network connection, and puts it in the bml queue.
private  class TCPIPRealizerBridgeServer.BMLRedirector
          The process that reads bml from the bml queue, and sends it to the BML realizer.
static class TCPIPRealizerBridgeServer.ServerState
           
 
Field Summary
private static long BML_REDIRECT_WAIT_MILLI
          Sleeping time of the BmlRequestRedirector if the bml queue is empty
private  int bmlPort
           
private  ConcurrentLinkedQueue<String> bmlQ
          Incoming bml from the client are stored here.
private  TCPIPRealizerBridgeServer.BMLReader bmlReader
           
private  Thread bmlReaderThread
           
private  BufferedReader bmlReadReader
           
private  ServerSocket bmlReadServerSocket
           
private  Socket bmlReadSocket
           
private  TCPIPRealizerBridgeServer.BMLRedirector bmlRedirector
          redirects bml that has come in over the socket to the realizerbridge
private  Thread bmlRedirectorThread
          the thread under which the bmlRedirector runs.
private static int CONNECT_RETRY_WAIT
           
private  Object connectionLock
          Locking object for the states of the main networking loop.
protected  List<ConnectionStateListener> connectionStateListeners
           
private  int feedbackPort
           
private  ConcurrentLinkedQueue<String> feedbackQ
          Incoming Feedback from the RealizerBridge is stored here.
private  Socket feedbackSendSocket
           
private  InetSocketAddress feedbackSendSocketAddress
           
private  PrintWriter feedbackSendWriter
           
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 someone connected on the bmlrequest port, and a feedback connection needs to be established
private  boolean mustdisconnect
          Internal state var: true if a connection failed and the feedback connection needs to be cleaned up
private  boolean mustshutdown
          Internal state var: true if a shutdown request has been given
private  long nextMainLoopWait
          Waiting time for next run() loop.
private  RealizerBridge realizerBridge
          The realizerbridge that will handle incoming bml
private  TCPIPRealizerBridgeServer.ServerState serverState
           
private static int SOCKET_TIMEOUT
           
private static int WAIT_AFTER_DROPPED_CLIENT
           
 
Constructor Summary
TCPIPRealizerBridgeServer(RealizerBridge bridge, int bmlPort, int feedbackPort)
          Set the state variables to appropriate values, start the main processing loop, and start the processing loop that will deliver bml messages to the Realizer.
 
Method Summary
 void addConnectionStateListener(ConnectionStateListener l)
           
private  void doconnect()
          Attempt to connect to client feedback channel.
private  void dodisconnect()
          Disconnect feedback connection to client.
private  void dosendFeedback(String feedback)
          Send given feedbacki.
private  void doshutdown()
          Disconnect.
 void exception(BMLExceptionFeedback be)
           
 TCPIPRealizerBridgeServer.ServerState getStatus()
           
 boolean isConnectedToClient()
          Returns true iff there is a connection to a client active (and has not been lost).
 void performanceStart(BMLPerformanceStartFeedback psf)
           
 void performanceStop(BMLPerformanceStopFeedback psf)
           
 void planningFinished(BMLTPlanningFinishedFeedback pff)
           
 void planningStart(BMLTPlanningStartFeedback psf)
           
private  void queueFeedback(String fb)
           
private  void retryConnect(String msg)
          Error connecting, prepare to retry
 void run()
          The main loop!
private  void setServerState(TCPIPRealizerBridgeServer.ServerState state)
           
 void shutdown()
          Instigates total shutdown.
 void stopBmlReadSockets()
          Stop bml reading sockets preparatory to completely shutting down, or preparatory to connecting new client
 void stopFeedbackWriteSockets()
          Stop feedback sending socket when to dropping client
 void syncProgress(BMLSyncPointProgressFeedback spp)
           
 void warn(BMLWarningFeedback bw)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

private static org.slf4j.Logger logger

BML_REDIRECT_WAIT_MILLI

private static final long BML_REDIRECT_WAIT_MILLI
Sleeping time of the BmlRequestRedirector if the bml 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

WAIT_AFTER_DROPPED_CLIENT

private static final int WAIT_AFTER_DROPPED_CLIENT
See Also:
Constant Field Values

serverState

private TCPIPRealizerBridgeServer.ServerState serverState

connectionStateListeners

protected List<ConnectionStateListener> connectionStateListeners

feedbackQ

private ConcurrentLinkedQueue<String> feedbackQ
Incoming Feedback from the RealizerBridge is stored here. The main networking loop will get the feedback and send them to the client 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


mustdisconnect

private boolean mustdisconnect
Internal state var: true if a connection failed and the feedback connection needs to be cleaned up


mustconnect

private boolean mustconnect
Internal state var: true if someone connected on the bmlrequest port, and a feedback connection needs to be established


nextMainLoopWait

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


feedbackSendSocketAddress

private InetSocketAddress feedbackSendSocketAddress

feedbackSendSocket

private Socket feedbackSendSocket

feedbackSendWriter

private PrintWriter feedbackSendWriter

bmlReadServerSocket

private ServerSocket bmlReadServerSocket

bmlReadSocket

private Socket bmlReadSocket

bmlReadReader

private BufferedReader bmlReadReader

bmlReader

private TCPIPRealizerBridgeServer.BMLReader bmlReader

bmlReaderThread

private Thread bmlReaderThread

bmlPort

private int bmlPort

feedbackPort

private int feedbackPort

bmlRedirector

private TCPIPRealizerBridgeServer.BMLRedirector bmlRedirector
redirects bml that has come in over the socket to the realizerbridge


bmlRedirectorThread

private Thread bmlRedirectorThread
the thread under which the bmlRedirector runs.


realizerBridge

private RealizerBridge realizerBridge
The realizerbridge that will handle incoming bml


bmlQ

private ConcurrentLinkedQueue<String> bmlQ
Incoming bml from the client are stored here. The bmlredirector loop will get them and send them to the BML realizer.

Constructor Detail

TCPIPRealizerBridgeServer

public TCPIPRealizerBridgeServer(RealizerBridge bridge,
                                 int bmlPort,
                                 int feedbackPort)
Set the state variables to appropriate values, start the main processing loop, and start the processing loop that will deliver bml messages to the Realizer. Also, start the loop that waits for new clients to connect.

Method Detail

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


isConnectedToClient

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


getStatus

public TCPIPRealizerBridgeServer.ServerState getStatus()

addConnectionStateListener

public void addConnectionStateListener(ConnectionStateListener l)

setServerState

private void setServerState(TCPIPRealizerBridgeServer.ServerState state)

exception

public void exception(BMLExceptionFeedback be)
Specified by:
exception in interface BMLExceptionListener

warn

public void warn(BMLWarningFeedback bw)
Specified by:
warn in interface BMLWarningListener

syncProgress

public void syncProgress(BMLSyncPointProgressFeedback spp)
Specified by:
syncProgress in interface BMLFeedbackListener

performanceStop

public void performanceStop(BMLPerformanceStopFeedback psf)
Specified by:
performanceStop in interface BMLFeedbackListener

performanceStart

public void performanceStart(BMLPerformanceStartFeedback psf)
Specified by:
performanceStart in interface BMLFeedbackListener

planningFinished

public void planningFinished(BMLTPlanningFinishedFeedback pff)
Specified by:
planningFinished in interface BMLTPlanningListener

planningStart

public void planningStart(BMLTPlanningStartFeedback psf)
Specified by:
planningStart in interface BMLTPlanningListener

queueFeedback

private void queueFeedback(String fb)

stopBmlReadSockets

public void stopBmlReadSockets()
Stop bml reading sockets preparatory to completely shutting down, or preparatory to connecting new client


stopFeedbackWriteSockets

public void stopFeedbackWriteSockets()
Stop feedback sending socket when to dropping client


run

public void run()
The main loop!

Specified by:
run in interface Runnable

dodisconnect

private void dodisconnect()
Disconnect feedback connection to client. Called from the run() loop.


doconnect

private void doconnect()
Attempt to connect to client feedback channel. If successful, isconnected is true; else, try again next round with wait = 1000. Called from the run() loop.


retryConnect

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


doshutdown

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


dosendFeedback

private void dosendFeedback(String feedback)
Send given feedbacki. If fail: drop request, drop client.