Class MrcPacketizer
- java.lang.Object
-
- jmri.jmrix.mrc.MrcTrafficController
-
- jmri.jmrix.mrc.MrcPacketizer
-
- All Implemented Interfaces:
MrcInterface
public class MrcPacketizer extends MrcTrafficController
Converts Stream-based I/O to/from Mrc messages. The "MrcInterface" side sends/receives MrcMessage objects. The connection to a MrcPortController is via a pair of *Streams, which then carry sequences of characters for transmission.This is based upon the Packetizer used for LocoNet Connections due to its speed and efficiency to handle messages. This also takes some code from the AbstractMRTrafficController, when dealing with handling replies to messages sent. The MRC Command Station sends out a poll message to each handset which then has approximately 20ms to initially respond with a request. Otherwise the Command Station will poll the next handset.
Messages come to this via the main GUI thread, and are forwarded back to listeners in that same thread. Reception and transmission are handled in dedicated threads by RcvHandler and XmtHandler objects. Those are internal classes defined here. The thread priorities are:
- RcvHandler - at highest available priority
- XmtHandler - down one, which is assumed to be above the GUI
- (everything else)
Some of the message formats used in this class are Copyright MRC, Inc. and used with permission as part of the JMRI project. That permission does not extend to uses in other software products. If you wish to use this code, algorithm or these message formats outside of JMRI, please contact Mrc Inc for separate permission.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
MrcPacketizer.Echo
(package private) static class
MrcPacketizer.Failed
(package private) class
MrcPacketizer.RcvHandler
Captive class to handle incoming characters.(package private) class
MrcPacketizer.XmtHandler
Captive class to handle transmission
-
Field Summary
Fields Modifier and Type Field Description (package private) static int
BADCOMMAND
(package private) static int
CONFIRMATIONONLY
(package private) int
consecutiveMissedPolls
MrcPortController
controller
(package private) static int
DOUBLELOCOCONTROL
protected boolean
echo
true if the external hardware is not echoing messages, so we must(package private) static int
IDLESTATE
java.io.DataInputStream
istream
(package private) int
mCurrentState
(package private) static int
MISSEDPOLL
(package private) MrcMessage
noData
(package private) byte[]
noDataMsg
java.io.OutputStream
ostream
protected java.lang.Runnable
rcvHandler
RcvHandler (a local class) object to implement the receive thread(package private) static java.lang.Object
transmitLock
(package private) static int
WAITFORCMDRECEIVED
protected java.lang.Runnable
xmtHandler
XmtHandler (a local class) object to implement the transmit threadjava.util.LinkedList<MrcMessage>
xmtList
Synchronized list used as a transmit queue.-
Fields inherited from class jmri.jmrix.mrc.MrcTrafficController
adaptermemo, cabAddress, receivedByteCount, receivedMsgCount, trafficListeners, transmittedMsgCount
-
Fields inherited from interface jmri.jmrix.mrc.MrcInterface
ALL, CLOCK, POLL, POWER, PROGRAMMING, SENSORS, THROTTLEINFO, TURNOUTS
-
-
Constructor Summary
Constructors Constructor Description MrcPacketizer()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connectPort(MrcPortController p)
Make connection to existing MrcPortController object.void
disconnectPort(MrcPortController p)
Break connection to existing MrcPortController object.boolean
isXmtBusy()
Implement abstract method to signal if there's a backlog of information waiting to be sent.protected void
messageFailed(MrcMessage m)
protected void
messageTransmitted(MrcMessage msg)
When a message is finally transmitted, forward it to listeners if echoing is needed.protected byte
readByteProtected(java.io.DataInputStream istream)
Read a single byte, protecting against various timeouts, etc.void
sendMrcMessage(MrcMessage m)
Forward a preformatted MrcMessage to the actual interface.void
startThreads()
Invoked at startup to start the threads needed here.boolean
status()
protected void
transmitWait(int waitTime, int state, java.lang.String InterruptMessage, int x)
-
Methods inherited from class jmri.jmrix.mrc.MrcTrafficController
addTrafficListener, changeTrafficListener, getAdapterMemo, getCabNumber, getReceivedByteCount, getReceivedMsgCount, getSystemPrefix, getTransmittedMsgCount, getUserName, notifyRcv, notifyXmit, removeTrafficListener, resetStatistics, setAdapterMemo, setCabNumber
-
-
-
-
Field Detail
-
echo
protected boolean echo
true if the external hardware is not echoing messages, so we must
-
xmtList
public java.util.LinkedList<MrcMessage> xmtList
Synchronized list used as a transmit queue.This is public to allow access from the internal class(es) when compiling with Java 1.1
-
xmtHandler
protected java.lang.Runnable xmtHandler
XmtHandler (a local class) object to implement the transmit thread
-
rcvHandler
protected java.lang.Runnable rcvHandler
RcvHandler (a local class) object to implement the receive thread
-
controller
public MrcPortController controller
-
istream
public java.io.DataInputStream istream
-
ostream
public java.io.OutputStream ostream
-
IDLESTATE
static final int IDLESTATE
- See Also:
- Constant Field Values
-
WAITFORCMDRECEIVED
static final int WAITFORCMDRECEIVED
- See Also:
- Constant Field Values
-
DOUBLELOCOCONTROL
static final int DOUBLELOCOCONTROL
- See Also:
- Constant Field Values
-
MISSEDPOLL
static final int MISSEDPOLL
- See Also:
- Constant Field Values
-
BADCOMMAND
static final int BADCOMMAND
- See Also:
- Constant Field Values
-
CONFIRMATIONONLY
static final int CONFIRMATIONONLY
- See Also:
- Constant Field Values
-
mCurrentState
int mCurrentState
-
consecutiveMissedPolls
int consecutiveMissedPolls
-
noData
final MrcMessage noData
-
noDataMsg
final byte[] noDataMsg
-
transmitLock
static final java.lang.Object transmitLock
-
-
Constructor Detail
-
MrcPacketizer
public MrcPacketizer()
-
-
Method Detail
-
status
public boolean status()
- Specified by:
status
in interfaceMrcInterface
- Specified by:
status
in classMrcTrafficController
-
sendMrcMessage
public void sendMrcMessage(MrcMessage m)
Forward a preformatted MrcMessage to the actual interface. The message is converted to a byte array and queue for transmission- Specified by:
sendMrcMessage
in interfaceMrcInterface
- Specified by:
sendMrcMessage
in classMrcTrafficController
- Parameters:
m
- Message to send;
-
isXmtBusy
public boolean isXmtBusy()
Implement abstract method to signal if there's a backlog of information waiting to be sent.- Specified by:
isXmtBusy
in classMrcTrafficController
- Returns:
- true if busy, false if nothing waiting to send
-
connectPort
public void connectPort(MrcPortController p)
Make connection to existing MrcPortController object.- Parameters:
p
- Port controller for connected. Save this for a later disconnect call
-
disconnectPort
public void disconnectPort(MrcPortController p)
Break connection to existing MrcPortController object. Once broken, attempts to send via "message" member will fail.- Parameters:
p
- previously connected port
-
readByteProtected
protected byte readByteProtected(java.io.DataInputStream istream) throws java.io.IOException
Read a single byte, protecting against various timeouts, etc.When a port is set to have a receive timeout (via the enableReceiveTimeout() method), some will return zero bytes or an EOFException at the end of the timeout. In that case, the read should be repeated to get the next real character.
- Parameters:
istream
- data input stream from layout- Returns:
- byte stream from interface
- Throws:
java.io.IOException
- from read errors
-
transmitWait
protected void transmitWait(int waitTime, int state, java.lang.String InterruptMessage, int x)
-
messageFailed
protected void messageFailed(MrcMessage m)
-
messageTransmitted
protected void messageTransmitted(MrcMessage msg)
When a message is finally transmitted, forward it to listeners if echoing is needed.- Parameters:
msg
- message to tag a transmitted message
-
startThreads
public void startThreads()
Invoked at startup to start the threads needed here.
-
-