@Override
  public void run() {
    this.subSocket = this.mqm.getSubscriber();

    this.subSocket.subscribe("EUR/USD".getBytes()); // @TODO: Enable other pairs!
    this.subSocket.subscribe("USD/CHF".getBytes());
    this.subSocket.subscribe("EUR/CHF".getBytes());

    int index = 0;
    EVENT event = null;

    while (true) {
      PAIR pair = this.getPair(new String(this.subSocket.recv(0)));
      assert (pair != null);

      UUID uuid = UUID.fromString(new String(this.subSocket.recv(0)));
      assert (uuid != null);

      StringTokenizer st = new StringTokenizer(new String(this.subSocket.recv(0)), "|");
      assert (st != null && st.hasMoreTokens());

      RATE_EVENT_INFO rei =
          new RATE_EVENT_INFO(
              pair,
              new TICK(
                  Long.parseLong(st.nextToken()),
                  Double.parseDouble(st.nextToken()),
                  Double.parseDouble(st.nextToken())));

      logger.log(Level.INFO, rei.toString());
      index = 0;
      while (true) {
        event = null;
        synchronized (this.events) {
          if (index < this.events.size()) {
            event = this.events.get(index++);
          } else {
            break;
          }
        }

        if (event != null) {
          if (event.match(rei)) {
            this.mqm.setUuid(uuid);
            event.handle(rei, this);
          }
        } else {
          throw new NullPointerException("event");
        }
      }
    }
  }
Exemple #2
0
  /** Bucle principal del thread. */
  public void run() {

    try {
      while (true) {
        mylogger.finer("Waiting for a message.");
        String pkt = myPP.receive();

        mylogger.finer("Received message: " + pkt);

        if (pkt == null) break;

        StringTokenizer st = new StringTokenizer(pkt);

        int nsec = Integer.parseInt(st.nextToken());

        if (st.hasMoreTokens()) {
          String disp = st.nextToken();
          mylogger.fine("Requesting Start to Peers (" + nsec + "," + disp + ")");
          NeReDa.peers.start(this, nsec, disp, null);
        } else {
          mylogger.fine("Requesting Stop to Peers (" + nsec + ")");
          NeReDa.peers.stop(this, nsec);
        }
      }
    } catch (Exception e) {
      mylogger.warning("Terminated DataAttendant: " + e.getMessage());
    }

    try {
      synchronized (sock) {
        sock.close();
        sock.notify();
      }

    } catch (Exception e) {
      mylogger.warning("Closing DataAttendant socket: " + e.getMessage());
    }

    mylogger.fine("Deregistering");
    NeReDa.peers.Del(this);
  }
  /**
   * Examines the contents of the string passed as a received message and checks it for problems. If
   * there is any reasons why the string may be considered erroneous then it is thrown out. Accepted
   * strings are broken into their constituent parts and stored in the back-end database and
   * outputted to the screen.
   *
   * <p>Checks against the contents of the received message are performed in the following order:
   *
   * <ul>
   *   <li>Is the string greater than 33 characters?
   *   <li>Does the string have '(', ')' or ' '?
   *   <li>Is the first token (from the start of the string to the first space) in the format (n)?
   *   <li>Is the token for the PSE name less than or equal to 8 characters?
   * </ul>
   *
   * If any of these checks fail then the string being processed is rejected and considered to be a
   * transmission error, error in configuration or link error.
   */
  public void formatMessage() {
    log.finer("Starting processing of message: " + incomingMessage);
    // Trim any whitespace from the string
    incomingMessage = incomingMessage.trim();
    if (incomingMessage.length() < 33) {
      // If the string is too short then throw it out
      return;
    }
    if (!incomingMessage.contains("(")
        && !incomingMessage.contains(")")
        && !incomingMessage.contains(" ")) {
      // If the string doesn't contain any brackets (parentheses) or spaces then throw it out
      return;
    }
    StringTokenizer stMsg = new StringTokenizer(incomingMessage);
    String token = "";
    String nextToken = "";
    String sSev = "5"; // Messages received should vary from 1-4. This is to spot problems
    token = stMsg.nextToken();
    if (token.length() != 3 && !token.startsWith("(") && !token.endsWith(")")) {
      // The first token should be the severity level of the message and be in the format (n).
      // If it's too long or not in the right format then throw the message out.
      return;
    } else {
      sSev = token.substring(1, 2);
    }
    // Second token should be the PSE name
    token = stMsg.nextToken();
    nextToken = stMsg.nextToken();
    String sPSENode = "";
    if (token.length() < 8) {
      // See if the next token is part of the PSE name.
      // If the total length of the two strings is 8 or less then they're part of the PSE name
      if ((token.length() + nextToken.length()) <= 8) {
        sPSENode = token + nextToken;
        nextToken = "";
      } else {
        sPSENode = token;
      }

    } else if (token.length() == 8) {
      sPSENode = token;
    } else {
      // The value for the token is too long to be a valid PSE name so throw it out.
      return;
    }
    // Try and get the date. If the nextToken is blank then we need to get the
    // date. If not then it holds the date
    if (nextToken.equals("")) {
      token = stMsg.nextToken(); // This will put the date into the token string
      nextToken = stMsg.nextToken(); // This will put the time into the next string
    } else {
      token = nextToken.toString(); // Move the date into the first string
      nextToken = stMsg.nextToken(); // Get the time and place into the second string
    }
    String sDate = "";
    String sDay = token.substring(0, 2);
    String sMonth = token.substring(3, 6);
    String sYear = token.substring(7, 11);
    String sHour = nextToken.substring(0, 2);
    String sMin = nextToken.substring(3, 5);
    if (sMonth.equals("JAN")) {
      sMonth = "01";
    }
    if (sMonth.equals("FEB")) {
      sMonth = "02";
    }
    if (sMonth.equals("MAR")) {
      sMonth = "03";
    }
    if (sMonth.equals("APR")) {
      sMonth = "04";
    }
    if (sMonth.equals("MAY")) {
      sMonth = "05";
    }
    if (sMonth.equals("JUN")) {
      sMonth = "06";
    }
    if (sMonth.equals("JUL")) {
      sMonth = "07";
    }
    if (sMonth.equals("AUG")) {
      sMonth = "08";
    }
    if (sMonth.equals("SEP")) {
      sMonth = "09";
    }
    if (sMonth.equals("OCT")) {
      sMonth = "10";
    }
    if (sMonth.equals("NOV")) {
      sMonth = "11";
    }
    if (sMonth.equals("DEC")) {
      sMonth = "12";
    }
    sDate = new String(sDay + "/" + sMonth + "/" + sYear + " " + sHour + ":" + sMin);
    sDay = null;
    sMonth = null;
    sYear = null;
    sHour = null;
    sMin = null;

    String sLinkID = "?";
    String sError = "";
    token = stMsg.nextToken();
    boolean hasChannel = false;
    String sChannel = "";
    if ((token.length() < 7) && token.contains("-")) {
      // We've got to pick out the link id on the PSE
      sLinkID = token.toString();
      // Skip the next token as it's only a hyphen
      token = stMsg.nextToken();
    }
    if (token.length() < 11 && token.contains("(") && token.contains(")")) {
      // If the code enters here then the token includes the channel number X25-nn(xx)
      sLinkID = token.substring(0, token.indexOf("("));
      sChannel = "on channel " + token.substring(token.indexOf("(") + 1, token.indexOf(")"));
      hasChannel = true;
      // Skip the next token as it's only a hyphen
      token = stMsg.nextToken();
    }
    if (token.equals("-")) {
      token = stMsg.nextToken();
      sError = sError + token + " ";
    } else {
      sError = sError + token + " ";
    }
    while (stMsg.hasMoreTokens()) {
      token = stMsg.nextToken();
      sError = sError + token + " ";
    }
    if (hasChannel) {
      sError = sError + sChannel;
    }
    sError = sError.trim();
    log.finer(sSev + ", " + sPSENode + ", " + sDate + ", " + sLinkID + ", " + sError);

    // Severity, PSE, Link, Message, Received, Unique ID
    formattedMessage[0] = sSev;
    formattedMessage[1] = sPSENode;
    formattedMessage[2] = sLinkID;
    formattedMessage[3] = sError;
    formattedMessage[4] = sDate;
    formattedMessage[5] = UUID.randomUUID().toString();

    log.entering("formatMessage", "UnformattedMessage.storeDataInDb(String)", formattedMessage);
    storeDataInDb(formattedMessage);
    log.entering(
        "formatMessage",
        "JNGui.Client.addAlarmData(String, String)",
        new Object[] {formattedMessage, originatingPort});
    parent.addAlarmData(formattedMessage);
  }
  /**
   * Tokenizes a command string into a list.
   *
   * @throws Exception if the command cannot be tokenized
   */
  protected static LinkedList _tokenizeCommand(String cmd) throws Exception {

    LinkedList tokens = new LinkedList();
    int startIndex = 0;
    int dQuoteAt = cmd.indexOf('"');
    int sQuoteAt = cmd.indexOf('\'');

    if (dQuoteAt == -1 && sQuoteAt == -1) {
      StringTokenizer st = new StringTokenizer(cmd.trim());
      while (st.hasMoreTokens()) {
        tokens.add(st.nextToken());
      }
      return tokens;
    }

    char[] chArray = cmd.trim().toCharArray();

    int endIndex = 0;
    boolean inQuotes = false;
    char c = 0;
    char lastc = 0;
    char lastqc = 0;
    StringBuffer sb = new StringBuffer(80);

    while (endIndex < chArray.length) {
      c = chArray[endIndex];
      if (!Character.isWhitespace(c)) {
        if (c == '"' || c == '\'') {
          if (inQuotes && lastc != '\\' && lastqc == c) {
            tokens.add(sb.toString());
            inQuotes = false;
            sb.setLength(0);
          } else if (!inQuotes) {
            inQuotes = true;
            lastqc = c;
          } else {
            sb.append(c);
          }
        } else if (c == '\\') {
          if (lastc == '\\') sb.append(c);
        } else {
          sb.append(c);
        }
      } else {
        if (inQuotes) {
          sb.append(c);
        } else {
          if (sb.length() > 0) {
            tokens.add(sb.toString());
            sb.setLength(0);
          }
        }
      }
      lastc = c;
      ++endIndex;
    }

    if (inQuotes) {
      throw new Exception(
          WDExUtil.formatMessage(WDExConstants.UNTERMINATED_STRING, WDUtil.toArray(cmd)));
    }
    return tokens;
  }