use of jmri.jmrix.AbstractMRReply in project JMRI by JMRI.
the class Dcc4PcTrafficController method handleOneIncomingReply.
/**
* Handle each reply when complete.
* <P>
* (This is public for testing purposes) Runs in the "Receive" thread.
*
*/
@Override
public void handleOneIncomingReply() throws java.io.IOException {
// we sit in this until the message is complete, relying on
// threading to let other stuff happen
// Create message off the right concrete class
AbstractMRReply msg = newReply();
// message exists, now fill it
loadChars(msg, istream);
if (mLastSentMessage != null) {
//log.debug(mLastMessage.getElement(0));
if (mLastSentMessage.isForChildBoard()) {
if (log.isDebugEnabled()) {
log.debug("This is a message for a child board " + ((Dcc4PcReply) msg).toHexString());
log.debug("Originate " + (mLastMessage).toHexString());
}
if ((mLastSentMessage.getNumDataElements() - 1) == msg.getElement(1)) {
log.debug("message lengths match");
waitingForMore = true;
try {
Thread.sleep(10);
} catch (Exception ex) {
log.debug(ex.getLocalizedMessage(), ex);
}
//log.debug("We do not forward the response to the listener as it has not been formed");
lastIncomplete = null;
forwardToPort(Dcc4PcMessage.getResponse());
return;
} else {
if (log.isDebugEnabled()) {
log.debug("Not all of the command was sent, we need to figure out a way to resend the bits");
log.debug("Original Message length " + mLastSentMessage.getNumDataElements());
log.debug("What CID has procced in size " + (byte) msg.getElement(1));
log.debug("Reply is in error " + ((Dcc4PcReply) msg).toHexString());
}
}
} else if (mLastSentMessage.getElement(0) == 0x0C) {
if (log.isDebugEnabled()) {
log.debug("last message was a get response " + ((Dcc4PcReply) msg).toHexString());
}
if (msg.getElement(0) == Dcc4PcReply.SUCCESS) {
((Dcc4PcReply) msg).strip();
if (lastIncomplete != null) {
//Append this message to the last incomplete message
if (msg.getNumDataElements() != 0) {
int iOrig = lastIncomplete.getNumDataElements();
int iNew = 0;
while (iNew < msg.getNumDataElements()) {
lastIncomplete.setElement(iOrig, msg.getElement(iNew));
iOrig++;
iNew++;
}
}
//set the last incomplete message as the one to return
msg = lastIncomplete;
}
((Dcc4PcReply) msg).setError(false);
lastIncomplete = null;
waitingForMore = false;
mLastMessage = null;
mLastSentMessage = null;
} else if (msg.getElement(0) == Dcc4PcReply.INCOMPLETE) {
waitingForMore = true;
((Dcc4PcReply) msg).strip();
if (lastIncomplete != null) {
//Append this message to the last incomplete message
if (msg.getNumDataElements() != 0) {
int iOrig = lastIncomplete.getNumDataElements();
int iNew = 0;
while (iNew < msg.getNumDataElements()) {
lastIncomplete.setElement(iOrig, msg.getElement(iNew));
iOrig++;
iNew++;
}
}
} else if (msg.getNumDataElements() > 1) {
lastIncomplete = (Dcc4PcReply) msg;
}
//We do not forward the response to the listener as it has not been formed
forwardToPort(Dcc4PcMessage.getResponse());
return;
} else {
log.debug("Reply is an error mesage");
((Dcc4PcReply) msg).setError(true);
mLastMessage.setRetries(mLastMessage.getRetries() - 1);
if (mLastMessage.getRetries() >= 0) {
synchronized (xmtRunnable) {
mCurrentState = AUTORETRYSTATE;
replyInDispatch = false;
xmtRunnable.notify();
}
return;
}
}
}
} else {
log.debug("Last message sent was null " + ((Dcc4PcReply) msg).toHexString());
}
// message is complete, dispatch it !!
replyInDispatch = true;
if (log.isDebugEnabled()) {
log.debug("dispatch reply of length " + msg.getNumDataElements() + " contains " + msg.toString() + " state " + mCurrentState);
}
// forward the message to the registered recipients,
// which includes the communications monitor
// return a notification via the Swing event queue to ensure proper thread
Runnable r = newRcvNotifier(msg, mLastSender, this);
try {
javax.swing.SwingUtilities.invokeAndWait(r);
} catch (Exception e) {
log.error("Unexpected exception in invokeAndWait:" + e);
e.printStackTrace();
}
if (log.isDebugEnabled()) {
log.debug("dispatch thread invoked");
}
if (!msg.isUnsolicited()) {
// effect on transmit:
switch(mCurrentState) {
case WAITMSGREPLYSTATE:
{
// message, otherwise go on to the next message
if (msg.isRetransmittableErrorMsg()) {
if (log.isDebugEnabled()) {
log.debug("Automatic Recovery from Error Message: " + msg.toString());
}
synchronized (xmtRunnable) {
mCurrentState = AUTORETRYSTATE;
replyInDispatch = false;
xmtRunnable.notify();
}
} else {
// update state, and notify to continue
synchronized (xmtRunnable) {
mCurrentState = NOTIFIEDSTATE;
replyInDispatch = false;
xmtRunnable.notify();
}
}
break;
}
case WAITREPLYINPROGMODESTATE:
{
// entering programming mode
mCurrentMode = PROGRAMINGMODE;
replyInDispatch = false;
// check to see if we need to delay to allow decoders to become
// responsive
int warmUpDelay = enterProgModeDelayTime();
if (warmUpDelay != 0) {
try {
synchronized (xmtRunnable) {
xmtRunnable.wait(warmUpDelay);
}
} catch (InterruptedException e) {
// retain if needed later
Thread.currentThread().interrupt();
}
}
// update state, and notify to continue
synchronized (xmtRunnable) {
mCurrentState = OKSENDMSGSTATE;
xmtRunnable.notify();
}
break;
}
case WAITREPLYINNORMMODESTATE:
{
// entering normal mode
mCurrentMode = NORMALMODE;
replyInDispatch = false;
// update state, and notify to continue
synchronized (xmtRunnable) {
mCurrentState = OKSENDMSGSTATE;
xmtRunnable.notify();
}
break;
}
default:
{
replyInDispatch = false;
log.error("reply complete in unexpected state: " + mCurrentState + " was " + msg.toString());
}
}
// Unsolicited message
} else {
if (log.isDebugEnabled()) {
log.debug("Unsolicited Message Received " + msg.toString());
}
replyInDispatch = false;
}
}
use of jmri.jmrix.AbstractMRReply in project JMRI by JMRI.
the class LawicellTrafficController method decodeFromHardware.
/**
* Make a CanReply from a system-specific reply
*/
@Override
public CanReply decodeFromHardware(AbstractMRReply m) {
if (log.isDebugEnabled()) {
log.debug("Decoding from hardware: '" + m + "'\n");
}
Reply gc = (Reply) m;
CanReply ret = gc.createReply();
if (log.isDebugEnabled()) {
log.debug("Decoded " + gc + " as " + ret);
}
return ret;
}
use of jmri.jmrix.AbstractMRReply in project JMRI by JMRI.
the class AbstractCanTrafficController method handleOneIncomingReply.
/**
* Handle each reply when complete.
* <P>
* Overridden to include translation form the CAN hardware format
*
*/
@SuppressFBWarnings(value = "DLS_DEAD_LOCAL_STORE")
// Ignore false positive that msg is never used
@Override
public void handleOneIncomingReply() throws java.io.IOException {
// we sit in this until the message is complete, relying on
// threading to let other stuff happen
// Create messages off the right concrete classes
// for the CanReply
CanReply msg = new CanReply();
// and for the incoming reply from the hardware
AbstractMRReply hmsg = newReply();
// wait for start if needed
waitForStartOfReply(istream);
// message exists, now fill it
loadChars(hmsg, istream);
// Decode the message from the hardware into a CanReply
msg = decodeFromHardware(hmsg);
if (msg == null) {
// some replies don't get forwarded
return;
}
// message is complete, dispatch it !!
replyInDispatch = true;
if (log.isDebugEnabled()) {
log.debug("dispatch reply of length " + msg.getNumDataElements() + " contains " + msg.toString() + " state " + mCurrentState);
}
// actually distribute the reply
distributeOneReply(msg, mLastSender);
if (!msg.isUnsolicited()) {
if (log.isDebugEnabled()) {
log.debug("switch on state " + mCurrentState);
}
// effect on transmit:
switch(mCurrentState) {
case WAITMSGREPLYSTATE:
{
// update state, and notify to continue
synchronized (xmtRunnable) {
mCurrentState = NOTIFIEDSTATE;
replyInDispatch = false;
xmtRunnable.notify();
}
break;
}
case WAITREPLYINPROGMODESTATE:
{
// entering programming mode
mCurrentMode = PROGRAMINGMODE;
replyInDispatch = false;
// check to see if we need to delay to allow decoders to become
// responsive
int warmUpDelay = enterProgModeDelayTime();
if (warmUpDelay != 0) {
try {
synchronized (xmtRunnable) {
xmtRunnable.wait(warmUpDelay);
}
} catch (InterruptedException e) {
// retain if needed later
Thread.currentThread().interrupt();
}
}
// update state, and notify to continue
synchronized (xmtRunnable) {
mCurrentState = OKSENDMSGSTATE;
xmtRunnable.notify();
}
break;
}
case WAITREPLYINNORMMODESTATE:
{
// entering normal mode
mCurrentMode = NORMALMODE;
replyInDispatch = false;
// update state, and notify to continue
synchronized (xmtRunnable) {
mCurrentState = OKSENDMSGSTATE;
xmtRunnable.notify();
}
break;
}
default:
{
replyInDispatch = false;
if (allowUnexpectedReply == true) {
if (log.isDebugEnabled()) {
log.debug("Allowed unexpected reply received in state: " + mCurrentState + " was " + msg.toString());
}
} else {
log.error("reply complete in unexpected state: " + mCurrentState + " was " + msg.toString());
}
}
}
// Unsolicited message
} else {
replyInDispatch = false;
}
}
use of jmri.jmrix.AbstractMRReply in project JMRI by JMRI.
the class LawicellTrafficController method newReply.
// New reply from hardware
@Override
protected AbstractMRReply newReply() {
log.debug("New Reply created");
Reply reply = new Reply();
return reply;
}
Aggregations