changeset 681:d48d77c2270b

NC file in progress
author Devel 2
date Wed, 22 Nov 2017 15:40:56 +0100
parents 0ff49c5a275c
children e420a96bed43
files 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/block/NcSegmentBlock.java stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSessionBlock.java stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSessionPayloadBlock.java stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSessionStatusBlock.java
diffstat 6 files changed, 108 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockReader.java	Wed Nov 22 15:38:55 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockReader.java	Wed Nov 22 15:40:56 2017 +0100
@@ -36,9 +36,9 @@
 
     private NcHeader header;
 
-    private DataHelper dataHelper = DataHelper.BIG_ENDIAN;
+    private final DataHelper dataHelper = DataHelper.BIG_ENDIAN;
 
-    private NcDataHelper ncDataHelper = NcDataHelper.getInstance();
+    private final NcDataHelper ncDataHelper = NcDataHelper.getInstance();
 
     private final Path path;
 
@@ -176,14 +176,18 @@
         nioBuffer.position(NcHeader.PREAMBULE.length - 1);
     }
 
+    private void checkBlockType(byte type, byte requiredType) throws IOException {
+        if (type != requiredType) {
+            throw new IOException("Invalid block type (required " + requiredType + " given " + type + ")");
+        }
+    }
+
     private NcSegmentBlock readSegmentBlock(boolean skipBlockType) throws IOException {
-        read(NcSegmentBlock.SIZE);
+        read(NcSegmentBlock.HEADER_SIZE);
 
         if (!skipBlockType) {
             byte blockType = buffer.read();
-            if (blockType != NcSegmentBlock.TYPE) {
-                throw new IOException("Invalid block type (required " + NcSegmentBlock.TYPE + " given " + blockType + ")");
-            }
+            checkBlockType(blockType, NcSegmentBlock.TYPE);
         }
 
         long totalSize = dataHelper.readLong8(buffer);
@@ -197,9 +201,7 @@
 
         if (!skipBlockType) {
             byte blockType = buffer.read();
-            if (blockType != NcSegmentBlock.TYPE) {
-                throw new IOException("Invalid block type (required " + NcSessionInfoBlock.TYPE + " given " + blockType + ").");
-            }
+            checkBlockType(blockType, NcSessionInfoBlock.TYPE);
         }
 
         int totalSize = dataHelper.readInt4(buffer);
@@ -225,24 +227,52 @@
     }
 
     private NcSessionStatusBlock readSessionStatusBlock(boolean skipBlockType) throws IOException {
-        read(NcSessionStatusBlock.SIZE);
+        read(NcSessionStatusBlock.HEADER_SIZE);
 
         if (!skipBlockType) {
             byte blockType = buffer.read();
-            if (blockType != NcSegmentBlock.TYPE) {
-                throw new IOException("Invalid block type (required " + NcSegmentBlock.TYPE + " given " + blockType + ").");
-            }
+            checkBlockType(blockType, NcSessionStatusBlock.TYPE);
+        }
+
+        long timestamp = dataHelper.readLong8(buffer);
+        int sessionId = dataHelper.readInt4(buffer);
+        SessionInfo session = sessionIdMap.get(sessionId);
+        if (session == null) {
+            throw new IOException("Invalid session id '" + sessionId + "'.");
         }
 
         byte status = buffer.read();
-        return new NcSessionStatusBlock(status);
+        return new NcSessionStatusBlock(timestamp, sessionId, session, status);
     }
 
     private NcSessionPayloadBlock readSessionPayloadBlock(boolean skipBlockType) throws IOException {
-        //read(NcSessionPayloadBlock.SIZE);
-        return null;
+        read(NcSessionPayloadBlock.MAX_HEADER_SIZE);
+
+        if (!skipBlockType) {
+            byte blockType = buffer.read();
+            checkBlockType(blockType, NcSessionPayloadBlock.TYPE);
+        }
+
+        long timestamp = dataHelper.readLong8(buffer);
+        int sessionId = dataHelper.readInt4(buffer);
+        SessionInfo session = sessionIdMap.get(sessionId);
+        if (session == null) {
+            throw new IOException("Invalid session id '" + sessionId + "'.");
+        }
+        long totalSize = dataHelper.readLong8(buffer);
+        int proto = dataHelper.readInt2(buffer);
+        long dataLength = ncDataHelper.readLongVLC(buffer);
+        long optionsLength = ncDataHelper.readLongVLC(buffer);
+
+        //TODO Odczyt danych
+        Map<String, Object> options = null;
+        if (optionsLength != 0) {
+            throw new RuntimeException("optionsLength != 0 - not implemented");
+        }
+
+        return new NcSessionPayloadBlock(timestamp, sessionId, session, totalSize, proto, dataLength, optionsLength, null, options);
     }
-    
+
     public int peekNextBlockType() throws IOException {
         read(1);
         return buffer.get();
@@ -265,6 +295,7 @@
                 readSessionStatusBlock(true);
                 break;
             case NcSessionPayloadBlock.TYPE:
+                readSessionPayloadBlock(true);
                 break;
         }
 
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockWriter.java	Wed Nov 22 15:38:55 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/NcDataBlockWriter.java	Wed Nov 22 15:40:56 2017 +0100
@@ -37,7 +37,7 @@
 
     private static final int MAX_SEGMENT_BLOCKS = Short.MAX_VALUE;
 
-    private final NcDataHelper dataHelper = NcDataHelper.getInstance();
+    private final NcDataHelper ncDataHelper = NcDataHelper.getInstance();
 
     private final Path path;
 
@@ -286,11 +286,11 @@
         int sizePosition = buffer.position();
         buffer.putInt(0);
         buffer.putInt(sessionId);
-        size += dataHelper.writeStringNullTerminated(sessionInfo.getSourceName(), buffer);
+        size += ncDataHelper.writeStringNullTerminated(sessionInfo.getSourceName(), buffer);
         buffer.put((byte) sessionInfo.getTransport());
-        size += dataHelper.writeIpAddress(sessionInfo.getSrcIp(), buffer);
+        size += ncDataHelper.writeIpAddress(sessionInfo.getSrcIp(), buffer);
         buffer.putShort((short) sessionInfo.getSrcPort());
-        size += dataHelper.writeIpAddress(sessionInfo.getDstIp(), buffer);
+        size += ncDataHelper.writeIpAddress(sessionInfo.getDstIp(), buffer);
         buffer.putShort((short) sessionInfo.getDstPort());
         closeBlock(NcSessionInfoBlock.TYPE, false);
         buffer.putInt(sizePosition, size);
@@ -314,7 +314,7 @@
 
         writeBuffer();
         closeBlock(NcSessionStatusBlock.TYPE);
-        updateSegmentInfo(NcSessionStatusBlock.SIZE);
+        updateSegmentInfo(NcSessionStatusBlock.HEADER_SIZE);
     }
 
     public void writeSessionPayloadHeader(long timestamp, SessionInfo session, byte proto, long dataLength) throws IOException {
@@ -328,7 +328,8 @@
         buffer.put(proto);
 
         int size = 14;
-        size += dataHelper.writeLongVLC(dataLength, buffer);
+        size += ncDataHelper.writeLongVLC(dataLength, buffer);
+        size += ncDataHelper.writeLongVLC(0, buffer); // OptionsLength
 
         updateSegmentInfo(size);
         writeBuffer();
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSegmentBlock.java	Wed Nov 22 15:38:55 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSegmentBlock.java	Wed Nov 22 15:40:56 2017 +0100
@@ -8,7 +8,7 @@
 
     public static final byte TYPE = 1;
 
-    public static final int SIZE = 10;
+    public static final int HEADER_SIZE = 10;
 
     private long totalSize;
 
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSessionBlock.java	Wed Nov 22 15:38:55 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSessionBlock.java	Wed Nov 22 15:40:56 2017 +0100
@@ -1,15 +1,30 @@
 package com.passus.st.reader.nc.block;
 
+import com.passus.st.emitter.SessionInfo;
+
 /**
  *
  * @author Mirosław Hawrot
  */
 public abstract class NcSessionBlock implements NcDataBlock {
 
+    public static final int HEADER_SIZE = 12;
+
     private long timestamp;
 
     private int sessionId;
 
+    private SessionInfo sessionInfo;
+
+    public NcSessionBlock() {
+    }
+
+    public NcSessionBlock(long timestamp, int sessionId, SessionInfo sessionInfo) {
+        this.timestamp = timestamp;
+        this.sessionId = sessionId;
+        this.sessionInfo = sessionInfo;
+    }
+
     public long timestamp() {
         return timestamp;
     }
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSessionPayloadBlock.java	Wed Nov 22 15:38:55 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSessionPayloadBlock.java	Wed Nov 22 15:40:56 2017 +0100
@@ -1,5 +1,6 @@
 package com.passus.st.reader.nc.block;
 
+import com.passus.st.emitter.SessionInfo;
 import java.util.Map;
 
 /**
@@ -10,25 +11,32 @@
 
     public static final byte TYPE = 3;
 
+    public static final int MAX_HEADER_SIZE = NcSessionBlock.HEADER_SIZE + 29;
+
     private long totalSize;
 
     private int proto;
 
     private long dataLength;
 
+    private long optionsLength = 0;
+
     private Object data;
 
-    private int optionsLength = 0;
-
     private Map<String, Object> options;
 
     public NcSessionPayloadBlock() {
     }
 
-    public NcSessionPayloadBlock(long totalSize, int proto, long dataLength, Object data) {
+    public NcSessionPayloadBlock(long timestamp, int sessionId, SessionInfo sessionInfo,
+            long totalSize, int proto, long dataLength, long optionsLength,
+            Object data, Map<String, Object> options) {
+        super(timestamp, sessionId, sessionInfo);
         this.totalSize = totalSize;
         this.proto = proto;
         this.dataLength = dataLength;
+        this.optionsLength = optionsLength;
+        this.options = options;
         this.data = data;
     }
 
@@ -61,6 +69,14 @@
         this.dataLength = dataLength;
     }
 
+    public long optionsLength() {
+        return optionsLength;
+    }
+
+    public void optionsLength(long optionsLength) {
+        this.optionsLength = optionsLength;
+    }
+
     public Object data() {
         return data;
     }
@@ -69,12 +85,12 @@
         this.data = data;
     }
 
-    public int optionsLength() {
-        return optionsLength;
+    public Map<String, Object> options() {
+        return options;
     }
 
-    public void optionsLength(int optionsLength) {
-        this.optionsLength = optionsLength;
+    public void options(Map<String, Object> options) {
+        this.options = options;
     }
 
 }
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSessionStatusBlock.java	Wed Nov 22 15:38:55 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/reader/nc/block/NcSessionStatusBlock.java	Wed Nov 22 15:40:56 2017 +0100
@@ -1,35 +1,38 @@
 package com.passus.st.reader.nc.block;
 
+import com.passus.st.emitter.SessionInfo;
+
 /**
  *
  * @author Mirosław Hawrot
  */
 public class NcSessionStatusBlock extends NcSessionBlock {
-
+    
     public static final byte TYPE = 4;
-
-    public static final int SIZE = 2;
-
+    
+    public static final int HEADER_SIZE = NcSessionBlock.HEADER_SIZE + 2;
+    
     private byte status;
-
+    
     public NcSessionStatusBlock() {
     }
-
-    public NcSessionStatusBlock(byte status) {
+    
+    public NcSessionStatusBlock(long timestamp, int sessionId, SessionInfo sessionInfo, byte status) {
+        super(timestamp, sessionId, sessionInfo);
         this.status = status;
     }
-
+    
     @Override
     public byte type() {
         return TYPE;
     }
-
+    
     public byte status() {
         return status;
     }
-
+    
     public void status(byte status) {
         this.status = status;
     }
-
+    
 }