From: Niki Roo Date: Fri, 3 May 2019 15:33:23 +0000 (+0200) Subject: server: re-introduce the client/server versions X-Git-Url: https://git.nikiroo.be/?a=commitdiff_plain;h=3087aeb5f7b9fffcb57d51030c4674f9768e7f02;p=fanfix-jexer.git server: re-introduce the client/server versions --- diff --git a/src/be/nikiroo/utils/serial/server/ConnectAction.java b/src/be/nikiroo/utils/serial/server/ConnectAction.java index 0f2f1b4..b9bf224 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -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. + *

+ * Thus, it is only called on the server. + *

+ * 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 server) */ - 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(); } diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionClient.java b/src/be/nikiroo/utils/serial/server/ConnectActionClient.java index 1b92b42..f556cf6 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionClient.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionClient.java @@ -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. *

@@ -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 { } /** diff --git a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java index 42a562d..04bf336 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectActionServer.java +++ b/src/be/nikiroo/utils/serial/server/ConnectActionServer.java @@ -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. *

@@ -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. + *

+ * 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 diff --git a/src/be/nikiroo/utils/serial/server/ServerBridge.java b/src/be/nikiroo/utils/serial/server/ServerBridge.java index 25d451c..b976996 100644 --- a/src/be/nikiroo/utils/serial/server/ServerBridge.java +++ b/src/be/nikiroo/utils/serial/server/ServerBridge.java @@ -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(""); } diff --git a/src/be/nikiroo/utils/serial/server/ServerObject.java b/src/be/nikiroo/utils/serial/server/ServerObject.java index dcd1bad..0941f70 100644 --- a/src/be/nikiroo/utils/serial/server/ServerObject.java +++ b/src/be/nikiroo/utils/serial/server/ServerObject.java @@ -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()) { diff --git a/src/be/nikiroo/utils/serial/server/ServerString.java b/src/be/nikiroo/utils/serial/server/ServerString.java index 6a0d4f4..b321ced 100644 --- a/src/be/nikiroo/utils/serial/server/ServerString.java +++ b/src/be/nikiroo/utils/serial/server/ServerString.java @@ -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; diff --git a/src/be/nikiroo/utils/test_code/SerialServerTest.java b/src/be/nikiroo/utils/test_code/SerialServerTest.java index 28eb8bb..265e96b 100644 --- a/src/be/nikiroo/utils/test_code/SerialServerTest.java +++ b/src/be/nikiroo/utils/test_code/SerialServerTest.java @@ -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); }