server: re-introduce the client/server versions
authorNiki Roo <niki@nikiroo.be>
Fri, 3 May 2019 15:33:23 +0000 (17:33 +0200)
committerNiki Roo <niki@nikiroo.be>
Fri, 3 May 2019 15:33:23 +0000 (17:33 +0200)
src/be/nikiroo/utils/serial/server/ConnectAction.java
src/be/nikiroo/utils/serial/server/ConnectActionClient.java
src/be/nikiroo/utils/serial/server/ConnectActionServer.java
src/be/nikiroo/utils/serial/server/ServerBridge.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 0f2f1b4319fa799950119815c31dd9bf81043ee8..b9bf2240ef3cd62c6a2c6f0abc7506c6ac0cca0b 100644 (file)
@@ -10,6 +10,7 @@ import javax.net.ssl.SSLException;
 import be.nikiroo.utils.CryptUtils;
 import be.nikiroo.utils.IOUtils;
 import be.nikiroo.utils.StringUtils;
+import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.Exporter;
 import be.nikiroo.utils.serial.Importer;
 import be.nikiroo.utils.streams.BufferedOutputStream;
@@ -37,6 +38,9 @@ abstract class ConnectAction {
        private Socket s;
        private boolean server;
 
+       private Version clientVersion;
+       private Version serverVersion;
+
        private CryptUtils crypt;
 
        private Object lock = new Object();
@@ -48,10 +52,28 @@ abstract class ConnectAction {
         * Method that will be called when an action is performed on either the
         * client or server this {@link ConnectAction} represent.
         * 
+        * @param version
+        *            the version on the other side of the communication (client or
+        *            server)
+        * 
         * @throws Exception
         *             in case of I/O error
         */
-       abstract protected void action() throws Exception;
+       abstract protected void action(Version version) throws Exception;
+
+       /**
+        * Method called when we negotiate the version with the client.
+        * <p>
+        * Thus, it is only called on the server.
+        * <p>
+        * Will return the actual server version by default.
+        * 
+        * @param clientVersion
+        *            the client version
+        * 
+        * @return the version to send to the client
+        */
+       abstract protected Version negotiateVersion(Version clientVersion);
 
        /**
         * Handler called when an unexpected error occurs in the code.
@@ -73,13 +95,40 @@ abstract class ConnectAction {
         * @param key
         *            an optional key to encrypt all the communications (if NULL,
         *            everything will be sent in clear text)
+        * @param version
+        *            the client-or-server version (depending upon the boolean
+        *            parameter <tt>server</tt>)
         */
-       protected ConnectAction(Socket s, boolean server, String key) {
+       protected ConnectAction(Socket s, boolean server, String key,
+                       Version version) {
                this.s = s;
                this.server = server;
                if (key != null) {
                        crypt = new CryptUtils(key);
                }
+
+               if (version == null) {
+                       version = new Version();
+               }
+
+               if (server) {
+                       serverVersion = version;
+               } else {
+                       clientVersion = version;
+               }
+       }
+
+       /**
+        * The version of this client-or-server.
+        * 
+        * @return the version
+        */
+       public Version getVersion() {
+               if (server) {
+                       return serverVersion;
+               }
+
+               return clientVersion;
        }
 
        /**
@@ -110,7 +159,7 @@ abstract class ConnectAction {
                        try {
                                out = new BufferedOutputStream(s.getOutputStream());
                                try {
-                                       action();
+                                       action(server ? serverVersion : clientVersion);
                                } finally {
                                        out.close();
                                }
index 1b92b42235a91e471cb650b2e8dc48608a1d6dc3..f556cf64e43fb81bc5d5447891a4a5dcc708bed1 100644 (file)
@@ -6,6 +6,8 @@ import java.net.UnknownHostException;
 
 import javax.net.ssl.SSLException;
 
+import be.nikiroo.utils.Version;
+
 /**
  * Base class used for the client basic handling.
  * <p>
@@ -23,7 +25,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
@@ -33,6 +36,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
@@ -43,12 +47,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
@@ -56,17 +88,39 @@ 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 {
+                       protected void action(Version serverVersion) throws Exception {
                                ConnectActionClient.this.clientHello();
-                               ConnectActionClient.this.action();
+                               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;
+                       }
                };
        }
 
@@ -111,11 +165,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 {
        }
 
        /**
index 42a562d2069bcf275d15dc7dc9316ea1875dcd83..04bf3360512567163e021308f88f35c249e2f124 100644 (file)
@@ -5,6 +5,8 @@ import java.net.Socket;
 
 import javax.net.ssl.SSLException;
 
+import be.nikiroo.utils.Version;
+
 /**
  * Base class used for the server basic handling.
  * <p>
@@ -24,7 +26,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,17 +35,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 {
+                       protected void action(Version clientVersion) throws Exception {
                                ConnectActionServer.this.serverHello();
-                               ConnectActionServer.this.action();
+                               ConnectActionServer.this.action(clientVersion);
                        }
 
                        @Override
                        protected void onError(Exception e) {
                                ConnectActionServer.this.onError(e);
                        }
+
+                       @Override
+                       protected Version negotiateVersion(Version clientVersion) {
+                               return ConnectActionServer.this.negotiateVersion(clientVersion);
+                       }
                };
        }
 
@@ -132,11 +154,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 +174,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
index 25d451c51577cb581c05c1daf14e702aec99b86b..b97699622b3a8e8b0c2325a758c9eb3f5b567eb3 100644 (file)
@@ -9,6 +9,7 @@ import java.net.UnknownHostException;
 
 import be.nikiroo.utils.StringUtils;
 import be.nikiroo.utils.TraceHandler;
+import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.Importer;
 
 /**
@@ -127,16 +128,17 @@ public class ServerBridge extends Server {
                // Bad impl, not up to date (should work, but not efficient)
                return new ConnectActionServerString(s, key) {
                        @Override
-                       public void action() throws Exception {
-                               onClientContact();
+                       public void action(Version clientVersion) throws Exception {
+                               onClientContact(clientVersion);
                                final ConnectActionServerString bridge = this;
 
                                try {
                                        new ConnectActionClientString(forwardToHost, forwardToPort,
                                                        forwardToKey) {
                                                @Override
-                                               public void action() throws Exception {
-                                                       onServerContact();
+                                               public void action(Version serverVersion)
+                                                               throws Exception {
+                                                       onServerContact(serverVersion);
 
                                                        for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge
                                                                        .rec()) {
@@ -165,15 +167,15 @@ public class ServerBridge extends Server {
        /**
         * This is the method that is called each time a client contact us.
         */
-       protected void onClientContact() {
-               getTraceHandler().trace(">>> CLIENT ");
+       protected void onClientContact(Version clientVersion) {
+               getTraceHandler().trace(">>> CLIENT " + clientVersion);
        }
 
        /**
         * This is the method that is called each time a client contact us.
         */
-       protected void onServerContact() {
-               getTraceHandler().trace("<<< SERVER");
+       protected void onServerContact(Version serverVersion) {
+               getTraceHandler().trace("<<< SERVER " + serverVersion);
                getTraceHandler().trace("");
        }
 
index dcd1badd1ad4ade60b87313c3b0960b2b883752e..0941f7047f5363e612c299024150663bbadf5f5a 100644 (file)
@@ -4,6 +4,8 @@ import java.io.IOException;
 import java.net.Socket;
 import java.net.UnknownHostException;
 
+import be.nikiroo.utils.Version;
+
 /**
  * This class implements a simple server that can listen for connections and
  * send/receive objects.
@@ -66,7 +68,7 @@ abstract public class ServerObject extends Server {
        protected ConnectActionServer createConnectActionServer(Socket s) {
                return new ConnectActionServerObject(s, key) {
                        @Override
-                       public void action() throws Exception {
+                       public void action(Version clientVersion) throws Exception {
                                long id = getNextId();
                                try {
                                        for (Object data = rec(); true; data = rec()) {
index 6a0d4f462c48d1adfc46f5723bd240514cdf020e..b321cedf1cceb936f4afab4099905084ae4652ed 100644 (file)
@@ -4,6 +4,8 @@ import java.io.IOException;
 import java.net.Socket;
 import java.net.UnknownHostException;
 
+import be.nikiroo.utils.Version;
+
 /**
  * This class implements a simple server that can listen for connections and
  * send/receive Strings.
@@ -66,7 +68,7 @@ abstract public class ServerString extends Server {
        protected ConnectActionServer createConnectActionServer(Socket s) {
                return new ConnectActionServerString(s, key) {
                        @Override
-                       public void action() throws Exception {
+                       public void action(Version clientVersion) throws Exception {
                                long id = getNextId();
                                for (String data = rec(); data != null; data = rec()) {
                                        String rep = null;
index 28eb8bb6fc04f48b4cdcbd92711aa7cb1670f0b1..265e96bbaa106f6f945c9e74ec1e878f94a13105 100644 (file)
@@ -2,7 +2,7 @@ package be.nikiroo.utils.test_code;
 
 import java.net.URL;
 
-import be.nikiroo.utils.TraceHandler;
+import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.server.ConnectActionClientObject;
 import be.nikiroo.utils.serial.server.ConnectActionClientString;
 import be.nikiroo.utils.serial.server.ConnectActionServerObject;
@@ -84,7 +84,8 @@ class SerialServerTest extends TestLauncher {
                                        try {
                                                new ConnectActionClientString(null, port, key) {
                                                        @Override
-                                                       public void action() throws Exception {
+                                                       public void action(Version version)
+                                                                       throws Exception {
                                                                rec[0] = "ok";
                                                        }
                                                }.connect();
@@ -140,7 +141,8 @@ class SerialServerTest extends TestLauncher {
                                        try {
                                                new ConnectActionClientString(null, port, key) {
                                                        @Override
-                                                       public void action() throws Exception {
+                                                       public void action(Version version)
+                                                                       throws Exception {
                                                                recd[0] = send("ping");
                                                        }
                                                }.connect();
@@ -203,7 +205,8 @@ class SerialServerTest extends TestLauncher {
                                        try {
                                                new ConnectActionClientString(null, port, key) {
                                                        @Override
-                                                       public void action() throws Exception {
+                                                       public void action(Version version)
+                                                                       throws Exception {
                                                                recd[0] = send("ping");
                                                                recd[1] = send("ping2");
                                                        }
@@ -267,7 +270,8 @@ class SerialServerTest extends TestLauncher {
                                        try {
                                                new ConnectActionClientString(null, port, key) {
                                                        @Override
-                                                       public void action() throws Exception {
+                                                       public void action(Version version)
+                                                                       throws Exception {
                                                                for (int i = 0; i < 3; i++) {
                                                                        recd[i] = send("" + i);
                                                                }
@@ -338,7 +342,8 @@ class SerialServerTest extends TestLauncher {
                                        try {
                                                new ConnectActionClientObject(null, port, key) {
                                                        @Override
-                                                       public void action() throws Exception {
+                                                       public void action(Version version)
+                                                                       throws Exception {
                                                                rec[0] = true;
                                                        }
 
@@ -398,7 +403,8 @@ class SerialServerTest extends TestLauncher {
                                        try {
                                                new ConnectActionClientObject(null, port, key) {
                                                        @Override
-                                                       public void action() throws Exception {
+                                                       public void action(Version version)
+                                                                       throws Exception {
                                                                recd[0] = send("ping");
                                                        }
                                                }.connect();
@@ -461,7 +467,8 @@ class SerialServerTest extends TestLauncher {
                                        try {
                                                new ConnectActionClientObject(null, port, key) {
                                                        @Override
-                                                       public void action() throws Exception {
+                                                       public void action(Version version)
+                                                                       throws Exception {
                                                                recd[0] = send("ping");
                                                                recd[1] = send("ping2");
                                                        }
@@ -525,7 +532,8 @@ class SerialServerTest extends TestLauncher {
                                        try {
                                                new ConnectActionClientObject(null, port, key) {
                                                        @Override
-                                                       public void action() throws Exception {
+                                                       public void action(Version version)
+                                                                       throws Exception {
                                                                recd[0] = send(new Object[] {
                                                                                "key",
                                                                                new URL(
@@ -596,7 +604,8 @@ class SerialServerTest extends TestLauncher {
                                        try {
                                                new ConnectActionClientObject(null, port, key) {
                                                        @Override
-                                                       public void action() throws Exception {
+                                                       public void action(Version version)
+                                                                       throws Exception {
                                                                for (int i = 0; i < 3; i++) {
                                                                        recd[i] = send(i);
                                                                }