public void run() { // start a new thread
    // this thread has one task.  It repeatedly reads from the input pipe
    // and writes modified data to the output pipe.  This is the heart
    // of the command station simulation.
    if (log.isDebugEnabled()) {
      log.debug("Simulator Thread Started");
    }

    Random rgen = new Random();

    ConnectionStatus.instance()
        .setConnectionState(this.getCurrentPortName(), ConnectionStatus.CONNECTION_UP);
    for (; ; ) {
      DCCppMessage m = readMessage();
      if (log.isDebugEnabled()) {
        log.debug("Simulator Thread received message " + m.toString());
      }
      DCCppReply r = generateReply(m);
      // If generateReply() returns null, do nothing. No reply to send.
      if (r != null) {
        writeReply(r);
        if (log.isDebugEnabled()) {
          log.debug("Simulator Thread sent Reply" + r.toString());
        }
      }

      // Once every SENSOR_MSG_RATE loops, generate a random Sensor message.
      int rand = rgen.nextInt(SENSOR_MSG_RATE);
      if (rand == 1) {
        generateRandomSensorReply();
      }
    }
  }
 public DataOutputStream getOutputStream() {
   if (pout == null) {
     log.error("getOutputStream called before load(), stream not available");
     ConnectionStatus.instance()
         .setConnectionState(this.getCurrentPortName(), ConnectionStatus.CONNECTION_DOWN);
   }
   return pout;
 }
Exemple #3
0
 protected void buildLine(ConnectionConfig conn, JLabel cs, JPanel pane) {
   if (conn.name().equals(JmrixConfigPane.NONE)) {
     cs.setText(" ");
     return;
   }
   ConnectionStatus.instance().addConnection(conn.name(), conn.getInfo());
   cs.setFont(pane.getFont());
   updateLine(conn, cs);
   pane.add(cs);
 }
 // readMessage reads one incoming message from the buffer
 // and sets outputBufferEmpty to true.
 private DCCppMessage readMessage() {
   DCCppMessage msg = null;
   try {
     msg = loadChars();
   } catch (java.io.IOException e) {
     // should do something meaningful here.
     ConnectionStatus.instance()
         .setConnectionState(this.getCurrentPortName(), ConnectionStatus.CONNECTION_DOWN);
   }
   setOutputBufferEmpty(true);
   return (msg);
 }
 private void writeReply(DCCppReply r) {
   int i;
   int len = r.getLength(); // opCode+Nbytes+ECC
   // If r == null, there is no reply to be sent.
   try {
     outpipe.writeByte((byte) '<');
     for (i = 0; i < len; i++) {
       outpipe.writeByte((byte) r.getElement(i));
     }
     outpipe.writeByte((byte) '>');
   } catch (java.io.IOException ex) {
     ConnectionStatus.instance()
         .setConnectionState(this.getCurrentPortName(), ConnectionStatus.CONNECTION_DOWN);
   }
 }
Exemple #6
0
  /**
   * Fill in the logo and status panel
   *
   * @return Properly-filled out JPanel
   */
  protected JPanel statusPanel() {
    JPanel pane1 = new JPanel();
    pane1.setLayout(new BoxLayout(pane1, BoxLayout.X_AXIS));
    log.debug("Fetch main logo: {}", logo());
    pane1.add(
        new JLabel(
            new ImageIcon(
                getToolkit().getImage(FileUtil.findURL(logo(), FileUtil.Location.INSTALLED)),
                "JMRI logo"),
            JLabel.LEFT));
    pane1.add(
        Box.createRigidArea(new Dimension(15, 0))); // Some spacing between logo and status panel

    log.debug("start labels");
    JPanel pane2 = new JPanel();

    pane2.setLayout(new BoxLayout(pane2, BoxLayout.Y_AXIS));
    pane2.add(new JLabel(line1()));
    pane2.add(new JLabel(line2()));
    pane2.add(new JLabel(line3()));
    pane2.add(
        new JLabel(
            Bundle.getMessage(
                "ActiveProfile", ProfileManager.getDefault().getActiveProfile().getName())));

    // add listerner for Com port updates
    ConnectionStatus.instance().addPropertyChangeListener(this);
    int i = 0;
    for (ConnectionConfig conn : InstanceManager.getDefault(ConnectionConfigManager.class)) {
      if (!conn.getDisabled()) {
        connection[i] = conn;
        i++;
      }
      if (i > 3) {
        break;
      }
    }
    buildLine4(pane2);
    buildLine5(pane2);
    buildLine6(pane2);
    buildLine7(pane2);

    pane2.add(new JLabel(line8()));
    pane2.add(new JLabel(line9()));
    pane1.add(pane2);
    return pane1;
  }
  @Override
  public void receiveLoop() {
    log.debug("receiveLoop starts");
    ObjectReader reader = this.mapper.reader();

    while (true) {
      try {
        JsonNode root = reader.readTree(this.istream);
        String type = root.path(TYPE).asText();
        JsonNode data = root.path(DATA);
        log.debug("Processing {} with {}", type, data);
        if (type.equals(GOODBYE)) {
          log.info("Connection closing from server.");
          break;
        } else if (type.equals(HELLO)) {
          this.receiveHello(data);
        } else if (type.equals(LOCALE)) {
          this.receiveHello(data);
        } else if (type.equals(PONG)) {
          // silently ignore
        } else if (!data.isMissingNode() || (root.isArray() && ((ArrayNode) root).size() > 0)) {
          // process replies with a data node or non-empty arrays
          JsonClientReply reply = new JsonClientReply(root);
          Runnable r = new RcvNotifier(reply, mLastSender, this);
          try {
            SwingUtilities.invokeAndWait(r);
          } catch (InterruptedException e) {
            log.error("Exception notifying listeners of reply: {}", e.getMessage(), e);
          } catch (InvocationTargetException e) {
            log.error("Exception notifying listeners of reply: {}", e.getMessage(), e);
          }
        }
      } catch (IOException e) {
        this.rcvException = true;
        reportReceiveLoopException(e);
        break;
      } catch (NoSuchElementException nse) {
        // we get an NSE when we are finished with this client
        // so break out of the loop
        break;
      }
    }
    ConnectionStatus.instance()
        .setConnectionState(this.controller.getCurrentPortName(), ConnectionStatus.CONNECTION_DOWN);
    log.error("Exit from rcv loop");
    this.recovery();
  }
Exemple #8
0
  protected void updateLine(ConnectionConfig conn, JLabel cs) {
    if (conn.getDisabled()) {
      return;
    }
    String name = conn.getConnectionName();
    if (name == null) {
      name = conn.getManufacturer();
    }
    if (ConnectionStatus.instance().isConnectionOk(conn.getInfo())) {
      cs.setForeground(Color.black);
      String cf = Bundle.getMessage("ConnectionSucceeded", name, conn.name(), conn.getInfo());
      cs.setText(cf);
    } else {
      cs.setForeground(Color.red);
      String cf = Bundle.getMessage("ConnectionFailed", name, conn.name(), conn.getInfo());
      cf = cf.toUpperCase();
      cs.setText(cf);
    }

    this.revalidate();
  }