changeset 661:e274cacdc4b8

refactoring
author Devel 1
date Mon, 13 Nov 2017 11:51:20 +0100
parents 92e6eadc7f0d
children 01fe872c79d1
files client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java client-server/src/main/java/com/passus/st/clientserver/ChannelClient.java client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java client-server/src/main/java/com/passus/st/clientserver/SocketClient.java client-server/src/main/java/com/passus/st/utils/CliUtils.java
diffstat 5 files changed, 28 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java	Mon Nov 13 11:51:20 2017 +0100
@@ -17,17 +17,13 @@
  */
 public abstract class AbstractClient {
 
-    private final int localAddrNum;
-    private final InetAddress[] localAddresses;
-    static final String COMMON_OPTIONS = " [-p port=400] [-t threads=4] [--bind] [--la 192.168.60.1] [--lan 10] [--force-local-port] [-trx msgLen]";
     static final float MEGA = 1024f * 1024f;
 
     static void addCommonOptions(Options options) {
         options.addOption(CliUtils.createOption("op", "operation", "[conn-num] or conn-rate", "name"));
         options.addOption(CliUtils.createOption("p", "port", "remote port", "port"));
         options.addOption(CliUtils.createOption("t", "threads", "number of worker threads", "threads"));
-        options.addOption(CliUtils.createOption("b", "bind", "bind before connect", null));
-        options.addOption(CliUtils.createOption("la", "local-address", "first local address", "address"));
+        options.addOption(CliUtils.createOption("la", "local-address", "first local address to bind to", "address"));
         options.addOption(CliUtils.createOption("lan", "local-address-number", "local address number", "addresses"));
         options.addOption(CliUtils.createOption("flp", "force-local-port", "force local port", null));
         options.addOption(CliUtils.createOption("trx", "transmit-receive", "transmit and receive message length", "bytes"));
@@ -47,6 +43,9 @@
     }
 
     static InetAddress[] createInetAddresses(String first, int n) {
+        if (first == null) {
+            return null;
+        }
         try {
             String[] strings = createAddresses(first, n);
             InetAddress[] addresses = new InetAddress[n];
@@ -90,11 +89,11 @@
     }
 
     InetAddress getAddressFor(int iii) {
-        return localAddresses[iii % localAddrNum];
+        return localAddresses[iii % localAddresses.length];
     }
 
     int getPortFor(int iii) {
-        return 1200 + (iii / localAddrNum);
+        return 1200 + (iii / localAddresses.length);
     }
 
     void sleepIfNeeded(int i) {
@@ -103,13 +102,13 @@
         }
     }
 
+    final InetAddress[] localAddresses;
     final Operation operation;
     final byte[] msg;
     final Worker[] workers;
     final SocketAddress remote;
     final String host;
     final int port;
-    final boolean bind;
     final boolean forceLocalPort;
     final int connBurstSize;
     final int connBurstTime;
@@ -119,11 +118,9 @@
         remote = new InetSocketAddress(cs.host, cs.port);
         host = cs.host;
         port = cs.port;
-        bind = cs.bind;
         forceLocalPort = cs.flp;
         msg = cs.trx >= 12 ? createMessage(cs.trx) : null;
-        localAddrNum = cs.localAddrNum;
-        localAddresses = createInetAddresses(cs.localAddr, localAddrNum);
+        localAddresses = createInetAddresses(cs.localAddr, cs.localAddrNum);
         connBurstSize = cs.connBurstSize;
         connBurstTime = cs.connBurstTime;
 
@@ -136,7 +133,7 @@
         }
     }
 
-    abstract void connect(int bgn, int end, int rank);
+    abstract int connect(int bgn, int end, int rank);
 
     abstract void disconnect(int bgn, int end) throws IOException;
 
@@ -158,8 +155,8 @@
     void runConnSendRecvDisc(final int bgn, final int end, final int rank) throws IOException {
         String threadName = "[" + Thread.currentThread().getName() + "] ";
         long millis = System.currentTimeMillis();
-        connect(bgn, end, rank);
-        System.out.println(threadName + "connected in " + (System.currentTimeMillis() - millis));
+        int errors = connect(bgn, end, rank);
+        System.out.println(threadName + "connected in " + (System.currentTimeMillis() - millis) + "ms, erorrs: " + errors);
 
         long time;
         float Mbps;
@@ -212,7 +209,7 @@
         }
 
         float avgSpeed = (end - bgn) / (total / 1000.f);
-        System.out.println(threadName + " " + (end - bgn) + " connections in " + total + "ms (" + avgSpeed + " per second)");
+        System.out.println(threadName + " TOTAL " + (end - bgn) + " connections in " + total + "ms (" + avgSpeed + " per second)");
     }
 
     void runWorkers() {
--- a/client-server/src/main/java/com/passus/st/clientserver/ChannelClient.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/ChannelClient.java	Mon Nov 13 11:51:20 2017 +0100
@@ -21,7 +21,7 @@
     private final boolean asyncConnect;
 
     public static void main(String[] args) throws IOException, InterruptedException {
-        final String syntax = "client <num connections> <remote adderss>" + COMMON_OPTIONS + " [--async]";
+        final String syntax = "client <num connections> <remote host> options";
         final Options options = new Options();
         addCommonOptions(options);
         options.addOption(CliUtils.createOption("ac", "async-connect", "asynchronous connect", null));
@@ -43,7 +43,7 @@
                 client.runWorkers();
                 client.joinWorkers();
             }
-            client.printStats();
+//            client.printStats();
         } catch (org.apache.commons.cli.ParseException ex) {
             CliUtils.printHelp(options, syntax);
         }
@@ -61,7 +61,7 @@
     }
 
     @Override
-    void connect(int bgn, int end, int rank) {
+    int connect(int bgn, int end, int rank) {
         int errors = 0;
         int stride = workers.length > 0 ? workers.length : 1;
         int iii = rank;
@@ -70,7 +70,7 @@
             try {
                 SocketChannel ch = SocketChannel.open();
                 connections[i].ch = ch;
-                if (bind) {
+                if (localAddresses != null) {
                     InetAddress localAddress = getAddressFor(iii);
                     int localPort = forceLocalPort ? getPortFor(iii) : 0;
                     SocketAddress local = new InetSocketAddress(localAddress, localPort);
@@ -98,7 +98,7 @@
                 ++errors;
             }
         }
-        System.out.println("erorrs: " + errors);
+        return errors;
     }
 
     @Override
--- a/client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java	Mon Nov 13 11:51:20 2017 +0100
@@ -20,7 +20,6 @@
     final int trx;
     final String localAddr;
     final int localAddrNum;
-    final boolean bind;
     final boolean flp;
     final int connBurstSize;
     final int connBurstTime;
@@ -30,11 +29,10 @@
         host = clArgs[1];
         port = Integer.parseInt(cl.getOptionValue('p', "400"));
         connections = Integer.parseInt(clArgs[0]);
-        workers = Integer.parseInt(cl.getOptionValue('t', "4"));
+        workers = Integer.parseInt(cl.getOptionValue('t', "0"));
         trx = Integer.parseInt(cl.getOptionValue("trx", "0"));
-        localAddr = cl.getOptionValue("la", "192.168.60.1");
-        localAddrNum = Integer.parseInt(cl.getOptionValue("lan", "10"));
-        bind = cl.hasOption('b');
+        localAddr = cl.getOptionValue("la");
+        localAddrNum = Integer.parseInt(cl.getOptionValue("lan", "1"));
         flp = cl.hasOption("flp");
         connBurstSize = Integer.parseInt(cl.getOptionValue("cbs", "100"));
         connBurstTime = Integer.parseInt(cl.getOptionValue("cbt", "0"));
@@ -62,7 +60,7 @@
     @Override
     public String toString() {
         return "conn=" + connections + " workers=" + workers + " host= " + host + " port=" + port
-                + " bind=" + bind + " forceLocalPort=" + flp + " msgLen=" + trx
+                + " forceLocalPort=" + flp + " msgLen=" + trx
                 + " localAddrNum=" + localAddrNum + " localAddr=" + localAddr
                 + " connBurstSize=" + connBurstSize + " connBurstTime=" + connBurstTime;
     }
--- a/client-server/src/main/java/com/passus/st/clientserver/SocketClient.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/SocketClient.java	Mon Nov 13 11:51:20 2017 +0100
@@ -22,7 +22,7 @@
     private final Socket[] sockets;
 
     public static void main(String[] args) throws IOException, InterruptedException {
-        final String syntax = "client <num connections> <remote adderss>" + COMMON_OPTIONS;
+        final String syntax = "client <num connections> <remote host> options";
         final Options options = new Options();
         addCommonOptions(options);
 //        options.addOption(CliUtils.createOption("ac", "async-connect", "asynchronous connect", null));
@@ -58,9 +58,9 @@
     // TODO: cache local if not FORCE_LOCAL_PORT
     // TODO: cache remote as REMOTES[] if OVERRIDE_DST_IP
     @Override
-    void connect(int bgn, int end, int rank) {
+    int connect(int bgn, int end, int rank) {
         int errors = 0;
-        if (bind) {
+        if (localAddresses != null) {
             int stride = workers.length > 0 ? workers.length : 1;
             int iii = rank;
             for (int i = bgn; i < end;) {
@@ -95,7 +95,7 @@
                 }
             }
         }
-        System.out.println("erorrs: " + errors);
+        return errors;
     }
 
     @Override
--- a/client-server/src/main/java/com/passus/st/utils/CliUtils.java	Mon Nov 13 11:51:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/utils/CliUtils.java	Mon Nov 13 11:51:20 2017 +0100
@@ -1,5 +1,6 @@
 package com.passus.st.utils;
 
+import java.util.Comparator;
 import org.apache.commons.cli.HelpFormatter;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
@@ -21,6 +22,8 @@
 
     public static void printHelp(Options options, String syntax) {
         HelpFormatter formatter = new HelpFormatter();
+        Comparator<Option> comparator = (o1, o2) -> 0;
+        formatter.setOptionComparator(comparator);
         formatter.printHelp(syntax, "---", options, "===");
     }
 }