Version 3.1.0: ServerBridge
authorNiki Roo <niki@nikiroo.be>
Wed, 29 Nov 2017 17:17:26 +0000 (18:17 +0100)
committerNiki Roo <niki@nikiroo.be>
Wed, 29 Nov 2017 17:17:26 +0000 (18:17 +0100)
VERSION
changelog.md
src/be/nikiroo/utils/TraceHandler.java
src/be/nikiroo/utils/serial/server/Server.java
src/be/nikiroo/utils/serial/server/ServerBridge.java [new file with mode: 0644]
src/be/nikiroo/utils/serial/server/ServerObject.java
src/be/nikiroo/utils/serial/server/ServerString.java
src/be/nikiroo/utils/test/SerialServerTest.java [new file with mode: 0644]
src/be/nikiroo/utils/test/SerialTest.java
src/be/nikiroo/utils/test/Test.java

diff --git a/VERSION b/VERSION
index 4a36342fcab700951adb18ae7adc930997f6c3f4..fd2a01863fdd3035fac5918c59666363544bfe23 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.0.0
+3.1.0
index 1df57c7ed66d3447bcd5c51ba9712f36d50f6f09..07ebcd443896b3037a385e5fba5374df36d0fd85 100644 (file)
@@ -1,5 +1,9 @@
 # nikiroo-utils
 
+## Version 3.1.0
+
+- New ServerBridge (including tests)
+
 ## Version 3.0.0
 
 - jDoc
index 6541a8bfd5b9add51643686a7f641f250b201809..daa3743f387832d446f91cea0ba119747f1167aa 100644 (file)
@@ -7,9 +7,9 @@ package be.nikiroo.utils;
  * @author niki
  */
 public class TraceHandler {
-       private boolean showErrors;
-       private boolean showTraces;
-       private boolean showErrorDetails;
+       private final boolean showErrors;
+       private final boolean showErrorDetails;
+       private final int traceLevel;
 
        /**
         * Create a default {@link TraceHandler} that will print errors on stderr
@@ -27,13 +27,38 @@ public class TraceHandler {
         * @param showErrorDetails
         *            show more details when printing errors
         * @param showTraces
-        *            show traces on stdout
+        *            show level 1 traces on stdout, or no traces at all
         */
        public TraceHandler(boolean showErrors, boolean showErrorDetails,
                        boolean showTraces) {
+               this(showErrors, showErrorDetails, showTraces ? 1 : 0);
+       }
+
+       /**
+        * Create a default {@link TraceHandler}.
+        * 
+        * @param showErrors
+        *            show errors on stderr
+        * @param showErrorDetails
+        *            show more details when printing errors
+        * @param traceLevel
+        *            show traces of this level or lower (0 means "no traces",
+        *            higher means more traces)
+        */
+       public TraceHandler(boolean showErrors, boolean showErrorDetails,
+                       int traceLevel) {
                this.showErrors = showErrors;
                this.showErrorDetails = showErrorDetails;
-               this.showTraces = showTraces;
+               this.traceLevel = Math.max(traceLevel, 0);
+       }
+
+       /**
+        * The trace level of this {@link TraceHandler}.
+        * 
+        * @return the level
+        */
+       public int getTraceLevel() {
+               return traceLevel;
        }
 
        /**
@@ -67,13 +92,32 @@ public class TraceHandler {
        /**
         * A trace happened, show it.
         * <p>
-        * Will only be effective if {@link TraceHandler#showTraces} is true.
+        * By default, will only be effective if {@link TraceHandler#showTraces} is
+        * true
+        * <p>
+        * A call to this method is equivalent to a call to
+        * {@link TraceHandler#trace(String, int)} with a level of 1.
         * 
         * @param message
         *            the trace message
         */
        public void trace(String message) {
-               if (showTraces) {
+               trace(message, 1);
+       }
+
+       /**
+        * A trace happened, show it.
+        * <p>
+        * By default, will only be effective if {@link TraceHandler#showTraces} is
+        * true and the level is lower or equal to {@link TraceHandler#traceLevel}.
+        * 
+        * @param message
+        *            the trace message
+        * @param level
+        *            the trace level
+        */
+       public void trace(String message, int level) {
+               if (traceLevel > 0 && level <= traceLevel) {
                        System.out.println(message);
                }
        }
index 36806d6b61924bd60d5d0d1d2c6afca68d878893..be847d02884ec3112affd0d730d0d364b158e458 100644 (file)
@@ -184,8 +184,17 @@ abstract class Server implements Runnable {
 
                        while (started && !exiting) {
                                count(1);
-                               Socket s = ss.accept();
-                               createConnectActionServer(s).connectAsync();
+                               final Socket s = ss.accept();
+                               new Thread(new Runnable() {
+                                       @Override
+                                       public void run() {
+                                               try {
+                                                       createConnectActionServer(s).connect();
+                                               } finally {
+                                                       count(-1);
+                                               }
+                                       }
+                               }).start();
                        }
 
                        // Will be covered by @link{Server#stop(long)} for timeouts
@@ -283,19 +292,6 @@ abstract class Server implements Runnable {
                }
        }
 
-       /**
-        * This method will be called on errors.
-        * <p>
-        * By default, it will only call the trace handler (so you may want to call
-        * super {@link Server#onError} if you override it).
-        * 
-        * @param e
-        *            the error
-        */
-       protected void onError(Exception e) {
-               tracer.error(e);
-       }
-
        /**
         * Change the number of currently serviced actions.
         * 
@@ -304,13 +300,26 @@ abstract class Server implements Runnable {
         * 
         * @return the current number after this operation
         */
-       int count(int change) {
+       private int count(int change) {
                synchronized (counterLock) {
                        counter += change;
                        return counter;
                }
        }
 
+       /**
+        * This method will be called on errors.
+        * <p>
+        * By default, it will only call the trace handler (so you may want to call
+        * super {@link Server#onError} if you override it).
+        * 
+        * @param e
+        *            the error
+        */
+       protected void onError(Exception e) {
+               tracer.error(e);
+       }
+
        /**
         * Create a {@link Socket}.
         * 
diff --git a/src/be/nikiroo/utils/serial/server/ServerBridge.java b/src/be/nikiroo/utils/serial/server/ServerBridge.java
new file mode 100644 (file)
index 0000000..786d018
--- /dev/null
@@ -0,0 +1,252 @@
+package be.nikiroo.utils.serial.server;
+
+import java.io.IOException;
+import java.net.Socket;
+
+import be.nikiroo.utils.TraceHandler;
+import be.nikiroo.utils.Version;
+import be.nikiroo.utils.serial.Importer;
+
+/**
+ * This class implements a simple server that can bridge two other
+ * {@link Server}s.
+ * <p>
+ * It can, of course, inspect the data that goes through it (by default, it
+ * prints traces of the data).
+ * <p>
+ * Note: this {@link ServerBridge} has to be discarded after use (cannot be
+ * started twice).
+ * 
+ * @author niki
+ */
+public class ServerBridge extends Server {
+       private final String forwardToHost;
+       private final int forwardToPort;
+       private final boolean forwardToSsl;
+
+       /**
+        * Create a new server that will start listening on the network when
+        * {@link ServerBridge#start()} is called.
+        * 
+        * @param port
+        *            the port to listen on, or 0 to assign any unallocated port
+        *            found (which can later on be queried via
+        *            {@link ServerBridge#getPort()}
+        * @param ssl
+        *            use a SSL connection (or not)
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public ServerBridge(int port, boolean ssl, String forwardToHost,
+                       int forwardToPort, boolean forwardToSsl) throws IOException {
+               super(port, ssl);
+               this.forwardToHost = forwardToHost;
+               this.forwardToPort = forwardToPort;
+               this.forwardToSsl = forwardToSsl;
+       }
+
+       /**
+        * Create a new server that will start listening on the network when
+        * {@link ServerBridge#start()} is called.
+        * 
+        * @param name
+        *            the server name (only used for debug info and traces)
+        * @param port
+        *            the port to listen on
+        * @param ssl
+        *            use a SSL connection (or not)
+        * 
+        * @throws IOException
+        *             in case of I/O error
+        */
+       public ServerBridge(String name, int port, boolean ssl,
+                       String forwardToHost, int forwardToPort, boolean forwardToSsl)
+                       throws IOException {
+               super(name, port, ssl);
+               this.forwardToHost = forwardToHost;
+               this.forwardToPort = forwardToPort;
+               this.forwardToSsl = forwardToSsl;
+       }
+
+       /**
+        * The traces handler for this {@link Server}.
+        * <p>
+        * <ul>
+        * <li>At level 1, it will only print basic IN/OUT messages with length.</li>
+        * <li>At level 2, it will also print the data as a String.</li>
+        * <li>At level 3, it will try to interpret it (SLOW) and print the object
+        * type if possible.</li>
+        * <li>At level 4, it will try to print the {@link Object#toString()} value.
+        * </li>
+        * </ul>
+        * 
+        * @param tracer
+        *            the new traces handler
+        */
+       @Override
+       public void setTraceHandler(TraceHandler tracer) {
+               super.setTraceHandler(tracer);
+       }
+
+       @Override
+       protected ConnectActionServer createConnectActionServer(Socket s) {
+               return new ConnectActionServerString(s) {
+                       @Override
+                       public void action(final Version clientVersion) throws Exception {
+                               onClientContact(clientVersion);
+                               final ConnectActionServerString bridge = this;
+
+                               new ConnectActionClientString(forwardToHost, forwardToPort,
+                                               forwardToSsl, clientVersion) {
+                                       @Override
+                                       public void action(final Version serverVersion)
+                                                       throws Exception {
+                                               onServerContact(serverVersion);
+
+                                               for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge
+                                                               .rec()) {
+                                                       onRec(clientVersion, fromClient);
+                                                       String fromServer = send(fromClient);
+                                                       onSend(serverVersion, fromServer);
+                                                       bridge.send(fromServer);
+                                               }
+                                       }
+                               }.connect();
+                       }
+               };
+       }
+
+       /**
+        * This is the method that is called each time a client contact us.
+        * 
+        * @param clientVersion
+        *            the client version
+        * @param data
+        *            the data sent by the client
+        */
+       protected void onClientContact(Version clientVersion) {
+               getTraceHandler().trace("CLIENT " + clientVersion);
+       }
+
+       /**
+        * This is the method that is called each time a client contact us.
+        * 
+        * @param serverVersion
+        *            the server version
+        * @param data
+        *            the data sent by the client
+        */
+       protected void onServerContact(Version serverVersion) {
+               getTraceHandler().trace("SERVER " + serverVersion);
+       }
+
+       /**
+        * This is the method that is called each time a client contact us.
+        * 
+        * @param clientVersion
+        *            the client version
+        * @param data
+        *            the data sent by the client
+        */
+       protected void onRec(Version clientVersion, String data) {
+               trace("<<< CLIENT (" + clientVersion + ")", data);
+       }
+
+       /**
+        * This is the method that is called each time the forwarded server contact
+        * us.
+        * 
+        * @param serverVersion
+        *            the client version
+        * @param data
+        *            the data sent by the client
+        */
+       protected void onSend(Version serverVersion, String data) {
+               trace(">>> SERVER (" + serverVersion + ")", data);
+       }
+
+       /**
+        * Trace the data with the given prefix.
+        * 
+        * @param prefix
+        *            the prefix (client, server, version...)
+        * @param data
+        *            the data to trace
+        */
+       private void trace(String prefix, String data) {
+               getTraceHandler().trace(prefix + ": " + data.length() + " characters",
+                               1);
+
+               if (getTraceHandler().getTraceLevel() >= 2) {
+                       try {
+                               Object obj = new Importer().read(data).getValue();
+                               if (obj == null) {
+                                       getTraceHandler().trace("NULL", 3);
+                                       getTraceHandler().trace("NULL", 4);
+                               } else {
+                                       getTraceHandler().trace("(" + obj.getClass() + ")", 3);
+                                       getTraceHandler().trace("" + obj.toString(), 4);
+                               }
+                       } catch (Exception e) {
+                               getTraceHandler().trace("(not an object)", 3);
+                               getTraceHandler().trace(data, 4);
+                       }
+
+                       getTraceHandler().trace("", 4);
+               }
+       }
+
+       /**
+        * Start a bridge between 2 servers.
+        * 
+        * @param args
+        *            an array containing:
+        *            <ul>
+        *            <li>The bridge name</li>
+        *            <li>The bridge port</li>
+        *            <li>TRUE for a ssl bridge, FALSE for plain text</li>
+        *            <li>The forward server host</li>
+        *            <li>The forward server port</li>
+        *            <li>TRUE for a ssl forward server, FALSE for plain text</li>
+        *            <li>(optional) a trace level</li>
+        *            </ul>
+        */
+       public static void main(String[] args) {
+               final TraceHandler tracer = new TraceHandler(true, false, 0);
+               try {
+                       if (args.length < 6) {
+                               tracer.error("Invalid syntax.\n"
+                                               + "Syntax: [name] [port] [ssl] [fhost] [fport] [fssl] ([trace level])\n"
+                                               + "\tname: the bridge name\n"
+                                               + "\tport: the bridge port\n"
+                                               + "\tssl: TRUE for an SSL bridge, FALSE for plain text\n"
+                                               + "\tfhost: the forward server host\n"
+                                               + "\tfport: the forward server port\n"
+                                               + "\tfssl: TRUE for an SSL forward server, FALSE for plain text\n"
+                                               + "\ttrace level: the optional trace level (default is 1)\n");
+                               return;
+                       }
+
+                       int i = 0;
+                       String name = args[i++];
+                       int port = Integer.parseInt(args[i++]);
+                       boolean ssl = Boolean.parseBoolean(args[i++]);
+                       String fhost = args[i++];
+                       int fport = Integer.parseInt(args[i++]);
+                       boolean fssl = Boolean.parseBoolean(args[i++]);
+
+                       int traceLevel = 1;
+                       if (args.length > 6) {
+                               traceLevel = Integer.parseInt(args[i++]);
+                       }
+
+                       ServerBridge bridge = new ServerBridge(name, port, ssl, fhost,
+                                       fport, fssl);
+                       bridge.setTraceHandler(new TraceHandler(true, true, traceLevel));
+                       bridge.run();
+               } catch (Exception e) {
+                       tracer.error(e);
+               }
+       }
+}
index 6ff81f512b3dd50642b79a3aac8da97d769c91b0..1559ae455b27c9300424b349ddf188cc8a55ef15 100644 (file)
@@ -73,15 +73,6 @@ abstract public class ServerObject extends Server {
                                                                        + ": client has data no more, stopping connection");
                                }
                        }
-
-                       @Override
-                       public void connect() {
-                               try {
-                                       super.connect();
-                               } finally {
-                                       count(-1);
-                               }
-                       }
                };
        }
 
index 4a185da50baf3ae03190a3e0d3b64c09bbbf285c..186afa07ed6d2c85f94e7e2db1d7f5dfd601efc5 100644 (file)
@@ -56,35 +56,19 @@ abstract public class ServerString extends Server {
                return new ConnectActionServerString(s) {
                        @Override
                        public void action(Version clientVersion) throws Exception {
-                               try {
-                                       for (String data = rec(); data != null; data = rec()) {
-                                               String rep = null;
-                                               try {
-                                                       rep = onRequest(this, clientVersion, data);
-                                               } catch (Exception e) {
-                                                       onError(e);
-                                               }
-
-                                               if (rep == null) {
-                                                       rep = "";
-                                               }
+                               for (String data = rec(); data != null; data = rec()) {
+                                       String rep = null;
+                                       try {
+                                               rep = onRequest(this, clientVersion, data);
+                                       } catch (Exception e) {
+                                               onError(e);
+                                       }
 
-                                               send(rep);
+                                       if (rep == null) {
+                                               rep = "";
                                        }
-                               } catch (NullPointerException e) {
-                                       // Client has no data any more, we quit
-                                       getTraceHandler()
-                                                       .trace(getName()
-                                                                       + ": client has data no more, stopping connection");
-                               }
-                       }
 
-                       @Override
-                       public void connect() {
-                               try {
-                                       super.connect();
-                               } finally {
-                                       count(-1);
+                                       send(rep);
                                }
                        }
                };
diff --git a/src/be/nikiroo/utils/test/SerialServerTest.java b/src/be/nikiroo/utils/test/SerialServerTest.java
new file mode 100644 (file)
index 0000000..885ee8c
--- /dev/null
@@ -0,0 +1,542 @@
+package be.nikiroo.utils.test;
+
+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;
+import be.nikiroo.utils.serial.server.ConnectActionServerString;
+import be.nikiroo.utils.serial.server.ServerBridge;
+import be.nikiroo.utils.serial.server.ServerObject;
+import be.nikiroo.utils.serial.server.ServerString;
+
+class SerialServerTest extends TestLauncher {
+       private TestLauncher createServerStringTestCases(final String[] args,
+                       final boolean ssl, final boolean bridge) {
+               final String ssls = (ssl ? "(ssl)" : "(plain text)");
+               final String bridges = (bridge ? " with bridge" : "");
+               TestLauncher series = new TestLauncher(
+                               "ServerString " + ssls + bridges, args);
+
+               series.addTest(new TestCase("Simple connection " + ssls) {
+                       @Override
+                       public void test() throws Exception {
+                               final String[] rec = new String[1];
+
+                               ServerString server = new ServerString(this.getName(), 0, ssl) {
+                                       @Override
+                                       protected String onRequest(
+                                                       ConnectActionServerString action,
+                                                       Version clientVersion, String data)
+                                                       throws Exception {
+                                               return null;
+                                       }
+                               };
+
+                               int port = server.getPort();
+                               assertEquals("A port should have been assigned", true, port > 0);
+
+                               server.start();
+
+                               ServerBridge br = null;
+                               if (bridge) {
+                                       br = new ServerBridge(0, ssl, "", port, ssl);
+
+                                       port = br.getPort();
+                                       assertEquals(
+                                                       "A port should have been assigned to the bridge",
+                                                       true, port > 0);
+
+                                       br.start();
+                               }
+
+                               try {
+                                       try {
+                                               new ConnectActionClientObject(null, port, ssl) {
+                                                       @Override
+                                                       public void action(Version serverVersion)
+                                                                       throws Exception {
+                                                               rec[0] = "ok";
+                                                       }
+                                               }.connect();
+                                       } finally {
+                                               server.stop();
+                                       }
+                               } finally {
+                                       if (br != null) {
+                                               br.stop();
+                                       }
+                               }
+
+                               assertNotNull("The client action was not run", rec[0]);
+                               assertEquals("ok", rec[0]);
+                       }
+               });
+
+               series.addTest(new TestCase("Simple exchange " + ssls) {
+                       final String[] sent = new String[1];
+                       final String[] recd = new String[1];
+                       final Exception[] err = new Exception[1];
+
+                       @Override
+                       public void test() throws Exception {
+                               ServerString server = new ServerString(this.getName(), 0, ssl) {
+                                       @Override
+                                       protected String onRequest(
+                                                       ConnectActionServerString action,
+                                                       Version clientVersion, String data)
+                                                       throws Exception {
+                                               sent[0] = data;
+                                               return "pong";
+                                       }
+
+                                       @Override
+                                       protected void onError(Exception e) {
+                                               super.onError(e);
+                                               err[0] = e;
+                                       }
+                               };
+
+                               int port = server.getPort();
+
+                               server.start();
+
+                               ServerBridge br = null;
+                               if (bridge) {
+                                       br = new ServerBridge(0, ssl, "", port, ssl);
+                                       port = br.getPort();
+                                       br.start();
+                               }
+
+                               try {
+                                       try {
+                                               new ConnectActionClientString(null, port, ssl) {
+                                                       @Override
+                                                       public void action(Version serverVersion)
+                                                                       throws Exception {
+                                                               recd[0] = send("ping");
+                                                       }
+                                               }.connect();
+                                       } finally {
+                                               server.stop();
+                                       }
+                               } finally {
+                                       if (br != null) {
+                                               br.stop();
+                                       }
+                               }
+
+                               if (err[0] != null) {
+                                       fail("An exception was thrown: " + err[0].getMessage());
+                               }
+
+                               assertEquals("ping", sent[0]);
+                               assertEquals("pong", recd[0]);
+                       }
+               });
+
+               series.addTest(new TestCase("Multiple exchanges " + ssls) {
+                       final String[] sent = new String[3];
+                       final String[] recd = new String[3];
+                       final Exception[] err = new Exception[1];
+
+                       @Override
+                       public void test() throws Exception {
+                               ServerString server = new ServerString(this.getName(), 0, ssl) {
+                                       @Override
+                                       protected String onRequest(
+                                                       ConnectActionServerString action,
+                                                       Version clientVersion, String data)
+                                                       throws Exception {
+                                               sent[0] = data;
+                                               action.send("pong");
+                                               sent[1] = action.rec();
+                                               return "pong2";
+                                       }
+
+                                       @Override
+                                       protected void onError(Exception e) {
+                                               super.onError(e);
+                                               err[0] = e;
+                                       }
+                               };
+
+                               int port = server.getPort();
+
+                               server.start();
+
+                               ServerBridge br = null;
+                               if (bridge) {
+                                       br = new ServerBridge(0, ssl, "", port, ssl);
+                                       port = br.getPort();
+                                       br.start();
+                               }
+
+                               try {
+                                       try {
+                                               new ConnectActionClientString(null, port, ssl) {
+                                                       @Override
+                                                       public void action(Version serverVersion)
+                                                                       throws Exception {
+                                                               recd[0] = send("ping");
+                                                               recd[1] = send("ping2");
+                                                       }
+                                               }.connect();
+                                       } finally {
+                                               server.stop();
+                                       }
+                               } finally {
+                                       if (br != null) {
+                                               br.stop();
+                                       }
+                               }
+
+                               if (err[0] != null) {
+                                       fail("An exception was thrown: " + err[0].getMessage());
+                               }
+
+                               assertEquals("ping", sent[0]);
+                               assertEquals("pong", recd[0]);
+                               assertEquals("ping2", sent[1]);
+                               assertEquals("pong2", recd[1]);
+                       }
+               });
+
+               series.addTest(new TestCase("Multiple call from client " + ssls) {
+                       final String[] sent = new String[3];
+                       final String[] recd = new String[3];
+                       final Exception[] err = new Exception[1];
+
+                       @Override
+                       public void test() throws Exception {
+                               ServerString server = new ServerString(this.getName(), 0, ssl) {
+                                       @Override
+                                       protected String onRequest(
+                                                       ConnectActionServerString action,
+                                                       Version clientVersion, String data)
+                                                       throws Exception {
+                                               sent[Integer.parseInt(data)] = data;
+                                               return "" + (Integer.parseInt(data) * 2);
+                                       }
+
+                                       @Override
+                                       protected void onError(Exception e) {
+                                               super.onError(e);
+                                               err[0] = e;
+                                       }
+                               };
+
+                               int port = server.getPort();
+
+                               server.start();
+
+                               ServerBridge br = null;
+                               if (bridge) {
+                                       br = new ServerBridge(0, ssl, "", port, ssl);
+                                       port = br.getPort();
+                                       br.start();
+                               }
+
+                               try {
+                                       try {
+                                               new ConnectActionClientString(null, port, ssl) {
+                                                       @Override
+                                                       public void action(Version serverVersion)
+                                                                       throws Exception {
+                                                               for (int i = 0; i < 3; i++) {
+                                                                       recd[i] = send("" + i);
+                                                               }
+                                                       }
+                                               }.connect();
+                                       } finally {
+                                               server.stop();
+                                       }
+                               } finally {
+                                       if (br != null) {
+                                               br.stop();
+                                       }
+                               }
+
+                               if (err[0] != null) {
+                                       fail("An exception was thrown: " + err[0].getMessage());
+                               }
+
+                               assertEquals("0", sent[0]);
+                               assertEquals("0", recd[0]);
+                               assertEquals("1", sent[1]);
+                               assertEquals("2", recd[1]);
+                               assertEquals("2", sent[2]);
+                               assertEquals("4", recd[2]);
+                       }
+               });
+
+               return series;
+       }
+
+       private TestLauncher createServerObjectTestCases(final String[] args,
+                       final boolean ssl, final boolean bridge) {
+               final String ssls = (ssl ? "(ssl)" : "(plain text)");
+               final String bridges = (bridge ? " with bridge" : "");
+               TestLauncher series = new TestLauncher(
+                               "ServerObject " + ssls + bridges, args);
+
+               series.addTest(new TestCase("Simple connection " + ssls) {
+                       @Override
+                       public void test() throws Exception {
+                               final Object[] rec = new Object[1];
+
+                               ServerObject server = new ServerObject(this.getName(), 0, ssl) {
+                                       @Override
+                                       protected Object onRequest(
+                                                       ConnectActionServerObject action,
+                                                       Version clientVersion, Object data)
+                                                       throws Exception {
+                                               return null;
+                                       }
+                               };
+
+                               int port = server.getPort();
+                               assertEquals("A port should have been assigned", true, port > 0);
+
+                               server.start();
+
+                               ServerBridge br = null;
+                               if (bridge) {
+                                       br = new ServerBridge(0, ssl, "", port, ssl);
+                                       port = br.getPort();
+                                       br.start();
+                               }
+
+                               try {
+                                       try {
+                                               new ConnectActionClientObject(null, port, ssl) {
+                                                       @Override
+                                                       public void action(Version serverVersion)
+                                                                       throws Exception {
+                                                               rec[0] = true;
+                                                       }
+                                               }.connect();
+                                       } finally {
+                                               server.stop();
+                                       }
+                               } finally {
+                                       if (br != null) {
+                                               br.stop();
+                                       }
+                               }
+
+                               assertNotNull("The client action was not run", rec[0]);
+                               assertEquals(true, (boolean) ((Boolean) rec[0]));
+                       }
+               });
+
+               series.addTest(new TestCase("Simple exchange " + ssls) {
+                       final Object[] sent = new Object[1];
+                       final Object[] recd = new Object[1];
+                       final Exception[] err = new Exception[1];
+
+                       @Override
+                       public void test() throws Exception {
+                               ServerObject server = new ServerObject(this.getName(), 0, ssl) {
+                                       @Override
+                                       protected Object onRequest(
+                                                       ConnectActionServerObject action,
+                                                       Version clientVersion, Object data)
+                                                       throws Exception {
+                                               sent[0] = data;
+                                               return "pong";
+                                       }
+
+                                       @Override
+                                       protected void onError(Exception e) {
+                                               super.onError(e);
+                                               err[0] = e;
+                                       }
+                               };
+
+                               int port = server.getPort();
+
+                               server.start();
+
+                               ServerBridge br = null;
+                               if (bridge) {
+                                       br = new ServerBridge(0, ssl, "", port, ssl);
+                                       port = br.getPort();
+                                       br.start();
+                               }
+
+                               try {
+                                       try {
+                                               new ConnectActionClientObject(null, port, ssl) {
+                                                       @Override
+                                                       public void action(Version serverVersion)
+                                                                       throws Exception {
+                                                               recd[0] = send("ping");
+                                                       }
+                                               }.connect();
+                                       } finally {
+                                               server.stop();
+                                       }
+                               } finally {
+                                       if (br != null) {
+                                               br.stop();
+                                       }
+                               }
+
+                               if (err[0] != null) {
+                                       fail("An exception was thrown: " + err[0].getMessage());
+                               }
+
+                               assertEquals("ping", sent[0]);
+                               assertEquals("pong", recd[0]);
+                       }
+               });
+
+               series.addTest(new TestCase("Multiple exchanges " + ssls) {
+                       final Object[] sent = new Object[3];
+                       final Object[] recd = new Object[3];
+                       final Exception[] err = new Exception[1];
+
+                       @Override
+                       public void test() throws Exception {
+                               ServerObject server = new ServerObject(this.getName(), 0, ssl) {
+                                       @Override
+                                       protected Object onRequest(
+                                                       ConnectActionServerObject action,
+                                                       Version clientVersion, Object data)
+                                                       throws Exception {
+                                               sent[0] = data;
+                                               action.send("pong");
+                                               sent[1] = action.rec();
+                                               return "pong2";
+                                       }
+
+                                       @Override
+                                       protected void onError(Exception e) {
+                                               super.onError(e);
+                                               err[0] = e;
+                                       }
+                               };
+
+                               int port = server.getPort();
+
+                               server.start();
+
+                               ServerBridge br = null;
+                               if (bridge) {
+                                       br = new ServerBridge(0, ssl, "", port, ssl);
+                                       port = br.getPort();
+                                       br.start();
+                               }
+
+                               try {
+                                       try {
+                                               new ConnectActionClientObject(null, port, ssl) {
+                                                       @Override
+                                                       public void action(Version serverVersion)
+                                                                       throws Exception {
+                                                               recd[0] = send("ping");
+                                                               recd[1] = send("ping2");
+                                                       }
+                                               }.connect();
+                                       } finally {
+                                               server.stop();
+                                       }
+                               } finally {
+                                       if (br != null) {
+                                               br.stop();
+                                       }
+                               }
+
+                               if (err[0] != null) {
+                                       fail("An exception was thrown: " + err[0].getMessage());
+                               }
+
+                               assertEquals("ping", sent[0]);
+                               assertEquals("pong", recd[0]);
+                               assertEquals("ping2", sent[1]);
+                               assertEquals("pong2", recd[1]);
+                       }
+               });
+
+               series.addTest(new TestCase("Multiple call from client " + ssls) {
+                       final Object[] sent = new Object[3];
+                       final Object[] recd = new Object[3];
+                       final Exception[] err = new Exception[1];
+
+                       @Override
+                       public void test() throws Exception {
+                               ServerObject server = new ServerObject(this.getName(), 0, ssl) {
+                                       @Override
+                                       protected Object onRequest(
+                                                       ConnectActionServerObject action,
+                                                       Version clientVersion, Object data)
+                                                       throws Exception {
+                                               sent[(Integer) data] = data;
+                                               return ((Integer) data) * 2;
+                                       }
+
+                                       @Override
+                                       protected void onError(Exception e) {
+                                               super.onError(e);
+                                               err[0] = e;
+                                       }
+                               };
+
+                               int port = server.getPort();
+
+                               server.start();
+
+                               ServerBridge br = null;
+                               if (bridge) {
+                                       br = new ServerBridge(0, ssl, "", port, ssl);
+                                       port = br.getPort();
+                                       br.start();
+                               }
+
+                               try {
+                                       try {
+                                               new ConnectActionClientObject(null, port, ssl) {
+                                                       @Override
+                                                       public void action(Version serverVersion)
+                                                                       throws Exception {
+                                                               for (int i = 0; i < 3; i++) {
+                                                                       recd[i] = send(i);
+                                                               }
+                                                       }
+                                               }.connect();
+                                       } finally {
+                                               server.stop();
+                                       }
+                               } finally {
+                                       if (br != null) {
+                                               br.stop();
+                                       }
+                               }
+
+                               if (err[0] != null) {
+                                       fail("An exception was thrown: " + err[0].getMessage());
+                               }
+
+                               assertEquals(0, sent[0]);
+                               assertEquals(0, recd[0]);
+                               assertEquals(1, sent[1]);
+                               assertEquals(2, recd[1]);
+                               assertEquals(2, sent[2]);
+                               assertEquals(4, recd[2]);
+                       }
+               });
+
+               return series;
+       }
+
+       public SerialServerTest(String[] args) {
+               super("SerialServer test", args);
+
+               for (boolean ssl : new Boolean[] { false, true }) {
+                       for (boolean bridge : new Boolean[] { false, true }) {
+                               addSeries(createServerObjectTestCases(args, ssl, bridge));
+                               addSeries(createServerStringTestCases(args, ssl, bridge));
+                       }
+               }
+       }
+}
index b1b5c8b53bc3a823f5cfd142c5d628a76122d314..bb62144ac94ebe8781ce4470a949c2a56fe6e763 100644 (file)
 package be.nikiroo.utils.test;
 
-import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.Exporter;
 import be.nikiroo.utils.serial.Importer;
-import be.nikiroo.utils.serial.server.ConnectActionClientObject;
-import be.nikiroo.utils.serial.server.ConnectActionClientString;
-import be.nikiroo.utils.serial.server.ConnectActionServerObject;
-import be.nikiroo.utils.serial.server.ConnectActionServerString;
-import be.nikiroo.utils.serial.server.ServerObject;
-import be.nikiroo.utils.serial.server.ServerString;
 
 class SerialTest extends TestLauncher {
-       private SerialTest() {
-               super("Serial test", null);
-       }
-
-       private TestLauncher createServerStringTestCases(final String[] args,
-                       final boolean ssl) {
-               final String ssls = (ssl ? "(ssl)" : "(plain text)");
-               TestLauncher series = new TestLauncher("Client/Server " + ssls, args);
-
-               series.addTest(new TestCase("Simple connection " + ssls) {
-                       @Override
-                       public void test() throws Exception {
-                               final String[] rec = new String[1];
-
-                               ServerString server = new ServerString(this.getName(), 0, ssl) {
-                                       @Override
-                                       protected String onRequest(
-                                                       ConnectActionServerString action,
-                                                       Version clientVersion, String data)
-                                                       throws Exception {
-                                               return null;
-                                       }
-                               };
-
-                               assertEquals("A port should have been assigned", true,
-                                               server.getPort() > 0);
-
-                               server.start();
-
-                               try {
-                                       new ConnectActionClientObject(null, server.getPort(), ssl) {
-                                               @Override
-                                               public void action(Version serverVersion)
-                                                               throws Exception {
-                                                       rec[0] = "ok";
-                                               }
-                                       }.connect();
-                               } finally {
-                                       server.stop();
-                               }
-
-                               assertNotNull("The client action was not run", rec[0]);
-                               assertEquals("ok", rec[0]);
-                       }
-               });
-
-               series.addTest(new TestCase("Simple exchange " + ssls) {
-                       final String[] sent = new String[1];
-                       final String[] recd = new String[1];
-                       final Exception[] err = new Exception[1];
-
-                       @Override
-                       public void test() throws Exception {
-                               ServerString server = new ServerString(this.getName(), 0, ssl) {
-                                       @Override
-                                       protected String onRequest(
-                                                       ConnectActionServerString action,
-                                                       Version clientVersion, String data)
-                                                       throws Exception {
-                                               sent[0] = data;
-                                               return "pong";
-                                       }
-
-                                       @Override
-                                       protected void onError(Exception e) {
-                                               super.onError(e);
-                                               err[0] = e;
-                                       }
-                               };
-
-                               server.start();
-
-                               try {
-                                       new ConnectActionClientString(null, server.getPort(), ssl) {
-                                               @Override
-                                               public void action(Version serverVersion)
-                                                               throws Exception {
-                                                       recd[0] = send("ping");
-                                               }
-                                       }.connect();
-                               } finally {
-                                       server.stop();
-                               }
-
-                               if (err[0] != null) {
-                                       fail("An exception was thrown: " + err[0].getMessage());
-                               }
-
-                               assertEquals("ping", sent[0]);
-                               assertEquals("pong", recd[0]);
-                       }
-               });
-
-               series.addTest(new TestCase("Multiple exchanges " + ssls) {
-                       final String[] sent = new String[3];
-                       final String[] recd = new String[3];
-                       final Exception[] err = new Exception[1];
-
-                       @Override
-                       public void test() throws Exception {
-                               ServerString server = new ServerString(this.getName(), 0, ssl) {
-                                       @Override
-                                       protected String onRequest(
-                                                       ConnectActionServerString action,
-                                                       Version clientVersion, String data)
-                                                       throws Exception {
-                                               sent[0] = data;
-                                               action.send("pong");
-                                               sent[1] = action.rec();
-                                               return "pong2";
-                                       }
-
-                                       @Override
-                                       protected void onError(Exception e) {
-                                               super.onError(e);
-                                               err[0] = e;
-                                       }
-                               };
-
-                               server.start();
-
-                               try {
-                                       new ConnectActionClientString(null, server.getPort(), ssl) {
-                                               @Override
-                                               public void action(Version serverVersion)
-                                                               throws Exception {
-                                                       recd[0] = send("ping");
-                                                       recd[1] = send("ping2");
-                                               }
-                                       }.connect();
-                               } finally {
-                                       server.stop();
-                               }
-
-                               if (err[0] != null) {
-                                       fail("An exception was thrown: " + err[0].getMessage());
-                               }
-
-                               assertEquals("ping", sent[0]);
-                               assertEquals("pong", recd[0]);
-                               assertEquals("ping2", sent[1]);
-                               assertEquals("pong2", recd[1]);
-                       }
-               });
-
-               series.addTest(new TestCase("Multiple call from client " + ssls) {
-                       final String[] sent = new String[3];
-                       final String[] recd = new String[3];
-                       final Exception[] err = new Exception[1];
-
-                       @Override
-                       public void test() throws Exception {
-                               ServerString server = new ServerString(this.getName(), 0, ssl) {
-                                       @Override
-                                       protected String onRequest(
-                                                       ConnectActionServerString action,
-                                                       Version clientVersion, String data)
-                                                       throws Exception {
-                                               sent[Integer.parseInt(data)] = data;
-                                               return "" + (Integer.parseInt(data) * 2);
-                                       }
-
-                                       @Override
-                                       protected void onError(Exception e) {
-                                               super.onError(e);
-                                               err[0] = e;
-                                       }
-                               };
-
-                               server.start();
-
-                               try {
-                                       new ConnectActionClientString(null, server.getPort(), ssl) {
-                                               @Override
-                                               public void action(Version serverVersion)
-                                                               throws Exception {
-                                                       for (int i = 0; i < 3; i++) {
-                                                               recd[i] = send("" + i);
-                                                       }
-                                               }
-                                       }.connect();
-                               } finally {
-                                       server.stop();
-                               }
-
-                               if (err[0] != null) {
-                                       fail("An exception was thrown: " + err[0].getMessage());
-                               }
-
-                               assertEquals("0", sent[0]);
-                               assertEquals("0", recd[0]);
-                               assertEquals("1", sent[1]);
-                               assertEquals("2", recd[1]);
-                               assertEquals("2", sent[2]);
-                               assertEquals("4", recd[2]);
-                       }
-               });
-
-               return series;
-       }
-
-       private TestLauncher createServerObjectTestCases(final String[] args,
-                       final boolean ssl) {
-               final String ssls = (ssl ? "(ssl)" : "(plain text)");
-               TestLauncher series = new TestLauncher("Client/Server " + ssls, args);
-
-               series.addTest(new TestCase("Simple connection " + ssls) {
-                       @Override
-                       public void test() throws Exception {
-                               final Object[] rec = new Object[1];
-
-                               ServerObject server = new ServerObject(this.getName(), 0, ssl) {
-                                       @Override
-                                       protected Object onRequest(
-                                                       ConnectActionServerObject action,
-                                                       Version clientVersion, Object data)
-                                                       throws Exception {
-                                               return null;
-                                       }
-                               };
-
-                               assertEquals("A port should have been assigned", true,
-                                               server.getPort() > 0);
-
-                               server.start();
-
-                               try {
-                                       new ConnectActionClientObject(null, server.getPort(), ssl) {
-                                               @Override
-                                               public void action(Version serverVersion)
-                                                               throws Exception {
-                                                       rec[0] = true;
-                                               }
-                                       }.connect();
-                               } finally {
-                                       server.stop();
-                               }
-
-                               assertNotNull("The client action was not run", rec[0]);
-                               assertEquals(true, (boolean) ((Boolean) rec[0]));
-                       }
-               });
-
-               series.addTest(new TestCase("Simple exchange " + ssls) {
-                       final Object[] sent = new Object[1];
-                       final Object[] recd = new Object[1];
-                       final Exception[] err = new Exception[1];
-
-                       @Override
-                       public void test() throws Exception {
-                               ServerObject server = new ServerObject(this.getName(), 0, ssl) {
-                                       @Override
-                                       protected Object onRequest(
-                                                       ConnectActionServerObject action,
-                                                       Version clientVersion, Object data)
-                                                       throws Exception {
-                                               sent[0] = data;
-                                               return "pong";
-                                       }
-
-                                       @Override
-                                       protected void onError(Exception e) {
-                                               super.onError(e);
-                                               err[0] = e;
-                                       }
-                               };
-
-                               server.start();
-
-                               try {
-                                       new ConnectActionClientObject(null, server.getPort(), ssl) {
-                                               @Override
-                                               public void action(Version serverVersion)
-                                                               throws Exception {
-                                                       recd[0] = send("ping");
-                                               }
-                                       }.connect();
-                               } finally {
-                                       server.stop();
-                               }
-
-                               if (err[0] != null) {
-                                       fail("An exception was thrown: " + err[0].getMessage());
-                               }
-
-                               assertEquals("ping", sent[0]);
-                               assertEquals("pong", recd[0]);
-                       }
-               });
-
-               series.addTest(new TestCase("Multiple exchanges " + ssls) {
-                       final Object[] sent = new Object[3];
-                       final Object[] recd = new Object[3];
-                       final Exception[] err = new Exception[1];
-
-                       @Override
-                       public void test() throws Exception {
-                               ServerObject server = new ServerObject(this.getName(), 0, ssl) {
-                                       @Override
-                                       protected Object onRequest(
-                                                       ConnectActionServerObject action,
-                                                       Version clientVersion, Object data)
-                                                       throws Exception {
-                                               sent[0] = data;
-                                               action.send("pong");
-                                               sent[1] = action.rec();
-                                               return "pong2";
-                                       }
-
-                                       @Override
-                                       protected void onError(Exception e) {
-                                               super.onError(e);
-                                               err[0] = e;
-                                       }
-                               };
-
-                               server.start();
-
-                               try {
-                                       new ConnectActionClientObject(null, server.getPort(), ssl) {
-                                               @Override
-                                               public void action(Version serverVersion)
-                                                               throws Exception {
-                                                       recd[0] = send("ping");
-                                                       recd[1] = send("ping2");
-                                               }
-                                       }.connect();
-                               } finally {
-                                       server.stop();
-                               }
-
-                               if (err[0] != null) {
-                                       fail("An exception was thrown: " + err[0].getMessage());
-                               }
-
-                               assertEquals("ping", sent[0]);
-                               assertEquals("pong", recd[0]);
-                               assertEquals("ping2", sent[1]);
-                               assertEquals("pong2", recd[1]);
-                       }
-               });
-
-               series.addTest(new TestCase("Multiple call from client " + ssls) {
-                       final Object[] sent = new Object[3];
-                       final Object[] recd = new Object[3];
-                       final Exception[] err = new Exception[1];
-
-                       @Override
-                       public void test() throws Exception {
-                               ServerObject server = new ServerObject(this.getName(), 0, ssl) {
-                                       @Override
-                                       protected Object onRequest(
-                                                       ConnectActionServerObject action,
-                                                       Version clientVersion, Object data)
-                                                       throws Exception {
-                                               sent[(Integer) data] = data;
-                                               return ((Integer) data) * 2;
-                                       }
-
-                                       @Override
-                                       protected void onError(Exception e) {
-                                               super.onError(e);
-                                               err[0] = e;
-                                       }
-                               };
-
-                               server.start();
-
-                               try {
-                                       new ConnectActionClientObject(null, server.getPort(), ssl) {
-                                               @Override
-                                               public void action(Version serverVersion)
-                                                               throws Exception {
-                                                       for (int i = 0; i < 3; i++) {
-                                                               recd[i] = send(i);
-                                                       }
-                                               }
-                                       }.connect();
-                               } finally {
-                                       server.stop();
-                               }
-
-                               if (err[0] != null) {
-                                       fail("An exception was thrown: " + err[0].getMessage());
-                               }
-
-                               assertEquals(0, sent[0]);
-                               assertEquals(0, recd[0]);
-                               assertEquals(1, sent[1]);
-                               assertEquals(2, recd[1]);
-                               assertEquals(2, sent[2]);
-                               assertEquals(4, recd[2]);
-                       }
-               });
-
-               return series;
-       }
-
        public SerialTest(String[] args) {
                super("Serial test", args);
 
-               addSeries(createServerObjectTestCases(args, false));
-
-               addSeries(createServerObjectTestCases(args, true));
-
-               addSeries(createServerStringTestCases(args, false));
-
-               addSeries(createServerStringTestCases(args, true));
-
                addTest(new TestCase("Simple class Import/Export") {
                        @Override
                        public void test() throws Exception {
index 16829559e6453525e390c8266f3e01cf3b1fa3ea..4f20182093dc08eb7dbb5ac49c7d094d264f8d93 100644 (file)
@@ -21,6 +21,7 @@ public class Test extends TestLauncher {
                addSeries(new IOUtilsTest(args));
                addSeries(new VersionTest(args));
                addSeries(new SerialTest(args));
+               addSeries(new SerialServerTest(args));
                addSeries(new StringUtilsTest(args));
        }