[Tinyos-beta-commits] CVS: tinyos-1.x/beta/Drain/tools/java/net/tinyos/drain Drain.java, NONE, 1.1 DrainConnector.java, NONE, 1.1 DrainLib.java, NONE, 1.1 Makefile, NONE, 1.1

Gilman Tolle gtolle at users.sourceforge.net
Mon Mar 14 17:19:16 PST 2005


Update of /cvsroot/tinyos/tinyos-1.x/beta/Drain/tools/java/net/tinyos/drain
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv12804/tools/java/net/tinyos/drain

Added Files:
	Drain.java DrainConnector.java DrainLib.java Makefile 
Log Message:
- Added some retry logic to the Drain registration procedure to enable
routing messages from the base station to an individual node. This
doesn't guarantee that every node will find a home in the tree and
become routable, but increases the probability that they will do so.

- Increased the number of children per hop to 8, again to make it more
likely that every node will find a place.

This is probably as far as the basic reverse-routing system will go
for now. There are some great problems in this space, and this is just
a quick-n-dirty solution.

Also, changed the Drain tools to their own package, so they can be
referenced by the Nucleus tools and others.

The Java side of the Drain reverse-routing system still needs to be
determined. Here's the problem: one Java class has to store the child
state and dispatch messages, and other Java classes in other JVMs
should be able to use it. This suggests a software interconnection
architecture that's more flexible than TCP to the JVM and then
intra-JVM for the rest.



--- NEW FILE: Drain.java ---
package net.tinyos.drain;

import net.tinyos.message.*;
import net.tinyos.util.*;

import java.io.*; 
import java.text.*;
import java.util.*;

public class Drain implements MessageListener {

  private static boolean DEBUG = false;
  private static int DRAIN_MAX_CHILDREN = 8;
  private static int DRAIN_MAX_CHILDREN_LOG2 = 3;

  private MoteIF moteIF;
  private int spAddr;

  private ArrayList children = new ArrayList();

  private static int BEACON_DELAY = 4;
  
  public Drain() {
    spAddr = DrainLib.setSPAddr();
    moteIF = DrainLib.startMoteIF();
    moteIF.registerListener(new DrainRegisterMsg(), this);
    moteIF.registerListener(new DrainMsg(), new DrainListener());
  }

  public void buildTree() {
    buildTree(BEACON_DELAY);
  }

  public void buildTree(int delay) {

    int treeInstance = (int)((double)Math.random() * (double)255);
    DrainBeaconMsg beaconMsg = new DrainBeaconMsg();

    children.clear();
    children.add(new Integer(0));

    beaconMsg.set_linkSource(spAddr);
    beaconMsg.set_treeInstance((byte)treeInstance);
    beaconMsg.set_source(spAddr);
    beaconMsg.set_parent(0xffff);
    beaconMsg.set_cost(0);
    beaconMsg.set_ttl((short)15);
    
    beaconMsg.set_beaconSeqno((short)0);
    beaconMsg.set_beaconDelay((byte)delay);
    beaconMsg.set_beaconOffset((short)0);

    System.err.println("Drain Tree Builder");
    System.err.println("myAddr = " + spAddr + 
		       ", treeInstance = " + treeInstance + 
		       ", delay = " + delay);
    
    send(beaconMsg);
  }

  public void send(int to, int type, Message m, int size) {
    
    DrainMsg drainMsg = new DrainMsg(DrainMsg.DEFAULT_MESSAGE_SIZE + size);

    int numBits = DRAIN_MAX_CHILDREN;
    int index = to & (int)(Math.pow(2,numBits) - 1);
    
    int remainderAddr = to >> numBits;
    
    System.err.println("bitsNeeded=" + numBits + 
		       ", index=" + index + 
		       ", remainderAddr=" + remainderAddr);
    
    int linkDest = ((Integer)children.get(index)).intValue();
    
    System.err.println("linkDest=" + linkDest);
    
    drainMsg.set_type((byte)type);
    drainMsg.set_dir((byte)1);
    drainMsg.set_ttl((byte)15);
    drainMsg.set_source(spAddr);
    drainMsg.set_dest(remainderAddr);
    drainMsg.dataSet(m.dataGet(), 0, drainMsg.offset_data(0), size);
    
    System.err.println(drainMsg);

    sendTo(linkDest, drainMsg);
  }

  synchronized public void messageReceived(int to, Message m) {
    DrainRegisterMsg regMsg = (DrainRegisterMsg) m;

    if (to == spAddr) {

      Integer addr = new Integer(regMsg.get_linkSource()); 

      if (children.size() >= DRAIN_MAX_CHILDREN) {

	regMsg.set_linkSource(spAddr);
	regMsg.set_op((byte)DrainConsts.DRAIN_REGISTER_OP_FULL);
	
	sendTo(addr.intValue(), regMsg);
	return;
      } 
	
      int index;

      if (children.contains(addr)) {
	System.out.println("Repeat child: linkAddr=" + addr + 
			   ", childNum=" + children.indexOf(addr) +
			   ", totalChildren=" + (children.size() - 1));

      } else {	

	children.add(addr);
	System.out.println("New child: linkAddr=" + addr + 
			   ", childNum=" + children.indexOf(addr) +
			   ", totalChildren=" + (children.size() - 1));
      }
	
      regMsg.set_linkSource(spAddr);
      regMsg.set_destAddr(children.indexOf(addr));
      regMsg.set_addrLength((byte)DRAIN_MAX_CHILDREN);
      regMsg.set_op((byte)DrainConsts.DRAIN_REGISTER_OP_JOINED);
      
      sendTo(addr.intValue(), regMsg);
    }
  }
  
  private class DrainListener implements MessageListener {
    
    synchronized public void messageReceived(int to, Message m) {
      
      DrainMsg mhMsg = (DrainMsg)m;
      
      if (DEBUG) {
	System.out.println(" linkTo: " + to +
			   " type:" + mhMsg.get_type() +
			   " hops:" + (16 - mhMsg.get_ttl()) +
			   " source:" + mhMsg.get_source() + 
			   " dest:" + mhMsg.get_dest());
      }
    }
  }
  
  public synchronized void send(Message m) {
    sendTo(MoteIF.TOS_BCAST_ADDR, m);
  }
 
  public synchronized void sendTo(int to, Message m) {
    try {
      moteIF.send(to, m);
    } catch (IOException e) {
      e.printStackTrace();
      System.out.println("ERROR: Can't send message");
      System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  private int numBits( int n ) {
    return (int)Math.round(Math.ceil(Math.log(n)/Math.log(2)));
  }

  /*
    public static void sendQuery(Drain drain, int to) {
    int ATTR_SIZE = 3;
    int numAttrs = 2;

    MgmtQueryMsg mqMsg = new MgmtQueryMsg(MgmtQueryMsg.DEFAULT_MESSAGE_SIZE +
    ATTR_SIZE * numAttrs);
    
    mqMsg.set_queryID((int)0x1234);
    mqMsg.set_delay((short)10);
    mqMsg.set_active((byte)1);
    mqMsg.set_repeat((byte)0);
    mqMsg.set_numAttrs((byte)numAttrs);
    mqMsg.set_ramQuery((byte)0);
    mqMsg.set_destination(MgmtQueryConsts.MGMTQUERY_DEST_COLLECTION);

    mqMsg.setElement_attrList_id(0, 1);
    mqMsg.setElement_attrList_id(1, 3);
    mqMsg.setElement_attrList_pos(1, (byte)0);
    
    System.out.println(mqMsg);

    drain.send(to, MgmtQueryMsg.AM_TYPE, 
    mqMsg, MgmtQueryMsg.DEFAULT_MESSAGE_SIZE +
    ATTR_SIZE * numAttrs);
    }
  */

  public static void main(String args[]) {

    if (args.length > 1) {
      System.err.println("usage: java net.tinyos.nucleus.Drain [period]");
      System.exit(1);
    }

    Drain drain = new Drain();
    int delay = BEACON_DELAY;

    if (args.length == 1) {
      delay = Integer.parseInt(args[0]);
    }

    drain.buildTree(delay);

    try {
      Thread.sleep(1024*delay*4);
    } catch (InterruptedException e) {}

    /*
      int to; 
      String line = null; 
      BufferedReader stdin = 
      new BufferedReader(new InputStreamReader(System.in)); 
      
      do {
      try { 
      line = null;
      System.out.print("Addr? ");
      line = stdin.readLine();
      to = Integer.parseInt(line); 
      
      System.out.println("Sending to: " + to); 
      
      sendQuery(drain, to);
      } catch (Exception e) {}
      } while (true);
    */

    System.exit(0);
  }
}

--- NEW FILE: DrainConnector.java ---
package net.tinyos.drain;

import net.tinyos.message.*;
import net.tinyos.util.*;

import java.io.*; 
import java.text.*;
import java.util.*;
import java.net.*;

public class DrainConnector implements MessageListener {

  private static int DEBUG = 0;

  public static int TOS_BCAST_ADDR = 0xffff;
  public static int DEFAULT_MOTE_ID = 0xfffe;
  public static int BCAST_ID = 0xff;
    
  private MoteIF moteIF;
    
  private int spAddr;

  private HashMap idTable = new HashMap();
    
  public DrainConnector() {

    spAddr = DrainLib.setSPAddr();
    moteIF = DrainLib.startMoteIF();
    moteIF.registerListener(new DrainMsg(), this);
  }
    
  public void registerListener(int id, MessageListener m) {

    HashSet listenerSet = (HashSet) idTable.get(new Integer(id));
    
    if (listenerSet == null) {
      listenerSet = new HashSet();
      idTable.put(new Integer(id), listenerSet);
    }
    listenerSet.add(m);
  }

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

    DrainMsg mhMsg = (DrainMsg)m;

    if (DEBUG > 1) {
      System.out.println(" linkTo: " + to +
			 " type:" + mhMsg.get_type() +
			 " hops:" + (16 - mhMsg.get_ttl()) +
			 " source:" + mhMsg.get_source() + 
			 " dest:" + mhMsg.get_dest());
    }

    if (to != spAddr && to != MoteIF.TOS_BCAST_ADDR) {
      //	  System.err.println(mhMsg);
      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) {
      if (DEBUG > 1) {
	// System.out.println("No Listener for type: " + mhMsg.get_type());
	//	      System.out.println(mhMsg);
      }
      return;
    }

    for(Iterator it = listenerSet.iterator(); it.hasNext(); ) {
      MessageListener ml = (MessageListener) it.next();
      ml.messageReceived(to, mhMsg);
    }
  }
  
  public static void main(String args[]) {
    new DrainConnector();
  }
}

--- NEW FILE: DrainLib.java ---
package net.tinyos.drain;

import java.net.*;

import net.tinyos.message.*;
import net.tinyos.util.*;

public class DrainLib {

  public static int setSPAddr() {
    String moteid = Env.getenv("MOTEID");
    int spAddr;
    
    if (moteid != null) {
      spAddr = Integer.parseInt(moteid);
    } else {
      try {
	byte[] localAddr = InetAddress.getLocalHost().getAddress();
	spAddr = localAddr[2];
	spAddr <<= 8;
	spAddr += localAddr[3];
      } catch (Exception e) {
	spAddr = 0xFFFE;
      }
    }
    
    return spAddr;
  }

  public static MoteIF startMoteIF() {
    MoteIF moteif = null;
    try {
      moteif = new MoteIF((Messenger)null);
    } catch (Exception e) {
      System.out.println("ERROR: Couldn't contact serial forwarder.");
      System.exit(1);
    }  
    return moteif;
  }
}

--- NEW FILE: Makefile ---
TOS = $(shell ncc -print-tosdir)
PACKAGE = net.tinyos.drain
MIG = mig java
NCG = ncg java
BETA = $(TOSDIR)/../beta
LIB = $(TOSDIR)/../contrib/nucleus/tos/lib

# List of message classes to build
MSGS = DrainMsg.java DrainBeaconMsg.java DrainRegisterMsg.java DrainConsts.java
CONSTS = 
INITIAL_TARGETS = $(MSGS) $(CONSTS)
OTHER_CLEAN = cleanmig

ROOT = $(TOSDIR)/../tools/java
include $(ROOT)/Makefile.include

DrainMsg.java: $(BETA)/Drain/Drain.h
	$(MIG) -java-classname=$(PACKAGE).DrainMsg $(BETA)/Drain/Drain.h DrainMsg -o $@

DrainBeaconMsg.java: $(BETA)/Drain/Drain.h
	$(MIG) -java-classname=$(PACKAGE).DrainBeaconMsg $(BETA)/Drain/Drain.h DrainBeaconMsg -o $@

DrainRegisterMsg.java: $(BETA)/Drain/Drain.h
	$(MIG) -java-classname=$(PACKAGE).DrainRegisterMsg $(BETA)/Drain/Drain.h DrainRegisterMsg -o $@

DrainConsts.java: $(BETA)/Drain/Drain.h
	$(NCG) -java-classname=$(PACKAGE).DrainConsts -I$(BETA)/Drain $(BETA)/Drain/Drain.h Drain.h -o $@

cleanmig:
	rm -f $(MSGS) $(CONSTS) platforms.properties




More information about the Tinyos-beta-commits mailing list