예제 #1
0
파일: Receiver.java 프로젝트: Zengwn/liteos
  public void packetReceived(byte[] packet) {
    // XXX: hack: with the new packetsource format, packet does not
    // contain a crc field, so numElements_data() will be wrong. But we
    // access the data area via dataSet/dataGet, so we're ok.

    // this is where the source comes in to create the correct packet

    final TOSMsg msg = messageFactory.createTOSMsg(packet);

    if (DEBUG) Dump.dump("Received message", packet);

    if (drop_bad_crc && msg.get_crc() != 0x01) {
      // Drop message
      if (DISPLAY_ERROR_MSGS) Dump.dump("Dropping packet with bad CRC", packet);
      return;
    }

    if (groupId == MoteIF.ANY_GROUP_ID || msg.get_group() == groupId) {
      Integer type = new Integer(msg.get_type());
      Vector vec = (Vector) templateTbl.get(type);
      if (vec == null) {
        if (DEBUG)
          Dump.dump(
              "Received packet with type " + msg.get_type() + ", but no listeners registered",
              packet);
        return;
      }
      int length = msg.get_length();

      Enumeration en = vec.elements();
      while (en.hasMoreElements()) {
        msgTemplate temp = (msgTemplate) en.nextElement();

        Message received;

        // Erk - end up cloning the message multiple times in case
        // different templates used for different listeners
        try {
          received = temp.template.clone(length);
          received.dataSet(msg.dataGet(), msg.offset_data(0), 0, length);
        } catch (ArrayIndexOutOfBoundsException e) {
          /*
           * Note: this will not catch messages whose length is
           * incorrect, but less than DATA_LENGTH (see AM.h) + 2
           */
          error(temp, "invalid length message received (too long)");
          continue;
        } catch (Exception e) {
          error(temp, "couldn't clone message!");
          continue;
        }

        /*
         * Messages that are longer than the template might have a
         * variable-sized array at their end
         */
        if (temp.template.dataGet().length > length) {
          error(temp, "invalid length message received (too short)");
          continue;
        }
        temp.listener.messageReceived(msg.get_addr(), received);
      }

    } else {
      if (DISPLAY_ERROR_MSGS) Dump.dump("Dropping packet with bad group ID", packet);
    }
  }
 public String toString() {
   return "VariableRequestResponse [" + msg.toString() + "]";
 }