Merge pull request #1502 from yatch/pr-radiologger

Fix timestamps of packets recorded in pcap files
This commit is contained in:
Simon Duquennoy 2016-11-27 11:58:03 +01:00 committed by GitHub
commit 728e417e04
5 changed files with 25 additions and 8 deletions

View File

@ -71,6 +71,7 @@ public class Simulation extends Observable implements Runnable {
private long speedLimitLastSimtime; private long speedLimitLastSimtime;
private long speedLimitLastRealtime; private long speedLimitLastRealtime;
private long lastStartTime;
private long currentSimulationTime = 0; private long currentSimulationTime = 0;
private String title = null; private String title = null;
@ -248,7 +249,7 @@ public class Simulation extends Observable implements Runnable {
} }
public void run() { public void run() {
long lastStartTime = System.currentTimeMillis(); lastStartTime = System.currentTimeMillis();
logger.info("Simulation main loop started, system time: " + lastStartTime); logger.info("Simulation main loop started, system time: " + lastStartTime);
isRunning = true; isRunning = true;
speedLimitLastRealtime = System.currentTimeMillis(); speedLimitLastRealtime = System.currentTimeMillis();
@ -1075,6 +1076,16 @@ public class Simulation extends Observable implements Runnable {
return currentSimulationTime / MILLISECOND; return currentSimulationTime / MILLISECOND;
} }
/**
* Return the actual time value corresponding to an argument which
* is a simulation time value in microseconds.
*
* @return Actual time (microseconds)
*/
public long convertSimTimeToActualTime(long simTime) {
return simTime + lastStartTime * 1000;
}
/** /**
* Changes radio medium of this simulation to the given. * Changes radio medium of this simulation to the given.
* *

View File

@ -694,8 +694,8 @@ public class RadioLogger extends VisPlugin {
StringBuilder verbose = new StringBuilder(); StringBuilder verbose = new StringBuilder();
/* default analyzer */ /* default analyzer */
PacketAnalyzer.Packet packet = new PacketAnalyzer.Packet(data, PacketAnalyzer.MAC_LEVEL); PacketAnalyzer.Packet packet = new PacketAnalyzer.Packet(data, PacketAnalyzer.MAC_LEVEL,
simulation.convertSimTimeToActualTime(conn.startTime));
if (analyzePacket(packet, brief, verbose)) { if (analyzePacket(packet, brief, verbose)) {
if (packet.hasMoreData()) { if (packet.hasMoreData()) {
byte[] payload = packet.getPayload(); byte[] payload = packet.getPayload();

View File

@ -69,7 +69,7 @@ public class IEEE802154Analyzer extends PacketAnalyzer {
if (pcapExporter != null) { if (pcapExporter != null) {
try { try {
pcapExporter.exportPacketData(packet.getPayload()); pcapExporter.exportPacketData(packet.getPayload(), packet.getTimestamp());
} catch (IOException e) { } catch (IOException e) {
logger.error("Could not export PCap data", e); logger.error("Could not export PCap data", e);
} }

View File

@ -18,6 +18,7 @@ public abstract class PacketAnalyzer {
int level; int level;
/* size = length - consumed bytes at tail */ /* size = length - consumed bytes at tail */
int size; int size;
long ts; /* in microseconds */
/* L2 addresseses */ /* L2 addresseses */
byte[] llsender; byte[] llsender;
@ -25,10 +26,11 @@ public abstract class PacketAnalyzer {
byte lastDispatch = 0; byte lastDispatch = 0;
public Packet(byte[] data, int level) { public Packet(byte[] data, int level, long ts) {
this.level = level; this.level = level;
this.data = data.clone(); this.data = data.clone();
this.size = data.length; this.size = data.length;
this.ts = ts;
} }
public void consumeBytesStart(int bytes) { public void consumeBytesStart(int bytes) {
@ -79,6 +81,10 @@ public abstract class PacketAnalyzer {
public byte[] getLLReceiver() { public byte[] getLLReceiver() {
return llreceiver; return llreceiver;
} }
public long getTimestamp() {
return ts;
}
}; };
public abstract boolean matchPacket(Packet packet); public abstract boolean matchPacket(Packet packet);

View File

@ -41,15 +41,15 @@ public class PcapExporter {
out = null; out = null;
} }
public void exportPacketData(byte[] data) throws IOException { public void exportPacketData(byte[] data, long ts) throws IOException {
if (out == null) { if (out == null) {
/* pcap file never set, open default */ /* pcap file never set, open default */
openPcap(null); openPcap(null);
} }
try { try {
/* pcap packet header */ /* pcap packet header */
out.writeInt((int) (System.currentTimeMillis() / 1000)); out.writeInt((int) (ts / 1000000));
out.writeInt((int) ((System.currentTimeMillis() % 1000) * 1000)); out.writeInt((int) (ts % 1000000));
out.writeInt(data.length); out.writeInt(data.length);
out.writeInt(data.length); out.writeInt(data.length);
/* and the data */ /* and the data */