server: re-introduce the client/server versions
[fanfix.git] / src / be / nikiroo / utils / serial / server / ConnectActionServer.java
index 699f3074b44b228ac797e186c0f18da58208d08c..04bf3360512567163e021308f88f35c249e2f124 100644 (file)
@@ -1,7 +1,10 @@
 package be.nikiroo.utils.serial.server;
 
+import java.io.IOException;
 import java.net.Socket;
 
+import javax.net.ssl.SSLException;
+
 import be.nikiroo.utils.Version;
 
 /**
@@ -23,8 +26,7 @@ abstract class ConnectActionServer {
        protected ConnectAction action;
 
        /**
-        * Create a new {@link ConnectActionServer} with the current application
-        * version (see {@link Version#getCurrentVersion()}) as the server version.
+        * Create a new {@link ConnectActionServer}, using the current version.
         * 
         * @param s
         *            the socket to bind to
@@ -44,13 +46,14 @@ abstract class ConnectActionServer {
         * @param key
         *            an optional key to encrypt all the communications (if NULL,
         *            everything will be sent in clear text)
-        * @param version
-        *            the server version
+        * @param serverVersion
+        *            the version of this server,that will be sent to the client
         */
-       public ConnectActionServer(Socket s, String key, Version version) {
-               action = new ConnectAction(s, true, key, version) {
+       public ConnectActionServer(Socket s, String key, Version serverVersion) {
+               action = new ConnectAction(s, true, key, serverVersion) {
                        @Override
                        protected void action(Version clientVersion) throws Exception {
+                               ConnectActionServer.this.serverHello();
                                ConnectActionServer.this.action(clientVersion);
                        }
 
@@ -66,6 +69,23 @@ abstract class ConnectActionServer {
                };
        }
 
+       /**
+        * Send the HELLO message (check that the client sends a String "HELLO" and
+        * send it back, to check I/O and encryption modes).
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        * @throws SSLException
+        *             in case of encryption error
+        */
+       protected void serverHello() throws IOException, SSLException {
+               String HELLO = action.recString();
+               if (!"HELLO".equals(HELLO)) {
+                       throw new SSLException("Server did not accept the encryption key");
+               }
+               action.sendString(HELLO);
+       }
+
        /**
         * Actually start the process and call the action (synchronous).
         */
@@ -128,14 +148,14 @@ abstract class ConnectActionServer {
         * @return the amount of bytes sent
         */
        public long getBytesSent() {
-               return action.getBytesSent();
+               return action.getBytesWritten();
        }
 
        /**
         * Method that will be called when an action is performed on the server.
         * 
         * @param clientVersion
-        *            the client version
+        *            the version of the client connected to this server
         * 
         * @throws Exception
         *             in case of I/O error