[Tinyos-beta-commits]
CVS: tinyos-1.x/beta/Drip/tools/java/net/tinyos/drip
Drip.java, NONE, 1.1 Makefile, NONE, 1.1
Gilman Tolle
gtolle at users.sourceforge.net
Mon Mar 14 17:11:18 PST 2005
Update of /cvsroot/tinyos/tinyos-1.x/beta/Drip/tools/java/net/tinyos/drip
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11646/tools/java/net/tinyos/drip
Added Files:
Drip.java Makefile
Log Message:
Moved Drip tools into a proper package, so that they can be referenced by the Nucleus tools (and any other tools)
--- NEW FILE: Drip.java ---
/**
*
* Reliable Message Injector for the Drip protocol.
*
* @author Gilman Tolle <get at cs.berkeley.edu>
* @since 0.1
*/
package net.tinyos.drip;
import net.tinyos.message.*;
import net.tinyos.util.*;
import java.io.*;
import java.text.*;
import java.util.*;
public class Drip extends TimerTask implements MessageListener {
public static boolean DEBUG = false;
int id;
int seqno;
int sendCount = 0;
Timer trickle;
MoteIF moteIF;
DripMsg dripMsg;
boolean hasMessage = false;
boolean sentOK = 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;
}
public synchronized void send(Message msg, int msgSize) {
trickle = new Timer();
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;
trickle.schedule(this, 0, 1024);
try {
wait();
} catch (InterruptedException e) {
// return
}
}
private synchronized void sendDone() {
notifyAll();
}
public void run() {
sendCount++;
if (sendCount > 6) {
trickle.cancel();
sendDone();
}
if (hasMessage) {
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) {
if (DEBUG) {
System.out.println("Message Injected Successfully");
}
sentOK = true;
hasMessage = false;
sendDone();
}
}
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 data = "1000";
if (args.length > 0)
data = args[0];
Drip drip = new Drip(254);
TestDripMsg msg = new TestDripMsg();
msg.set_data((short)Integer.parseInt(data));
drip.send(msg, TestDripMsg.DEFAULT_MESSAGE_SIZE);
System.exit(0);
}
}
--- NEW FILE: Makefile ---
TOS = $(shell ncc -print-tosdir)
PACKAGE = net.tinyos.drip
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