FourMS
Class MessageListener

java.lang.Object
  extended by FourMS.MessageListener
All Implemented Interfaces:
java.lang.Runnable, java.util.EventListener, javax.comm.SerialPortEventListener

public class MessageListener
extends java.lang.Object
implements java.lang.Runnable, javax.comm.SerialPortEventListener

This class is a threaded class which will be used to monitor the input port which will be responsible for piping into an application data that is coming from an input stream from for example, a 4MS simulator or from the 4MS itself. It will also be responsible for delagating incoming messages to a message handler which will not only parse the message for correctness, but also then pass the message on to the appropriate object which can then ascertain the messages semantics.

It should be noted that this class is used by both the 4MS instrument controller tasks and the simulation tasks as well

Version:
$Id: MessageListener.java,v 1.13 2006/05/18 09:19:43 cb Exp $
Author:
Craige Bevil


Field Summary
private  int BufferSize
          This is the default size of the incoming message buffer
(package private)  java.net.Socket clientSocket
          This is a connection to a simulator if there we are in simulation mode
private  FourMSMessageHandler ER412MessageHandler
          This object will be informed when a message comes in and it will be it's responsibility to handle the message and ultimately pass it on to another object which will have the responsibility of processing the message
private  boolean FourMSTransceiverClient
          Signifies if this is a 4MS transceiver client
private  java.io.BufferedReader inputStream
          These will be used for sending and receiving from the 4MS simulator
private  java.io.InputStreamReader portServerInputStream
          This will be used for sending data back to the
private  java.lang.String PortServerPort
          This is the name of the port on the local machine which we will open in order to communicate with the port server should be running with a portserver.
private  char[] readBuffer
          This is a buffer into which data will be read from the portserver and then stored.
private  java.lang.Thread Runner
          This object runs within it's own thread
(package private)  javax.comm.SerialPort serialPort
          If we are connected to a port server then this is the file handle through which we can send and receive data
private  int SocketNumber
          In the case that we are using a standard TCP/IP socket to connect to the ER412 transmitter, we need to know the port number of the listening socket which we need to open
private  int SocketType
          This is the type of socket which is being used.
private  MessageHandler SyslogMessageHandler
          This is a syslog object which will be used to report errors etc
(package private)  java.net.ServerSocket TCPIPServerSocket
          This holds the TCP/IP server socket which will be used for communication should we be using normal TCP/IP for communication with the ER412 transmitter
private  long TimeOfLast4MSComms
          This is used to get the time of the last comms from the 4MS which is attached
 
Constructor Summary
MessageListener(boolean FourMSTransceiverClient, int SocketNumber, int BufferSize, FourMSMessageHandler ER412MessageHandler, MessageHandler SyslogMessageHandler)
          This is the constructor for the class which will be used to define an object that will use a TCP/IP socket for input.
MessageListener(java.lang.String PortServerPort, FourMSMessageHandler ER412MessageHandler, MessageHandler SyslogMessageHandler)
          This is the constructor which will be used to set up th message listener so that it will talk to FourMS instrument through a port portserver.
 
Method Summary
 long getTimeOfLast4MSComms()
          This will return the time of the last communications with the 4MS module
 void listenForInput()
          When called it starts the listening thread which will result in the object listening for input from the simulator if we are in simulation mode or alternatively, from the 4MS portserver.
 void run()
          This satifies the requirement of the runnable interface as this object has it's own thread This method is called and results in the object going into a wait state waiting for input from the transmitter and subsequently sending the incoming data onto the message handler.
 void serialEvent(javax.comm.SerialPortEvent event)
          This is the event handler which will be called when there is an event on the portserver serial port file descriptor .
 void setupPortServerPort()
          This will be used to open up the serial port which goes out to the portserver and subsequently out to the 4MS.
 void shutdownConnections()
          Closes down the connection to the portserver or the simulator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

clientSocket

java.net.Socket clientSocket
This is a connection to a simulator if there we are in simulation mode


serialPort

javax.comm.SerialPort serialPort
If we are connected to a port server then this is the file handle through which we can send and receive data


readBuffer

private char[] readBuffer
This is a buffer into which data will be read from the portserver and then stored.


inputStream

private java.io.BufferedReader inputStream
These will be used for sending and receiving from the 4MS simulator


portServerInputStream

private java.io.InputStreamReader portServerInputStream
This will be used for sending data back to the


BufferSize

private int BufferSize
This is the default size of the incoming message buffer


SocketType

private int SocketType
This is the type of socket which is being used. This can either be a standard TCP/IP connection or it can be a port server connection.


SocketNumber

private int SocketNumber
In the case that we are using a standard TCP/IP socket to connect to the ER412 transmitter, we need to know the port number of the listening socket which we need to open


FourMSTransceiverClient

private boolean FourMSTransceiverClient
Signifies if this is a 4MS transceiver client


ER412MessageHandler

private FourMSMessageHandler ER412MessageHandler
This object will be informed when a message comes in and it will be it's responsibility to handle the message and ultimately pass it on to another object which will have the responsibility of processing the message


Runner

private java.lang.Thread Runner
This object runs within it's own thread


TimeOfLast4MSComms

private long TimeOfLast4MSComms
This is used to get the time of the last comms from the 4MS which is attached


PortServerPort

private java.lang.String PortServerPort
This is the name of the port on the local machine which we will open in order to communicate with the port server should be running with a portserver.


SyslogMessageHandler

private MessageHandler SyslogMessageHandler
This is a syslog object which will be used to report errors etc


TCPIPServerSocket

java.net.ServerSocket TCPIPServerSocket
This holds the TCP/IP server socket which will be used for communication should we be using normal TCP/IP for communication with the ER412 transmitter

Constructor Detail

MessageListener

public MessageListener(java.lang.String PortServerPort,
                       FourMSMessageHandler ER412MessageHandler,
                       MessageHandler SyslogMessageHandler)
This is the constructor which will be used to set up th message listener so that it will talk to FourMS instrument through a port portserver.

Parameters:
PortServerPort - This is the name of the device which will be used to communicate with the portserver

ER412MessageHandler - This will be used for parsing and delagating incoming messages from the 4MS to other objects for assigning semantic actions to the messages

SyslogMessageHandler - This will be used for reporting errors

MessageListener

public MessageListener(boolean FourMSTransceiverClient,
                       int SocketNumber,
                       int BufferSize,
                       FourMSMessageHandler ER412MessageHandler,
                       MessageHandler SyslogMessageHandler)
This is the constructor for the class which will be used to define an object that will use a TCP/IP socket for input. This constructor will be called in the case that the system will be run in simulation mode. The simulator software will call this constructor and the instrument controller software will call this method should it determine that it has been instructed to run in simulation mode.

Parameters:
FourMSTransceiverClient - If this is true then we consider ourselves to be a ER412 client.
SocketNumber - - This is the socket number to open a listening socket upon in the case that we will be using standard TCP/IP sockets in order to connect to the 4MS controller software from the simulator
BufferSize - This is the size of the input buffer which is to be used by the object
ER412MessageHandler - This will be used by the class when handing over incoming messages to another object for processing
SyslogMessageHandler - This is the syslog handler which will be used for reporting errors when they occur
Method Detail

getTimeOfLast4MSComms

public long getTimeOfLast4MSComms()
This will return the time of the last communications with the 4MS module

Returns:
long Time in seconds since 1st Jan 1970 when the last communications were recived from the 4MS module

setupPortServerPort

public void setupPortServerPort()
This will be used to open up the serial port which goes out to the portserver and subsequently out to the 4MS. The source of the name of the port to be used is held in the WHT ICS database table instrument.

The transmission characteristics for the 4MS modules and the portserver are set to the following;

Characteristic Setting
Baud 9600
Stop bits 1
Parity Even
Data bits 7
XON/XOFF On


listenForInput

public void listenForInput()
When called it starts the listening thread which will result in the object listening for input from the simulator if we are in simulation mode or alternatively, from the 4MS portserver.


run

public void run()
This satifies the requirement of the runnable interface as this object has it's own thread This method is called and results in the object going into a wait state waiting for input from the transmitter and subsequently sending the incoming data onto the message handler.

Note that in the case that we are connected to a portserver using the javax.comm API, events on that port will come into the application (such as incoming data) and processed through the SerialPortEventListener method.

Specified by:
run in interface java.lang.Runnable

serialEvent

public void serialEvent(javax.comm.SerialPortEvent event)
This is the event handler which will be called when there is an event on the portserver serial port file descriptor . The only event we are registered for is the DATA_AVAILABLE event which is called when there is input coming from the 4MS module.

This method is not used when the software is running in simulation mode.

Specified by:
serialEvent in interface javax.comm.SerialPortEventListener
Parameters:
event - This contains the details of the event on the serial port.

shutdownConnections

public void shutdownConnections()
Closes down the connection to the portserver or the simulator.