|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjmri.jmrix.AbstractMRTrafficController
public abstract class AbstractMRTrafficController
Abstract base for TrafficControllers in a Message/Reply protocol.
Two threads are used for the actual communication. The "Transmit" thread handles pushing characters to the port, and also changing the mode. The "Receive" thread converts characters from the input stream into replies.
A third thread is registered by the constructor as a shutdown hook. It triggers the necessary cleanup code
"Mode" refers to the state of the command station communications.
"State" refers to the internal state machine used to control the mode,
e.g. to send commands to change mode.
"Idle" is a special case, where there is no communications in process,
and the port is waiting to do something.
| Nested Class Summary | |
|---|---|
(package private) static class |
AbstractMRTrafficController.CleanupHook
Internal class to handle traffic controller cleanup. |
protected static class |
AbstractMRTrafficController.RcvNotifier
Internal class to remember the Reply object and destination listener with a reply is received. |
protected static class |
AbstractMRTrafficController.XmtNotifier
Internal class to remember the Message object and destination listener when a message is queued for notification. |
| Constructor Summary | |
|---|---|
AbstractMRTrafficController()
|
|
| Method Summary | |
|---|---|
protected int |
addHeaderToOutput(byte[] msg,
AbstractMRMessage m)
Add header to the outgoing byte stream. |
protected void |
addListener(AbstractMRListener 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 necessary |
protected void |
connectionWarn()
|
void |
connectPort(AbstractPortController p)
Make connection to existing PortController object. |
void |
disconnectPort(AbstractPortController p)
Break connection to existing PortController object. |
protected abstract boolean |
endOfMessage(AbstractMRReply r)
|
protected abstract AbstractMRMessage |
enterNormalMode()
|
protected abstract AbstractMRMessage |
enterProgMode()
|
protected int |
enterProgModeDelayTime()
|
protected void |
finalize()
|
protected abstract void |
forwardMessage(AbstractMRListener client,
AbstractMRMessage m)
Implement this to forward a specific message type to a protocol-specific listener interface. |
protected abstract void |
forwardReply(AbstractMRListener client,
AbstractMRReply m)
|
protected void |
forwardToPort(AbstractMRMessage m,
AbstractMRListener reply)
Actually transmits the next message to the port |
AbstractMRListener |
getLastSender()
|
String |
getPortName()
Get the port name for this connection |
AbstractMRTrafficController |
getSelfLock()
|
void |
handleOneIncomingReply()
Handle each reply when complete. |
protected void |
handleTimeout(AbstractMRMessage msg,
AbstractMRListener l)
|
boolean |
hasTimeouts()
|
protected int |
lengthOfByteStream(AbstractMRMessage m)
Determine how much many bytes the entire message will take, including space for header and trailer |
protected void |
loadChars(AbstractMRReply msg,
DataInputStream istream)
Get characters from the input source, and file a message. |
protected AbstractMRTrafficController.RcvNotifier |
newRcvNotifier(AbstractMRReply pMsg,
AbstractMRListener pDest,
AbstractMRTrafficController pTC)
|
protected abstract AbstractMRReply |
newReply()
|
protected void |
notifyMessage(AbstractMRMessage m,
AbstractMRListener notMe)
Forward a Message to registered listeners. |
protected void |
notifyReply(AbstractMRReply r,
AbstractMRListener dest)
Forward a "Reply" from layout to registered listeners. |
protected abstract 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 TC should just sleep. |
protected abstract AbstractMRListener |
pollReplyHandler()
|
boolean |
portReadyToSend(AbstractPortController p)
Check to see if PortController object can be sent to. |
protected void |
portWarn(Exception e)
|
protected void |
portWarnTCP(Exception e)
|
protected boolean |
programmerIdle()
|
protected byte |
readByteProtected(DataInputStream istream)
Read a single byte, protecting against various timeouts, etc. |
void |
receiveLoop()
Handle incoming characters. |
protected void |
removeListener(AbstractMRListener l)
|
protected void |
reportReceiveLoopException(Exception e)
Report error on receive loop. |
protected void |
resetTimeout(AbstractMRMessage msg)
|
protected void |
sendMessage(AbstractMRMessage m,
AbstractMRListener reply)
This is invoked with messages to be forwarded to the port. |
protected void |
setAllowUnexpectedReply(boolean expected)
|
protected abstract void |
setInstance()
|
boolean |
status()
|
protected void |
terminate()
|
protected void |
transmitWait(int waitTime,
int state,
String InterruptMessage)
|
protected void |
waitForStartOfReply(DataInputStream istream)
Dummy routine, to be filled by protocols that have to skip some start-of-message characters. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected Vector<AbstractMRListener> cmdListeners
protected AbstractMRListener mLastSender
protected volatile int mCurrentMode
public static final int NORMALMODE
public static final int PROGRAMINGMODE
protected volatile int mCurrentState
public static final int IDLESTATE
public static final int NOTIFIEDSTATE
public static final int WAITMSGREPLYSTATE
public static final int WAITREPLYINPROGMODESTATE
public static final int WAITREPLYINNORMMODESTATE
public static final int OKSENDMSGSTATE
public static final int AUTORETRYSTATE
public static final int POLLSTATE
protected boolean allowUnexpectedReply
LinkedList<AbstractMRMessage> msgQueue
LinkedList<AbstractMRListener> listenerQueue
protected boolean replyInDispatch
protected boolean flushReceiveChars
protected int mWaitBeforePoll
protected long waitTimePoll
protected boolean xmtException
boolean connectionError
public AbstractPortController controller
Thread xmtThread
protected Runnable xmtRunnable
Thread rcvThread
protected DataInputStream istream
protected OutputStream ostream
protected boolean rcvException
protected int maxRcvExceptionCount
static org.slf4j.Logger log
| Constructor Detail |
|---|
public AbstractMRTrafficController()
| Method Detail |
|---|
public AbstractMRTrafficController getSelfLock()
protected abstract void setInstance()
protected void addListener(AbstractMRListener l)
protected void removeListener(AbstractMRListener l)
protected void notifyMessage(AbstractMRMessage m,
AbstractMRListener notMe)
m - Message to be forwarded intactnotMe - One (optional) listener to be skipped, usually
because it's the originating object.
protected abstract void forwardMessage(AbstractMRListener client,
AbstractMRMessage m)
protected abstract AbstractMRMessage pollMessage()
protected abstract AbstractMRListener pollReplyHandler()
protected abstract AbstractMRMessage enterProgMode()
protected abstract AbstractMRMessage enterNormalMode()
protected boolean programmerIdle()
protected int enterProgModeDelayTime()
protected void setAllowUnexpectedReply(boolean expected)
protected void notifyReply(AbstractMRReply r,
AbstractMRListener dest)
r - Reply to be forwarded intactdest - One (optional) listener to be skipped, usually
because it's the originating object.
protected abstract void forwardReply(AbstractMRListener client,
AbstractMRReply m)
protected void sendMessage(AbstractMRMessage m,
AbstractMRListener reply)
protected void transmitWait(int waitTime,
int state,
String InterruptMessage)
public boolean hasTimeouts()
protected void handleTimeout(AbstractMRMessage msg,
AbstractMRListener l)
protected void resetTimeout(AbstractMRMessage msg)
protected int addHeaderToOutput(byte[] msg,
AbstractMRMessage m)
msg - The output byte stream
protected void addTrailerToOutput(byte[] msg,
int offset,
AbstractMRMessage m)
msg - The output byte streamoffset - the first byte not yet usedprotected int lengthOfByteStream(AbstractMRMessage m)
m - The message to be sent
protected void forwardToPort(AbstractMRMessage m,
AbstractMRListener reply)
protected void connectionWarn()
protected void portWarn(Exception e)
protected void portWarnTCP(Exception e)
public boolean status()
public void connectPort(AbstractPortController p)
public String getPortName()
public void disconnectPort(AbstractPortController p)
public boolean portReadyToSend(AbstractPortController p)
throws Exception
Exceptionpublic void receiveLoop()
connectPort.
Each turn of the loop is the receipt of a single message.
protected void reportReceiveLoopException(Exception e)
protected abstract AbstractMRReply newReply()
protected abstract boolean endOfMessage(AbstractMRReply r)
protected void waitForStartOfReply(DataInputStream istream)
throws IOException
IOException
protected byte readByteProtected(DataInputStream istream)
throws IOException
When a gnu.io 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.
IOException
protected void loadChars(AbstractMRReply msg,
DataInputStream istream)
throws IOException
Returns only when the message is complete.
Only used in the Receive thread.
Handles timeouts on read by ignoring zero-length reads.
msg - message to fillistream - character source.
IOException - when presented by the input source.protected boolean canReceive()
public void handleOneIncomingReply()
throws IOException
(This is public for testing purposes) Runs in the "Receive" thread.
IOExceptionpublic AbstractMRListener getLastSender()
protected final void finalize()
throws Throwable
finalize in class ObjectThrowableprotected void terminate()
protected AbstractMRTrafficController.RcvNotifier newRcvNotifier(AbstractMRReply pMsg,
AbstractMRListener pDest,
AbstractMRTrafficController pTC)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||