count the bytes we receive/send
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ConnectAction.java
index 8f57c628fb18e861c9805174468f5ccb0ce0fba1..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).
         */
@@ -108,6 +131,20 @@ abstract class ConnectAction {
                                out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
                                try {
                                        if (server) {
+                                               String line = in.readLine();
+                                               if (line != null && line.startsWith("VERSION ")) {
+                                                       // "VERSION client-version" (VERSION 1.0.0)
+                                                       Version clientVersion = new Version(
+                                                                       line.substring("VERSION ".length()));
+                                                       this.clientVersion = clientVersion;
+                                                       Version v = negotiateVersion(clientVersion);
+                                                       if (v == null) {
+                                                               v = new Version();
+                                                       }
+
+                                                       sendString("VERSION " + v.toString());
+                                               }
+
                                                action(clientVersion);
                                        } else {
                                                String v = sendString("VERSION " + version.toString());
@@ -119,11 +156,26 @@ abstract class ConnectAction {
                                        }
                                } finally {
                                        out.close();
+                                       out = null;
                                }
                        } finally {
                                in.close();
+                               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 {
@@ -159,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();
                        }
@@ -220,6 +273,7 @@ abstract class ConnectAction {
                synchronized (lock) {
                        out.write(line);
                        out.write("\n");
+                       bytesSent += line.length() + 1;
 
                        if (server) {
                                out.flush();
@@ -254,18 +308,8 @@ abstract class ConnectAction {
                                }
 
                                String line = in.readLine();
-                               if (server && line != null && line.startsWith("VERSION ")) {
-                                       // "VERSION client-version" (VERSION 1.0.0)
-                                       Version clientVersion = new Version(
-                                                       line.substring("VERSION ".length()));
-                                       this.clientVersion = clientVersion;
-                                       Version v = negotiateVersion(clientVersion);
-                                       if (v == null) {
-                                               v = new Version();
-                                       }
-                                       sendString("VERSION " + v.toString());
-
-                                       line = in.readLine();
+                               if (line != null) {
+                                       bytesReceived += line.length();
                                }
 
                                return line;