public synchronized void messageReceived(int to, Message m) {

    DrainMsg mhMsg = (DrainMsg) m;

    log.debug(
        "incoming: localDest: "
            + to
            + " type:"
            + mhMsg.get_type()
            + " hops:"
            + (16 - mhMsg.get_ttl())
            + " seqNo:"
            + mhMsg.get_seqNo()
            + " source:"
            + mhMsg.get_source()
            + " finalDest:"
            + mhMsg.get_dest());

    // lets assume that the network cannot buffer more than 25 drain msgs from a single source at a
    // time (should be more than reasonable)
    if (seqNos.containsKey(new Integer(mhMsg.get_source()))) {
      int oldSeqNo = ((Integer) seqNos.get(new Integer(mhMsg.get_source()))).intValue();
      int upperBound = mhMsg.get_seqNo() + 25;
      int wrappedUpperBound = 25 - (255 - mhMsg.get_seqNo());
      if ((oldSeqNo >= mhMsg.get_seqNo() && oldSeqNo < upperBound)
          || (oldSeqNo >= 0 && oldSeqNo < wrappedUpperBound)) {
        log.debug(
            "Dropping message from "
                + mhMsg.get_source()
                + " with duplicate seqNo: "
                + mhMsg.get_seqNo());
        return;
      }
    }
    seqNos.put(new Integer(mhMsg.get_source()), new Integer(mhMsg.get_seqNo()));

    if (to != spAddr && to != MoteIF.TOS_BCAST_ADDR && to != TOS_UART_ADDR) {
      log.debug("Dropping message not for me.");
      return;
    }

    HashSet promiscuousSet = (HashSet) idTable.get(new Integer(BCAST_ID));
    HashSet listenerSet = (HashSet) idTable.get(new Integer(mhMsg.get_type()));

    if (listenerSet != null && promiscuousSet != null) {
      listenerSet.addAll(promiscuousSet);
    } else if (listenerSet == null && promiscuousSet != null) {
      listenerSet = promiscuousSet;
    }

    if (listenerSet == null) {
      log.debug("No Listener for type: " + mhMsg.get_type());
      return;
    }

    for (Iterator it = listenerSet.iterator(); it.hasNext(); ) {
      MessageListener ml = (MessageListener) it.next();
      ml.messageReceived(to, mhMsg);
    }
  }
Example #2
0
 public int hashCode() {
   return listener.hashCode();
 }
  protected Object handleUpEvent(Event evt) throws Exception {
    switch (evt.getType()) {
      case Event.MSG:
        if (msg_listener != null) msg_listener.receive((Message) evt.getArg());
        break;

      case Event.GET_APPLSTATE: // reply with GET_APPLSTATE_OK
        byte[] tmp_state = null;
        if (msg_listener != null) {
          ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
          msg_listener.getState(output);
          tmp_state = output.toByteArray();
        }
        return new StateTransferInfo(null, 0L, tmp_state);

      case Event.GET_STATE_OK:
        if (msg_listener != null) {
          StateTransferResult result = (StateTransferResult) evt.getArg();
          if (result.hasBuffer()) {
            ByteArrayInputStream input = new ByteArrayInputStream(result.getBuffer());
            msg_listener.setState(input);
          }
        }
        break;

      case Event.STATE_TRANSFER_OUTPUTSTREAM:
        OutputStream os = (OutputStream) evt.getArg();
        if (msg_listener != null && os != null) {
          msg_listener.getState(os);
        }
        break;

      case Event.STATE_TRANSFER_INPUTSTREAM:
        InputStream is = (InputStream) evt.getArg();
        if (msg_listener != null && is != null) msg_listener.setState(is);
        break;

      case Event.VIEW_CHANGE:
        View v = (View) evt.getArg();
        List<Address> new_mbrs = v.getMembers();
        setMembers(new_mbrs);
        if (membership_listener != null) membership_listener.viewAccepted(v);
        break;

      case Event.SET_LOCAL_ADDRESS:
        if (log.isTraceEnabled())
          log.trace("setting local_addr (" + local_addr + ") to " + evt.getArg());
        local_addr = (Address) evt.getArg();
        break;

      case Event.SUSPECT:
        if (membership_listener != null) membership_listener.suspect((Address) evt.getArg());
        break;

      case Event.BLOCK:
        if (membership_listener != null) membership_listener.block();
        break;
      case Event.UNBLOCK:
        if (membership_listener != null) membership_listener.unblock();
        break;
    }

    return null;
  }