Package jmri.jmrix.grapevine
Class SerialTrafficController
- java.lang.Object
-
- jmri.jmrix.AbstractMRTrafficController
-
- jmri.jmrix.AbstractMRNodeTrafficController
-
- jmri.jmrix.grapevine.SerialTrafficController
-
- All Implemented Interfaces:
SerialInterface
public class SerialTrafficController extends AbstractMRNodeTrafficController implements SerialInterface
Convert Stream-based I/O to/from Grapevine 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 SerialNodes.
-
-
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) byte[]
buffer
protected int
currentAddr
(package private) boolean
logDebug
(package private) GrapevineSystemConnectionMemo
mMemo
Reference to the system connection memo.(package private) SerialSensorManager
mSensorManager
(package private) int
nextReplyLen
(package private) int
state
-
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(GrapevineSystemConnectionMemo adaptermemo)
Create a new TrafficController instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected int
addHeaderToOutput(byte[] msg, AbstractMRMessage m)
Add header to the outgoing byte stream.void
addSerialListener(SerialListener l)
protected void
addTrailerToOutput(byte[] msg, int offset, AbstractMRMessage m)
Although this protocol doesn't use a trailer, we implement this method to set the expected reply address for this message.void
connectPort(SerialPortController p)
Make connection to existing PortController (adapter) object.(package private) boolean
doNextStep(AbstractMRReply msg, java.io.DataInputStream istream)
Execute a state machine to parse messages from the input characters.protected boolean
endOfMessage(AbstractMRReply msg)
protected AbstractMRMessage
enterNormalMode()
Sets the system to normal mode during programming while in IDLESTATE.protected AbstractMRMessage
enterProgMode()
Set the system to programming mode.protected void
forwardMessage(AbstractMRListener client, AbstractMRMessage m)
Forward a SerialMessage to all registered SerialInterface listeners.protected void
forwardReply(AbstractMRListener client, AbstractMRReply m)
Forward a SerialReply to all registered SerialInterface listeners.protected void
forwardToPort(AbstractMRMessage m, AbstractMRListener reply)
Actually transmit the next message to the port.int
getMinimumNodeAddress()
Get minimum address of an Grapevine node as set on this TrafficController.GrapevineSystemConnectionMemo
getSystemConnectionMemo()
Get access to the system connection memo associated with this traffic controller.protected void
handleTimeout(AbstractMRMessage m, AbstractMRListener l)
void
initializeSerialNode(SerialNode node)
Set up for initialization of a Serial node.protected int
lengthOfByteStream(AbstractMRMessage m)
Determine how much many bytes the entire message will take, including space for header and trailerprotected void
loadBuffer(AbstractMRReply msg)
protected void
loadChars(AbstractMRReply msg, java.io.DataInputStream istream)
Get characters from the input source, and file a message.protected AbstractMRReply
newReply()
protected AbstractMRMessage
pollMessage()
Handle initialization, output and polling for Grapevine from within the running thread.protected AbstractMRListener
pollReplyHandler()
void
removeSerialListener(SerialListener l)
protected void
resetTimeout(AbstractMRMessage m)
void
sendSerialMessage(SerialMessage m, SerialListener reply)
Forward a preformatted message to the actual interface.void
setSensorManager(SerialSensorManager m)
void
setSystemConnectionMemo(GrapevineSystemConnectionMemo m)
Set the system connection memo associated with this traffic controller.protected void
waitForStartOfReply(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, 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, unexpectedReplyStateError, 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.grapevine.SerialInterface
status
-
-
-
-
Field Detail
-
logDebug
boolean logDebug
-
mSensorManager
SerialSensorManager mSensorManager
-
mMemo
GrapevineSystemConnectionMemo mMemo
Reference to the system connection memo.
-
currentAddr
protected int currentAddr
-
nextReplyLen
int nextReplyLen
-
buffer
byte[] buffer
-
state
int state
-
-
Constructor Detail
-
SerialTrafficController
public SerialTrafficController(GrapevineSystemConnectionMemo adaptermemo)
Create a new TrafficController instance.- Parameters:
adaptermemo
- the associated SystemConnectionMemo
-
-
Method Detail
-
getMinimumNodeAddress
public int getMinimumNodeAddress()
Get minimum address of an Grapevine node as set on this TrafficController.- Returns:
- minimum node address.
-
connectPort
public void connectPort(SerialPortController p)
Make connection to existing PortController (adapter) object.- Parameters:
p
- the Adapter we're connecting to
-
addSerialListener
public void addSerialListener(SerialListener l)
- Specified by:
addSerialListener
in interfaceSerialInterface
-
removeSerialListener
public void removeSerialListener(SerialListener l)
- Specified by:
removeSerialListener
in interfaceSerialInterface
-
initializeSerialNode
public void initializeSerialNode(SerialNode node)
Set up for initialization of a Serial node.- Parameters:
node
- node to initialize.
-
enterProgMode
protected AbstractMRMessage enterProgMode()
Description copied from class:AbstractMRTrafficController
Set the system to programming mode.- Specified by:
enterProgMode
in 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:AbstractMRTrafficController
Sets the system to normal mode during programming while in IDLESTATE. IfAbstractMRTrafficController.programmerIdle()
returns true, enterNormalMode() is called after a timeout.- Specified by:
enterNormalMode
in 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 SerialMessage to all registered SerialInterface listeners.- Specified by:
forwardMessage
in 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:
forwardReply
in classAbstractMRTrafficController
-
setSensorManager
public void setSensorManager(SerialSensorManager m)
-
pollMessage
protected AbstractMRMessage pollMessage()
Handle initialization, output and polling for Grapevine from within the running thread.- Specified by:
pollMessage
in classAbstractMRTrafficController
- Returns:
- Formatted poll message
-
handleTimeout
protected void handleTimeout(AbstractMRMessage m, AbstractMRListener l)
- Overrides:
handleTimeout
in classAbstractMRTrafficController
-
resetTimeout
protected void resetTimeout(AbstractMRMessage m)
- Overrides:
resetTimeout
in classAbstractMRTrafficController
-
pollReplyHandler
protected AbstractMRListener pollReplyHandler()
- Specified by:
pollReplyHandler
in classAbstractMRTrafficController
-
sendSerialMessage
public void sendSerialMessage(SerialMessage m, SerialListener reply)
Forward a preformatted message to the actual interface.- Specified by:
sendSerialMessage
in interfaceSerialInterface
-
getSystemConnectionMemo
public GrapevineSystemConnectionMemo getSystemConnectionMemo()
Get access to the system connection memo associated with this traffic controller.- Returns:
- associated systemConnectionMemo object
-
setSystemConnectionMemo
public void setSystemConnectionMemo(GrapevineSystemConnectionMemo m)
Set the system connection memo associated with this traffic controller.- Parameters:
m
- associated systemConnectionMemo object
-
newReply
protected AbstractMRReply newReply()
- Specified by:
newReply
in classAbstractMRTrafficController
-
endOfMessage
protected boolean endOfMessage(AbstractMRReply msg)
- Specified by:
endOfMessage
in classAbstractMRTrafficController
-
forwardToPort
protected void forwardToPort(AbstractMRMessage m, AbstractMRListener reply)
Description copied from class:AbstractMRTrafficController
Actually transmit the next message to the port.- Overrides:
forwardToPort
in classAbstractMRTrafficController
- Parameters:
m
- the message to sendreply
- the Listener sending the message, often provided as 'this'- See Also:
AbstractMRTrafficController.sendMessage(AbstractMRMessage, AbstractMRListener)
-
loadChars
protected void loadChars(AbstractMRReply msg, java.io.DataInputStream istream) throws java.io.IOException
Description copied from class:AbstractMRTrafficController
Get 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:
loadChars
in classAbstractMRTrafficController
- Parameters:
msg
- message to fillistream
- character source.- Throws:
java.io.IOException
- when presented by the input source.
-
doNextStep
boolean doNextStep(AbstractMRReply msg, java.io.DataInputStream istream) throws java.io.IOException
Execute a state machine to parse messages from the input characters. May consume one or more than one character.- Parameters:
msg
- Message to parseistream
- Source of data- Returns:
- true when the message has been completely loaded
- Throws:
java.io.IOException
- from underlying operation
-
loadBuffer
protected void loadBuffer(AbstractMRReply msg)
-
waitForStartOfReply
protected void waitForStartOfReply(java.io.DataInputStream istream) throws java.io.IOException
Description copied from class:AbstractMRTrafficController
Dummy routine, to be filled by protocols that have to skip some start-of-message characters.- Overrides:
waitForStartOfReply
in 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:
addHeaderToOutput
in 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)
Although this protocol doesn't use a trailer, we implement this method to set the expected reply address for this message.- Overrides:
addTrailerToOutput
in classAbstractMRTrafficController
- Parameters:
msg
- The output byte streamoffset
- the first byte not yet usedm
- the original message
-
lengthOfByteStream
protected int lengthOfByteStream(AbstractMRMessage m)
Determine how much many bytes the entire message will take, including space for header and trailer- Overrides:
lengthOfByteStream
in classAbstractMRTrafficController
- Parameters:
m
- The message to be sent- Returns:
- Number of bytes
-
-