changeset 1030:170c8ce25bef

introducing PcapOutput
author Devel 1
date Thu, 02 Apr 2020 10:40:41 +0200
parents e47bfb487cfd
children 37d098b33b23
files stress-tester/src/main/java/com/passus/st/emitter/pcap/PcapOutput.java stress-tester/src/main/java/com/passus/st/emitter/pcap/UnidirectionalPcapChannelContext.java stress-tester/src/main/java/com/passus/st/emitter/pcap/UnidirectionalPcapWorker.java
diffstat 3 files changed, 89 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/emitter/pcap/PcapOutput.java	Thu Apr 02 10:40:41 2020 +0200
@@ -0,0 +1,78 @@
+package com.passus.st.emitter.pcap;
+
+import com.passus.pcap.Pcap;
+import com.passus.pcap.PcapDumper;
+import com.passus.pcap.PcapLinkType;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public interface PcapOutput {
+
+    Pcap pcap();
+
+    int sendPacket(int length, byte[] payload);
+
+    void close();
+
+    public static class Sender implements PcapOutput {
+
+        private final Pcap pcap;
+
+        public Sender(Pcap pcap) {
+            this.pcap = pcap;
+        }
+
+        @Override
+        public Pcap pcap() {
+            return pcap;
+        }
+
+        @Override
+        public int sendPacket(int length, byte[] payload) {
+            return pcap.sendPacket(length, payload);
+        }
+
+        @Override
+        public void close() {
+            pcap.close();
+        }
+    }
+
+    public static class Writer implements PcapOutput {
+
+        private final Pcap pcap;
+        private final PcapDumper dumper;
+
+        public Writer(Pcap pcap, PcapDumper dumper) {
+            this.pcap = pcap;
+            this.dumper = dumper;
+        }
+
+        @Override
+        public Pcap pcap() {
+            return pcap;
+        }
+
+        @Override
+        public int sendPacket(int length, byte[] payload) {
+            dumper.dump(System.currentTimeMillis(), length, payload);
+            return 0;
+        }
+
+        @Override
+        public void close() {
+            dumper.close();
+            pcap.close();
+        }
+
+    }
+
+    public static Writer writer(String file) {
+        Pcap pcap = Pcap.openDead(PcapLinkType.DLT_EN10MB, 65536);
+        StringBuilder sb = new StringBuilder();
+        PcapDumper pd = PcapDumper.open(pcap, "", sb);
+        return new Writer(pcap, pd);
+    }
+}
--- a/stress-tester/src/main/java/com/passus/st/emitter/pcap/UnidirectionalPcapChannelContext.java	Thu Apr 02 10:26:17 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/pcap/UnidirectionalPcapChannelContext.java	Thu Apr 02 10:40:41 2020 +0200
@@ -18,7 +18,7 @@
 
     private final UnidirectionalPcapWorker worker;
 
-    private Pcap pcap;
+    private PcapOutput pcapOut;
 
     final Queue<byte[]> dataQueue;
 
@@ -55,12 +55,12 @@
         this.buffer = new byte[DEFAULT_BUFFER_SIZE];
     }
 
-    Pcap getPcap() {
-        return pcap;
+    PcapOutput getPcapOut() {
+        return pcapOut;
     }
 
-    void setPcap(Pcap pcap) {
-        this.pcap = pcap;
+    void setPcapOut(PcapOutput pcapOut) {
+        this.pcapOut = pcapOut;
     }
 
     String getDevice() {
--- a/stress-tester/src/main/java/com/passus/st/emitter/pcap/UnidirectionalPcapWorker.java	Thu Apr 02 10:26:17 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/emitter/pcap/UnidirectionalPcapWorker.java	Thu Apr 02 10:40:41 2020 +0200
@@ -22,7 +22,6 @@
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.net.SocketException;
-import java.nio.ByteBuffer;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
@@ -245,11 +244,11 @@
                     LOGGER.debug("Pcap instance for device {} created.", device);
                 }
 
-                pcapInstance = new PcapInstance(pcap);
+                pcapInstance = new PcapInstance(new PcapOutput.Sender(pcap));
                 pcaps.put(device, pcapInstance);
             }
 
-            channelContext.setPcap(pcapInstance.pcap);
+            channelContext.setPcapOut(pcapInstance.pcap);
             channelContext.setDevice(device);
             pcapInstance.borrows++;
 
@@ -279,7 +278,7 @@
 
         int written = 0;
         try {
-            Pcap pcap = channelContext.getPcap();
+            PcapOutput pcap = channelContext.getPcapOut();
 
             while (!queue.isEmpty()) {
                 byte[] buf = queue.poll();
@@ -287,7 +286,7 @@
                 if (res == 0) {
                     written += buf.length;
                 } else {
-                    throw new IOException("Unable to send packet. Pcap error: " + pcap.getErr());
+                    throw new IOException("Unable to send packet. Pcap error: " + pcap.pcap().getErr());
                 }
             }
         } catch (Exception e) {
@@ -391,11 +390,11 @@
 
     private static class PcapInstance {
 
-        private final Pcap pcap;
+        private final PcapOutput pcap;
 
         private int borrows;
 
-        public PcapInstance(Pcap pcap) {
+        public PcapInstance(PcapOutput pcap) {
             this.pcap = pcap;
         }
     }