changeset 659:827609bd0999

client-server - connBurstSize, connBurtsTime
author Devel 1
date Mon, 13 Nov 2017 11:51:20 +0100
parents c310cbb9c8ff
children 92e6eadc7f0d
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
diffstat 4 files changed, 50 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java	Mon Nov 13 09:32:07 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java	Mon Nov 13 11:51:20 2017 +0100
@@ -29,6 +29,8 @@
         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"));
+        options.addOption(CliUtils.createOption("cbs", "connBurstSize", "connection burst size", "connections"));
+        options.addOption(CliUtils.createOption("cbt", "connBurstTime", "connection burst time", "milliseconds"));
     }
 
     static byte[] createMessage(int length) {
@@ -69,6 +71,20 @@
         return result;
     }
 
+    static void printException(int i, SocketAddress local, SocketAddress remote, Exception ex) {
+        System.out.println("index=" + i);
+        System.out.println("local=" + local);
+        System.out.println("remote=" + remote);
+        System.out.println(ex.getMessage());
+    }
+
+    static void sleepSilently(long millis) {
+        try {
+            Thread.sleep(millis);
+        } catch (InterruptedException ignore) {
+        }
+    }
+
     InetAddress getAddressFor(int iii) {
         return localAddresses[iii % localAddrNum];
     }
@@ -77,11 +93,10 @@
         return 1200 + (iii / localAddrNum);
     }
 
-    static void printException(int i, SocketAddress local, SocketAddress remote, Exception ex) {
-        System.out.println("index=" + i);
-        System.out.println("local=" + local);
-        System.out.println("remote=" + remote);
-        System.out.println(ex.getMessage());
+    void sleepIfNeeded(int i) {
+        if ((i % connBurstSize) == 0) {
+            sleepSilently(connBurstTime);
+        }
     }
 
     final byte[] msg;
@@ -91,16 +106,20 @@
     final int port;
     final boolean bind;
     final boolean forceLocalPort;
+    final int connBurstSize;
+    final int connBurstTime;
 
     public AbstractClient(ClientSettings cs) {
-        this.remote = new InetSocketAddress(cs.host, cs.port);
-        this.host = cs.host;
-        this.port = cs.port;
-        this.bind = cs.bind;
-        this.forceLocalPort = cs.flp;
-        this.msg = cs.trx >= 12 ? createMessage(cs.trx) : null;
-        this.localAddrNum = cs.localAddrNum;
-        this.localAddresses = createInetAddresses(cs.localAddr, localAddrNum);
+        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);
+        connBurstSize = cs.connBurstSize;
+        connBurstTime = cs.connBurstTime;
 
         System.out.println(cs);
 
--- a/client-server/src/main/java/com/passus/st/clientserver/ChannelClient.java	Mon Nov 13 09:32:07 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/ChannelClient.java	Mon Nov 13 11:51:20 2017 +0100
@@ -89,11 +89,11 @@
                     ch.configureBlocking(false);
                     ch.connect(remote);
                     ch.finishConnect();
-                    throw new RuntimeException("hola hola");
                 } else {
                     ch.connect(remote);
                 }
                 ++i;
+                sleepIfNeeded(i);
             } catch (IOException ex) {
                 ex.printStackTrace();
                 ++errors;
--- a/client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java	Mon Nov 13 09:32:07 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java	Mon Nov 13 11:51:20 2017 +0100
@@ -17,24 +17,29 @@
     final int localAddrNum;
     final boolean bind;
     final boolean flp;
+    final int connBurstSize;
+    final int connBurstTime;
 
     public ClientSettings(CommandLine cl, String[] clArgs) {
-        this.host = clArgs[1];
-        this.port = Integer.parseInt(cl.getOptionValue('p', "400"));
-        this.connections = Integer.parseInt(clArgs[0]);
-        this.workers = Integer.parseInt(cl.getOptionValue('t', "4"));
-        this.trx = Integer.parseInt(cl.getOptionValue("trx", "0"));
-        this.localAddr = cl.getOptionValue("la", "192.168.60.1");
-        this.localAddrNum = Integer.parseInt(cl.getOptionValue("lan", "10"));
-        this.bind = cl.hasOption('b');
-        this.flp = cl.hasOption("flp");
+        host = clArgs[1];
+        port = Integer.parseInt(cl.getOptionValue('p', "400"));
+        connections = Integer.parseInt(clArgs[0]);
+        workers = Integer.parseInt(cl.getOptionValue('t', "4"));
+        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');
+        flp = cl.hasOption("flp");
+        connBurstSize = Integer.parseInt(cl.getOptionValue("cbs", "1000"));
+        connBurstTime = Integer.parseInt(cl.getOptionValue("cbt", "0"));
     }
 
     @Override
     public String toString() {
         return "conn=" + connections + " workers=" + workers + " host= " + host + " port=" + port
                 + " bind=" + bind + " forceLocalPort=" + flp + " msgLen=" + trx
-                + " localAddrNum=" + localAddrNum + " localAddr=" + localAddr;
+                + " localAddrNum=" + localAddrNum + " localAddr=" + localAddr
+                + " connBurstSize=" + connBurstSize + " connBurstTime=" + connBurstTime;
     }
 
 }
--- a/client-server/src/main/java/com/passus/st/clientserver/SocketClient.java	Mon Nov 13 09:32:07 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/SocketClient.java	Mon Nov 13 11:51:20 2017 +0100
@@ -76,6 +76,7 @@
                     sockets[i] = s;
                     sockets[i].setSoTimeout(1);
                     ++i;
+                    sleepIfNeeded(i);
                 } catch (IOException ex) {
                     printException(iii, local, remote1, ex);
                     ++errors;
@@ -88,6 +89,7 @@
                     sockets[i] = new Socket(host, port);
                     sockets[i].setSoTimeout(1);
                     ++i;
+                    sleepIfNeeded(i);
                 } catch (IOException ex) {
                     ex.printStackTrace();
                     ++errors;