FourMS
Class MessagePoster

java.lang.Object
  extended by java.util.TimerTask
      extended by FourMS.MessagePoster
All Implemented Interfaces:
java.lang.Runnable

public class MessagePoster
extends java.util.TimerTask

Describes a single message which is sent from software/simulator controller and includes methods which will be used to verify the correct acknowledgments are received, timeouts are executed and retries are made. This class is responsible for sending the actual message to the 4MS down the wire via the Port Server. Once the ACK is received for this message then the object can be used to deliver another message should the parent need to.

Version:
$Id: MessagePoster.java,v 1.7 2006/04/07 08:05:39 cb Exp $
Author:
Craige Bevil

Field Summary
private  java.io.OutputStream ControllerFD
          This is the file descriptor of the connection on the port server that corresponds to the 4MS which is being used.
private  i4MSMessageSender MessageOriginator
          This is a reference to the object that sent the message in the first instance which will be informed when the message has either been delivered or not delivered
private static java.util.Timer MessagePosterTimer
          This is a timer object which will be used for scheduling actions on the message posters.
private  int MessageSequenceNumber
          This is the message sequence number that is associated with the message that we sent in the first instance
private  int MessageState
          This is the current state of the message, this should be one of the following from the fourMSConstants class.
private  byte[] PostedMessage
          This is the original message which was to be delivered by this message poster
private  int SecsLeftBeforeResend
          The amount of seconds left before we attempt to resend the message
private  int SendRetryCount
          This is the number of times that we will attempt to send the message before we give up completely
private  MessageHandler SyslogHandler
          This is a reference to a syslog handler which will be used to report errors etc.
 
Constructor Summary
MessagePoster(java.io.OutputStream ControllerFD, MessageHandler SyslogHandler)
          This is the constructor for the class.
 
Method Summary
 boolean checkForAck(FourMSMessage FourMSReply)
          This method will be called when there is an incoming message from the 4MS and will check if the ACK received corresponds to the message that was sent from by this message poster.
 void deliverMessage(byte[] MessageToSend, i4MSMessageSender MessageInitiator, int MessageSeqNo)
          The purpose of this method is to send the string which represents a well formed 4MS message, to the 4MS microprocessor via the Port Server.
 int getMessageState()
          Returns the delivery status of the message which has been sent to the 4MS transmitter
static void main(java.lang.String[] argv)
          This is for testing the class to ensure that it is working ok
 void run()
          This method represents the obligation of the class to implement the abstract interface of the TimerTask superclass which it extends.
private  void setMessageState(int State)
          This method allows the current state of the message to be set.
 void setOutputDevice(java.io.OutputStream ControllerFD)
          This will be used to set the output file descriptor which will be used when sending messages.
 
Methods inherited from class java.util.TimerTask
cancel, scheduledExecutionTime
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MessageSequenceNumber

private int MessageSequenceNumber
This is the message sequence number that is associated with the message that we sent in the first instance


MessageState

private int MessageState
This is the current state of the message, this should be one of the following from the fourMSConstants class. UNSENT DELIVERED ACKNOWLEDGED UNDELIVERABLE


PostedMessage

private byte[] PostedMessage
This is the original message which was to be delivered by this message poster


SendRetryCount

private int SendRetryCount
This is the number of times that we will attempt to send the message before we give up completely


SecsLeftBeforeResend

private int SecsLeftBeforeResend
The amount of seconds left before we attempt to resend the message


MessagePosterTimer

private static final java.util.Timer MessagePosterTimer
This is a timer object which will be used for scheduling actions on the message posters. These will use these to keep track of when they need to resend message etc when ACKs are not received.


ControllerFD

private java.io.OutputStream ControllerFD
This is the file descriptor of the connection on the port server that corresponds to the 4MS which is being used.


MessageOriginator

private i4MSMessageSender MessageOriginator
This is a reference to the object that sent the message in the first instance which will be informed when the message has either been delivered or not delivered


SyslogHandler

private MessageHandler SyslogHandler
This is a reference to a syslog handler which will be used to report errors etc.

Constructor Detail

MessagePoster

public MessagePoster(java.io.OutputStream ControllerFD,
                     MessageHandler SyslogHandler)
This is the constructor for the class.

Parameters:
ControllerFD - - This will be used when transmitting a message back to a 4MS transmitter.
SyslogHandler - This will be used to report errors that arise in the transmission of the messages to the receiver.
Method Detail

deliverMessage

public void deliverMessage(byte[] MessageToSend,
                           i4MSMessageSender MessageInitiator,
                           int MessageSeqNo)
                    throws UnableToCommunicateWith4MSException
The purpose of this method is to send the string which represents a well formed 4MS message, to the 4MS microprocessor via the Port Server.

Parameters:
MessageToSend - - This is the well formatted message to be delivered to the 4MS. All conversion should have been done at this point and the string should be exactly as it should be in order to be sent to the 4MS.
MessageInitiator - This is a reference to the oject that sent the message in the first place
MessageSeqNo - We must record the number of the message that we sent so that when ACKs come in we can check to see if they were for the message that we sent
Throws:
UnableToCommunicateWith4MSException

checkForAck

public boolean checkForAck(FourMSMessage FourMSReply)
This method will be called when there is an incoming message from the 4MS and will check if the ACK received corresponds to the message that was sent from by this message poster. If it is then we can conclude that the message has been sent and acknowledged, so we can inform the original sender of the message that the message has now been delivered and that we can return true to indicate to the ER412 message handler that this message is now completed.

Parameters:
FourMSReply - This is the message that came back from the 4MS.
Returns:
boolean Returns true if the message was an acknowledgment

setMessageState

private void setMessageState(int State)
This method allows the current state of the message to be set. It should be one of FourMSConstants.UNSENT FourMSConstants.WAITINGACK FourMSConstants.UNDELIVERABLE FourMSConstants.ACKNOWLEDGED

Parameters:
State - - This is the new state of the message.

run

public void run()
This method represents the obligation of the class to implement the abstract interface of the TimerTask superclass which it extends. This method is called every second and if there is a message waiting for an ACK coming back and the prescribed time expires waiting for it to expire then we send it again

Specified by:
run in interface java.lang.Runnable
Specified by:
run in class java.util.TimerTask

getMessageState

public int getMessageState()
Returns the delivery status of the message which has been sent to the 4MS transmitter

Returns:
int

main

public static void main(java.lang.String[] argv)
This is for testing the class to ensure that it is working ok

Parameters:
argv -

setOutputDevice

public void setOutputDevice(java.io.OutputStream ControllerFD)
This will be used to set the output file descriptor which will be used when sending messages. This will be called throughout the execution of the application should the 4MS transmitter at the other end closes it's connection and then is restarted

Parameters:
ControllerFD - - This is the output stream associated with the controller