FourMS
Class FourMSMessageHandler

java.lang.Object
  extended by FourMS.FourMSMessageHandler

public class FourMSMessageHandler
extends java.lang.Object

This class will form the lowest level interface to the 4MS controller and will access the port server device drivers in order to do so. It will be responsible for ultimately sending messages out to the 4MS, handling the low level acknowledging and resending of messages should there be a need.

This class will not be responsible for processing the incoming messages simply just handling them and passing them onto the objects whcih will establish the semantics of the messages. The data will be read in from an object outside of this class and then passed into this class for appropriating it to the relevent object for processing.

Version:
$Id: FourMSMessageHandler.java,v 1.9 2005/11/22 10:00:28 cb Exp $
Author:
Craige Bevil

Field Summary
private  java.lang.String Destination
          This is the value of the destination field in the ER412 message which is returned to the 4MS transmitter.
(package private)  java.util.regex.Pattern FourMSMessageACKMatchRE
          This will be used for matching ACKs and broadcast messages which are messages with no message body
(package private)  java.util.regex.Pattern FourMSMessageMatchRE
          This a precompiled regular expression which will be used to find the first 4MS message which adheres to ER412 protocol contained in a string in the input buffer.
private  java.lang.StringBuffer Incoming4MSMessageBuffer
          We keep all of the incoming message data buffered so that in the event of parts of messages being received, we can buffer them until the rest of the message is received
private  int MAXNOOFTRANSMISSIONFAILURES
          This is the maximum number of consecutive communications errors which we can receive before we reset the communications layer
private  int MAXOUTSTANDINGMSGS
          This is the maximum number of outstanding messages which can be active at any one point, by this I mean the number of messages that have been sent by the object which are awaiting acknowledgement from the intended receiver.
private  iDispatchMessageInterface MessageDispatcher
          This is a reference to an object which will be responsible for dispatching the incoming messages to the appropriate object for providing the semantics associated with the requested action
private  int MessageSequenceNumber
          This is the message sequence number of the next message to be transmitted, this will be used in constructing any responses to the ER412 protocol transmitter which initiated a response from the object in the first instance
(package private)  java.util.regex.Pattern NET200MatchRE
          This is a precompiled regular expression which will be used to find NET200 meessages in the incoming message buffer.
private  java.io.OutputStream OutputPort
          This is an object through which we can access the 4MS through the port server.
 MessagePoster[] SentMessages
          Maintains a record of all of the outstanding messages which have not been acknowledged by the 4MS after been sent.
private  java.lang.String Source
          This is the value of the source field in the ER412 message which is returned to the 4MS transmitter.
private  MessageHandler SyslogMessageHandler
          This will be used for the syslog handling of errors by the class
private  int TransmissionErrorCount
          Records the number of communications errors which have occured on transmission.
 
Constructor Summary
FourMSMessageHandler(iDispatchMessageInterface Dispatcher, MessageHandler SyslogHandler)
          This is the constructor for the class, it accepts a file descriptor object which will be used to send messages to the transmitter
 
Method Summary
 void clearIncomingMessageBuffer()
          Instructs the class to clear out it's incoming message buffer of messages that have been received from the 4MS module.
static void main(java.lang.String[] args)
          A main method for testing the class to ensure that everything is working as expected.
private  FourMSMessage parseIncoming4MSMessage()
          The purpose of this method will be used to parse the incoming message buffer looking for the first well formed ER412 compliant message, then to break the message down into it's constituent parts and return the result to the caller.
 FourMSMessage processIncomingMessage(char[] IncomingMessage, int AmountRead)
          This method will be called when there is a ER412 protocol message received by the system.
private  boolean sendAcknowledgement(FourMSMessage MessageToReplyTo)
          This method results in an acknowledgment being sent to the sender for the last message that was sent to it.
 boolean sendMessage(int MessageType, java.lang.String MessageToSend, i4MSMessageSender MessageInitiator)
          Builds a message packet suitable to be sent to the 4MS and then creates a MessagePoster object which will be responsible for delivering the message and handling the retries should not ACK be sent.
private  boolean sendXONMessage()
          To send an XON message to the 4MS in order to provoke it into sending data which it might otherwise be buffering.
 void setFourMSOutputPort(java.io.OutputStream FourMSOutputPort)
          This method will be used to set the output port so that the message poster objects have a reference to a valid file descriptor upon which to transmit data.
 void setMsgEndPointParameters(java.lang.String ER412SourceName, java.lang.String ER412DestinationName)
          This allows the setting of the message end point ie the expected values for the source and destination fields in incoming messages to the software system.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAXOUTSTANDINGMSGS

private final int MAXOUTSTANDINGMSGS
This is the maximum number of outstanding messages which can be active at any one point, by this I mean the number of messages that have been sent by the object which are awaiting acknowledgement from the intended receiver.

See Also:
Constant Field Values

MAXNOOFTRANSMISSIONFAILURES

private final int MAXNOOFTRANSMISSIONFAILURES
This is the maximum number of consecutive communications errors which we can receive before we reset the communications layer

See Also:
Constant Field Values

TransmissionErrorCount

private int TransmissionErrorCount
Records the number of communications errors which have occured on transmission. Should we get defined number of transmission errors back to back then it results in the communications layer being reset.


Incoming4MSMessageBuffer

private java.lang.StringBuffer Incoming4MSMessageBuffer
We keep all of the incoming message data buffered so that in the event of parts of messages being received, we can buffer them until the rest of the message is received


MessageSequenceNumber

private int MessageSequenceNumber
This is the message sequence number of the next message to be transmitted, this will be used in constructing any responses to the ER412 protocol transmitter which initiated a response from the object in the first instance


Destination

private java.lang.String Destination
This is the value of the destination field in the ER412 message which is returned to the 4MS transmitter. The sending of the source and the destination fields in the ER412 messages that get sent from this task are no longer dynamic as the messages are sent point to point now instead of across a network


Source

private java.lang.String Source
This is the value of the source field in the ER412 message which is returned to the 4MS transmitter. The sending of the source and the destination fields in the ER412 messages that get sent from this task are no longer dynamic as the messages are sent point to point now instead of across a network.


SentMessages

public MessagePoster[] SentMessages
Maintains a record of all of the outstanding messages which have not been acknowledged by the 4MS after been sent. These are known as message slots.


OutputPort

private java.io.OutputStream OutputPort
This is an object through which we can access the 4MS through the port server. This will be passed into the routine sometime on start up. This class will not use it directly rather the Message classes which are instantiated will use this in order to send messages back to the 4MS or the controlling task in the case of the simulator.


NET200MatchRE

final java.util.regex.Pattern NET200MatchRE
This is a precompiled regular expression which will be used to find NET200 meessages in the incoming message buffer. Note that these messages do NOT adhere to the ER412 message protocol as they are followed by parenthesis therefore are not picked up by the normal regular expression. These therefore need to be addressed separately otherwise if I modify the main regular expression to detect messages without parenthesis then we could end up letting incomplete messages through.


FourMSMessageMatchRE

final java.util.regex.Pattern FourMSMessageMatchRE
This a precompiled regular expression which will be used to find the first 4MS message which adheres to ER412 protocol contained in a string in the input buffer.


SyslogMessageHandler

private MessageHandler SyslogMessageHandler
This will be used for the syslog handling of errors by the class


MessageDispatcher

private iDispatchMessageInterface MessageDispatcher
This is a reference to an object which will be responsible for dispatching the incoming messages to the appropriate object for providing the semantics associated with the requested action


FourMSMessageACKMatchRE

final java.util.regex.Pattern FourMSMessageACKMatchRE
This will be used for matching ACKs and broadcast messages which are messages with no message body

Constructor Detail

FourMSMessageHandler

public FourMSMessageHandler(iDispatchMessageInterface Dispatcher,
                            MessageHandler SyslogHandler)
This is the constructor for the class, it accepts a file descriptor object which will be used to send messages to the transmitter

Parameters:
Dispatcher - This is a class which will be used for dispatching the messages once that they identified in the input stream from the 4MS
SyslogHandler - This will be used for handling of error messages to the syslog
Method Detail

sendXONMessage

private boolean sendXONMessage()
To send an XON message to the 4MS in order to provoke it into sending data which it might otherwise be buffering. The 4MS modules seem to intermittingly stop sending data so this method is called every bow and again to ensure that they do not stop sending data. This was never necessary with PFIP but the CAGB and the ISIS 4MS modules can sometimes take a while to start sending data after the 4MS control software has been restarted. I am summising that this is because after the 4MS software has been stopped, the modules stop sending data and need to be instructed to do so again.

Returns:
boolean Whether the XON has been sent OK or not.

parseIncoming4MSMessage

private FourMSMessage parseIncoming4MSMessage()
                                       throws Malformed4MSMessageException
The purpose of this method will be used to parse the incoming message buffer looking for the first well formed ER412 compliant message, then to break the message down into it's constituent parts and return the result to the caller. If no message is found the null is returned.

Returns:
FourMS.FourMSMessage Returns null if no message is found
Throws:
Malformed4MSMessageException - In the case that the message that we receive is badly formatted we throw this exception

processIncomingMessage

public FourMSMessage processIncomingMessage(char[] IncomingMessage,
                                            int AmountRead)
                                     throws Malformed4MSMessageException
This method will be called when there is a ER412 protocol message received by the system. It will be responsible for parsing the input and ensuring that the structure of the message is within the constraints laid down in document ER-412. If the message structure is found to be correct then an ACK will be sent back to the 4MS to acknowledge the receipt of the message. If the incoming message is found to be syntactically correct then the method will hand the message over to the message dispatcher in order that it is delivered to the correct part of the software system for processing (this is normally the mechanism with which it is associated).

Parameters:
IncomingMessage - - This is a character array which holds the data that was read in from the 4MS. If this is set to null then it is assumed that there is no more input data to add.
AmountRead - - This is the number of characters that were read in.
Returns:
FourMSMessage
Throws:
Malformed4MSMessageException

sendAcknowledgement

private boolean sendAcknowledgement(FourMSMessage MessageToReplyTo)
This method results in an acknowledgment being sent to the sender for the last message that was sent to it. This is a message of type 81. Note that we do not need to use a MessagePoster object in order to send this message as we do not expect a response back.

Parameters:
MessageToReplyTo - - This is the message that we should reply to, it should contain all of the details that we will need to construct a messsage that we can.
Returns:
boolean - Returns true if the acknowledgement was sent, this should only be set to false if there were some serious problems with the communications to the port server or the simulation software was not running for instance

main

public static void main(java.lang.String[] args)
A main method for testing the class to ensure that everything is working as expected.

Parameters:
args - Command line arguments.

sendMessage

public boolean sendMessage(int MessageType,
                           java.lang.String MessageToSend,
                           i4MSMessageSender MessageInitiator)
Builds a message packet suitable to be sent to the 4MS and then creates a MessagePoster object which will be responsible for delivering the message and handling the retries should not ACK be sent.

Parameters:
MessageType - - This is the message type of the message which is to be sent, this should be one of COMMANDMSG, STATUSRESPONSEMSG, DATARESPONSEMSG, ACKMSG or BROADCASTMSG.
MessageToSend - - This is the message to be sent which should be in a format which adheres to ER412 protocol.
MessageInitiator - - This is the object which is attempting to send the message. This is necessary so that in the case no acknowledge is received from the 4MS in response to the transmission, the object may be informed. This should be set to null if the caller is not expecting to be informed of the delivery status of the message.
Returns:
boolean

setFourMSOutputPort

public void setFourMSOutputPort(java.io.OutputStream FourMSOutputPort)
This method will be used to set the output port so that the message poster objects have a reference to a valid file descriptor upon which to transmit data.

Parameters:
FourMSOutputPort - - This is the output stream through which we can communicate with the 4MS

setMsgEndPointParameters

public void setMsgEndPointParameters(java.lang.String ER412SourceName,
                                     java.lang.String ER412DestinationName)
This allows the setting of the message end point ie the expected values for the source and destination fields in incoming messages to the software system.

Parameters:
ER412SourceName - This is name of message source on the ER412 network. Normally this will be something like SYS.AGCA, SYS.ISIS or SYS.PFIP
ER412DestinationName - This is name of message destination on the ER412 network. Normally this will be something like AGCA, ISIS or PFIP

clearIncomingMessageBuffer

public void clearIncomingMessageBuffer()
Instructs the class to clear out it's incoming message buffer of messages that have been received from the 4MS module.