Class LnPacketizer
- All Implemented Interfaces:
LocoNetInterface
- Direct Known Subclasses:
IBLnPacketizer,LnOverTcpPacketizer,LnPacketizerStrict,LnPr2Packetizer,LnStreamPortPacketizer,UhlenbrockPacketizer
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)
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static classprotected classCaptive class to handle incoming characters.(package private) classCaptive class to handle transmission. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected LnPortControllerprotected booleanTrue if the external hardware is not echoing messages, so we must.protected RunnableRcvHandler (a local class) object to implement the receive threadprotected Threadprotected booleanFlag that threads should terminate as soon as they can.protected RunnableXmtHandler (a local class) object to implement the transmit thread.protected LinkedTransferQueue<byte[]>Synchronized list used as a transmit queue.protected ThreadFields inherited from class jmri.jmrix.loconet.LnTrafficController
listeners, memo, receivedByteCount, receivedMsgCount, transmittedMsgCountFields inherited from interface jmri.jmrix.loconet.LocoNetInterface
ALL, POWER, PROGRAMMING, SENSORS, SLOTINFO, TURNOUTS -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidMake connection to an existing LnPortController object.voidBreak connection to an existing LnPortController object.voiddispose()Clean up any resources, particularly threads.booleanImplement abstract method to signal if there's a backlog of information waiting to be sent.protected voidmessageTransmitted(byte[] msg) When a message is finally transmitted, forward it to listeners if echoing is needed.protected bytereadByteProtected(DataInputStream istream) Read a single byte, protecting against various timeouts, etc.voidForward a preformatted LocoNetMessage to the actual interface.voidInvoked at startup to start the threads needed here.booleanstatus()Check whether an implementation is operational.voidTerminate the receive and transmit threads.Methods inherited from class jmri.jmrix.loconet.LnTrafficController
addLocoNetListener, getReceivedByteCount, getReceivedMsgCount, getSystemConnectionMemo, getTransmittedMsgCount, notify, removeLocoNetListener, resetStatistics, setSystemConnectionMemo
-
Field Details
-
echo
True if the external hardware is not echoing messages, so we must. -
xmtList
Synchronized list used as a transmit queue. -
xmtHandler
XmtHandler (a local class) object to implement the transmit thread.We create this object in startThreads() as each packetizer uses different handlers. So long as the object is created before using it to sync it works.
-
rcvHandler
RcvHandler (a local class) object to implement the receive thread -
controller
-
istream
-
ostream
-
rcvThread
-
xmtThread
-
threadStopRequest
Flag that threads should terminate as soon as they can.
-
-
Constructor Details
-
LnPacketizer
-
-
Method Details
-
status
Check whether an implementation is operational. Returns true if operational.- Specified by:
statusin interfaceLocoNetInterface- Specified by:
statusin classLnTrafficController- Returns:
- true if implementation is operational.
-
sendLocoNetMessage
Forward a preformatted LocoNetMessage to the actual interface.Checksum is computed and overwritten here, then the message is converted to a byte array and queued for transmission.
- Specified by:
sendLocoNetMessagein interfaceLocoNetInterface- Specified by:
sendLocoNetMessagein classLnTrafficController- Parameters:
m- Message to send; will be updated with CRC
-
isXmtBusy
Implement abstract method to signal if there's a backlog of information waiting to be sent.- Specified by:
isXmtBusyin classLnTrafficController- Returns:
- true if busy, false if nothing waiting to send
-
connectPort
Make connection to an existing LnPortController object.- Parameters:
p- Port controller for connected. Save this for a later disconnect call
-
disconnectPort
Break connection to an existing LnPortController object. Once broken, attempts to send via "message" member will fail.- Parameters:
p- previously connected port
-
readByteProtected
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- stream to read from- Returns:
- buffer of received data
- Throws:
IOException- failure during stream read
-
messageTransmitted
When a message is finally transmitted, forward it to listeners if echoing is needed.- Parameters:
msg- message sent
-
startThreads
Invoked at startup to start the threads needed here. -
dispose
Clean up any resources, particularly threads.The object can't be used after this.
- Overrides:
disposein classLnTrafficController
-
terminateThreads
Terminate the receive and transmit threads.This is intended to be used only by testing subclasses.
-