changeset 1170:93826d05cd95

PcapNgReader implements DataBlockReader<PcapDataBlock>
author Devel 1
date Tue, 16 Jun 2020 14:44:22 +0200
parents af04f9f94d35
children 0fc158f0ca19
files stress-tester/src/main/java/com/passus/st/reader/pcap/PcapDataBlockReader.java stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgReader.java stress-tester/src/test/java/com/passus/st/reader/pcapng/PcapNgReaderTest.java
diffstat 3 files changed, 90 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/pcap/PcapDataBlockReader.java	Tue Jun 16 14:07:29 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/reader/pcap/PcapDataBlockReader.java	Tue Jun 16 14:44:22 2020 +0200
@@ -16,8 +16,8 @@
 public final class PcapDataBlockReader implements DataBlockReader<PcapDataBlock> {
 
     private final File pcapFile;
-    private FileInputStream fis = null;
-    private BufferedInputStream bis = null;
+    private FileInputStream fis;
+    private BufferedInputStream bis;
     private final byte[] phBuffer = new byte[16];
 
     private PcapFileHeader fileHeader;
@@ -38,7 +38,7 @@
     @Override
     public void open() throws IOException {
         try {
-            fis = new FileInputStream(this.pcapFile);
+            fis = new FileInputStream(pcapFile);
             bis = new BufferedInputStream(fis, 32 * 1024);
 
             fileSize = fis.getChannel().size();
@@ -64,24 +64,24 @@
 
     @Override
     public PcapDataBlock read() throws IOException {
-        if (position < fileSize) {
-            FileInputStreamUtils.read(bis, phBuffer);
-            position += phBuffer.length;
+        if (position >= fileSize) {
+            return null;
+        }  
 
-            PcapDataBlock pcapDataBlock = PcapDataBlock.decode(phBuffer, fileHeader);
-            int recordDataLength = pcapDataBlock.getLength();
-            if (recordDataLength > maxDataBlockLength) {
-                throw new IOException("Pcap record is too big: " + recordDataLength);
-            }
+        FileInputStreamUtils.read(bis, phBuffer);
+        position += phBuffer.length;
 
-            byte[] data = new byte[recordDataLength];
-            FileInputStreamUtils.read(bis, data);
-            position += recordDataLength;
-            pcapDataBlock.setData(data);
-            return pcapDataBlock;
+        PcapDataBlock pcapDataBlock = PcapDataBlock.decode(phBuffer, fileHeader);
+        int recordDataLength = pcapDataBlock.getLength();
+        if (recordDataLength > maxDataBlockLength) {
+            throw new IOException("Pcap record is too big: " + recordDataLength);
         }
 
-        return null;
+        byte[] data = new byte[recordDataLength];
+        FileInputStreamUtils.read(bis, data);
+        position += recordDataLength;
+        pcapDataBlock.setData(data);
+        return pcapDataBlock;
     }
 
     @Override
--- a/stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgReader.java	Tue Jun 16 14:07:29 2020 +0200
+++ b/stress-tester/src/main/java/com/passus/st/reader/pcapng/PcapNgReader.java	Tue Jun 16 14:44:22 2020 +0200
@@ -2,6 +2,7 @@
 
 import com.passus.commons.Assert;
 import com.passus.data.DataHelper;
+import com.passus.st.reader.DataBlockReader;
 import com.passus.st.reader.pcap.PcapDataBlock;
 import com.passus.st.reader.pcapng.block.Block;
 import com.passus.st.reader.pcapng.block.BlockFactory;
@@ -13,13 +14,13 @@
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.ByteOrder;
-import java.util.List;
+import org.apache.commons.io.IOUtils;
 
 /**
  *
  * @author mikolaj.podbielski
  */
-public class PcapNgReader {
+public class PcapNgReader implements DataBlockReader<PcapDataBlock> {
 
     private final File pcapFile;
     private FileInputStream fis;
@@ -35,19 +36,19 @@
     private long position = 0;
     private boolean shbProcessed = false;
 
-    public PcapNgReader(String filePath) throws IOException {
+    public PcapNgReader(String filePath) {
         this(new File(filePath));
     }
 
-    public PcapNgReader(File file) throws IOException {
+    public PcapNgReader(File file) {
         Assert.notNull(file);
         this.pcapFile = file;
-        openFile();
     }
 
-    private void openFile() throws IOException {
+    @Override
+    public void open() throws IOException {
         try {
-            fis = new FileInputStream(this.pcapFile);
+            fis = new FileInputStream(pcapFile);
             bis = new BufferedInputStream(fis, 32 * 1024);
 
             fileSize = fis.getChannel().size();
@@ -57,26 +58,45 @@
         }
     }
 
-    public void reset() throws IOException {
+    @Override
+    public void close() throws IOException {
+        if (fis != null) {
+            IOUtils.closeQuietly(fis);
+        }
+
+        if (bis != null) {
+            IOUtils.closeQuietly(bis);
+        }
+
+        position = 0;
+        fileSize = 0;
     }
 
-    private PcapDataBlock convertEnhancedBlockToPcapDataBlock(EnhancedPacketBlock block) {
-        PcapDataBlock result = new PcapDataBlock(block.getTimestampUs(), block.getLength(), block.getOriginalLength());
-        result.setData(block.getPacketData());
-        return result;
+    @Override
+    public void reset() throws IOException {
+        close();
+        open();
     }
 
+    @Override
     public PcapDataBlock read() throws IOException {
-        Block result = this.readNg();
-        while (!(result instanceof EnhancedPacketBlock)) {
-            result = this.readNg();
-        }
+        Block result;
+        do {
+            result = readNg();
+            if (result == null) {
+                return null;
+            }
+        } while ((!(result instanceof EnhancedPacketBlock)));
 
-        return result instanceof EnhancedPacketBlock ? convertEnhancedBlockToPcapDataBlock((EnhancedPacketBlock) result) : null;
+        return result instanceof EnhancedPacketBlock ? toPcapDataBlock((EnhancedPacketBlock) result) : null;
     }
 
     public Block readNg() throws IOException {
-        FileInputStreamUtils.read(fis, headerBuffer);
+        if (position >= fileSize) {
+            return null;
+        }  
+
+        FileInputStreamUtils.read(bis, headerBuffer);
         position += headerBuffer.length;
 
         int type = mbu.getInt4(headerBuffer, 0);
@@ -87,7 +107,7 @@
             shbProcessed = true;
 
             byte byteOrderMagic[] = new byte[4];
-            FileInputStreamUtils.read(fis, byteOrderMagic);
+            FileInputStreamUtils.read(bis, byteOrderMagic);
             position += 4;
 
             ByteOrder newByteOrder = SectionHeaderBlock.isLittleEndian(byteOrderMagic)
@@ -110,29 +130,25 @@
             throw new IllegalArgumentException("Pcap record is too big: " + recordDataLength);
         }
         byte[] recordData = new byte[recordDataLength];
-        FileInputStreamUtils.read(fis, recordData);
+        FileInputStreamUtils.read(bis, recordData);
         position += recordData.length;
 
-        FileInputStreamUtils.read(fis, discardBuffer);
+        FileInputStreamUtils.read(bis, discardBuffer);
         position += discardBuffer.length;
         Block block = factory.createBlock(type, recordData, byteOrder);
 
         long padded = DataHelper.pad4(position);
         if (padded > position) {
-            FileInputStreamUtils.skip(fis, padded - position);
+            FileInputStreamUtils.skip(bis, padded - position);
             position = padded;
         }
         return block;
     }
 
-    public void close() throws IOException {
-    }
-
-    @Deprecated
-    void readAll(String path, List<Block> blocks) throws IOException {
-        while (position < fileSize) {
-            blocks.add(readNg());
-        }
+    static PcapDataBlock toPcapDataBlock(EnhancedPacketBlock block) {
+        PcapDataBlock result = new PcapDataBlock(block.getTimestampUs(), block.getLength(), block.getOriginalLength());
+        result.setData(block.getPacketData());
+        return result;
     }
 
 }
--- a/stress-tester/src/test/java/com/passus/st/reader/pcapng/PcapNgReaderTest.java	Tue Jun 16 14:07:29 2020 +0200
+++ b/stress-tester/src/test/java/com/passus/st/reader/pcapng/PcapNgReaderTest.java	Tue Jun 16 14:44:22 2020 +0200
@@ -21,12 +21,29 @@
  */
 public class PcapNgReaderTest {
 
+    static List<Block> readAllNgBlocks(PcapNgReader instance) throws Exception {
+        List<Block> blocks = new ArrayList<>();
+        Block block;
+        while ((block = instance.readNg()) != null) {
+            blocks.add(block);
+        }
+        return blocks;
+    }
+
+    static List<PcapDataBlock> readAllDataBlocks(PcapNgReader instance) throws Exception {
+        List<PcapDataBlock> blocks = new ArrayList<>();
+        PcapDataBlock block;
+        while ((block = instance.read()) != null) {
+            blocks.add(block);
+        }
+        return blocks;
+    }
+
     @Test
     public void testRead() throws Exception {
-        String path = TestResourceUtils.getFile("pcap/http/wget.pcapng").getAbsolutePath();
         PcapNgReader instance = new PcapNgReader(TestResourceUtils.getFile("pcap/http/wget.pcapng"));
-        List<Block> blocks = new ArrayList<>();
-        instance.readAll(path, blocks);
+        instance.open();
+        List<Block> blocks = readAllNgBlocks(instance);
 
         assertEquals(blocks.size(), 70);
         assertTrue(blocks.get(0) instanceof SectionHeaderBlock);
@@ -61,10 +78,9 @@
 
     @Test
     public void testNtarWithPB() throws Exception {
-        String path = TestResourceUtils.getFile("pcap/http/http.littleendian.ntar").getAbsolutePath();
         PcapNgReader instance = new PcapNgReader(TestResourceUtils.getFile("pcap/http/http.littleendian.ntar"));
-        List<Block> blocks = new ArrayList<>();
-        instance.readAll(path, blocks);
+        instance.open();
+        List<Block> blocks = readAllNgBlocks(instance);
 
         assertEquals(blocks.size(), 45);
 
@@ -92,11 +108,13 @@
 
     @Test
     public void testReadSingleBlock() throws Exception {
-
         PcapNgReader instance = new PcapNgReader(TestResourceUtils.getFile("pcap/http/wget.pcapng"));
+        instance.open();
+        List<PcapDataBlock> blocks = readAllDataBlocks(instance);
 
-        PcapDataBlock result = instance.read();
-        
+        assertEquals(blocks.size(), 67);
+        PcapDataBlock result = blocks.get(0);
+
         assertEquals(result.getTimestamp(), 1_482_233_232_872L);
         assertEquals(result.getLength(), 66);
         assertEquals(result.getOriginalLength(), 66);