Change Server behaviour for start():
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / Server.java
index e15680e5862d799291a732c52e7877b5fcad6c7a..b2771863f9be0aab58626c50f39e768a88d3e721 100644 (file)
@@ -114,6 +114,8 @@ abstract public class Server implements Runnable {
 
        /**
         * Return the assigned port.
+        * 
+        * @return the assigned port
         */
        public int getPort() {
                return port;
@@ -123,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");
@@ -189,13 +210,8 @@ abstract public class Server implements Runnable {
                                exiting = true;
 
                                try {
-                                       new ConnectActionClient(createSocket(null, port, ssl)) {
-                                               @Override
-                                               public void action(Version serverVersion)
-                                                               throws Exception {
-                                               }
-                                       }.connect();
-
+                                       new ConnectActionClient(createSocket(null, port, ssl))
+                                                       .connect();
                                        long time = 0;
                                        while (ss != null && timeout > 0 && timeout > time) {
                                                Thread.sleep(10);
@@ -222,17 +238,14 @@ abstract public class Server implements Runnable {
        @Override
        public void run() {
                try {
-                       tracer.trace(name + ": server starting on port " + port);
                        while (started && !exiting) {
                                count(1);
                                Socket s = ss.accept();
                                new ConnectActionServer(s) {
-                                       private Version clientVersion = new Version();
-
                                        @Override
-                                       public void action(Version dummy) throws Exception {
+                                       public void action(Version clientVersion) throws Exception {
                                                try {
-                                                       for (Object data = flush(); true; data = flush()) {
+                                                       for (Object data = rec(); true; data = rec()) {
                                                                Object rep = null;
                                                                try {
                                                                        rep = onRequest(this, clientVersion, data);
@@ -243,6 +256,7 @@ abstract public class Server implements Runnable {
                                                        }
                                                } catch (NullPointerException e) {
                                                        // Client has no data any more, we quit
+                                                       tracer.trace("Client has data no more, stopping connection");
                                                }
                                        }
 
@@ -254,11 +268,6 @@ abstract public class Server implements Runnable {
                                                        count(-1);
                                                }
                                        }
-
-                                       @Override
-                                       protected void onClientVersionReceived(Version clientVersion) {
-                                               this.clientVersion = clientVersion;
-                                       }
                                }.connectAsync();
                        }