Package jmri.jmrix.cmri.serial
Class SerialTrafficController
- java.lang.Object
-
- jmri.jmrix.AbstractMRTrafficController
-
- jmri.jmrix.AbstractMRNodeTrafficController
-
- jmri.jmrix.cmri.serial.SerialTrafficController
-
- All Implemented Interfaces:
SerialInterface
public class SerialTrafficController extends AbstractMRNodeTrafficController implements SerialInterface
Converts Stream-based I/O to/from C/MRI 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 java.util.ArrayList<java.lang.Integer>cmriNetPollList(package private) CMRInetMetricsCollectormetricsCollectorCreate a new C/MRI SerialTrafficController instance.(package private) SerialSensorManagermSensorManagerbooleanpollNetwork-
Fields inherited from class jmri.jmrix.AbstractMRNodeTrafficController
curSerialNodeIndex, maxNode, minNode, nodeArray, numNodes
-
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()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected intaddHeaderToOutput(byte[] msg, AbstractMRMessage m)Add header to the outgoing byte stream.voidaddSerialListener(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 message to all registered listeners.protected voidforwardReply(AbstractMRListener client, AbstractMRReply m)Forward a reply to all registered listeners.intgetInitTimeout()CMRInetMetricsDatagetMetricsData()Expose metrics databooleangetPollNetwork()intgetXmitTimeout()protected voidhandleTimeout(AbstractMRMessage m, AbstractMRListener l)voidinitializeSerialNode(SerialNode node)Initialize a CMRI node.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()Handles initialization, output and polling for C/MRI Serial Nodes from within the running thread.protected AbstractMRListenerpollReplyHandler()voidremoveSerialListener(SerialListener l)protected voidresetTimeout(AbstractMRMessage m)voidsendSerialMessage(SerialMessage m, SerialListener reply)Forward a pre-formatted message to the actual interface.voidsetInitTimeout(int init_Timeout)voidsetPollNetwork(boolean OnOff)voidsetSensorManager(SerialSensorManager m)voidsetXmitTimeout(int init_XmitTimeout)protected voidunexpectedReplyStateError(int State, java.lang.String msgString)Log an error message for a message received in an unexpected state.protected voidwaitForStartOfReply(java.io.DataInputStream istream)Dummy routine, to be filled by protocols that have to skip some start-of-message characters.-
Methods inherited from class jmri.jmrix.AbstractMRNodeTrafficController
deleteNode, getMustInit, getNode, getNodeFromAddress, getNumNodes, init, registerNode, setMustInit, setMustInit
-
Methods inherited from class jmri.jmrix.AbstractMRTrafficController
addConsoleListener, addListener, canReceive, connectionWarn, connectPort, disconnectPort, distributeReply, enterProgModeDelayTime, forwardToPort, getLastSender, getPortName, getSynchronizeRx, handleOneIncomingReply, hasTimeouts, newRcvNotifier, notifyMessage, notifyReply, portReadyToSend, portWarn, portWarnTCP, programmerIdle, readByteProtected, receiveLoop, recovery, removeListener, reportReceiveLoopException, sendMessage, setAllowUnexpectedReply, setSynchronizeRx, status, terminate, terminateThreads, transmitLoop, transmitWait, warnOnTimeout
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jmri.jmrix.cmri.serial.SerialInterface
status
-
-
-
-
Field Detail
-
metricsCollector
CMRInetMetricsCollector metricsCollector
Create a new C/MRI SerialTrafficController instance.
-
mSensorManager
SerialSensorManager mSensorManager
-
pollNetwork
public boolean pollNetwork
-
cmriNetPollList
public java.util.ArrayList<java.lang.Integer> cmriNetPollList
-
-
Constructor Detail
-
SerialTrafficController
public SerialTrafficController()
-
-
Method Detail
-
addSerialListener
public void addSerialListener(SerialListener l)
- Specified by:
addSerialListenerin interfaceSerialInterface
-
removeSerialListener
public void removeSerialListener(SerialListener l)
- Specified by:
removeSerialListenerin interfaceSerialInterface
-
initializeSerialNode
public void initializeSerialNode(SerialNode node)
Initialize a CMRI node.- Parameters:
node- the node to initialize
-
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()
-
getMetricsData
public CMRInetMetricsData getMetricsData()
Expose metrics data- Returns:
- metrics data
-
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()
-
forwardMessage
protected void forwardMessage(AbstractMRListener client, AbstractMRMessage m)
Forward a message to all registered listeners.- Specified by:
forwardMessagein classAbstractMRTrafficController- Parameters:
client- the listener to receive the message; may throw an uncaught exception if not aSerialListenerm- the message to forward; may throw an uncaught exception if not aSerialMessage
-
forwardReply
protected void forwardReply(AbstractMRListener client, AbstractMRReply m)
Forward a reply to all registered listeners.- Specified by:
forwardReplyin classAbstractMRTrafficController- Parameters:
client- the listener to receive the reply; may throw an uncaught exception if not aSerialListenerm- the reply to forward; may throw an uncaught exception if not aSerialMessage
-
setSensorManager
public void setSensorManager(SerialSensorManager m)
-
setPollNetwork
public void setPollNetwork(boolean OnOff)
-
getPollNetwork
public boolean getPollNetwork()
-
setInitTimeout
public void setInitTimeout(int init_Timeout)
-
getInitTimeout
public int getInitTimeout()
-
setXmitTimeout
public void setXmitTimeout(int init_XmitTimeout)
-
getXmitTimeout
public int getXmitTimeout()
-
pollMessage
protected AbstractMRMessage pollMessage()
Handles initialization, output and polling for C/MRI Serial Nodes from within the running thread.Invoked if it's appropriate to do low-priority polling of the command station, this should return the next message to send, or null if the TrafficController should just sleep.
- Specified by:
pollMessagein classAbstractMRTrafficController- Returns:
- Formatted poll message
-
unexpectedReplyStateError
protected void unexpectedReplyStateError(int State, java.lang.String msgString)
Description copied from class:AbstractMRTrafficControllerLog an error message for a message received in an unexpected state.- Overrides:
unexpectedReplyStateErrorin classAbstractMRTrafficController- Parameters:
State- message state.msgString- message string.
-
handleTimeout
protected void handleTimeout(AbstractMRMessage m, AbstractMRListener l)
- Overrides:
handleTimeoutin classAbstractMRTrafficController
-
resetTimeout
protected void resetTimeout(AbstractMRMessage m)
- Overrides:
resetTimeoutin classAbstractMRTrafficController
-
pollReplyHandler
protected AbstractMRListener pollReplyHandler()
- Specified by:
pollReplyHandlerin classAbstractMRTrafficController
-
sendSerialMessage
public void sendSerialMessage(SerialMessage m, SerialListener reply)
Forward a pre-formatted message to the actual interface.- Specified by:
sendSerialMessagein interfaceSerialInterface- Parameters:
m- the message to forwardreply- the listener for the response to m
-
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)
Add header to the outgoing byte stream.- Overrides:
addHeaderToOutputin classAbstractMRTrafficController- Parameters:
msg- the output byte streamm- the message in msg- 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- the message in msg
-
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
-
-