changeset 662:01fe872c79d1

NC file in progress
author Devel 2
date Thu, 16 Nov 2017 11:23:20 +0100
parents e274cacdc4b8
children c6d0c5284865
files stress-tester/src/main/java/com/passus/st/reader/DataBlockReader.java stress-tester/src/main/java/com/passus/st/reader/DataBlockWriter.java stress-tester/src/main/java/com/passus/st/reader/DataReader.java stress-tester/src/main/java/com/passus/st/reader/DataWriter.java stress-tester/src/main/java/com/passus/st/reader/DataWriterImpl.java stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockReader.java stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockWriter.java stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java stress-tester/src/main/java/com/passus/st/reader/nc/NcHeader.java stress-tester/src/main/java/com/passus/st/reader/nc/block/SegmentBlock.java stress-tester/src/main/java/com/passus/st/reader/pcap/PcapDataBlockReader.java stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java stress-tester/src/test/java/com/passus/st/reader/pcap/PcapDataBlockReaderTest.java
diffstat 13 files changed, 460 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/DataBlockReader.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/DataBlockReader.java	Thu Nov 16 11:23:20 2017 +0100
@@ -10,6 +10,8 @@
  */
 public interface DataBlockReader extends Closeable {
 
+    public void open() throws IOException;
+    
     public void reset() throws IOException;
     
     public PcapDataBlock read() throws IOException;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/DataBlockWriter.java	Thu Nov 16 11:23:20 2017 +0100
@@ -0,0 +1,16 @@
+package com.passus.st.reader;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public interface DataBlockWriter extends Closeable {
+
+    public void open() throws IOException;
+    
+    public void write(DataBlock block) throws IOException;
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/DataReader.java	Thu Nov 16 11:23:20 2017 +0100
@@ -0,0 +1,45 @@
+package com.passus.st.reader;
+
+import com.passus.net.IpAddress;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public interface DataReader {
+
+    public long size();
+    
+    public long remains();
+    
+    public int position();
+
+    public void position(long pos);
+
+    public void skip(int length);
+    
+    public int read();
+    
+    public long readLength();
+
+    public byte readByte();
+
+    public byte[] readBytes(int length);
+
+    public int readInt2();
+
+    public int readInt3();
+
+    public int readInt4();
+
+    public long readLong4();
+
+    public long readLong8();
+
+    public String readString();
+
+    public String readStringNullTerminated();
+
+    public IpAddress readIpAddress();
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/DataWriter.java	Thu Nov 16 11:23:20 2017 +0100
@@ -0,0 +1,33 @@
+package com.passus.st.reader;
+
+import com.passus.net.IpAddress;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public interface DataWriter {
+
+    public void writeLength(long length);
+
+    public void writeByte(byte value);
+
+    public void writeBytes(byte[] value);
+
+    public void writeInt2(int value);
+
+    public void writeInt3(int value);
+
+    public void writeInt4(int value);
+
+    public void writeLong4(long value);
+
+    public void writeLong8(long value);
+
+    public void writeString(String value);
+
+    public void writeStringNullTerminated(String value);
+
+    public void writeIpAddress(IpAddress ipAddress);
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/DataWriterImpl.java	Thu Nov 16 11:23:20 2017 +0100
@@ -0,0 +1,61 @@
+package com.passus.st.reader;
+
+import com.passus.net.IpAddress;
+
+public class DataWriterImpl implements DataWriter {
+
+    @Override
+    public void writeLength(long length) {
+        
+    }
+
+    public void writeBytes(byte[] value) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void writeByte(byte value) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void writeInt2(int value) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void writeInt3(int value) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void writeInt4(int value) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void writeLong4(long value) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void writeLong8(long value) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void writeString(String value) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void writeStringNullTerminated(String value) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void writeIpAddress(IpAddress ipAddress) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockReader.java	Thu Nov 16 11:23:20 2017 +0100
@@ -0,0 +1,128 @@
+package com.passus.st.reader.nc;
+
+import com.passus.commons.Assert;
+import com.passus.data.ByteBuff;
+import com.passus.data.DataHelper;
+import com.passus.data.DataProcessor;
+import com.passus.data.DataUtils;
+import com.passus.data.compression.DeflaterProcessor;
+import com.passus.data.compression.GZIPDecompressionProcessor;
+import com.passus.st.reader.DataBlockReader;
+import com.passus.st.reader.pcap.PcapDataBlock;
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.Files;
+import java.nio.file.StandardOpenOption;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class NcDataBlockReader implements DataBlockReader {
+
+    public static final byte VERSION_MAJOR = 1;
+    public static final byte VERSION_MINOR = 0;
+
+    public static final int COMPRESSION_NONE = 0;
+    public static final int COMPRESSION_GZIP = 1;
+    public static final int COMPRESSION_DEFLATE = 2;
+
+    private static final int DEFAULT_BUFFER_SIZE = 64 * 1024;
+
+    private NcHeader header;
+
+    private DataHelper helper = DataHelper.BIG_ENDIAN;
+
+    private DataProcessor decompressProc;
+
+    private final Path path;
+
+    private FileChannel ch;
+
+    private int bufferSize = DEFAULT_BUFFER_SIZE;
+
+    private ByteBuffer byteBuffer;
+
+    public NcDataBlockReader(String fileName) {
+        this.path = Paths.get(fileName);
+    }
+
+    public NcDataBlockReader(File file) {
+        this.path = file.toPath();
+    }
+
+    public NcDataBlockReader(Path path) {
+        Assert.notNull(path, "path");
+        this.path = path;
+    }
+
+    public int getBufferSize() {
+        return bufferSize;
+    }
+
+    public void setBufferSize(int bufferSize) {
+        Assert.greaterThanZero(bufferSize, "bufferSize");
+        this.bufferSize = bufferSize;
+    }
+
+    @Override
+    public void open() throws IOException {
+        if (!Files.exists(path)) {
+            throw new IOException("File '" + path + "' does not exist.");
+        } else if (!Files.isReadable(path)) {
+            throw new IOException("File '" + path + "' is not readable.");
+        }
+
+        ch = FileChannel.open(path, StandardOpenOption.READ);
+        byteBuffer = ByteBuffer.allocate(bufferSize);
+    }
+
+    @Override
+    public void close() throws IOException {
+        if (ch != null) {
+            ch.close();
+            ch = null;
+            byteBuffer = null;
+        }
+    }
+
+    @Override
+    public void reset() throws IOException {
+        header = null;
+        decompressProc = null;
+    }
+
+    private void readHeader(ByteBuff buffer) throws IOException {
+        if (!NcHeader.isPreambule(buffer.buffer(), 0)) {
+            throw new IOException("Invalid preambule.");
+        }
+        buffer.skipBytes(NcHeader.PREAMBULE.length);
+
+        byte verMajor = DataUtils.readByte(buffer);
+        byte varMinor = DataUtils.readByte(buffer);
+        if (varMinor != VERSION_MAJOR || varMinor != VERSION_MINOR) {
+            throw new IOException("Not supported version.");
+        }
+
+        byte compressionMethod = DataUtils.readByte(buffer);
+        if (compressionMethod == COMPRESSION_GZIP) {
+            decompressProc = new GZIPDecompressionProcessor();
+        } else if (compressionMethod == COMPRESSION_DEFLATE) {
+            decompressProc = new DeflaterProcessor();
+        } else if (compressionMethod != COMPRESSION_NONE) {
+            throw new IOException("Not supported compression method code '" + compressionMethod + "'.");
+        }
+
+        header = new NcHeader(verMajor, varMinor, compressionMethod);
+    }
+
+    @Override
+    public PcapDataBlock read() throws IOException {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockWriter.java	Thu Nov 16 11:23:20 2017 +0100
@@ -0,0 +1,28 @@
+package com.passus.st.reader.nc;
+
+import com.passus.st.reader.DataBlock;
+import com.passus.st.reader.DataBlockWriter;
+import java.io.IOException;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class NcDataBlockWriter implements DataBlockWriter {
+
+    @Override
+    public void open() throws IOException {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void write(DataBlock block) throws IOException {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void close() throws IOException {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+    
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataUtils.java	Thu Nov 16 11:23:20 2017 +0100
@@ -0,0 +1,73 @@
+package com.passus.st.reader.nc;
+
+import com.passus.commons.AsciiUtils;
+import com.passus.data.ByteBuff;
+import com.passus.data.ByteBuffUtils;
+import com.passus.data.ByteString;
+import com.passus.data.DataUtils;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class NcDataUtils {
+
+    public static final int LONG_LENGTH = 0x80;
+
+    public static final int SHORT_LENGTH_MAX = 0x7F;
+
+    private NcDataUtils() {
+    }
+
+    public static ByteString readByteString(ByteBuff buffer) {
+        int len = readLength(buffer);
+        return readByteString(buffer, len);
+    }
+
+    public static ByteString readByteString(ByteBuff buffer, int len) {
+        int startIndex = buffer.startIndex();
+        return buffer.toByteString(startIndex, startIndex + len);
+    }
+
+    public static ByteString readByteStringNullTerminated(ByteBuff buffer) {
+        return readByteStringTerminated(buffer, AsciiUtils.NUL);
+    }
+
+    public static ByteString readByteStringTerminated(ByteBuff buffer, byte delim) {
+        int startIndex = buffer.startIndex();
+        int endIndex = ByteBuffUtils.localize(buffer, startIndex, delim);
+        if (endIndex == -1) {
+            return null;
+        }
+
+        return buffer.toByteString(startIndex, endIndex);
+    }
+
+    public static int readLength(ByteBuff buffer) {
+        int readed = 1;
+        int length = DataUtils.readInt(buffer, 1);
+
+        if ((length & LONG_LENGTH) != 0) {
+            length &= ~LONG_LENGTH;
+            if (length == 0 || length > 4) {
+                throw new IllegalArgumentException("Invalid length byte value.");
+            }
+
+            readed += length;
+            length = DataUtils.readInt(buffer, length);
+        }
+
+        buffer.skipBytes(readed);
+        return length;
+    }
+
+    public static int writeLength(ByteBuff buffer, long length) {
+        if (length <= SHORT_LENGTH_MAX) {
+            buffer.append((byte) length);
+            return 1;
+        } else {
+            throw new RuntimeException("Not supported yet.");
+            //DataUtils.writeLongToArray(length, arr, LONG_LENGTH);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcHeader.java	Thu Nov 16 11:23:20 2017 +0100
@@ -0,0 +1,53 @@
+package com.passus.st.reader.nc;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class NcHeader {
+
+    public static final byte[] PREAMBULE = {
+        (byte) 'S', (byte) 'T',
+        (byte) 'N', (byte) 'e', (byte) 't',
+        (byte) 'C', (byte) 'a', (byte) 'p'
+    };
+
+    private final int versionMajor;
+
+    private final int versionMinor;
+
+    private final int compressionMethod;
+
+    public NcHeader(int versionMajor, int versionMinor, int compressionMethod) {
+        this.versionMajor = versionMajor;
+        this.versionMinor = versionMinor;
+        this.compressionMethod = compressionMethod;
+    }
+
+    public int getVersionMajor() {
+        return versionMajor;
+    }
+
+    public int getVersionMinor() {
+        return versionMinor;
+    }
+
+    public int getCompressionMethod() {
+        return compressionMethod;
+    }
+
+    public static boolean isPreambule(byte[] data, int offset) {
+        if (data.length - offset < PREAMBULE.length) {
+            return false;
+        }
+
+        for (int i = offset, j = 0; j < PREAMBULE.length; i++, j++) {
+            if (PREAMBULE[j] != data[i]) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/block/SegmentBlock.java	Thu Nov 16 11:23:20 2017 +0100
@@ -0,0 +1,11 @@
+package com.passus.st.reader.nc.block;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class SegmentBlock {
+
+    private long totalSize;
+
+}
--- a/stress-tester/src/main/java/com/passus/st/reader/pcap/PcapDataBlockReader.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/pcap/PcapDataBlockReader.java	Thu Nov 16 11:23:20 2017 +0100
@@ -31,12 +31,12 @@
     }
 
     public PcapDataBlockReader(File file) throws IOException {
-        Assert.notNull(file);
+        Assert.notNull(file, "file");
         this.pcapFile = file;
-        openFile();
     }
 
-    private void openFile() throws IOException {
+    @Override
+    public void open() throws IOException {
         try {
             fis = new FileInputStream(this.pcapFile);
             bis = new BufferedInputStream(fis, 32 * 1024);
@@ -59,7 +59,7 @@
     @Override
     public void reset() throws IOException {
         close();
-        openFile();
+        open();
     }
 
     @Override
--- a/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/PcapSessionEventSource.java	Thu Nov 16 11:23:20 2017 +0100
@@ -264,6 +264,7 @@
         private PcapThread(String pcapFile, int loop) throws IOException {
             super(PcapSessionEventSource.class.getSimpleName() + ".PcapThread");
             reader = new PcapDataBlockReader(pcapFile);
+            reader.open();
             this.loop = loop;
         }
 
--- a/stress-tester/src/test/java/com/passus/st/reader/pcap/PcapDataBlockReaderTest.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/stress-tester/src/test/java/com/passus/st/reader/pcap/PcapDataBlockReaderTest.java	Thu Nov 16 11:23:20 2017 +0100
@@ -20,6 +20,8 @@
         File file = TestResourceUtils.getFile(phpFileName);
         List<PcapDataBlock> blocks = new ArrayList<>();
         try (PcapDataBlockReader reader = new PcapDataBlockReader(file)) {
+            reader.open();
+            
             PcapDataBlock dataBlock;
             while ((dataBlock = reader.read()) != null) {
                 blocks.add(dataBlock);
@@ -46,6 +48,8 @@
         File file = TestResourceUtils.getFile(phpFileName);
         List<PcapDataBlock> blocks = new ArrayList<>();
         try (PcapDataBlockReader reader = new PcapDataBlockReader(file)) {
+            reader.open();
+            
             PcapDataBlock dataBlock;
             while ((dataBlock = reader.read()) != null) {
                 blocks.add(dataBlock);
@@ -71,6 +75,7 @@
         File file = TestResourceUtils.getFile(phpFileName);
         List<PcapDataBlock> blocks = new ArrayList<>();
         try (PcapDataBlockReader reader = new PcapDataBlockReader(file)) {
+            reader.open();
             PcapDataBlock dataBlock;
             while ((dataBlock = reader.read()) != null) {
                 blocks.add(dataBlock);