Change Server behaviour for start():
authorNiki Roo <niki@nikiroo.be>
Mon, 27 Nov 2017 22:24:20 +0000 (23:24 +0100)
committerNiki Roo <niki@nikiroo.be>
Mon, 27 Nov 2017 22:24:20 +0000 (23:24 +0100)
- start() is now synchronous, a new start(boolean wait) is there

src/be/nikiroo/utils/serial/Server.java
src/be/nikiroo/utils/test/SerialTest.java

index f2a01c5deaa6a2db2f970d041f5b0b8178444e2c..b2771863f9be0aab58626c50f39e768a88d3e721 100644 (file)
@@ -125,19 +125,38 @@ abstract public class Server implements Runnable {
         * Start the server (listen on the network for new connections).
         * <p>
         * Can only be called once.
+        * <p>
+        * This call is synchronous.
         */
        public void start() {
+               start(true);
+       }
+
+       /**
+        * Start the server (listen on the network for new connections).
+        * <p>
+        * Can only be called once.
+        * 
+        * @param wait
+        *            TRUE for synchronous, FALSE for asynchronous
+        */
+       public void start(boolean wait) {
                boolean ok = false;
                synchronized (lock) {
                        if (!started && ss != null) {
-                               started = true;
-                               new Thread(this).start();
                                ok = true;
+                               started = true;
+                               if (!wait) {
+                                       new Thread(this).start();
+                               }
                        }
                }
 
                if (ok) {
                        tracer.trace(name + ": server started on port " + port);
+                       if (wait) {
+                               run();
+                       }
                } else if (ss == null) {
                        tracer.error(name + ": cannot start server on port " + port
                                        + ", it has already been used");
index e66bda8df9444b1706c71b1ec0e39b1a5c1ca0dd..e5bafa9cf76c498e4ce70938df2bad7495300457 100644 (file)
@@ -38,7 +38,7 @@ class SerialTest extends TestLauncher {
                                // TODO: remove
                                server.setTraceHandler(new TraceHandler(true, true, true));
 
-                               server.start();
+                               server.start(false);
 
                                try {
                                        new ConnectActionClient(null, server.getPort(), ssl) {
@@ -80,7 +80,7 @@ class SerialTest extends TestLauncher {
                                        }
                                };
 
-                               server.start();
+                               server.start(false);
 
                                try {
                                        new ConnectActionClient(null, server.getPort(), ssl) {
@@ -128,7 +128,7 @@ class SerialTest extends TestLauncher {
                                        }
                                };
 
-                               server.start();
+                               server.start(false);
 
                                try {
                                        new ConnectActionClient(null, server.getPort(), ssl) {
@@ -177,7 +177,7 @@ class SerialTest extends TestLauncher {
                                        }
                                };
 
-                               server.start();
+                               server.start(false);
 
                                try {
                                        new ConnectActionClient(null, server.getPort(), ssl) {