server: limit API breakage
authorNiki Roo <niki@nikiroo.be>
Sat, 4 May 2019 15:25:31 +0000 (17:25 +0200)
committerNiki Roo <niki@nikiroo.be>
Sat, 4 May 2019 15:25:31 +0000 (17:25 +0200)
src/be/nikiroo/utils/serial/server/ConnectActionClientObject.java
src/be/nikiroo/utils/serial/server/ConnectActionClientString.java
src/be/nikiroo/utils/serial/server/ServerObject.java
src/be/nikiroo/utils/serial/server/ServerString.java
src/be/nikiroo/utils/test_code/SerialServerTest.java

index 791f850b085819e1ac24480a2226358d708d194a..938564510753f36f70f66bae639afe3294c2e9a3 100644 (file)
@@ -4,6 +4,8 @@ import java.io.IOException;
 import java.net.Socket;
 import java.net.UnknownHostException;
 
+import be.nikiroo.utils.Version;
+
 /**
  * Class used for the client basic handling.
  * <p>
@@ -26,6 +28,21 @@ public class ConnectActionClientObject extends ConnectActionClient {
                super(s, key);
        }
 
+       /**
+        * Create a new {@link ConnectActionClientObject} .
+        * 
+        * @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 version of the client
+        */
+       public ConnectActionClientObject(Socket s, String key, Version clientVersion) {
+               super(s, key, clientVersion);
+       }
+
        /**
         * Create a new {@link ConnectActionClientObject}.
         * 
@@ -50,6 +67,32 @@ public class ConnectActionClientObject extends ConnectActionClient {
                super(host, port, key);
        }
 
+       /**
+        * Create a new {@link ConnectActionClientObject}.
+        * 
+        * @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 version of the client
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        * @throws UnknownHostException
+        *             if the IP address of the host could not be determined
+        * @throws IllegalArgumentException
+        *             if the port parameter is outside the specified range of valid
+        *             port values, which is between 0 and 65535, inclusive
+        */
+       public ConnectActionClientObject(String host, int port, String key,
+                       Version clientVersion) throws IOException {
+               super(host, port, key, clientVersion);
+       }
+
        /**
         * Serialise and send the given object to the server (and return the
         * deserialised answer).
@@ -74,4 +117,59 @@ public class ConnectActionClientObject extends ConnectActionClient {
                        NoSuchMethodException, ClassNotFoundException {
                return action.sendObject(data);
        }
+
+       // Deprecated //
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @Deprecated
+       public ConnectActionClientObject(String host, int port, boolean ssl)
+                       throws IOException {
+               this(host, port, ssl ? "" : null);
+       }
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @Deprecated
+       public ConnectActionClientObject(String host, int port, boolean ssl,
+                       Version version) throws IOException {
+               this(host, port, ssl ? "" : null, version);
+       }
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @SuppressWarnings("unused")
+       @Deprecated
+       public ConnectActionClientObject(Socket s, boolean ssl) throws IOException {
+               this(s, ssl ? "" : null);
+       }
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @SuppressWarnings("unused")
+       @Deprecated
+       public ConnectActionClientObject(Socket s, boolean ssl, Version version)
+                       throws IOException {
+               this(s, ssl ? "" : null, version);
+       }
 }
\ No newline at end of file
index 17da668a6db7f5e737f683d270e9396509d35d4f..3005cee56b5ab7cbe24a0f3056c6498731ab3e95 100644 (file)
@@ -4,6 +4,8 @@ import java.io.IOException;
 import java.net.Socket;
 import java.net.UnknownHostException;
 
+import be.nikiroo.utils.Version;
+
 /**
  * Class used for the client basic handling.
  * <p>
@@ -26,6 +28,21 @@ public class ConnectActionClientString extends ConnectActionClient {
                super(s, key);
        }
 
+       /**
+        * Create a new {@link ConnectActionClientString}.
+        * 
+        * @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 version of this client
+        */
+       public ConnectActionClientString(Socket s, String key, Version clientVersion) {
+               super(s, key, clientVersion);
+       }
+
        /**
         * Create a new {@link ConnectActionClientString}.
         * 
@@ -50,6 +67,32 @@ public class ConnectActionClientString extends ConnectActionClient {
                super(host, port, key);
        }
 
+       /**
+        * Create a new {@link ConnectActionClientString}.
+        * 
+        * @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 version of this client
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        * @throws UnknownHostException
+        *             if the IP address of the host could not be determined
+        * @throws IllegalArgumentException
+        *             if the port parameter is outside the specified range of valid
+        *             port values, which is between 0 and 65535, inclusive
+        */
+       public ConnectActionClientString(String host, int port, String key,
+                       Version clientVersion) throws IOException {
+               super(host, port, key, clientVersion);
+       }
+
        /**
         * Send the given object to the server (and return the answer).
         * 
@@ -64,4 +107,59 @@ public class ConnectActionClientString extends ConnectActionClient {
        public String send(String data) throws IOException {
                return action.sendString(data);
        }
+
+       // Deprecated //
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @Deprecated
+       public ConnectActionClientString(String host, int port, boolean ssl)
+                       throws IOException {
+               this(host, port, ssl ? "" : null);
+       }
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @Deprecated
+       public ConnectActionClientString(String host, int port, boolean ssl,
+                       Version version) throws IOException {
+               this(host, port, ssl ? "" : null, version);
+       }
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @SuppressWarnings("unused")
+       @Deprecated
+       public ConnectActionClientString(Socket s, boolean ssl) throws IOException {
+               this(s, ssl ? "" : null);
+       }
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @SuppressWarnings("unused")
+       @Deprecated
+       public ConnectActionClientString(Socket s, boolean ssl, Version version)
+                       throws IOException {
+               this(s, ssl ? "" : null, version);
+       }
 }
\ No newline at end of file
index 0941f7047f5363e612c299024150663bbadf5f5a..a6a5dd1b83573c3ca0814f7c409ae872d2061e59 100644 (file)
@@ -74,7 +74,7 @@ abstract public class ServerObject extends Server {
                                        for (Object data = rec(); true; data = rec()) {
                                                Object rep = null;
                                                try {
-                                                       rep = onRequest(this, data, id);
+                                                       rep = onRequest(this, clientVersion, data, id);
                                                        if (isClosing()) {
                                                                return;
                                                        }
@@ -122,6 +122,59 @@ abstract public class ServerObject extends Server {
         * @throws Exception
         *             in case of an exception, the error will only be logged
         */
-       abstract protected Object onRequest(ConnectActionServerObject action,
-                       Object data, long id) throws Exception;
+       protected Object onRequest(ConnectActionServerObject action,
+                       Version clientVersion, Object data,
+                       @SuppressWarnings("unused") long id) throws Exception {
+               // TODO: change to abstract when deprecated method is removed
+               // Default implementation for compat
+               return onRequest(action, clientVersion, data);
+       }
+
+       // Deprecated //
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @Deprecated
+       public ServerObject(int port, boolean ssl) throws IOException {
+               this(port, ssl ? "" : null);
+       }
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @Deprecated
+       public ServerObject(String name, int port, boolean ssl) throws IOException {
+               this(name, port, ssl ? "" : null);
+       }
+
+       /**
+        * Will be called if the correct version is not overrided.
+        * 
+        * @deprecated use the version with the id.
+        * 
+        * @param action
+        *            the client action
+        * @param data
+        *            the data sent by the client
+        * 
+        * @return the answer to return to the client
+        * 
+        * @throws Exception
+        *             in case of an exception, the error will only be logged
+        */
+       @Deprecated
+       @SuppressWarnings("unused")
+       protected Object onRequest(ConnectActionServerObject action,
+                       Version version, Object data) throws Exception {
+               return null;
+       }
 }
index b321cedf1cceb936f4afab4099905084ae4652ed..3c982fd585371ee1694497880a918b2b5149a7e6 100644 (file)
@@ -73,7 +73,7 @@ abstract public class ServerString extends Server {
                                for (String data = rec(); data != null; data = rec()) {
                                        String rep = null;
                                        try {
-                                               rep = onRequest(this, data, id);
+                                               rep = onRequest(this, clientVersion, data, id);
                                                if (isClosing()) {
                                                        return;
                                                }
@@ -112,6 +112,8 @@ abstract public class ServerString extends Server {
         * 
         * @param action
         *            the client action
+        * @param clientVersion
+        *            the client version
         * @param data
         *            the data sent by the client
         * @param id
@@ -123,6 +125,59 @@ abstract public class ServerString extends Server {
         * @throws Exception
         *             in case of an exception, the error will only be logged
         */
-       abstract protected String onRequest(ConnectActionServerString action,
-                       String data, long id) throws Exception;
+       protected String onRequest(ConnectActionServerString action,
+                       Version clientVersion, String data,
+                       @SuppressWarnings("unused") long id) throws Exception {
+               // TODO: change to abstract when deprecated method is removed
+               // Default implementation for compat
+               return onRequest(action, clientVersion, data);
+       }
+
+       // Deprecated //
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @Deprecated
+       public ServerString(int port, boolean ssl) throws IOException {
+               this(port, ssl ? "" : null);
+       }
+
+       /**
+        * @deprecated SSL support has been replaced by key-based encryption.
+        *             <p>
+        *             Please use the version with key encryption (this deprecated
+        *             version uses an empty key when <tt>ssl</tt> is TRUE and no
+        *             key (NULL) when <tt>ssl</tt> is FALSE).
+        */
+       @Deprecated
+       public ServerString(String name, int port, boolean ssl) throws IOException {
+               this(name, port, ssl ? "" : null);
+       }
+
+       /**
+        * Will be called if the correct version is not overrided.
+        * 
+        * @deprecated use the version with the id.
+        * 
+        * @param action
+        *            the client action
+        * @param data
+        *            the data sent by the client
+        * 
+        * @return the answer to return to the client
+        * 
+        * @throws Exception
+        *             in case of an exception, the error will only be logged
+        */
+       @Deprecated
+       @SuppressWarnings("unused")
+       protected String onRequest(ConnectActionServerString action,
+                       Version version, String data) throws Exception {
+               return null;
+       }
 }
index 265e96bbaa106f6f945c9e74ec1e878f94a13105..c10a158c347c1f9e0ed64f096a33352d8a3b4556 100644 (file)
@@ -52,8 +52,8 @@ class SerialServerTest extends TestLauncher {
                                ServerString server = new ServerString(this.getName(), 0, key) {
                                        @Override
                                        protected String onRequest(
-                                                       ConnectActionServerString action, String data,
-                                                       long id) throws Exception {
+                                                       ConnectActionServerString action, Version version,
+                                                       String data, long id) throws Exception {
                                                return null;
                                        }
 
@@ -113,8 +113,8 @@ class SerialServerTest extends TestLauncher {
                                ServerString server = new ServerString(this.getName(), 0, key) {
                                        @Override
                                        protected String onRequest(
-                                                       ConnectActionServerString action, String data,
-                                                       long id) throws Exception {
+                                                       ConnectActionServerString action, Version version,
+                                                       String data, long id) throws Exception {
                                                sent[0] = data;
                                                return "pong";
                                        }
@@ -175,8 +175,8 @@ class SerialServerTest extends TestLauncher {
                                ServerString server = new ServerString(this.getName(), 0, key) {
                                        @Override
                                        protected String onRequest(
-                                                       ConnectActionServerString action, String data,
-                                                       long id) throws Exception {
+                                                       ConnectActionServerString action, Version version,
+                                                       String data, long id) throws Exception {
                                                sent[0] = data;
                                                action.send("pong");
                                                sent[1] = action.rec();
@@ -242,8 +242,8 @@ class SerialServerTest extends TestLauncher {
                                ServerString server = new ServerString(this.getName(), 0, key) {
                                        @Override
                                        protected String onRequest(
-                                                       ConnectActionServerString action, String data,
-                                                       long id) throws Exception {
+                                                       ConnectActionServerString action, Version version,
+                                                       String data, long id) throws Exception {
                                                sent[Integer.parseInt(data)] = data;
                                                return "" + (Integer.parseInt(data) * 2);
                                        }
@@ -315,8 +315,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data,
-                                                       long id) throws Exception {
+                                                       ConnectActionServerObject action, Version version,
+                                                       Object data, long id) throws Exception {
                                                return null;
                                        }
 
@@ -375,8 +375,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data,
-                                                       long id) throws Exception {
+                                                       ConnectActionServerObject action, Version version,
+                                                       Object data, long id) throws Exception {
                                                sent[0] = data;
                                                return "pong";
                                        }
@@ -437,8 +437,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data,
-                                                       long id) throws Exception {
+                                                       ConnectActionServerObject action, Version version,
+                                                       Object data, long id) throws Exception {
                                                sent[0] = data;
                                                action.send("pong");
                                                sent[1] = action.rec();
@@ -504,8 +504,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data,
-                                                       long id) throws Exception {
+                                                       ConnectActionServerObject action, Version version,
+                                                       Object data, long id) throws Exception {
                                                sent[0] = data;
                                                return new Object[] { "ACK" };
                                        }
@@ -576,8 +576,8 @@ class SerialServerTest extends TestLauncher {
                                ServerObject server = new ServerObject(this.getName(), 0, key) {
                                        @Override
                                        protected Object onRequest(
-                                                       ConnectActionServerObject action, Object data,
-                                                       long id) throws Exception {
+                                                       ConnectActionServerObject action, Version version,
+                                                       Object data, long id) throws Exception {
                                                sent[(Integer) data] = data;
                                                return ((Integer) data) * 2;
                                        }