count the bytes we receive/send
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectAction.java
index 97243d567a2ed9f39dfcaddbf8deeaee51bac575..cef10adf1c80736a4603c8d99c1fe4c82710d480 100644 (file)
@@ -6,6 +6,8 @@ import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.net.Socket;
 
+import javax.net.ssl.SSLException;
+
 import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.Exporter;
 import be.nikiroo.utils.serial.Importer;
@@ -30,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.
@@ -97,6 +102,24 @@ 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).
         */
@@ -140,6 +163,19 @@ abstract class ConnectAction {
                                in = null;
                        }
                } catch (Exception e) {
+                       if (e instanceof SSLException) {
+                               String ciphers = "";
+                               for (String cipher : Server.getAnonCiphers()) {
+                                       if (!ciphers.isEmpty()) {
+                                               ciphers += ", ";
+                                       }
+                                       ciphers += cipher;
+                               }
+
+                               e = new SSLException("SSL error (available SSL ciphers: "
+                                               + ciphers + ")", e);
+                       }
+
                        onError(e);
                } finally {
                        try {
@@ -175,7 +211,8 @@ abstract class ConnectAction {
        protected Object sendObject(Object data) throws IOException,
                        NoSuchFieldException, NoSuchMethodException, ClassNotFoundException {
                synchronized (lock) {
-                       String rep = sendString(new Exporter().append(data).toString(true));
+                       String rep = sendString(new Exporter().append(data).toString(true,
+                                       true));
                        if (rep != null) {
                                return new Importer().read(rep).getValue();
                        }
@@ -236,6 +273,7 @@ abstract class ConnectAction {
                synchronized (lock) {
                        out.write(line);
                        out.write("\n");
+                       bytesSent += line.length() + 1;
 
                        if (server) {
                                out.flush();
@@ -269,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;