Package jmri.jmrix.tmcc
Class SerialTrafficController
- java.lang.Object
-
- jmri.jmrix.AbstractMRTrafficController
-
- jmri.jmrix.tmcc.SerialTrafficController
-
- All Implemented Interfaces:
SerialInterface
public class SerialTrafficController extends AbstractMRTrafficController implements SerialInterface
Convert Stream-based I/O to/from TMCC serial messages.The "SerialInterface" side sends/receives message objects.
The connection to a SerialPortController is via a pair of *Streams, which then carry sequences of characters for transmission. Note that this processing is handled in an independent thread.
This handles the state transitions, based on the necessary state in each message.
Handles initialization, polling, output, and input for multiple Serial Nodes.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class jmri.jmrix.AbstractMRTrafficController
AbstractMRTrafficController.RcvNotifier, AbstractMRTrafficController.XmtNotifier
-
-
Field Summary
Fields Modifier and Type Field Description (package private) TmccSystemConnectionMemomMemoReference to the system connection memo.-
Fields inherited from class jmri.jmrix.AbstractMRTrafficController
allowUnexpectedReply, AUTORETRYSTATE, cmdListeners, connectionError, controller, flushReceiveChars, IDLESTATE, istream, listenerQueue, maxRcvExceptionCount, mCurrentMode, mCurrentState, mLastSender, msgQueue, mWaitBeforePoll, NORMALMODE, NOTIFIEDSTATE, OKSENDMSGSTATE, ostream, POLLSTATE, PROGRAMINGMODE, rcvException, rcvThread, replyInDispatch, threadStopRequest, timeoutFlag, timeouts, WAITMSGREPLYSTATE, WAITREPLYINNORMMODESTATE, WAITREPLYINPROGMODESTATE, waitTimePoll, xmtException, xmtRunnable, xmtThread
-
-
Constructor Summary
Constructors Constructor Description SerialTrafficController(TmccSystemConnectionMemo adaptermemo)Create a new TMCC SerialTrafficController instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intaddHeaderToOutput(byte[] msg, AbstractMRMessage m)No header neededvoidaddSerialListener(SerialListener l)protected voidaddTrailerToOutput(byte[] msg, int offset, AbstractMRMessage m)Add trailer to the outgoing byte stream.protected booleanendOfMessage(AbstractMRReply msg)protected AbstractMRMessageenterNormalMode()Sets the system to normal mode during programming while in IDLESTATE.protected AbstractMRMessageenterProgMode()Set the system to programming mode.protected voidforwardMessage(AbstractMRListener client, AbstractMRMessage m)Forward a SerialMessage to all registered SerialInterface listeners.protected voidforwardReply(AbstractMRListener client, AbstractMRReply m)Forward a SerialReply to all registered SerialInterface listeners.protected voidforwardToPort(AbstractMRMessage m, AbstractMRListener reply)Actually transmits the next message to the portTmccSystemConnectionMemogetSystemConnectionMemo()Get access to the system connection memo associated with this traffic controller.protected intlengthOfByteStream(AbstractMRMessage m)Determine how much many bytes the entire message will take, including space for header and trailerprotected voidloadChars(AbstractMRReply msg, java.io.DataInputStream istream)Get characters from the input source, and file a message.protected AbstractMRReplynewReply()protected AbstractMRMessagepollMessage()Handle initialization, output and polling for TMCC from within the running thread.protected AbstractMRListenerpollReplyHandler()voidremoveSerialListener(SerialListener l)voidsendSerialMessage(SerialMessage m, SerialListener reply)Forward a preformatted message to the actual interface.voidsetSystemConnectionMemo(TmccSystemConnectionMemo m)Set the system connection memo associated with this traffic controller.protected voidwaitForStartOfReply(java.io.DataInputStream istream)Dummy routine, to be filled by protocols that have to skip some start-of-message characters.protected voidwarnOnTimeout(AbstractMRMessage msg, AbstractMRListener l)Skip warning message, as timeouts are routine in this protocol-
Methods inherited from class jmri.jmrix.AbstractMRTrafficController
addConsoleListener, addListener, canReceive, connectionWarn, connectPort, disconnectPort, distributeReply, enterProgModeDelayTime, getLastSender, getPortName, getSynchronizeRx, handleOneIncomingReply, handleTimeout, hasTimeouts, newRcvNotifier, notifyMessage, notifyReply, portReadyToSend, portWarn, portWarnTCP, programmerIdle, readByteProtected, receiveLoop, recovery, removeListener, reportReceiveLoopException, resetTimeout, sendMessage, setAllowUnexpectedReply, setSynchronizeRx, status, terminate, terminateThreads, transmitLoop, transmitWait, unexpectedReplyStateError
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jmri.jmrix.tmcc.SerialInterface
status
-
-
-
-
Field Detail
-
mMemo
TmccSystemConnectionMemo mMemo
Reference to the system connection memo.
-
-
Constructor Detail
-
SerialTrafficController
public SerialTrafficController(TmccSystemConnectionMemo adaptermemo)
Create a new TMCC SerialTrafficController instance.- Parameters:
adaptermemo- the associated SystemConnectionMemo
-
-
Method Detail
-
addSerialListener
public void addSerialListener(SerialListener l)
- Specified by:
addSerialListenerin interfaceSerialInterface
-
removeSerialListener
public void removeSerialListener(SerialListener l)
- Specified by:
removeSerialListenerin interfaceSerialInterface
-
enterProgMode
protected AbstractMRMessage enterProgMode()
Description copied from class:AbstractMRTrafficControllerSet the system to programming mode.- Specified by:
enterProgModein classAbstractMRTrafficController- Returns:
- any message that needs to be returned to the Command Station to change modes. If no message is needed, returns null.
- See Also:
AbstractMRTrafficController.enterNormalMode()
-
enterNormalMode
protected AbstractMRMessage enterNormalMode()
Description copied from class:AbstractMRTrafficControllerSets the system to normal mode during programming while in IDLESTATE. IfAbstractMRTrafficController.programmerIdle()returns true, enterNormalMode() is called after a timeout.- Specified by:
enterNormalModein classAbstractMRTrafficController- Returns:
- any message that needs to be returned to the Command Station to change modes. If no message is needed, returns null.
- See Also:
AbstractMRTrafficController.enterProgMode()
-
getSystemConnectionMemo
public TmccSystemConnectionMemo getSystemConnectionMemo()
Get access to the system connection memo associated with this traffic controller.- Returns:
- associated systemConnectionMemo object
-
setSystemConnectionMemo
public void setSystemConnectionMemo(TmccSystemConnectionMemo m)
Set the system connection memo associated with this traffic controller.- Parameters:
m- associated systemConnectionMemo object
-
forwardMessage
protected void forwardMessage(AbstractMRListener client, AbstractMRMessage m)
Forward a SerialMessage to all registered SerialInterface listeners.- Specified by:
forwardMessagein classAbstractMRTrafficController- Parameters:
client- abstract listener.m- message to forward.
-
forwardReply
protected void forwardReply(AbstractMRListener client, AbstractMRReply m)
Forward a SerialReply to all registered SerialInterface listeners.- Specified by:
forwardReplyin classAbstractMRTrafficController
-
pollMessage
protected AbstractMRMessage pollMessage()
Handle initialization, output and polling for TMCC from within the running thread.- Specified by:
pollMessagein classAbstractMRTrafficController- Returns:
- Formatted poll message
-
pollReplyHandler
protected AbstractMRListener pollReplyHandler()
- Specified by:
pollReplyHandlerin classAbstractMRTrafficController
-
sendSerialMessage
public void sendSerialMessage(SerialMessage m, SerialListener reply)
Forward a preformatted message to the actual interface.- Specified by:
sendSerialMessagein interfaceSerialInterface
-
newReply
protected AbstractMRReply newReply()
- Specified by:
newReplyin classAbstractMRTrafficController
-
endOfMessage
protected boolean endOfMessage(AbstractMRReply msg)
- Specified by:
endOfMessagein classAbstractMRTrafficController
-
loadChars
protected void loadChars(AbstractMRReply msg, java.io.DataInputStream istream) throws java.io.IOException
Description copied from class:AbstractMRTrafficControllerGet characters from the input source, and file a message.Returns only when the message is complete.
Only used in the Receive thread.
Handles timeouts on read by ignoring zero-length reads.
- Overrides:
loadCharsin classAbstractMRTrafficController- Parameters:
msg- message to fillistream- character source.- Throws:
java.io.IOException- when presented by the input source.
-
waitForStartOfReply
protected void waitForStartOfReply(java.io.DataInputStream istream) throws java.io.IOException
Description copied from class:AbstractMRTrafficControllerDummy routine, to be filled by protocols that have to skip some start-of-message characters.- Overrides:
waitForStartOfReplyin classAbstractMRTrafficController- Parameters:
istream- input source- Throws:
java.io.IOException- from underlying operations
-
addHeaderToOutput
protected int addHeaderToOutput(byte[] msg, AbstractMRMessage m)
No header needed- Overrides:
addHeaderToOutputin classAbstractMRTrafficController- Parameters:
msg- The output byte streamm- Message results- Returns:
- next location in the stream to fill
-
addTrailerToOutput
protected void addTrailerToOutput(byte[] msg, int offset, AbstractMRMessage m)
Add trailer to the outgoing byte stream.- Overrides:
addTrailerToOutputin classAbstractMRTrafficController- Parameters:
msg- The output byte streamoffset- the first byte not yet usedm- output message to extend
-
lengthOfByteStream
protected int lengthOfByteStream(AbstractMRMessage m)
Determine how much many bytes the entire message will take, including space for header and trailer- Overrides:
lengthOfByteStreamin classAbstractMRTrafficController- Parameters:
m- The message to be sent- Returns:
- Number of bytes for msg (3) plus preceeding NOP (3)
-
warnOnTimeout
protected void warnOnTimeout(AbstractMRMessage msg, AbstractMRListener l)
Skip warning message, as timeouts are routine in this protocol- Overrides:
warnOnTimeoutin classAbstractMRTrafficController
-
forwardToPort
protected void forwardToPort(AbstractMRMessage m, AbstractMRListener reply)
Actually transmits the next message to the port- Overrides:
forwardToPortin classAbstractMRTrafficController- Parameters:
m- the message to sendreply- the Listener sending the message, often provided as 'this'- See Also:
AbstractMRTrafficController.sendMessage(AbstractMRMessage, AbstractMRListener)
-
-