Boomerang Multihop

From TinyOS Wiki
Revision as of 11:02, 6 April 2011 by Martinvilu (talk | contribs) (Added to the "Boomerang" Category)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Multihop message dispatching

Tinyos 1.x tools included a mechanism for a single level of dispatch on messages (i.e. it only provided tools for dispatching of AM types). Boomerang includes enhancements that allow for arbitrary nesting and dispatch of messages; the code for doing so is located in /opt/moteiv/tools/java/net/tinyos/message/. The core of the system is a class called Dispatcher -- it implements a general dispatch behavior on a named field. The functionality is perhaps best demonstrated through a series of examples, below mote is an instance of MoteIF

  • At the root (top level messages) you register message handlers with MoteIF; for example OscopeMsg and MultiHopMsg are top level dispatches. For top level dispatches nothing changes compared with TinyOS 1.x:
  • For intermediate level dispatches, you create a dispatcher with the dispatcher of the upper layer (in this case MoteIF mote for the second layer). The dispatcher is created by specifying the class of the encapsulating message, the name of the field to dispatch on, and the name of the payload. For example, in MultiHopMsg the payload is called data and we dispatch on a field called id:
    Dispatcher d = new Dispatcher(mote, new MultiHopMsg(), "id", "data");
    
  • Now, that we have this intermediate dispatcher, we need to create handlers for encapsulated messages; the mechanics for that are the same as if we were registering handlers with the old MoteIF:
    d.registerListener(new MessageA(), myMessageAHandler);
    
  • If MessageA can also be transmitted single hop (i.e. without the encapsulating MultiHopMsg), you will also need to register a top level handler:
    mote.registerListener(new MessageA(), myMessageAHandler);
    
  • If inside the handler for MessageA you need to access the fields of the encapsulating message, you should tell mig when generating the MessageA class that it extends net.tinyos.message.LinkedMessage:
    mig java -target=telos -java-extends=net.tinyos.message.LinkedMessage -java-classname=MessageA $(FLAGS) MessageA.h  MessageA -o MessageA.java
    
    Then, to access the encapsulating message, you just need to access the parent field of the message passed in:
    public void messageReceived(int dest_addr, Message msg) {
      ...
      if (msg instanceof MessageA) {
        MessageA myMsgA = (MessageA) msg;
        ... //do some things with message A's fields
        Message encapsulatingMsg = myMsgA.getParent();
        ... // do some things with encapsulating message fields
        // encapsulating message is null if MessageA was received directly or
        // is an instance of MultiHopMsg if MessageA was received through a multihopmsg
      }
    }