count the bytes we receive/send
authorNiki Roo <niki@nikiroo.be>
Wed, 17 Apr 2019 15:57:43 +0000 (17:57 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 17 Apr 2019 15:57:43 +0000 (17:57 +0200)
src/be/nikiroo/utils/serial/server/ConnectAction.java
src/be/nikiroo/utils/serial/server/ConnectActionServer.java
src/be/nikiroo/utils/serial/server/Server.java

index 39417a77fca5c7671afd7d932d12cc22d4115397..cef10adf1c80736a4603c8d99c1fe4c82710d480 100644 (file)
@@ -32,6 +32,9 @@ abstract class ConnectAction {
        private OutputStreamWriter out;
        private boolean contentToSend;
 
+       private long bytesReceived;
+       private long bytesSent;
+
        /**
         * Method that will be called when an action is performed on either the
         * client or server this {@link ConnectAction} represent.
@@ -99,13 +102,31 @@ abstract class ConnectAction {
                return version;
        }
 
+       /**
+        * The total amount of bytes received.
+        * 
+        * @return the amount of bytes received
+        */
+       public long getBytesReceived() {
+               return bytesReceived;
+       }
+
+       /**
+        * The total amount of bytes sent.
+        * 
+        * @return the amount of bytes sent
+        */
+       public long getBytesSent() {
+               return bytesSent;
+       }
+
        /**
         * Actually start the process (this is synchronous).
         */
        public void connect() {
                try {
-                       in = new BufferedReader(
-                                       new InputStreamReader(s.getInputStream(), "UTF-8"));
+                       in = new BufferedReader(new InputStreamReader(s.getInputStream(),
+                                       "UTF-8"));
                        try {
                                out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
                                try {
@@ -151,11 +172,10 @@ abstract class ConnectAction {
                                        ciphers += cipher;
                                }
 
-                               e = new SSLException(
-                                               "SSL error (available SSL ciphers: " + ciphers + ")",
-                                               e);
+                               e = new SSLException("SSL error (available SSL ciphers: "
+                                               + ciphers + ")", e);
                        }
-                       
+
                        onError(e);
                } finally {
                        try {
@@ -188,12 +208,11 @@ abstract class ConnectAction {
         * @throws ClassNotFoundException
         *             if a class described in the serialised data cannot be found
         */
-       protected Object sendObject(Object data)
-                       throws IOException, NoSuchFieldException, NoSuchMethodException,
-                       ClassNotFoundException {
+       protected Object sendObject(Object data) throws IOException,
+                       NoSuchFieldException, NoSuchMethodException, ClassNotFoundException {
                synchronized (lock) {
-                       String rep = sendString(
-                                       new Exporter().append(data).toString(true, true));
+                       String rep = sendString(new Exporter().append(data).toString(true,
+                                       true));
                        if (rep != null) {
                                return new Importer().read(rep).getValue();
                        }
@@ -226,9 +245,9 @@ abstract class ConnectAction {
         * @throws java.lang.NullPointerException
         *             if the counter part has no data to send
         */
-       protected Object recObject()
-                       throws IOException, NoSuchFieldException, NoSuchMethodException,
-                       ClassNotFoundException, java.lang.NullPointerException {
+       protected Object recObject() throws IOException, NoSuchFieldException,
+                       NoSuchMethodException, ClassNotFoundException,
+                       java.lang.NullPointerException {
                String str = recString();
                if (str == null) {
                        throw new NullPointerException("No more data available");
@@ -254,6 +273,7 @@ abstract class ConnectAction {
                synchronized (lock) {
                        out.write(line);
                        out.write("\n");
+                       bytesSent += line.length() + 1;
 
                        if (server) {
                                out.flush();
@@ -287,7 +307,12 @@ abstract class ConnectAction {
                                        contentToSend = false;
                                }
 
-                               return in.readLine();
+                               String line = in.readLine();
+                               if (line != null) {
+                                       bytesReceived += line.length();
+                               }
+
+                               return line;
                        }
 
                        return null;
index 85b46a2fdd89f594528d166f78e26a7177eb8e2c..60f8db85db4f6f278a9b1f50ef1d41de92b0540e 100644 (file)
@@ -77,6 +77,24 @@ abstract class ConnectActionServer {
                }).start();
        }
 
+       /**
+        * The total amount of bytes received.
+        * 
+        * @return the amount of bytes received
+        */
+       public long getBytesReceived() {
+               return action.getBytesReceived();
+       }
+
+       /**
+        * The total amount of bytes sent.
+        * 
+        * @return the amount of bytes sent
+        */
+       public long getBytesSent() {
+               return action.getBytesSent();
+       }
+
        /**
         * Method that will be called when an action is performed on the server.
         * 
index f6dd7d86a13cbf94c4c08279458ae3724b8b2f5b..2dbc3bc2deb23739a35e73357f782bd9b611b8fe 100644 (file)
@@ -38,6 +38,9 @@ abstract class Server implements Runnable {
        private boolean exiting = false;
        private int counter;
 
+       private long bytesReceived;
+       private long bytesSent;
+
        private TraceHandler tracer = new TraceHandler();
 
        /**
@@ -146,6 +149,24 @@ abstract class Server implements Runnable {
                return port;
        }
 
+       /**
+        * The total amount of bytes received.
+        * 
+        * @return the amount of bytes received
+        */
+       public long getBytesReceived() {
+               return bytesReceived;
+       }
+
+       /**
+        * The total amount of bytes sent.
+        * 
+        * @return the amount of bytes sent
+        */
+       public long getBytesSent() {
+               return bytesSent;
+       }
+
        /**
         * Start the server (listen on the network for new connections).
         * <p>
@@ -209,10 +230,16 @@ abstract class Server implements Runnable {
                                new Thread(new Runnable() {
                                        @Override
                                        public void run() {
+                                               ConnectActionServer action = null;
                                                try {
-                                                       createConnectActionServer(s).connect();
+                                                       action = createConnectActionServer(s);
+                                                       action.connect();
                                                } finally {
                                                        count(-1);
+                                                       if (action != null) {
+                                                               bytesReceived += action.getBytesReceived();
+                                                               bytesSent += action.getBytesSent();
+                                                       }
                                                }
                                        }
                                }).start();