Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / be / nikiroo / utils / serial / server / ConnectActionServer.java
index 42a562d2069bcf275d15dc7dc9316ea1875dcd83..350d3fe40a1320b68f555458a43c3ceec11e8c25 100644 (file)
@@ -1,9 +1,8 @@
 package be.nikiroo.utils.serial.server;
 
-import java.io.IOException;
 import java.net.Socket;
 
-import javax.net.ssl.SSLException;
+import be.nikiroo.utils.Version;
 
 /**
  * Base class used for the server basic handling.
@@ -24,7 +23,7 @@ abstract class ConnectActionServer {
        protected ConnectAction action;
 
        /**
-        * Create a new {@link ConnectActionServer}.
+        * Create a new {@link ConnectActionServer}, using the current version.
         * 
         * @param s
         *            the socket to bind to
@@ -33,35 +32,37 @@ abstract class ConnectActionServer {
         *            everything will be sent in clear text)
         */
        public ConnectActionServer(Socket s, String key) {
-               action = new ConnectAction(s, true, key) {
+               this(s, key, Version.getCurrentVersion());
+       }
+
+       /**
+        * Create a new {@link ConnectActionServer}.
+        * 
+        * @param s
+        *            the socket to bind to
+        * @param key
+        *            an optional key to encrypt all the communications (if NULL,
+        *            everything will be sent in clear text)
+        * @param serverVersion
+        *            the version of this server,that will be sent to the client
+        */
+       public ConnectActionServer(Socket s, String key, Version serverVersion) {
+               action = new ConnectAction(s, true, key, serverVersion) {
                        @Override
-                       protected void action() throws Exception {
-                               ConnectActionServer.this.serverHello();
-                               ConnectActionServer.this.action();
+                       protected void action(Version clientVersion) throws Exception {
+                               ConnectActionServer.this.action(clientVersion);
                        }
 
                        @Override
                        protected void onError(Exception e) {
                                ConnectActionServer.this.onError(e);
                        }
-               };
-       }
 
-       /**
-        * 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);
+                       @Override
+                       protected Version negotiateVersion(Version clientVersion) {
+                               return ConnectActionServer.this.negotiateVersion(clientVersion);
+                       }
+               };
        }
 
        /**
@@ -132,11 +133,14 @@ abstract class ConnectActionServer {
        /**
         * Method that will be called when an action is performed on the server.
         * 
+        * @param clientVersion
+        *            the version of the client connected to this server
+        * 
         * @throws Exception
         *             in case of I/O error
         */
        @SuppressWarnings("unused")
-       public void action() throws Exception {
+       public void action(Version clientVersion) throws Exception {
        }
 
        /**
@@ -149,4 +153,19 @@ abstract class ConnectActionServer {
         */
        protected void onError(@SuppressWarnings("unused") Exception e) {
        }
+
+       /**
+        * Method called when we negotiate the version with the client.
+        * <p>
+        * Will return the actual server version by default.
+        * 
+        * @param clientVersion
+        *            the client version
+        * 
+        * @return the version to send to the client
+        */
+       protected Version negotiateVersion(
+                       @SuppressWarnings("unused") Version clientVersion) {
+               return action.getVersion();
+       }
 }
\ No newline at end of file