Commit 1db62ed6 authored by David Johnson's avatar David Johnson

Bunch of bug fixes.

parent be95cc12
This diff is collapsed.
......@@ -52,7 +52,7 @@ public final class PacketReader {
/* see http://www.tinyos.net/tinyos-1.x/doc/serialcomm/description.html */
final static int SYNC_BYTE = 0x7e;
final static int SYNC_BYTE = 0x7e;
final static int ESCAPE_BYTE = 0x7d;
final static int MTU = 256;
// don't need this cause we're not responding.
......@@ -71,6 +71,14 @@ public final class PacketReader {
this.in = in;
}
private void debug(int level,String msg) {
MoteLogger.globalDebug(level,"PacketReader: " + msg);
}
private void error(String msg) {
MoteLogger.globalError("PacketReader: " + msg);
}
// this method is meant to be called repeatedly, at a high rate.
// if "arrangements" in the caller (like synchronization with a queue)
// make fast, repetitious calls impossible, the timestamps will get off.
......@@ -84,6 +92,8 @@ public final class PacketReader {
// byte by byte for now -- ick!
// obviously, this method is NOT thread-safe.
debug(3,"trying to read a packet");
byte[] buf = new byte[MTU];
int currentOffset = 0;
boolean isEscaped = false;
......@@ -96,10 +106,13 @@ public final class PacketReader {
buf[i] = 0;
}
currentOffset = 0;
sync = false;
isEscaped = false;
}
// read byte -- let the exception fly through to the caller
byte b = (byte)in.read();
int b = (int)in.read();
debug(4,"read byte: "+b);
if (b == ESCAPE_BYTE) {
isEscaped = true;
......@@ -108,7 +121,8 @@ public final class PacketReader {
if (isEscaped && sync) {
// ex-xor it
b ^= 0x20;
buf[currentOffset++] = b;
buf[currentOffset++] = (byte)b;
isEscaped = false;
}
else if (isEscaped) {
// we're trying to sync via an escaped data byte, bad us
......@@ -116,9 +130,11 @@ public final class PacketReader {
}
else if (sync) {
// end of packet
debug(3,"saw end sync byte");
break;
}
else {
debug(3,"saw first sync byte");
sync = true;
}
}
......@@ -126,9 +142,10 @@ public final class PacketReader {
// normal data byte:
if (isEscaped) {
b ^= 0x20;
isEscaped = false;
}
buf[currentOffset++] = b;
buf[currentOffset++] = (byte)b;
}
}
......@@ -147,14 +164,18 @@ public final class PacketReader {
if (buf[0] == P_PACKET_NO_ACK) {
// no "prefix" byte
retval = new byte[buf.length-3];
retval = new byte[currentOffset-3];
System.arraycopy(buf,1,retval,0,retval.length);
debug(2,"recv no ack pkt; length="+retval.length);
return new LogPacket(vNodeName,t,retval,packetType,crc);
}
else if (buf[0] == P_PACKET_ACK) {
retval = new byte[buf.length-4];
System.arraycopy(buf,2,retval,0,retval.length);
debug(2,"recv ack pkt; length="+retval.length);
return new LogPacket(vNodeName,t,retval,packetType,crc);
}
......@@ -162,9 +183,13 @@ public final class PacketReader {
// do nothing for now; this is only sent by receiver on
// receipt of an unknown packet type anyway
;
debug(2,"recv ack/unknown pkt!");
}
else {
// XXX: might want to log these in the future...
debug(2,"recv TOTALLY unknown pkt!");
}
return null;
......
......@@ -32,7 +32,8 @@ public class SQLGenerator {
private ArrayList[] insertInfo;
private String className;
private String msgSpec;
private Class classInfo;
private String msgSpec[];
private String tag;
private int amType;
......@@ -47,6 +48,9 @@ public class SQLGenerator {
this.tag = tag;
Class c = Class.forName(className);
this.classInfo = c;
//debug(3,"sqlgen constructor for '"+name+"'");
// grab the am type first!
Object msgObj = c.newInstance();
......@@ -57,7 +61,7 @@ public class SQLGenerator {
// now figure out the methods. Basically, we figure out which
// get_* methods correspond to which arrays by finding the longest
// common substring following the get_ part of the string.
Methods[] m = c.getMethods();
Method[] m = c.getMethods();
Vector flatFieldMethods = new Vector();
Vector arrayFieldMethods = new Vector();
......@@ -111,9 +115,9 @@ public class SQLGenerator {
;
}
else {
// simple, one-table layout with two views.
// simple, n-table layout with two views; one master table.
int numTables = 1 + arrayFields.length;
int tableIndex = 1;
int tableIdx = 1;
tableCreates = new String[numTables];
inserts = new String[numTables];
......@@ -123,10 +127,10 @@ public class SQLGenerator {
// main table
String tableName = className + "__data_" + tag;
parentTableName = tableName;
String parentTableName = tableName;
tableCreates[0] = "" +
"CREATE TABLE " + tableName + " " +
"CREATE TABLE IF NOT EXISTS " + tableName + " " +
"(" + "id INT PRIMARY KEY AUTO_INCREMENT, " +
"time DATETIME, " +
"amType INT, " +
......@@ -134,6 +138,8 @@ public class SQLGenerator {
inserts[0] = "insert into " + tableName +
" values (NULL,?," + this.amType + ",?";
insertInfo[0] = new ArrayList();
insertInfo[0].add( new String[] { "date",null,null } );
// now get this statically.
......@@ -147,11 +153,11 @@ public class SQLGenerator {
String mn = ma[i].getName();
if (mn.startsWith("get_")) {
String mnSansGet = mn.subString(4,mnSansGet.length());
String mnSansGet = mn.substring(4,mn.length());
Method signMethod = null;
try {
signMethod = c.getMethod("isSigned_" + mnSansGet);
signMethod = c.getMethod("isSigned_" + mnSansGet,null);
}
catch (Exception e) {
// no big deal
......@@ -186,8 +192,8 @@ public class SQLGenerator {
// we add the table now:
String aTableName = mnSansGet + "__data_" + tag;
tableCreates[tableIdx] = "" +
"CREATE TABLE " + aTableName + " (" +
"ppid INT PRIMARY KEY NOT NULL, " +
"CREATE TABLE IF NOT EXISTS " + aTableName +
" (" + "ppid INT PRIMARY KEY NOT NULL, " +
"idx INT NOT NULL, " +
"" + mnSansGet + " " + internalArraySQLType +
")";
......@@ -225,10 +231,23 @@ public class SQLGenerator {
inserts[0] += ")";
}
for (int k = 0; k < tableCreates.length; ++k) {
debug(3,"tableCreates["+k+"] = '" + tableCreates[k] + "'.");
debug(3,"inserts["+k+"] = '" + inserts[k] + "'.");
}
tablesCreated = false;
}
private void debug(int level,String msg) {
MoteLogger.globalDebug(level,"SQLGenerator (" + className + "): "+msg);
}
private void error(String msg) {
MoteLogger.globalError("SQLGenerator (" + className + "): " + msg);
}
public String[] getTableCreates() {
return tableCreates;
}
......@@ -253,50 +272,84 @@ public class SQLGenerator {
tablesCreated = true;
}
catch (SQLException e) {
System.err.println("Problem while creating tables for " +
this.className + "!");
error("problem while creating tables!");
e.printStackTrace();
}
}
// run the inserts:
int errors = 0;
// the first table is always the master parent table...
int ppid = -1;
for (int i = 0; i < inserts.length; ++i) {
ArrayList aInfo = insertInfo[i];
// each element in the arrayList is a String[] with the
// following data:
// [ getMethodName, returnType, SQLType ]
//private ArrayList[] insertInfo;
// setup the prepared stmt:
try {
PreparedStatement ps = conn.prepareStatement(inserts[i]);
int ppid = -1;
for (int j = 0; j < aInfo.length(); ++j) {
String[] info = (String[])aInfo.get(j);
// if not an array insert (i.e., is main table insert):
if (true) {
for (int j = 0; j < aInfo.size(); ++j) {
String[] info = (String[])aInfo.get(j);
// now perform the ps.set op...
if (info[0].equals("date")) {
// insert the date manually
// note that we use j+1 since preparedStatement
// vars start at index 1
ps.setTimestamp(j+1,(new java.sql.Timestamp(lp.getTimeStamp().getTime())));
}
else if (info[0].equals("srcMote")) {
ps.setString(j+1,lp.getSrcVnodeName());
}
else if (info[0].equals("ppid")) {
ps.setInt(j+1,ppid);
}
else if (info[0].equals("idx")) {
//ps.setInt();
}
else {
// "normal" field :-)
if (info[1] != null &&
(info[1].equals("short")
|| info[1].equals("S"))) {
// now perform the ps.set op...
if (info[0].equals("date")) {
// insert the date manually
// note that we use j+1 since preparedStatement vars
// start at index 1
ps.setDate(j+1,lp.getDate());
}
else if (info[0].equals("srcMote")) {
ps.setString(j+1,lp.getSrcMote());
}
else if (info[0].equals("ppid")) {
ps.setInt(j+1,ppid);
}
else if (info[0].equals("idx")) {
;
}
else {
// "normal" field :-)
int retval = 0;
Object msgObject = lp.getMsgObject();
Method m = this.classInfo.getMethod(info[0],
null);
Short s = (Short)m.invoke(msgObject,null);
ps.setInt(j+1,(int)s.shortValue());
}
else if (info[1] != null &&
(info[1].equals("I")
|| info[1].equals("int"))) {
}
}
}
// dump the stmt...
ps.executeUpdate();
// now, if we were dumping into the main table (idx=0),
// grab the id, because it's the global packet id for
// this msg type.
if (j == 0) {
if (i == 0) {
// should be in the result set... ?
// this only works with mysql 5.x and up, with
......@@ -305,24 +358,31 @@ public class SQLGenerator {
// anytime soon, you'll need to try another technique
// -- there are a couple.
// i.e., select last_insert_id()
ResultSet rs = stmt.getGeneratedKeys();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
ppid = rs.getInt(1);
}
else {
System.err.println("Could not get ppid from " +
"main table insert -- cannot " +
"add anything to array tables!");
error("Could not get ppid from " +
"main table insert -- cannot " +
"add anything to array tables!");
}
rs.close();
}
}
else {
// XXXX we have an array insert... need to do
// multiple inserts on this prepared stmt, using ppid
// as the backref:
;
}
}
catch (Exception e) {
System.err.println("Problem with insert for msg of type " +
this.amType + ":");
error("problem with insert for msg of type " +
this.amType + ":");
e.printStackTrace();
++errors;
......@@ -350,12 +410,13 @@ public class SQLGenerator {
boolean getArrayType)
throws Exception {
String rt = ma.getReturnType().getName();
String rt = m.getReturnType().getName();
boolean isSigned = false;
if (signMethod != null) {
try {
isSigned = ((Boolean)isSignedMethod.invoke()).getBoolValue();
Object msgObj = classInfo.newInstance();
isSigned = ((Boolean)signMethod.invoke(msgObj,null)).booleanValue();
}
catch (Exception e) {
// do nothing; there just wasn't sign info...
......@@ -417,7 +478,7 @@ public class SQLGenerator {
retval += " UNSIGNED";
}
}
else if (rt.equals("S")) {
else if (rt.equals("S") || rt.equals("short")) {
// short
retval = "SMALLINT";
......@@ -430,6 +491,8 @@ public class SQLGenerator {
// (or a multidim array, but we ruled this out earlier)
// XXX: should throw exception or something
error("unrecognized return type '" + rt +"'!");
}
return retval;
......
......@@ -11,8 +11,12 @@ public class SynchQueue extends LinkedList {
this.addFirst(o);
}
public synchronized Object queueRemove(Object o) {
public synchronized Object queueRemove() {
return this.removeLast();
}
public synchronized Object peek() {
return this.getLast();
}
}
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