Class LnPacketizer
- java.lang.Object
-
- jmri.jmrix.loconet.LnTrafficController
-
- jmri.jmrix.loconet.LnPacketizer
-
- All Implemented Interfaces:
LocoNetInterface
- Direct Known Subclasses:
IBLnPacketizer
,LnOverTcpPacketizer
,LnPacketizerStrict
,LnPr2Packetizer
,LnStreamPortPacketizer
,UhlenbrockPacketizer
public class LnPacketizer extends LnTrafficController
Converts Stream-based I/O to/from LocoNet messages. The "LocoNetInterface" side sends/receives LocoNetMessage objects. The connection to a LnPortController is via a pair of *Streams, which then carry sequences of characters for transmission.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 Classes Modifier and Type Class Description (package private) static class
LnPacketizer.Echo
protected class
LnPacketizer.RcvHandler
Captive class to handle incoming characters.(package private) class
LnPacketizer.XmtHandler
Captive class to handle transmission.
-
Field Summary
Fields Modifier and Type Field Description protected LnPortController
controller
protected boolean
echo
True if the external hardware is not echoing messages, so we must.java.io.DataInputStream
istream
java.io.OutputStream
ostream
protected java.lang.Runnable
rcvHandler
RcvHandler (a local class) object to implement the receive threadprotected java.lang.Thread
rcvThread
protected boolean
threadStopRequest
Flag that threads should terminate as soon as they can.protected java.lang.Runnable
xmtHandler
XmtHandler (a local class) object to implement the transmit thread.protected java.util.concurrent.LinkedTransferQueue<byte[]>
xmtList
Synchronized list used as a transmit queue.protected java.lang.Thread
xmtThread
-
Fields inherited from class jmri.jmrix.loconet.LnTrafficController
listeners, memo, receivedByteCount, receivedMsgCount, transmittedMsgCount
-
Fields inherited from interface jmri.jmrix.loconet.LocoNetInterface
ALL, POWER, PROGRAMMING, SENSORS, SLOTINFO, TURNOUTS
-
-
Constructor Summary
Constructors Constructor Description LnPacketizer(LocoNetSystemConnectionMemo m)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
connectPort(LnPortController p)
Make connection to an existing LnPortController object.void
disconnectPort(LnPortController p)
Break connection to an existing LnPortController object.void
dispose()
Clean up any resources, particularly threads.boolean
isXmtBusy()
Implement abstract method to signal if there's a backlog of information waiting to be sent.protected void
messageTransmitted(byte[] msg)
When a message is finally transmitted, forward it to listeners if echoing is needed.protected byte
readByteProtected(java.io.DataInputStream istream)
Read a single byte, protecting against various timeouts, etc.void
sendLocoNetMessage(LocoNetMessage m)
Forward a preformatted LocoNetMessage to the actual interface.void
startThreads()
Invoked at startup to start the threads needed here.boolean
status()
Check whether an implementation is operational.void
terminateThreads()
Terminate the receive and transmit threads.-
Methods inherited from class jmri.jmrix.loconet.LnTrafficController
addLocoNetListener, getReceivedByteCount, getReceivedMsgCount, getSystemConnectionMemo, getTransmittedMsgCount, notify, removeLocoNetListener, resetStatistics, setSystemConnectionMemo
-
-
-
-
Field Detail
-
echo
protected boolean echo
True if the external hardware is not echoing messages, so we must.
-
xmtList
protected java.util.concurrent.LinkedTransferQueue<byte[]> xmtList
Synchronized list used as a transmit queue.
-
xmtHandler
protected java.lang.Runnable 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
protected java.lang.Runnable rcvHandler
RcvHandler (a local class) object to implement the receive thread
-
controller
protected LnPortController controller
-
istream
public java.io.DataInputStream istream
-
ostream
public java.io.OutputStream ostream
-
rcvThread
protected java.lang.Thread rcvThread
-
xmtThread
protected java.lang.Thread xmtThread
-
threadStopRequest
protected volatile boolean threadStopRequest
Flag that threads should terminate as soon as they can.
-
-
Constructor Detail
-
LnPacketizer
public LnPacketizer(LocoNetSystemConnectionMemo m)
-
-
Method Detail
-
status
public boolean status()
Check whether an implementation is operational. Returns true if operational.- Specified by:
status
in interfaceLocoNetInterface
- Specified by:
status
in classLnTrafficController
- Returns:
- true if implementation is operational.
-
sendLocoNetMessage
public void sendLocoNetMessage(LocoNetMessage m)
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:
sendLocoNetMessage
in interfaceLocoNetInterface
- Specified by:
sendLocoNetMessage
in classLnTrafficController
- Parameters:
m
- Message to send; will be updated with CRC
-
isXmtBusy
public boolean isXmtBusy()
Implement abstract method to signal if there's a backlog of information waiting to be sent.- Specified by:
isXmtBusy
in classLnTrafficController
- Returns:
- true if busy, false if nothing waiting to send
-
connectPort
public void connectPort(LnPortController p)
Make connection to an existing LnPortController object.- Parameters:
p
- Port controller for connected. Save this for a later disconnect call
-
disconnectPort
public void disconnectPort(LnPortController p)
Break connection to an existing LnPortController object. Once broken, attempts to send via "message" member will fail.- Parameters:
p
- previously connected port
-
readByteProtected
protected byte readByteProtected(java.io.DataInputStream istream) throws java.io.IOException
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:
java.io.IOException
- failure during stream read
-
messageTransmitted
protected void messageTransmitted(byte[] msg)
When a message is finally transmitted, forward it to listeners if echoing is needed.- Parameters:
msg
- message sent
-
startThreads
public void startThreads()
Invoked at startup to start the threads needed here.
-
dispose
public void dispose()
Clean up any resources, particularly threads.The object can't be used after this.
- Overrides:
dispose
in classLnTrafficController
-
terminateThreads
public void terminateThreads()
Terminate the receive and transmit threads.This is intended to be used only by testing subclasses.
-
-