[Tinyos-commits] CVS: tinyos-1.x/tools/java/net/tinyos/drain
Drain.java, NONE, 1.1 DrainConnector.java, NONE,
1.1 DrainLib.java, NONE, 1.1 DrainSniff.java, NONE,
1.1 Makefile, NONE, 1.1
Gilman Tolle
gtolle at users.sourceforge.net
Mon Oct 31 09:07:22 PST 2005
Update of /cvsroot/tinyos/tinyos-1.x/tools/java/net/tinyos/drain
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1688
Added Files:
Drain.java DrainConnector.java DrainLib.java DrainSniff.java
Makefile
Log Message:
Import of the Drain java tools.
--- NEW FILE: Drain.java ---
// $Id: Drain.java,v 1.1 2005/10/31 17:07:20 gtolle Exp $
/* tab:2
*
*
* "Copyright (c) 2000-2005 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
*/
/**
*
* Tree builder for the Drain protocol.
*
* @author Gilman Tolle <get at cs.berkeley.edu>
*/
package net.tinyos.drain;
import net.tinyos.message.*;
import net.tinyos.util.*;
import org.apache.log4j.*;
import java.io.*;
import java.text.*;
import java.util.*;
public class Drain {
public boolean VERBOSE = true;
private Logger log = Logger.getLogger(Drain.class.getName());
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;
private TreeMaintainer treeMaintainer = null;
public short ttl = 15;
public int delay = 4;
public int period = 0;
public int count = 1;
public boolean forever = false;
public boolean usingTosBase = true;
public int treeInstance = (int)((double)Math.random() * (double)255);
public boolean isCommandLine = false;
public Drain() {
spAddr = DrainLib.setSPAddr();
moteIF = DrainLib.startMoteIF();
}
public Drain(int p_spAddr, MoteIF p_moteIF) {
spAddr = p_spAddr;
moteIF = p_moteIF;
}
public void buildTree() {
buildTree(BEACON_DELAY);
}
public void buildTree(int delay) {
buildTree(delay, treeInstance, true);
}
public void buildTree(int delay, int treeInstance, boolean defaultRoute) {
DrainBeaconMsg beaconMsg = new DrainBeaconMsg();
children.clear();
children.add(new Integer(0));
beaconMsg.set_treeInstance((byte)treeInstance);
beaconMsg.set_source(spAddr);
beaconMsg.set_parent(0xffff);
if (usingTosBase){
beaconMsg.set_linkSource(spAddr);
}
else {
beaconMsg.set_linkSource(0x7e);
}
beaconMsg.set_cost(0);
beaconMsg.set_ttl(ttl);
beaconMsg.set_beaconSeqno((short)0);
beaconMsg.set_beaconDelay((byte)delay);
beaconMsg.set_beaconOffset((short)0);
if (defaultRoute) {
beaconMsg.set_defaultRoute((short)1);
} else {
beaconMsg.set_defaultRoute((short)0);
}
log.info("buildTree: root_address=" + beaconMsg.get_linkSource() + " instance=" + treeInstance + " delay=" + delay + " defaultRoute=" + defaultRoute);
send(beaconMsg);
}
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 class TreeMaintainer extends Thread {
public void run(){
while (count > 0 || forever) {
if (VERBOSE) {
System.err.println("Sending beacon...");
}
buildTree(delay, treeInstance, true);
try {
if (period > 0) {
Thread.sleep(period * 1000);
} else {
Thread.sleep(delay * 1000);
}
} catch (InterruptedException e) {}
count--;
}
if (isCommandLine) {
System.exit(0);
}
}
}
public void maintainTree(){
if (treeMaintainer != null && treeMaintainer.isAlive() ){
return;
}
treeMaintainer = new TreeMaintainer();
treeMaintainer.start();
}
public void stopTreeMaintenance() {
if (treeMaintainer != null && treeMaintainer.isAlive() ){
treeMaintainer.stop();
}
}
public static void main(String args[]) {
Drain drain = new Drain();
parseArgs(args, drain);
System.out.println("Drain: root_address=" + drain.spAddr + " instance=" + drain.treeInstance + " period=" + drain.period + " delay=" + drain.delay);
drain.maintainTree();
}
private static void parseArgs(String args[], Drain drain) {
ArrayList cleanedArgs = new ArrayList();
drain.isCommandLine = true;
for(int i = 0; i < args.length; i++) {
if (args[i].startsWith("--")) {
// Parse Long Options
String longopt = args[i].substring(2);
if (longopt.equals("help")) {
usage();
}
} else if (args[i].startsWith("-")) {
// Parse Short Options
String opt = args[i].substring(1);
if (opt.equals("t")) {
drain.delay = Integer.parseInt(args[++i]);
} else if (opt.equals("c")) {
drain.count = Integer.parseInt(args[++i]);
} else if (opt.equals("a")) {
drain.spAddr = Integer.parseInt(args[++i]);
} else if (opt.equals("b")) {
drain.usingTosBase = Boolean.valueOf(args[++i]).booleanValue();
} else if (opt.equals("n")) {
drain.forever = true;
} else if (opt.equals("s")) {
drain.spAddr = 0xfffe;
drain.usingTosBase = false;
} else if (opt.equals("i")) {
drain.treeInstance = Integer.parseInt(args[++i]);
} else if (opt.equals("p")) {
drain.period = Integer.parseInt(args[++i]);
} else if (opt.equals("h")) {
usage();
}
} else {
// Place into args string
cleanedArgs.add(args[i]);
}
}
}
private static void usage() {
System.err.println("usage: java net.tinyos.nucleus.Drain <opts>");
System.err.println(" -i <instance number>");
System.err.println(" -t <rebroadcast delay in secs>");
System.err.println(" -p <rebuilding period in secs>");
System.err.println(" -c <number of beacons to transmit>");
System.err.println(" -n : Keep rebuilding the tree forever");
System.err.println(" -b <true|false> : TOSBase is connected or not");
System.err.println(" -a <spAddress> : the address of the root (default=0)");
System.err.println(" -s : Using with tossim (equivalent to '-b false -a 0xfffe'");
System.err.println(" -h, --help : This message.");
System.exit(1);
}
}
--- NEW FILE: DrainConnector.java ---
// $Id: DrainConnector.java,v 1.1 2005/10/31 17:07:20 gtolle Exp $
/* tab:2
*
*
* "Copyright (c) 2000-2005 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
*/
/**
*
* Message client class for the Drain protocol.
*
* @author Gilman Tolle <get at cs.berkeley.edu>
*/
package net.tinyos.drain;
import net.tinyos.message.*;
import net.tinyos.util.*;
import org.apache.log4j.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
public class DrainConnector implements MessageListener {
public static int TOS_UART_ADDR = 0x7e;
public static int DEFAULT_MOTE_ID = 0xfffe;
public static int BCAST_ID = 0xff;
private MoteIF moteIF;
private Logger log = Logger.getLogger(DrainConnector.class.getName());
private int spAddr;
private Hashtable seqNos = new Hashtable();
private HashMap idTable = new HashMap();
public DrainConnector() {
this(DrainLib.setSPAddr(), DrainLib.startMoteIF());
}
public DrainConnector(int p_spAddr, MoteIF p_moteIF) {
spAddr = p_spAddr;
moteIF = p_moteIF;
moteIF.registerListener(new DrainMsg(), this);
log.info("Started myAddr = " + spAddr + ", listening for DrainMsg");
}
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);
log.info("New Listener for id=" + id);
}
public void deregisterListener(int id, MessageListener m) {
HashSet listenerSet = (HashSet) idTable.get(new Integer(id));
if (listenerSet == null) {
throw new IllegalArgumentException("No listeners registered for message type "+id);
}
listenerSet.remove(m);
}
synchronized public 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);
}
}
public static void main(String args[]) {
DrainConnector dc = new DrainConnector();
}
}
--- NEW FILE: DrainLib.java ---
// $Id: DrainLib.java,v 1.1 2005/10/31 17:07:20 gtolle Exp $
/* tab:2
*
*
* "Copyright (c) 2000-2005 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
*/
/**
*
* Utility functions for the Drain protocol classes.
*
* @author Gilman Tolle <get at cs.berkeley.edu>
*/
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: DrainSniff.java ---
// $Id: DrainSniff.java,v 1.1 2005/10/31 17:07:20 gtolle Exp $
/* tab:2
*
*
* "Copyright (c) 2000-2005 The Regents of the University of California.
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
* CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
*/
/**
*
* Packet Sniffer for the Drain protocol.
*
* @author Gilman Tolle <get at cs.berkeley.edu>
*/
package net.tinyos.drain;
import net.tinyos.message.*;
import net.tinyos.util.*;
import org.apache.log4j.*;
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
public class DrainSniff {
private MoteIF moteIF;
public DrainSniff() {
moteIF = new MoteIF();
moteIF.registerListener(new DrainMsg(), new DrainMsgReceiver());
moteIF.registerListener(new DrainBeaconMsg(), new DrainBeaconMsgReceiver());
}
private class DrainMsgReceiver implements MessageListener {
synchronized public void messageReceived(int to, Message m) {
DrainMsg mhMsg = (DrainMsg)m;
System.out.println("incoming: " +
" source: " + mhMsg.get_source() +
" dest: " + mhMsg.get_dest() +
" local-dest: " + to +
" type: " + mhMsg.get_type() +
" hops: " + (16 - mhMsg.get_ttl()));
}
}
private class DrainBeaconMsgReceiver implements MessageListener {
synchronized public void messageReceived(int to, Message m) {
DrainBeaconMsg mhMsg = (DrainBeaconMsg)m;
System.out.println("beacon: " +
" treeInstance: " + mhMsg.get_treeInstance() +
" root: " + mhMsg.get_source() +
" node: " + mhMsg.get_linkSource() +
" parent: " + mhMsg.get_parent() +
" cost: " + mhMsg.get_cost() +
" ttl: " + mhMsg.get_ttl());
}
}
public static void main(String args[]) {
DrainSniff ds = new DrainSniff();
}
}
--- NEW FILE: Makefile ---
TOS = $(shell ncc -print-tosdir)
PACKAGE = net.tinyos.drain
MIG = mig java
NCG = ncg java
LIB = $(TOSDIR)/lib
# List of message classes to build
MSGS = DrainMsg.java DrainBeaconMsg.java DrainGroupRegisterMsg.java DrainConsts.java
CONSTS =
INITIAL_TARGETS = $(MSGS) $(CONSTS)
OTHER_CLEAN = cleanmig
PLATFORM = telosb
PLATFORM_LIBS = -I$(TOS)/lib/CC2420Radio
ROOT = $(TOSDIR)/../tools/java
include $(ROOT)/Makefile.include
DrainMsg.java: $(LIB)/Drain/Drain.h
$(MIG) -target=$(PLATFORM) $(PLATFORM_LIBS) -java-classname=$(PACKAGE).DrainMsg $(LIB)/Drain/Drain.h DrainMsg -o $@
DrainBeaconMsg.java: $(LIB)/Drain/Drain.h
$(MIG) -target=$(PLATFORM) $(PLATFORM_LIBS) -java-classname=$(PACKAGE).DrainBeaconMsg $(LIB)/Drain/Drain.h DrainBeaconMsg -o $@
DrainGroupRegisterMsg.java: $(LIB)/Drain/Drain.h
$(MIG) -target=$(PLATFORM) $(PLATFORM_LIBS) -java-classname=$(PACKAGE).DrainGroupRegisterMsg $(LIB)/Drain/Drain.h DrainGroupRegisterMsg -o $@
DrainConsts.java: $(LIB)/Drain/Drain.h
$(NCG) -target=$(PLATFORM) $(PLATFORM_LIBS) -java-classname=$(PACKAGE).DrainConsts -I$(LIB)/Drain $(LIB)/Drain/Drain.h Drain.h -o $@
cleanmig:
rm -f $(MSGS) $(CONSTS) platforms.properties
More information about the Tinyos-commits
mailing list