changeset 663:c6d0c5284865

client-server - delayed action
author Devel 1
date Thu, 16 Nov 2017 13:04:46 +0100
parents 01fe872c79d1
children ac9b0c4c5065
files client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java client-server/src/main/java/com/passus/st/clientserver/ChannelServer.java client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java
diffstat 3 files changed, 47 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java	Thu Nov 16 11:23:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/AbstractClient.java	Thu Nov 16 13:04:46 2017 +0100
@@ -9,6 +9,7 @@
 import java.net.SocketAddress;
 import java.net.UnknownHostException;
 import java.nio.ByteOrder;
+import java.util.Date;
 import org.apache.commons.cli.Options;
 
 /**
@@ -29,6 +30,7 @@
         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"));
+        options.addOption(CliUtils.createOption("at", "run-at", "run at givent time", "time"));
     }
 
     static byte[] createMessage(int length) {
@@ -112,6 +114,7 @@
     final boolean forceLocalPort;
     final int connBurstSize;
     final int connBurstTime;
+    final long at;
 
     public AbstractClient(ClientSettings cs) {
         operation = cs.operation;
@@ -123,6 +126,7 @@
         localAddresses = createInetAddresses(cs.localAddr, cs.localAddrNum);
         connBurstSize = cs.connBurstSize;
         connBurstTime = cs.connBurstTime;
+        at = cs.at;
 
         System.out.println(cs);
 
@@ -142,6 +146,13 @@
     abstract void receive(int bgn, int end) throws IOException;
 
     void run(final int bgn, final int end, final int rank) throws IOException {
+        long now = System.currentTimeMillis();
+        if (now < at) {
+            long duration = at - now;
+            System.out.println("Waiting " + (duration/1000) + "s till " + new Date(at));
+            sleepSilently(duration);
+        }
+
         switch (operation) {
             case CONN_SEND_RECV_DISC:
                 runConnSendRecvDisc(bgn, end, rank);
--- a/client-server/src/main/java/com/passus/st/clientserver/ChannelServer.java	Thu Nov 16 11:23:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/ChannelServer.java	Thu Nov 16 13:04:46 2017 +0100
@@ -69,6 +69,8 @@
                             }
                         } catch (IOException ex) {
                             System.out.println(ex.getMessage());
+                            selectedKey.cancel();
+                            sc.close();
                         }
                     }
                     skit.remove();
--- a/client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java	Thu Nov 16 11:23:20 2017 +0100
+++ b/client-server/src/main/java/com/passus/st/clientserver/ClientSettings.java	Thu Nov 16 13:04:46 2017 +0100
@@ -1,5 +1,9 @@
 package com.passus.st.clientserver;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
 import org.apache.commons.cli.CommandLine;
 
 /**
@@ -8,6 +12,8 @@
  */
 public class ClientSettings {
 
+    static SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm:ss");
+
     enum Operation {
         CONN_SEND_RECV_DISC, CONN_DISC_LOOP
     }
@@ -23,6 +29,7 @@
     final boolean flp;
     final int connBurstSize;
     final int connBurstTime;
+    final long at;
 
     public ClientSettings(CommandLine cl, String[] clArgs) {
         operation = getOperation(cl);
@@ -36,6 +43,7 @@
         flp = cl.hasOption("flp");
         connBurstSize = Integer.parseInt(cl.getOptionValue("cbs", "100"));
         connBurstTime = Integer.parseInt(cl.getOptionValue("cbt", "0"));
+        at = parseTime(cl.getOptionValue("at"));
     }
 
     private static Operation getOperation(CommandLine cl) {
@@ -57,12 +65,37 @@
         }
     }
 
+    static long parseTime(String s) {
+        if (s == null) {
+            return -1;
+        }
+
+        try {
+            Calendar now = GregorianCalendar.getInstance();
+            Calendar at = GregorianCalendar.getInstance();
+            at.setTime(TIME_FORMAT.parse(s));
+
+            Calendar c = GregorianCalendar.getInstance();
+            c.setTime(now.getTime());
+            c.set(Calendar.HOUR_OF_DAY, at.get(Calendar.HOUR_OF_DAY));
+            c.set(Calendar.MINUTE, at.get(Calendar.MINUTE));
+            c.set(Calendar.SECOND, at.get(Calendar.SECOND));
+            if (c.before(now)) {
+                c.add(Calendar.DATE, 1);
+            }
+
+            return c.getTimeInMillis();
+        } catch (ParseException ex) {
+            throw new IllegalArgumentException(ex);
+        }
+    }
+
     @Override
     public String toString() {
         return "conn=" + connections + " workers=" + workers + " host= " + host + " port=" + port
                 + " forceLocalPort=" + flp + " msgLen=" + trx
                 + " localAddrNum=" + localAddrNum + " localAddr=" + localAddr
-                + " connBurstSize=" + connBurstSize + " connBurstTime=" + connBurstTime;
+                + " connBurstSize=" + connBurstSize + " connBurstTime=" + connBurstTime + " at=" + at;
     }
 
 }