Search in sources :

Example 1 with SimpleNode

use of jmri.jmrix.srcp.parser.SimpleNode 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)

Example 2 with SimpleNode

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

the class SRCPBusConnectionMemo method reply.

@Override
public void reply(jmri.jmrix.srcp.parser.SimpleNode n) {
    log.debug("SimpleNode Reply called with " + n.toString());
    reply(new SRCPReply(n));
    if (n.jjtGetChild(1) instanceof ASTinfo) {
        jmri.jmrix.srcp.parser.SimpleNode infonode = (jmri.jmrix.srcp.parser.SimpleNode) n.jjtGetChild(1);
        if (!((String) ((SimpleNode) (infonode.jjtGetChild(0))).jjtGetValue()).equals("" + _bus)) {
            // not for this bus.
            return;
        }
        // managers for this bus.
        if (infonode.jjtGetChild(1) instanceof jmri.jmrix.srcp.parser.ASTdescription) {
            SimpleNode descnode = (SimpleNode) infonode.jjtGetChild(1);
            for (int i = 0; i < descnode.jjtGetNumChildren(); i++) {
                jmri.jmrix.srcp.parser.SimpleNode child = (jmri.jmrix.srcp.parser.SimpleNode) descnode.jjtGetChild(i);
                log.debug("child node type " + child.toString() + " value " + (String) child.jjtGetValue());
                if (child instanceof jmri.jmrix.srcp.parser.ASTdevicegroup) {
                    String DeviceType = (String) child.jjtGetValue();
                    if (DeviceType.equals("FB")) {
                        setSensorManager(new jmri.jmrix.srcp.SRCPSensorManager(this, _bus));
                        jmri.InstanceManager.setSensorManager(getSensorManager());
                    } else if (DeviceType.equals("GA")) {
                        setTurnoutManager(new jmri.jmrix.srcp.SRCPTurnoutManager(this, _bus));
                        jmri.InstanceManager.setTurnoutManager(getTurnoutManager());
                    } else if (DeviceType.equals("SM")) {
                        setProgrammerManager(new SRCPProgrammerManager(new SRCPProgrammer(this), this));
                        jmri.InstanceManager.setProgrammerManager(getProgrammerManager());
                    } else if (DeviceType.equals("POWER")) {
                        setPowerManager(new jmri.jmrix.srcp.SRCPPowerManager(this, _bus));
                        jmri.InstanceManager.store(getPowerManager(), jmri.PowerManager.class);
                    } else if (DeviceType.equals("GL")) {
                        setThrottleManager(new jmri.jmrix.srcp.SRCPThrottleManager(this));
                        jmri.InstanceManager.setThrottleManager(getThrottleManager());
                    } else if (DeviceType.equals("TIME")) {
                        setClockControl(new jmri.jmrix.srcp.SRCPClockControl(this));
                    }
                }
            }
            configured = true;
            synchronized (this) {
                // wake up any thread that called configureManagers().
                this.notifyAll();
            }
        }
    }
}
Also used : SimpleNode(jmri.jmrix.srcp.parser.SimpleNode) PowerManager(jmri.PowerManager) SimpleNode(jmri.jmrix.srcp.parser.SimpleNode) ASTinfo(jmri.jmrix.srcp.parser.ASTinfo)

Aggregations

SimpleNode (jmri.jmrix.srcp.parser.SimpleNode)2 PowerManager (jmri.PowerManager)1 ASTinfo (jmri.jmrix.srcp.parser.ASTinfo)1 ParseException (jmri.jmrix.srcp.parser.ParseException)1 SRCPClientParser (jmri.jmrix.srcp.parser.SRCPClientParser)1 SRCPClientVisitor (jmri.jmrix.srcp.parser.SRCPClientVisitor)1