Package jmri.jmrix.dcc4pc
Class Dcc4PcTrafficController
- java.lang.Object
-
- jmri.jmrix.AbstractMRTrafficController
-
- jmri.jmrix.dcc4pc.Dcc4PcTrafficController
-
- All Implemented Interfaces:
Dcc4PcInterface
public class Dcc4PcTrafficController extends AbstractMRTrafficController implements Dcc4PcInterface
Converts Stream-based I/O to/from DCC4PC messages. The "Dcc4PcInterface" side sends/receives message objects.The connection to a Dcc4PcPortController 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.
-
-
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) Dcc4PcSystemConnectionMemo
adaptermemo
(package private) int
GETMOREDATA
(package private) Dcc4PcReply
lastIncomplete
(package private) boolean
loading
(package private) Dcc4PcMessage
mLastMessage
(package private) Dcc4PcMessage
mLastSentMessage
(package private) boolean
normalFlushReceiveChars
(package private) SerialPort
port
(package private) boolean
readingData
static int
RETRIEVINGDATA
protected boolean
unsolicitedSensorMessageSeen
(package private) boolean
waitingForMore
-
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 Dcc4PcTrafficController()
Create a new DccPcTrafficController instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addDcc4PcListener(Dcc4PcListener l)
protected void
addTrailerToOutput(byte[] msg, int offset, AbstractMRMessage m)
Add trailer to the outgoing byte stream.protected boolean
canReceive()
Override in the system specific code if necessaryvoid
connectPort(AbstractPortController p)
Make connection to an existing PortController object.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 Dcc4PcMessage to all registered Dcc4PcInterface listeners.protected void
forwardReply(AbstractMRListener client, AbstractMRReply r)
Forward a Dcc4PcReply to all registered Dcc4PcInterface listeners.protected void
forwardToPort(AbstractMRMessage m, AbstractMRListener reply)
Actually transmit the next message to the port.void
handleOneIncomingReply()
Handle each reply when complete.protected void
handleTimeout(AbstractMRMessage msg, AbstractMRListener l)
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()
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.protected AbstractMRListener
pollReplyHandler()
void
removeDcc4PcListener(Dcc4PcListener l)
void
sendDcc4PcMessage(Dcc4PcMessage m, Dcc4PcListener reply)
Forward a preformatted message to the actual interface.void
setAdapterMemo(Dcc4PcSystemConnectionMemo memo)
protected void
transmitWait(int waitTime, int state, java.lang.String InterruptMessage)
-
Methods inherited from class jmri.jmrix.AbstractMRTrafficController
addConsoleListener, addHeaderToOutput, addListener, connectionWarn, disconnectPort, distributeReply, enterProgModeDelayTime, getLastSender, getPortName, getSynchronizeRx, hasTimeouts, lengthOfByteStream, newRcvNotifier, notifyMessage, notifyReply, portReadyToSend, portWarn, portWarnTCP, programmerIdle, readByteProtected, receiveLoop, recovery, removeListener, reportReceiveLoopException, resetTimeout, sendMessage, setAllowUnexpectedReply, setSynchronizeRx, status, terminate, terminateThreads, transmitLoop, unexpectedReplyStateError, waitForStartOfReply, 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.dcc4pc.Dcc4PcInterface
status
-
-
-
-
Field Detail
-
adaptermemo
Dcc4PcSystemConnectionMemo adaptermemo
-
RETRIEVINGDATA
public static final int RETRIEVINGDATA
- See Also:
- Constant Field Values
-
unsolicitedSensorMessageSeen
protected boolean unsolicitedSensorMessageSeen
-
mLastMessage
Dcc4PcMessage mLastMessage
-
mLastSentMessage
Dcc4PcMessage mLastSentMessage
-
port
SerialPort port
-
lastIncomplete
Dcc4PcReply lastIncomplete
-
waitingForMore
boolean waitingForMore
-
loading
boolean loading
-
GETMOREDATA
final int GETMOREDATA
- See Also:
- Constant Field Values
-
normalFlushReceiveChars
boolean normalFlushReceiveChars
-
readingData
boolean readingData
-
-
Constructor Detail
-
Dcc4PcTrafficController
public Dcc4PcTrafficController()
Create a new DccPcTrafficController instance.
-
-
Method Detail
-
setAdapterMemo
public void setAdapterMemo(Dcc4PcSystemConnectionMemo memo)
-
addDcc4PcListener
public void addDcc4PcListener(Dcc4PcListener l)
- Specified by:
addDcc4PcListener
in interfaceDcc4PcInterface
-
removeDcc4PcListener
public void removeDcc4PcListener(Dcc4PcListener l)
- Specified by:
removeDcc4PcListener
in interfaceDcc4PcInterface
-
forwardMessage
protected void forwardMessage(AbstractMRListener client, AbstractMRMessage m)
Forward a Dcc4PcMessage to all registered Dcc4PcInterface listeners.- Specified by:
forwardMessage
in classAbstractMRTrafficController
- Parameters:
client
- abstract listener.m
- message to forward.
-
forwardReply
protected void forwardReply(AbstractMRListener client, AbstractMRReply r)
Forward a Dcc4PcReply to all registered Dcc4PcInterface listeners.- Specified by:
forwardReply
in classAbstractMRTrafficController
-
pollMessage
protected AbstractMRMessage pollMessage()
Description copied from class:AbstractMRTrafficController
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:
pollMessage
in classAbstractMRTrafficController
- Returns:
- Formatted poll message
-
pollReplyHandler
protected AbstractMRListener pollReplyHandler()
- Specified by:
pollReplyHandler
in classAbstractMRTrafficController
-
sendDcc4PcMessage
public void sendDcc4PcMessage(Dcc4PcMessage m, Dcc4PcListener reply)
Forward a preformatted message to the actual interface.- Specified by:
sendDcc4PcMessage
in interfaceDcc4PcInterface
- Parameters:
m
- Message to be sent.reply
- Listener to be notified of reply.
-
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()
-
addTrailerToOutput
protected void addTrailerToOutput(byte[] msg, int offset, AbstractMRMessage m)
Description copied from class:AbstractMRTrafficController
Add trailer to the outgoing byte stream.- Overrides:
addTrailerToOutput
in classAbstractMRTrafficController
- Parameters:
msg
- the output byte streamoffset
- the first byte not yet usedm
- output message to extend
-
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)
-
connectPort
public void connectPort(AbstractPortController p)
Description copied from class:AbstractMRTrafficController
Make connection to an existing PortController object.- Overrides:
connectPort
in classAbstractMRTrafficController
- Parameters:
p
- the PortController
-
newReply
protected AbstractMRReply newReply()
- Specified by:
newReply
in classAbstractMRTrafficController
-
canReceive
protected boolean canReceive()
Description copied from class:AbstractMRTrafficController
Override in the system specific code if necessary- Overrides:
canReceive
in classAbstractMRTrafficController
- Returns:
- true if it is okay to buffer receive characters into a reply message. When false, discard char received
-
endOfMessage
protected boolean endOfMessage(AbstractMRReply msg)
- Specified by:
endOfMessage
in classAbstractMRTrafficController
-
handleTimeout
protected void handleTimeout(AbstractMRMessage msg, AbstractMRListener l)
- Overrides:
handleTimeout
in classAbstractMRTrafficController
-
handleOneIncomingReply
public void handleOneIncomingReply() throws java.io.IOException
Handle each reply when complete.(This is public for testing purposes) Runs in the "Receive" thread.
- Overrides:
handleOneIncomingReply
in classAbstractMRTrafficController
- Throws:
java.io.IOException
- on error.
-
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.
-
transmitWait
protected void transmitWait(int waitTime, int state, java.lang.String InterruptMessage)
- Overrides:
transmitWait
in classAbstractMRTrafficController
-
-