changeset 710:da8f7d56a505

ReaderMain
author Devel 2
date Wed, 29 Nov 2017 11:49:09 +0100
parents 6505c7c305e6
children de7f825fdde4
files stress-tester/src/main/java/com/passus/st/ReaderMain.java
diffstat 1 files changed, 112 insertions(+), 0 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/ReaderMain.java	Wed Nov 29 11:49:09 2017 +0100
@@ -0,0 +1,112 @@
+package com.passus.st;
+
+import com.passus.net.http.HttpMessageHelper;
+import com.passus.st.client.Event;
+import com.passus.st.client.SessionStatusEvent;
+import com.passus.st.client.http.HttpSessionPayloadEvent;
+import com.passus.st.emitter.SessionInfo;
+import com.passus.st.source.NcEventSource;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Options;
+import org.apache.commons.io.FilenameUtils;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class ReaderMain {
+
+    private final HttpMessageHelper helper = HttpMessageHelper.get();
+
+    private void printHelp(Options options) {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp("[options] <file>", "description", options, "");
+    }
+
+    private Options createOptions() {
+        final Options options = new Options();
+
+        return options;
+    }
+
+    private void printError(String msg) {
+        System.err.print(msg);
+        System.exit(1);
+    }
+
+    private void printSessionInfo(SessionInfo session) {
+        System.out.print("----- ");
+        System.out.print(session);
+    }
+
+    private void printEvent(Event event) {
+        if (event.getType() == SessionStatusEvent.TYPE) {
+            SessionStatusEvent statusEvent = (SessionStatusEvent) event;
+            printSessionInfo(statusEvent.getSessionInfo());
+            System.out.print(" status: ");
+            System.out.println(SessionStatusEvent.statusToString(statusEvent.getStatus()));
+        } else if (event.getType() == HttpSessionPayloadEvent.TYPE) {
+            HttpSessionPayloadEvent payloadEvent = (HttpSessionPayloadEvent) event;
+            printSessionInfo(payloadEvent.getSessionInfo());
+            System.out.println(" payload: ");
+
+            if (payloadEvent.getRequest() != null) {
+                System.out.println(payloadEvent.getRequest());
+                System.out.println("");
+            }
+
+            if (payloadEvent.getResponse() != null) {
+                System.out.println(payloadEvent.getResponse());
+                System.out.println("");
+            } 
+        }
+    }
+
+    private void readNcFile(String filename) throws Exception {
+        NcEventSource src = new NcEventSource(filename);
+        src.setHandler((event) -> {
+            printEvent(event);
+        });
+
+        try {
+            src.start();
+            src.join();
+        } finally {
+            src.stop();
+        }
+    }
+
+    public void start(String[] args) {
+        AppUtils.registerAll();
+        Options options = createOptions();
+
+        try {
+            CommandLine cl = new DefaultParser().parse(options, args);
+            String[] clArgs = cl.getArgs();
+            if (clArgs.length != 1) {
+                System.err.println("Source file required.");
+                printHelp(options);
+                return;
+            }
+
+            String filename = clArgs[0];
+            String ext = FilenameUtils.getExtension(filename);
+            if ("nc".equals(ext)) {
+                readNcFile(filename);
+            } else {
+                printError("Not supported file extension '" + ext + "'.");
+            }
+        } catch (Exception e) {
+            e.printStackTrace(System.err);
+        } finally {
+            AppUtils.unregisterAll();
+        }
+    }
+
+    public static void main(String[] args) {
+        new ReaderMain().start(args);
+    }
+
+}