Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / be / nikiroo / utils / serial / server / ConnectActionClient.java
index 31b71b92af80eb8383fba1099e548fff5a4cd750..cb6bef3988c94e8f07d0306a8714ff85f3a9b1d8 100644 (file)
@@ -4,6 +4,8 @@ import java.io.IOException;
 import java.net.Socket;
 import java.net.UnknownHostException;
 
+import be.nikiroo.utils.Version;
+
 /**
  * Base class used for the client basic handling.
  * <p>
@@ -21,7 +23,8 @@ abstract class ConnectActionClient {
        protected ConnectAction action;
 
        /**
-        * Create a new {@link ConnectActionClient}.
+        * Create a new {@link ConnectActionClient}, using the current version of
+        * the program.
         * 
         * @param host
         *            the host to bind to
@@ -31,6 +34,7 @@ abstract class ConnectActionClient {
         *            an optional key to encrypt all the communications (if NULL,
         *            everything will be sent in clear text)
         * 
+        * 
         * @throws IOException
         *             in case of I/O error
         * @throws UnknownHostException
@@ -41,12 +45,40 @@ abstract class ConnectActionClient {
         */
        public ConnectActionClient(String host, int port, String key)
                        throws IOException {
-               this(new Socket(host, port), key);
+               this(host, port, key, Version.getCurrentVersion());
        }
 
        /**
         * Create a new {@link ConnectActionClient}.
         * 
+        * @param host
+        *            the host to bind to
+        * @param port
+        *            the port to bind to
+        * @param key
+        *            an optional key to encrypt all the communications (if NULL,
+        *            everything will be sent in clear text)
+        * @param clientVersion
+        *            the client version
+        * 
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        * @throws UnknownHostException
+        *             if the host is not known
+        * @throws IllegalArgumentException
+        *             if the port parameter is outside the specified range of valid
+        *             port values, which is between 0 and 65535, inclusive
+        */
+       public ConnectActionClient(String host, int port, String key,
+                       Version clientVersion) throws IOException {
+               this(new Socket(host, port), key, clientVersion);
+       }
+
+       /**
+        * Create a new {@link ConnectActionClient}, using the current version of
+        * the program.
+        * 
         * @param s
         *            the socket to bind to
         * @param key
@@ -54,16 +86,38 @@ abstract class ConnectActionClient {
         *            everything will be sent in clear text)
         */
        public ConnectActionClient(Socket s, String key) {
-               action = new ConnectAction(s, false, key) {
+               this(s, key, Version.getCurrentVersion());
+       }
+
+       /**
+        * Create a new {@link ConnectActionClient}.
+        * 
+        * @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 clientVersion
+        *            the client version
+        */
+       public ConnectActionClient(Socket s, String key, Version clientVersion) {
+               action = new ConnectAction(s, false, key, clientVersion) {
                        @Override
-                       protected void action() throws Exception {
-                               ConnectActionClient.this.action();
+                       protected void action(Version serverVersion) throws Exception {
+                               ConnectActionClient.this.action(serverVersion);
                        }
 
                        @Override
                        protected void onError(Exception e) {
                                ConnectActionClient.this.onError(e);
                        }
+
+                       @Override
+                       protected Version negotiateVersion(Version clientVersion) {
+                               new Exception("Should never be called on a client")
+                                               .printStackTrace();
+                               return null;
+                       }
                };
        }
 
@@ -89,11 +143,14 @@ abstract class ConnectActionClient {
        /**
         * Method that will be called when an action is performed on the client.
         * 
+        * @param serverVersion
+        *            the version of the server connected to this client
+        * 
         * @throws Exception
         *             in case of I/O error
         */
        @SuppressWarnings("unused")
-       public void action() throws Exception {
+       public void action(Version serverVersion) throws Exception {
        }
 
        /**