Commit 42bfa924 authored by David Johnson's avatar David Johnson

Most of the rest of the app code for logging packets. Still have to write

a bit more, then hack it into the testbed.
parent c94fe76c
import java.util.*; import java.util.*;
//import net.tinyos.message.Message; import net.tinyos.message.Message;
public class LogPacket { public class LogPacket {
...@@ -8,9 +8,12 @@ public class LogPacket { ...@@ -8,9 +8,12 @@ public class LogPacket {
private byte[] data; private byte[] data;
private int packetType; private int packetType;
private int crc; private int crc;
//private Message msg; private Object msg;
private String srcVnodeName;
public LogPacket(Date time,byte[] data,int packetType,int crc) { public LogPacket(String srcVnodeName,Date time,byte[] data,
int packetType,int crc) {
this.srcVnodeName = srcVnodeName;
this.timeStamp = time; this.timeStamp = time;
this.data = data; this.data = data;
this.packetType = packetType; this.packetType = packetType;
...@@ -34,4 +37,16 @@ public class LogPacket { ...@@ -34,4 +37,16 @@ public class LogPacket {
return this.crc; return this.crc;
} }
public String getSrcVnodeName() {
return this.srcVnodeName;
}
public void setMsgObject(Object obj) {
this.msg = obj;
}
public Object getMsgObject() {
return this.msg;
}
} }
...@@ -9,6 +9,7 @@ public class MoteLogger { ...@@ -9,6 +9,7 @@ public class MoteLogger {
private int debug; private int debug;
private File classDir; private File classDir;
private String[] classNames;
private File aclDir; private File aclDir;
private String logTag; private String logTag;
private String pid; private String pid;
...@@ -18,6 +19,7 @@ public class MoteLogger { ...@@ -18,6 +19,7 @@ public class MoteLogger {
private String dbURL = "jdbc.DriverMysql"; private String dbURL = "jdbc.DriverMysql";
private String dbUser = "root"; private String dbUser = "root";
private String dbPass = ""; private String dbPass = "";
private Hashtable classes;
public static void main(String args[]) { public static void main(String args[]) {
// //
...@@ -35,6 +37,7 @@ public class MoteLogger { ...@@ -35,6 +37,7 @@ public class MoteLogger {
public static void parseArgsAndRun(String args[]) { public static void parseArgsAndRun(String args[]) {
File classDir = null; File classDir = null;
String[] classNames = null;
String tag = null; String tag = null;
String pid = null; String pid = null;
String eid = null; String eid = null;
...@@ -44,12 +47,21 @@ public class MoteLogger { ...@@ -44,12 +47,21 @@ public class MoteLogger {
int i; int i;
for (i = 0; i < args.length; ++i) { for (i = 0; i < args.length; ++i) {
if (args[i].equals("-c")) { if (args[i].equals("-C")) {
if (++i < args.length && !args[i].startsWith("-")) { if (++i < args.length && !args[i].startsWith("-")) {
classDir = new File(args[i]); classDir = new File(args[i]);
} }
else { else {
System.err.println("option '-c' must have an argument!"); System.err.println("option '-C' must have an argument!");
usage();
}
}
else if (args[i].equals("-c")) {
if (++i < args.length && !args[i].startsWith("-")) {
classNames = args[i].split(",");
}
else {
System.err.println("option -c must have an argument!");
usage(); usage();
} }
} }
...@@ -113,7 +125,7 @@ public class MoteLogger { ...@@ -113,7 +125,7 @@ public class MoteLogger {
//this.motes = motes; //this.motes = motes;
// startup // startup
MoteLogger ml = new MoteLogger(classDir,aclDir,motes, MoteLogger ml = new MoteLogger(classDir,classNames,aclDir,motes,
pid,eid,tag,debug); pid,eid,tag,debug);
ml.run(); ml.run();
} }
...@@ -122,8 +134,10 @@ public class MoteLogger { ...@@ -122,8 +134,10 @@ public class MoteLogger {
String usage = "" + String usage = "" +
"Usage: java MoteLogger -cipMd \n" + "Usage: java MoteLogger -cipMd \n" +
"Options:\n" + "Options:\n" +
"\t-c <classdir> Directory containing packet-matching " + "\t-C <classdir> Directory containing packet-matching " +
"classfiles \n" + "classfiles \n" +
"\t-c <classfile list> Comma-separated list of fully-qualified " +
"classnames. \n" +
"\t-i <idtag> Alphanumeric tag for this logging set \n" + "\t-i <idtag> Alphanumeric tag for this logging set \n" +
"\t-p pid,eid \n" + "\t-p pid,eid \n" +
//"\t-m <vname,vname,...> (list of vnames present in acl dir) \n"+ //"\t-m <vname,vname,...> (list of vnames present in acl dir) \n"+
...@@ -135,15 +149,18 @@ public class MoteLogger { ...@@ -135,15 +149,18 @@ public class MoteLogger {
System.exit(-1); System.exit(-1);
} }
public MoteLogger(File classDir,File aclDir,String[] motes,String pid, public MoteLogger(File classDir,String[] classNames,
File aclDir,String[] motes,String pid,
String eid,String tag,int debug) { String eid,String tag,int debug) {
this.classDir = classDir; this.classDir = classDir;
this.classNames = classNames;
this.aclDir = aclDir; this.aclDir = aclDir;
this.pid = pid; this.pid = pid;
this.eid = eid; this.eid = eid;
this.logTag = tag; this.logTag = tag;
this.debug = debug; this.debug = debug;
this.motes = motes; this.motes = motes;
this.classes = new Hashtable();
} }
// I know, not a thread, but who cares // I know, not a thread, but who cares
...@@ -177,6 +194,55 @@ public class MoteLogger { ...@@ -177,6 +194,55 @@ public class MoteLogger {
System.exit(0); System.exit(0);
} }
// load classfiles and create Class objs so we can do instance objs
// on incoming packet data
// first, have to discover the classfiles, if they weren't specified.
if (this.classNames == null || classNames.length == 0) {
// try to read the classFile directory and use the XXX.class
// names as the classes -- will fail if classes are in package.
File[] classFiles = classDir.listFiles( new FilenameFilter() {
public boolean accept(File dir, String name) {
if (name != null && name.endsWith(".class")) {
return true;
}
else {
return false;
}
}
});
if (classFiles != null) {
this.classNames = new String[classFiles.length];
for (int i = 0; i < classFiles.length; ++i) {
String[] sa = classFiles[i].getName().split("\\.class");
this.classNames[i] = sa[0];
}
}
}
if (classFiles == null || classFiles.length == 0) {
System.out.println("Could not find any classfiles; exiting.");
System.exit(0);
}
// second, actually load them.
for (int i = 0; i < classNames.length; ++i) {
try {
Class c = Class.forName(classNames[i]);
// this call does the class.forName, redundant i know.
classes.put(c,new SQLGenerator(classNames[i],
null,
this.logTag));
}
catch (Exception e) {
System.err.println("Problem loading class " + classNames[i] +
":");
e.printStackTrace();
}
}
// get capture keys // get capture keys
Hashtable acls = new Hashtable(); Hashtable acls = new Hashtable();
for (int i = 0; i < motes.length; ++i) { for (int i = 0; i < motes.length; ++i) {
...@@ -194,22 +260,114 @@ public class MoteLogger { ...@@ -194,22 +260,114 @@ public class MoteLogger {
packetQueue = new SynchQueue(); packetQueue = new SynchQueue();
// connect to the database // connect to the database
; Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:" +
"//localhost/test?user=root");
}
catch (Exception e) {
System.err.println("FATAL -- couldn't connect to the database: " +
e.getMessage());
e.printStackTrace();
System.exit(-2);
}
// spawn connection threads // spawn connection threads
for (Enumeration e1 = acls.keys(); e1.hasMoreElements(); ) { for (Enumeration e1 = acls.keys(); e1.hasMoreElements(); ) {
String vNN = (String)e1.nextElement(); String vNN = (String)e1.nextElement();
ElabACL acl = (ElabACL)acls.get(vNN); ElabACL acl = (ElabACL)acls.get(vNN);
(new MoteLogThread(acl,packetQueue)).start(); (new MoteThread(acl,packetQueue)).start();
} }
// now, process the packet queue forever.
while (true) {
LogPacket lp = null;
synchronized(packetQueue) {
while(packetQueue.peek() == null) {
try {
packetQueue.wait();
}
catch (Exception e) {
e.printStackTrace();
}
}
// once we get a packet...
lp = packetQueue.queueRemove();
}
// dump it out to database:
// this is the real work:
try {
// first match the packet:
SQLGenerator sq = null;
boolean foundMatch = false;
for (Enumeration e1 = classes.keys();
!foundMatch && e1.hasMoreElements(); ) {
Class cc = (Class)e1.nextElement();
sq = (SQLGenerator)classes.get(cc);
// Need to create a BaseTOSMsg
// and extract the 'type' field... which is the am type
// ... and then match this with the amType() method of the
// invoked class.
// XXX: note that this must be changed depending on the
// architecture of the motes we're connecting to, in order
// to decode using the correct host byte order assumption.
// i.e., net.tinyos.message.telos.*
//
if (true) {
net.tinyos.message.avrmote.BaseTOSMsg btm =
new net.tinyos.message.avrmote.BaseTOSMsg(lp.getData());
if (lp.amType() == sq.getAMType()) {
// class match
// invoke the byte[] data constructor on the data
// from the basetosmsg:
Constructor ccc = cc.getConstructor( new Class[] {
byte[].class } );
Object msgObj = ccc.newInstance( new Object[] {
btm.get_data() } );
lp.setMsgObject(msgObj);
// ready to log!!!
foundMatch = true;
}
}
}
// do the db insert:
sq.storeMessage(msgObj,conn);
// that's all, folks!
}
catch (Exception e) {
System.err.println("Error while logging packet: ");
e.printStackTrace();
}
}
} }
class MoteLogThread extends Thread { class MoteThread extends Thread {
private String vNodeName;
private ElabACL acl; private ElabACL acl;
private SynchQueue q; private SynchQueue q;
private Socket sock; private Socket sock;
public MoteLogThread(ElabACL acl,SynchQueue packetQueue) { public MoteLogThread(String vNodeName,ElabACL acl,
SynchQueue packetQueue) {
this.vNodeName = vNodeName;
this.acl = acl; this.acl = acl;
this.q = packetQueue; this.q = packetQueue;
} }
...@@ -313,7 +471,7 @@ public class MoteLogger { ...@@ -313,7 +471,7 @@ public class MoteLogger {
PacketReader pr = null; PacketReader pr = null;
try { try {
pr = new PacketReader(sock.getInputStream()); pr = new PacketReader(vNodeName,sock.getInputStream());
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
...@@ -324,6 +482,11 @@ public class MoteLogger { ...@@ -324,6 +482,11 @@ public class MoteLogger {
lp = null; lp = null;
try { try {
lp = pr.readPacket(); lp = pr.readPacket();
synchronized(packetQueue) {
packetQueue.queueAdd(lp);
packetQueue.notifyAll();
}
} }
catch (Exception e) { catch (Exception e) {
System.err.println("Problem while reading from " + System.err.println("Problem while reading from " +
......
...@@ -64,8 +64,10 @@ public final class PacketReader { ...@@ -64,8 +64,10 @@ public final class PacketReader {
final static int P_UNKNOWN = 255; final static int P_UNKNOWN = 255;
private InputStream in; private InputStream in;
private String vNodeName;
public PacketReader(InputStream in) { public PacketReader(String vNodeName,InputStream in) {
this.vNodeName = vNodeName;
this.in = in; this.in = in;
} }
...@@ -148,13 +150,13 @@ public final class PacketReader { ...@@ -148,13 +150,13 @@ public final class PacketReader {
retval = new byte[buf.length-3]; retval = new byte[buf.length-3];
System.arraycopy(buf,1,retval,0,retval.length); System.arraycopy(buf,1,retval,0,retval.length);
return new LogPacket(t,retval,packetType,crc); return new LogPacket(vNodeName,t,retval,packetType,crc);
} }
else if (buf[0] == P_PACKET_ACK) { else if (buf[0] == P_PACKET_ACK) {
retval = new byte[buf.length-4]; retval = new byte[buf.length-4];
System.arraycopy(buf,2,retval,0,retval.length); System.arraycopy(buf,2,retval,0,retval.length);
return new LogPacket(t,retval,packetType,crc); return new LogPacket(vNodeName,t,retval,packetType,crc);
} }
else if (buf[0] == P_ACK || buf[0] == P_UNKNOWN) { else if (buf[0] == P_ACK || buf[0] == P_UNKNOWN) {
// do nothing for now; this is only sent by receiver on // do nothing for now; this is only sent by receiver on
...@@ -165,7 +167,7 @@ public final class PacketReader { ...@@ -165,7 +167,7 @@ public final class PacketReader {
// XXX: might want to log these in the future... // XXX: might want to log these in the future...
} }
return lp; return null;
} }
} }
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment