Search in sources :

Example 1 with SRCPClientParser

use of jmri.jmrix.srcp.parser.SRCPClientParser in project JMRI by JMRI.

the class SRCPReplyTest method testParserCtor.

// Test the parser constructor.
@Test
public void testParserCtor() {
    String s = "12345678910 400 ERROR Reason GOES HERE\n\r";
    SRCPClientParser p = new SRCPClientParser(new StringReader(s));
    SRCPReply m = null;
    try {
        m = new SRCPReply(p.commandresponse());
    } catch (ParseException pe) {
    // m is already null if there is an exception parsing the string
    }
    Assert.assertNotNull(m);
    Assert.assertEquals("Parser Constructor Correct", s, m.toString());
//Assert.assertTrue("Parser Constructor Correct", s.equals(m.toString()));
}
Also used : StringReader(java.io.StringReader) SRCPClientParser(jmri.jmrix.srcp.parser.SRCPClientParser) ParseException(jmri.jmrix.srcp.parser.ParseException) Test(org.junit.Test)

Example 2 with SRCPClientParser

use of jmri.jmrix.srcp.parser.SRCPClientParser in project JMRI by JMRI.

the class SRCPTrafficController method receiveLoop.

/*
     * We are going to override the receiveLoop() function so that we can
     * handle messages received by the system using the SRCP parser.
     */
@Override
public void receiveLoop() {
    if (log.isDebugEnabled()) {
        log.debug("SRCP receiveLoop starts");
    }
    SRCPClientParser parser = new SRCPClientParser(istream);
    while (true) {
        try {
            SimpleNode e;
            if (_memo.getMode() == HANDSHAKEMODE) {
                e = parser.handshakeresponse();
            } else {
                e = parser.commandresponse();
            }
            // 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 = new SRCPRcvNotifier(e, mLastSender, this);
            try {
                javax.swing.SwingUtilities.invokeAndWait(r);
            } catch (Exception ex) {
                log.error("Unexpected exception in invokeAndWait:" + ex);
                ex.printStackTrace();
            }
            if (log.isDebugEnabled()) {
                log.debug("dispatch thread invoked");
            }
            log.debug("Mode " + mode + " child contains " + ((SimpleNode) e.jjtGetChild(1)).jjtGetValue());
            //if (mode==HANDSHAKEMODE && ((String)((SimpleNode)e.jjtGetChild(1)).jjtGetValue()).contains("GO")) mode=RUNMODE;
            SRCPClientVisitor v = new SRCPClientVisitor();
            e.jjtAccept(v, _memo);
            //SRCPReply msg = new SRCPReply((SimpleNode)e.jjtGetChild(1));
            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 ex) {
                                // 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 " + e.toString());
                            }
                            synchronized (xmtRunnable) {
                                // The transmit thread sometimes gets stuck
                                // when unexpected replies are received.  Notify
                                // it to clear the block without a timeout.
                                // (do not change the current state)
                                xmtRunnable.notify();
                            }
                        } else {
                            log.error("reply complete in unexpected state: " + mCurrentState + " was " + e.toString());
                        }
                    }
            }
        } catch (ParseException pe) {
            rcvException = true;
            reportReceiveLoopException(pe);
            break;
        }
    }
}
Also used : SRCPClientVisitor(jmri.jmrix.srcp.parser.SRCPClientVisitor) SRCPClientParser(jmri.jmrix.srcp.parser.SRCPClientParser) ParseException(jmri.jmrix.srcp.parser.ParseException) ParseException(jmri.jmrix.srcp.parser.ParseException) SimpleNode(jmri.jmrix.srcp.parser.SimpleNode)

Aggregations

ParseException (jmri.jmrix.srcp.parser.ParseException)2 SRCPClientParser (jmri.jmrix.srcp.parser.SRCPClientParser)2 StringReader (java.io.StringReader)1 SRCPClientVisitor (jmri.jmrix.srcp.parser.SRCPClientVisitor)1 SimpleNode (jmri.jmrix.srcp.parser.SimpleNode)1 Test (org.junit.Test)1