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

Gilman Tolle gtolle at users.sourceforge.net
Tue Jan 18 16:01:30 PST 2005


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

Added Files:
	Drip.java Makefile 
Log Message:
Drip is now entirely independent of the Nucleus system. This is the official version of Drip, and the one that will see future improvements and bugfixes. There is a README.

--- NEW FILE: Drip.java ---
/** 
 *
 * Reliable Message Injector for the Drip protocol.
 * 
 * The default behavior in main() is to inject a 2-byte integer.
 * You, the developer, can inject whatever kind of message you want.
 *
 * @author Gilman Tolle <get at cs.berkeley.edu>
 * @since  0.1
 */

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

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

public class Drip extends TimerTask implements MessageListener {
    
    int state;

    int id;
    int seqno;
    
    int trickleStage;
    int trickleCountdown;
    int trickleAnnounce;
    boolean trickleSuppress;
    boolean trickleBeforeSend;

    int sendCount = 0;

    MoteIF moteIF;

    Timer trickle = new Timer();

    DripMsg dripMsg;
    boolean hasMessage = false;
    boolean sentOK = true;

    static boolean commandLine = false;
  
    public static boolean DEBUG = true;
	
    public Drip(int id) {

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

	this.id = id;

	dripMsg = new DripMsg();

	seqno = DripConsts.DRIP_SEQNO_OLDEST;
	trickle.schedule(this, 0, 1024);
    }

    public void injectMsg(Message msg, int msgSize) {
	dripMsg = new DripMsg(DripMsg.DEFAULT_MESSAGE_SIZE + msgSize);
	dripMsg.dataSet(msg.dataGet(), 0, dripMsg.offset_data(0),
			msgSize);
	hasMessage = true;
	seqno = DripConsts.DRIP_SEQNO_OLDEST;
	sendCount = 0;
    }

    public void run() {

	if (hasMessage) {

	    sendCount++;
	    
	    if (sendCount > 5 && seqno == DripConsts.DRIP_SEQNO_OLDEST) {
		seqno = DripConsts.DRIP_SEQNO_NEWEST;
		hasMessage = false;
	    }
	    
	    dripMsg.set_metadata_id((short)id);
	    dripMsg.set_metadata_seqno((byte)seqno);
	    
	    if (DEBUG) {
		System.out.println("Sending Msg: id=" + id + ",seqno=" + seqno);
	    }
	    send(dripMsg);
	}
    }

    private 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 messageReceived(int to, Message m) {
	
	DripMsg msg = (DripMsg)m;
	
	int newId = msg.get_metadata_id();
	int newSeqno = msg.get_metadata_seqno();
	
	if (newId != id)
	    return;
	
	if (DEBUG) {
	    System.out.println("Received Msg: id=" + newId + ",seqno=" + newSeqno);
	}
	
	if ((seqno == DripConsts.DRIP_SEQNO_OLDEST || sendCount > 5) 
	    && hasMessage) {
	    
	    seqno = newSeqno;
	    incrementSeqno();
	    sentOK = false;
	}
	
	if (sentOK == false && seqno == newSeqno) {
	    System.out.println("Message Injected Successfully");

	    if (commandLine) {
		System.exit(0);
	    } else {
		sentOK = true;
		hasMessage = false;
	    }
	}
    }
    
    private void incrementSeqno() {
	seqno = (seqno + 1) % 256;
	while (seqno == DripConsts.DRIP_SEQNO_OLDEST ||
	       seqno == DripConsts.DRIP_SEQNO_NEWEST) {
	    seqno = (seqno + 1) % 256;
	}
    }
    
    public static void main(String[] args) {
	String period = "1000";
	if (args.length > 0)
	    period = args[0];

	commandLine = true;

	Drip drip = new Drip(254);
	TestDripMsg msg = new TestDripMsg();
	msg.set_data((short)Integer.parseInt(period));

	drip.injectMsg(msg, TestDripMsg.DEFAULT_MESSAGE_SIZE);
    }
}

--- NEW FILE: Makefile ---
TOS = $(shell ncc -print-tosdir)
PACKAGE =
MIG = mig java
NCG = ncg java
LIB = $(TOSDIR)/../beta/Drip

# List of message classes to build
MSGS = DripMsg.java TestDripMsg.java
CONSTS = DripConsts.java
INITIAL_TARGETS = $(MSGS) $(CONSTS)
OTHER_CLEAN = cleanmig
PLATFORM = pc

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

DripMsg.java: $(LIB)/Drip.h
	$(MIG) -java-classname=$(PACKAGE).DripMsg $(LIB)/Drip.h DripMsg -o $@

DripConsts.java: $(LIB)/Drip.h
	$(NCG) -java-classname=$(PACKAGE).DripConsts -target=$(PLATFORM) -I$(LIB) $(LIB)/Drip.nc Drip.h -o $@

TestDripMsg.java: $(LIB)/TestDrip/TestDrip.h
	$(MIG) -java-classname=$(PACKAGE).TestDripMsg $(LIB)/TestDrip/TestDrip.h TestDripMsg -o $@

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




More information about the Tinyos-beta-commits mailing list