Added duplicate detection

This commit is contained in:
nifi 2010-09-06 22:19:09 +00:00
parent 1b87be975a
commit 8096e26f22
2 changed files with 81 additions and 16 deletions

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: SensorData.java,v 1.8 2010/08/31 13:05:40 nifi Exp $ * $Id: SensorData.java,v 1.9 2010/09/06 22:19:09 nifi Exp $
* *
* ----------------------------------------------------------------- * -----------------------------------------------------------------
* *
@ -34,8 +34,8 @@
* *
* Authors : Joakim Eriksson, Niclas Finne * Authors : Joakim Eriksson, Niclas Finne
* Created : 3 jul 2008 * Created : 3 jul 2008
* Updated : $Date: 2010/08/31 13:05:40 $ * Updated : $Date: 2010/09/06 22:19:09 $
* $Revision: 1.8 $ * $Revision: 1.9 $
*/ */
package se.sics.contiki.collect; package se.sics.contiki.collect;
@ -51,6 +51,7 @@ public class SensorData implements SensorInfo {
private final long nodeTime; private final long nodeTime;
private final long systemTime; private final long systemTime;
private int seqno; private int seqno;
private boolean isDuplicate;
public SensorData(Node node, int[] values, long systemTime) { public SensorData(Node node, int[] values, long systemTime) {
this.node = node; this.node = node;
@ -68,6 +69,14 @@ public class SensorData implements SensorInfo {
return node.getID(); return node.getID();
} }
public boolean isDuplicate() {
return isDuplicate;
}
public void setDuplicate(boolean isDuplicate) {
this.isDuplicate = isDuplicate;
}
public int getSeqno() { public int getSeqno() {
return seqno; return seqno;
} }

View file

@ -26,7 +26,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: SensorDataAggregator.java,v 1.3 2010/08/31 13:05:40 nifi Exp $ * $Id: SensorDataAggregator.java,v 1.4 2010/09/06 22:19:09 nifi Exp $
* *
* ----------------------------------------------------------------- * -----------------------------------------------------------------
* *
@ -34,8 +34,8 @@
* *
* Authors : Joakim Eriksson, Niclas Finne * Authors : Joakim Eriksson, Niclas Finne
* Created : 20 aug 2008 * Created : 20 aug 2008
* Updated : $Date: 2010/08/31 13:05:40 $ * Updated : $Date: 2010/09/06 22:19:09 $
* $Revision: 1.3 $ * $Revision: 1.4 $
*/ */
package se.sics.contiki.collect; package se.sics.contiki.collect;
@ -51,6 +51,7 @@ public class SensorDataAggregator implements SensorInfo {
private int maxSeqno = Integer.MIN_VALUE; private int maxSeqno = Integer.MIN_VALUE;
private int seqnoDelta = 0; private int seqnoDelta = 0;
private int dataCount; private int dataCount;
private int duplicates = 0;
public SensorDataAggregator(Node node) { public SensorDataAggregator(Node node) {
this.node = node; this.node = node;
@ -82,20 +83,63 @@ public class SensorDataAggregator implements SensorInfo {
} }
public void addSensorData(SensorData data) { public void addSensorData(SensorData data) {
int s = data.getValue(SEQNO) + seqnoDelta; int seqn = data.getValue(SEQNO);
for (int i = 0, n = Math.min(VALUES_COUNT, data.getValueCount()); i < n; i++) { int s = seqn + seqnoDelta;
values[i] += data.getValue(i);
if (s <= maxSeqno) {
// Check for duplicates among the last 5 packets
for(int n = node.getSensorDataCount(), i = n > 5 ? n - 5 : 0; i < n; i++) {
SensorData sd = node.getSensorData(i);
if (sd.getValue(SEQNO) != seqn || sd == data || sd.getValueCount() != data.getValueCount()) {
// Not a duplicate
} else if (Math.abs(data.getNodeTime() - sd.getNodeTime()) > 180000) {
// Too long time between packets. Not a duplicate.
// System.err.println("Too long time between packets with same seqno from "
// + data.getNode() + ": "
// + (Math.abs(data.getNodeTime() - sd.getNodeTime()) / 1000)
// + " sec, " + (n - i) + " packets ago");
} else {
data.setDuplicate(true);
// Verify that the packet is a duplicate
for(int j = DATA_LEN2, m = data.getValueCount(); j < m; j++) {
if (sd.getValue(j) != data.getValue(j)) {
data.setDuplicate(false);
// System.out.println("NOT Duplicate: " + data.getNode() + " ("
// + (n - i) + ": "
// + ((data.getNodeTime() - sd.getNodeTime()) / 1000) + "sek): "
// + seqn + " value[" + j + "]: " + sd.getValue(j) + " != "
// + data.getValue(j));
break;
}
}
if (data.isDuplicate()) {
// System.out.println("Duplicate: " + data.getNode() + ": " + seqn
// + ": "
// + (Math.abs(data.getNodeTime() - sd.getNodeTime()) / 1000)
// + " sec, " + (n - i) + " packets ago");
duplicates++;
break;
}
}
}
} }
// Handle wrapping sequence numbers if (!data.isDuplicate()) {
if (dataCount > 0 && maxSeqno - s > 2) { for (int i = 0, n = Math.min(VALUES_COUNT, data.getValueCount()); i < n; i++) {
s += maxSeqno - seqnoDelta; values[i] += data.getValue(i);
seqnoDelta = maxSeqno; }
// Handle wrapping sequence numbers
if (dataCount > 0 && maxSeqno - s > 2) {
s += maxSeqno - seqnoDelta;
seqnoDelta = maxSeqno;
}
if (s < minSeqno) minSeqno = s;
if (s > maxSeqno) maxSeqno = s;
dataCount++;
} }
data.setSeqno(s); data.setSeqno(s);
if (s < minSeqno) minSeqno = s;
if (s > maxSeqno) maxSeqno = s;
dataCount++;
} }
public void clear() { public void clear() {
@ -103,6 +147,10 @@ public class SensorDataAggregator implements SensorInfo {
values[i] = 0L; values[i] = 0L;
} }
dataCount = 0; dataCount = 0;
duplicates = 0;
minSeqno = Integer.MAX_VALUE;
maxSeqno = Integer.MIN_VALUE;
seqnoDelta = 0;
} }
public String toString() { public String toString() {
@ -172,6 +220,14 @@ public class SensorDataAggregator implements SensorInfo {
return getAverageValue(BEST_NEIGHBOR_ETX) / 16.0; return getAverageValue(BEST_NEIGHBOR_ETX) / 16.0;
} }
public int getPacketCount() {
return node.getSensorDataCount();
}
public int getDuplicateCount() {
return duplicates;
}
public int getMinSeqno() { public int getMinSeqno() {
return minSeqno; return minSeqno;
} }