changeset 566:11c45aa98eb1

reporter - ServerMain
author Devel 1
date Fri, 22 Sep 2017 14:15:27 +0200
parents 65c26bb5ca38
children 9b42aa39f460
files stress-tester-reporter/src/main/java/com/passus/st/reporter/server/ServerMain.java stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/NettyReporterServer.java stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/Server.java stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/ServerMain.java stress-tester-reporter/src/test/java/com/passus/st/reporter/server/TestCustom.java
diffstat 5 files changed, 128 insertions(+), 101 deletions(-) [+]
line wrap: on
line diff
--- a/stress-tester-reporter/src/main/java/com/passus/st/reporter/server/ServerMain.java	Thu Sep 21 16:49:06 2017 +0200
+++ b/stress-tester-reporter/src/main/java/com/passus/st/reporter/server/ServerMain.java	Fri Sep 22 14:15:27 2017 +0200
@@ -24,7 +24,7 @@
 
     static final int PORT = 11111;
 
-    static SnmpLogger create(CommandLine cl) throws ParseException {
+    public static SnmpLogger create(CommandLine cl) throws ParseException {
         if (cl.hasOption("s")) {
             // SNMP listener is enabled
             String snmpAddr = cl.getOptionValue("s");
@@ -63,6 +63,10 @@
         SnmpLogger snmp;
         try {
             CommandLine cl = new DefaultParser().parse(options, args);
+            if (cl.getArgs().length != 0) {
+                CliUtils.printHelp(options);
+                return;
+            }
             snmp = create(cl);
             merge = cl.hasOption("m");
         } catch (ParseException ex) {
@@ -74,9 +78,11 @@
         InetSocketAddress serverAddress = new InetSocketAddress(PORT);
         ReporterImpl reporter = new ReporterImpl(merge);
         reporter.setVerbose(false);
+
         Responder responder = new SpecificResponder(Reporter.class, reporter);
         Server server = new NettyServer(responder, serverAddress);
         System.out.println("Server started.");
+
         Runnable shutdown = () -> {
             server.close();
             reporter.close();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/NettyReporterServer.java	Fri Sep 22 14:15:27 2017 +0200
@@ -0,0 +1,82 @@
+package com.passus.st.reporter.trx;
+
+import com.passus.st.reporter.protocol.Reporter;
+import io.netty.bootstrap.ServerBootstrap;
+import io.netty.channel.Channel;
+import io.netty.channel.ChannelInitializer;
+import io.netty.channel.ChannelOption;
+import io.netty.channel.ChannelPipeline;
+import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.EpollEventLoopGroup;
+import io.netty.channel.epoll.EpollServerSocketChannel;
+import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.SocketChannel;
+import io.netty.channel.socket.nio.NioServerSocketChannel;
+import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
+import io.netty.handler.codec.bytes.ByteArrayDecoder;
+import java.net.SocketAddress;
+
+/**
+ *
+ * @author mikolaj.podbielski
+ */
+public class NettyReporterServer {
+    public static final boolean EPOLL = false;
+
+    private final SocketAddress serverAddress;
+    private final int maxFrameSize = 8192;
+
+    private final EventLoopGroup bossGroup;
+    private final EventLoopGroup workerGroup;
+    private final Class socketChannelClass;
+
+    private final Reporter reporter;
+
+    public NettyReporterServer(SocketAddress serverAddress, Reporter reporter) {
+        this.serverAddress = serverAddress;
+        this.reporter = reporter;
+
+        if (EPOLL) {
+            bossGroup = new EpollEventLoopGroup(1);
+            workerGroup = new EpollEventLoopGroup();
+            socketChannelClass = EpollServerSocketChannel.class;
+        } else {
+            bossGroup = new NioEventLoopGroup(1);
+            workerGroup = new NioEventLoopGroup();
+            socketChannelClass = NioServerSocketChannel.class;
+        }
+    }
+
+    public void start() {
+        try {
+            ServerBootstrap boot = new ServerBootstrap();
+            boot.option(ChannelOption.SO_BACKLOG, 100_000);
+            boot.option(ChannelOption.SO_REUSEADDR, true);
+            boot.group(bossGroup, workerGroup)
+                    .channel(socketChannelClass)
+                    .childHandler(new ServerInitializer());
+
+            Channel ch = boot.bind(serverAddress).sync().channel();
+            System.err.println("Server started " + serverAddress + " epoll: " + EPOLL);
+        } catch (Exception ex) {
+            ex.printStackTrace();
+        }
+    }
+
+    public void stop() {
+        bossGroup.shutdownGracefully();
+        workerGroup.shutdownGracefully();
+        System.err.println("Server stopped.");
+    }
+
+    public class ServerInitializer extends ChannelInitializer<SocketChannel> {
+
+        @Override
+        public void initChannel(SocketChannel ch) throws Exception {
+            ChannelPipeline pipeline = ch.pipeline();
+            pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(maxFrameSize, 0, 4, 0, 4));
+            pipeline.addLast("bytesDecoder", new ByteArrayDecoder());
+            pipeline.addLast(new ReporterDispatcher(reporter));
+        }
+    }
+}
--- a/stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/Server.java	Thu Sep 21 16:49:06 2017 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,82 +0,0 @@
-package com.passus.st.reporter.trx;
-
-import com.passus.st.reporter.protocol.Reporter;
-import io.netty.bootstrap.ServerBootstrap;
-import io.netty.channel.Channel;
-import io.netty.channel.ChannelInitializer;
-import io.netty.channel.ChannelOption;
-import io.netty.channel.ChannelPipeline;
-import io.netty.channel.EventLoopGroup;
-import io.netty.channel.epoll.EpollEventLoopGroup;
-import io.netty.channel.epoll.EpollServerSocketChannel;
-import io.netty.channel.nio.NioEventLoopGroup;
-import io.netty.channel.socket.SocketChannel;
-import io.netty.channel.socket.nio.NioServerSocketChannel;
-import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
-import io.netty.handler.codec.bytes.ByteArrayDecoder;
-import java.net.SocketAddress;
-
-/**
- *
- * @author mikolaj.podbielski
- */
-public class Server {
-    public static final boolean EPOLL = false;
-
-    private final SocketAddress serverAddress;
-    private final int maxFrameSize = 8192;
-
-    private final EventLoopGroup bossGroup;
-    private final EventLoopGroup workerGroup;
-    private final Class socketChannelClass;
-
-    private final Reporter reporter;
-
-    public Server(SocketAddress serverAddress, Reporter reporter) {
-        this.serverAddress = serverAddress;
-        this.reporter = reporter;
-
-        if (EPOLL) {
-            bossGroup = new EpollEventLoopGroup(1);
-            workerGroup = new EpollEventLoopGroup();
-            socketChannelClass = EpollServerSocketChannel.class;
-        } else {
-            bossGroup = new NioEventLoopGroup(1);
-            workerGroup = new NioEventLoopGroup();
-            socketChannelClass = NioServerSocketChannel.class;
-        }
-    }
-
-    public void start() {
-        try {
-            ServerBootstrap boot = new ServerBootstrap();
-            boot.option(ChannelOption.SO_BACKLOG, 100_000);
-            boot.option(ChannelOption.SO_REUSEADDR, true);
-            boot.group(bossGroup, workerGroup)
-                    .channel(socketChannelClass)
-                    .childHandler(new ServerInitializer());
-
-            Channel ch = boot.bind(serverAddress).sync().channel();
-            System.err.println("Server started " + serverAddress + " epoll: " + EPOLL);
-        } catch (Exception ex) {
-            ex.printStackTrace();
-        }
-    }
-
-    public void stop() {
-        bossGroup.shutdownGracefully();
-        workerGroup.shutdownGracefully();
-        System.err.println("Server stopped.");
-    }
-
-    public class ServerInitializer extends ChannelInitializer<SocketChannel> {
-
-        @Override
-        public void initChannel(SocketChannel ch) throws Exception {
-            ChannelPipeline pipeline = ch.pipeline();
-            pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(maxFrameSize, 0, 4, 0, 4));
-            pipeline.addLast("bytesDecoder", new ByteArrayDecoder());
-            pipeline.addLast(new ReporterDispatcher(reporter));
-        }
-    }
-}
--- a/stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/ServerMain.java	Thu Sep 21 16:49:06 2017 +0200
+++ b/stress-tester-reporter/src/main/java/com/passus/st/reporter/trx/ServerMain.java	Fri Sep 22 14:15:27 2017 +0200
@@ -1,6 +1,7 @@
 package com.passus.st.reporter.trx;
 
 import com.passus.st.reporter.ReporterImpl;
+import com.passus.st.reporter.SnmpLogger;
 import com.passus.st.utils.CliUtils;
 import static com.passus.st.utils.CliUtils.option;
 import java.io.IOException;
@@ -8,6 +9,7 @@
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.DefaultParser;
 import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
 
 /**
  *
@@ -15,37 +17,56 @@
  */
 public class ServerMain {
 
-    public static final int PORT = 11111;
+    static final int PORT = 11111;
 
     public static void main(String[] args) throws IOException {
         final Options options = new Options();
 
+        options.addOption(option("s", "snmp").desc("Collect SNMP metrics.")
+                .hasArg().argName("snmpAddr")
+                .build()
+        );
+        options.addOption(option("co", "snmpCommunity").desc("SNMP Community.")
+                .hasArg().argName("snmpCommunity")
+                .build()
+        );
+        options.addOption(option("p", "snmpPeriod").desc("Period in seconds of consequent SNMP readings.")
+                .hasArg().argName("snmpPeriod")
+                .build()
+        );
+
         options.addOption(option("m", "merged").desc("Write file with merged request and response data")
                 .hasArg(false)
                 .build()
         );
 
+        boolean merge;
+        SnmpLogger snmp;
         try {
             CommandLine cl = new DefaultParser().parse(options, args);
             if (cl.getArgs().length != 0) {
                 CliUtils.printHelp(options);
                 return;
             }
-
-            ReporterImpl reporter = new ReporterImpl(cl.hasOption("m"));
-            reporter.setVerbose(false);
-
-            InetSocketAddress serverAddress = new InetSocketAddress(PORT);
-            Server server = new Server(serverAddress, reporter);
-            server.start();
+            snmp = com.passus.st.reporter.server.ServerMain.create(cl);
+            merge = cl.hasOption("m");
+        } catch (ParseException ex) {
+            System.out.println(ex.getMessage());
+            CliUtils.printHelp(options);
+            return;
+        }
 
-            Runnable shutdown = () -> {
-                System.out.println("shutdown");
-                server.stop();
-            };
-            Runtime.getRuntime().addShutdownHook(new Thread(shutdown));
-        } catch (org.apache.commons.cli.ParseException ex) {
-            CliUtils.printHelp(options);
-        }
+        InetSocketAddress serverAddress = new InetSocketAddress(PORT);
+        ReporterImpl reporter = new ReporterImpl(merge);
+        reporter.setVerbose(false);
+
+        NettyReporterServer server = new NettyReporterServer(serverAddress, reporter);
+        server.start();
+
+        Runnable shutdown = () -> {
+            System.out.println("shutdown");
+            server.stop();
+        };
+        Runtime.getRuntime().addShutdownHook(new Thread(shutdown));
     }
 }
--- a/stress-tester-reporter/src/test/java/com/passus/st/reporter/server/TestCustom.java	Thu Sep 21 16:49:06 2017 +0200
+++ b/stress-tester-reporter/src/test/java/com/passus/st/reporter/server/TestCustom.java	Fri Sep 22 14:15:27 2017 +0200
@@ -4,7 +4,7 @@
 import com.passus.st.reporter.protocol.MetricRecord;
 import com.passus.st.reporter.trx.ReporterDispatcher;
 import com.passus.st.reporter.trx.ReporterEncoder;
-import com.passus.st.reporter.trx.Server;
+import com.passus.st.reporter.trx.NettyReporterServer;
 import com.passus.st.reporter.trx.SocketReporterClient;
 import java.io.DataOutputStream;
 import java.net.InetSocketAddress;
@@ -23,7 +23,7 @@
     public static void main(String[] args) throws Exception {
         ReporterImpl reporter = new ReporterImpl();
         reporter.setVerbose(true);
-        Server server = new Server(new InetSocketAddress(ServerMain.PORT), reporter);
+        NettyReporterServer server = new NettyReporterServer(new InetSocketAddress(ServerMain.PORT), reporter);
         server.start();
         System.out.println("server started");