changeset 748:92a55d662870

Main - nc file support
author Devel 2
date Fri, 08 Dec 2017 15:17:41 +0100
parents a86bd0a170ad
children 8d1fa0394251
files stress-tester/src/main/java/com/passus/st/Main.java stress-tester/src/main/java/com/passus/st/source/EventSource.java stress-tester/src/main/java/com/passus/st/source/NcEventSource.java
diffstat 3 files changed, 62 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/Main.java	Fri Dec 08 13:52:21 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/Main.java	Fri Dec 08 15:17:41 2017 +0100
@@ -25,10 +25,15 @@
 import com.passus.st.emitter.SessionMapper;
 import com.passus.st.emitter.nio.NioEmitter;
 import com.passus.st.metric.FileMetricsCollectionAppender;
+import com.passus.st.metric.MetricSource;
 import com.passus.st.metric.ScheduledMetricsCollector;
 import com.passus.st.metric.SummrizeMetricsCollectionHandler;
+import com.passus.st.reader.nc.NcHeader;
+import com.passus.st.reader.pcap.PcapFileHeader;
 import com.passus.st.reporter.ReporterClient;
 import com.passus.st.reporter.trx.SocketReporterClient;
+import com.passus.st.source.EventSource;
+import com.passus.st.source.NcEventSource;
 import com.passus.st.source.PcapSessionEventSource;
 import com.passus.st.utils.PeriodFormatter;
 import java.io.File;
@@ -52,6 +57,8 @@
 import org.apache.commons.cli.ParseException;
 import org.apache.commons.io.IOUtils;
 import static com.passus.st.utils.CliUtils.option;
+import java.io.InputStream;
+import static org.apache.commons.io.FileUtils.openInputStream;
 
 /**
  *
@@ -342,25 +349,47 @@
 
             client.start();
 
-            PcapSessionEventSource[] eventSrcs = new PcapSessionEventSource[clArgs.length];
+            EventSource[] eventSrcs = new EventSource[clArgs.length];
 
             for (int i = 0; i < clArgs.length; i++) {
-                PcapSessionEventSource eventSrc = new PcapSessionEventSource();
-                eventSrc.setPcapFile(clArgs[i]);
-                eventSrc.setAllowPartialSession(cl.hasOption("ps"));
-                eventSrc.setCollectMetrics(true);
-                eventSrcs[i] = eventSrc;
+                String srcFile = clArgs[i];
+                byte[] preambule = null;
+                InputStream in = null;
+                try {
+                    in = openInputStream(new File(srcFile));
+                    preambule = IOUtils.toByteArray(in, 20);
+                } finally {
+                    IOUtils.closeQuietly(in);
+                }
+
+                if (NcHeader.isPreambule(preambule, 0)) {
+                    NcEventSource eventSrc = new NcEventSource(srcFile);
+                    eventSrcs[i] = eventSrc;
+                } else if (PcapFileHeader.isFormatSupported(preambule)) {
+                    PcapSessionEventSource eventSrc = new PcapSessionEventSource();
+                    eventSrc.setPcapFile(clArgs[i]);
+                    eventSrc.setAllowPartialSession(cl.hasOption("ps"));
+                    eventSrc.setCollectMetrics(true);
+                    eventSrcs[i] = eventSrc;
+                } else {
+                    printError("Not supported file format '" + srcFile + "'.");
+                }
+
             }
 
             if (cl.hasOption("hp")) {
                 for (int i = 0; i < clArgs.length; i++) {
-                    PcapSessionEventSource eventSrc = eventSrcs[i];
-                    PortRangeSet portsRanges = eventSrc.getPortsRange();
-                    portsRanges.clear();
+                    EventSource eventSrc = eventSrcs[i];
+                    //TODO rozszerzyc na NcEventSource
+                    if (eventSrc instanceof PcapSessionEventSource) {
+                        PcapSessionEventSource pcapSessionEventSource = (PcapSessionEventSource) eventSrc;
+                        PortRangeSet portsRanges = pcapSessionEventSource.getPortsRange();
+                        portsRanges.clear();
 
-                    String[] ports = cl.getOptionValues("hp");
-                    for (String port : ports) {
-                        portsRanges.add(port);
+                        String[] ports = cl.getOptionValues("hp");
+                        for (String port : ports) {
+                            portsRanges.add(port);
+                        }
                     }
                 }
             }
@@ -385,7 +414,10 @@
 
             collector.addHandler(summMetricsHandler);
             for (int i = 0; i < clArgs.length; i++) {
-                collector.register(eventSrcs[i]);
+                EventSource eventSource = eventSrcs[i];
+                if (eventSource instanceof MetricSource) {
+                    collector.register((MetricSource) eventSrcs[i]);
+                }
             }
 
             collector.register(emitter);
@@ -395,13 +427,13 @@
             if (cl.hasOption("ca")) {
                 Set<String> sourcesName = new HashSet<>();
                 for (int i = 0; i < clArgs.length; i++) {
-                    PcapSessionEventSource eventSrc = eventSrcs[i];
+                    EventSource eventSrc = eventSrcs[i];
                     sourcesName.add(eventSrc.getName());
                 }
 
                 MemoryEventsCache cache = new MemoryEventsCache(client, sourcesName);
                 for (int i = 0; i < clArgs.length; i++) {
-                    PcapSessionEventSource eventSrc = eventSrcs[i];
+                    EventSource eventSrc = eventSrcs[i];
                     eventSrc.setHandler(cache);
                     eventSrc.start();
                 }
@@ -410,7 +442,7 @@
                 cache.send();
             } else {
                 for (int i = 0; i < clArgs.length; i++) {
-                    PcapSessionEventSource eventSrc = eventSrcs[i];
+                    EventSource eventSrc = eventSrcs[i];
                     eventSrc.setHandler(client);
                     eventSrc.setLoops(loops);
                     eventSrc.start();
--- a/stress-tester/src/main/java/com/passus/st/source/EventSource.java	Fri Dec 08 13:52:21 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/EventSource.java	Fri Dec 08 15:17:41 2017 +0100
@@ -17,6 +17,10 @@
 
     public void setName(String name);
 
+    public int getLoops();
+
+    public void setLoops(int loops);
+
     public EventHandler getHandler();
 
     public void setHandler(EventHandler handler);
--- a/stress-tester/src/main/java/com/passus/st/source/NcEventSource.java	Fri Dec 08 13:52:21 2017 +0100
+++ b/stress-tester/src/main/java/com/passus/st/source/NcEventSource.java	Fri Dec 08 15:17:41 2017 +0100
@@ -73,6 +73,16 @@
     }
 
     @Override
+    public int getLoops() {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
+    public void setLoops(int loops) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+    @Override
     public EventHandler getHandler() {
         return handler;
     }