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

Gilman Tolle gtolle at users.sourceforge.net
Wed Mar 2 18:04:16 PST 2005


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

Added Files:
	Drain.java DrainConnector.java Makefile 
Log Message:
Drain is now tested and working on telosb, and has its own Java tools for tree building and message reception.

--- NEW FILE: Drain.java ---
import net.tinyos.message.*;
import net.tinyos.util.*;

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

public class Drain implements Runnable {

  private MoteIF moteIF;

  private int treeInstance = (int)((double)Math.random() * (double)255);

  private static int BEACON_PERIOD = 4;
  private int beaconPeriod;
  private int spAddr;
  private int beaconSeqno = 1;
  DrainBeaconMsg beaconMsg = new DrainBeaconMsg();
  
  public Drain(int period) {

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

    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)period);
    beaconMsg.set_beaconOffset((short)0);

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

    System.out.println("Drain Tree Builder - myAddr = " + spAddr + ", tree = " + treeInstance);
    
    send(beaconMsg);

    System.exit(0);
    /*
    Thread thread = new Thread(this);
    thread.setDaemon(true);
    thread.start();
    */
  }

  public void run() {
    while(true) {
      try {

	System.out.print(".");
	send(beaconMsg);
	Thread.sleep(beaconPeriod*1024);

      } catch (Exception e) {
	e.printStackTrace();
      }
    }
  }

  public synchronized void send(Message m) {
    try {
      moteIF.send(MoteIF.TOS_BCAST_ADDR, m);
    } catch (IOException e) {
      e.printStackTrace();
      System.out.println("ERROR: Can't send message");
      System.exit(1);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String args[]) {

    if (args.length > 1) {
      System.err.println("usage: java net.tinyos.nucleus.Drain [period]");
      System.exit(1);
    }
    
    if (args.length == 1) {
      Drain mtb = new Drain(Integer.parseInt(args[0]));
    } else {
      Drain mtb = new Drain(BEACON_PERIOD);
    }
  }
}

--- NEW FILE: DrainConnector.java ---
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() {

	String moteid = Env.getenv("MOTEID");
	if (moteid != null) {
	    this.spAddr = Integer.parseInt(moteid);
	} else {
	  try {
	    byte[] localAddr = InetAddress.getLocalHost().getAddress();
	    this.spAddr = localAddr[2];
	    this.spAddr <<= 8;
	    this.spAddr += localAddr[3];
	  } catch (Exception e) {
	    this.spAddr = 0xFFFE;
	  }
	}
	
	try {
	    moteIF = new MoteIF((Messenger)null);
	    moteIF.registerListener(new DrainMsg(), this);
	} catch (Exception e) {
	    System.out.println("ERROR: Couldn't contact serial forwarder.");
	    System.exit(1);
	}
    }
    
    public synchronized void send(Message m) {
	try {
	    moteIF.send(MoteIF.TOS_BCAST_ADDR, m);
	} catch (IOException e) {
	    e.printStackTrace();
	    System.out.println("ERROR: Can't send message");
	    System.exit(1);
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }
    
    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: Makefile ---
TOS = $(shell ncc -print-tosdir)
PACKAGE = 
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 
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=DrainMsg $(BETA)/Drain/Drain.h DrainMsg -o $@

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

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




More information about the Tinyos-beta-commits mailing list