X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2FServer.java;h=b2771863f9be0aab58626c50f39e768a88d3e721;hb=b08aa70e2045404d935895fafac6d2fb69f9b92f;hp=e15680e5862d799291a732c52e7877b5fcad6c7a;hpb=08a58812f12617289463b00161c98d7c59490bf2;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/serial/Server.java b/src/be/nikiroo/utils/serial/Server.java index e15680e..b277186 100644 --- a/src/be/nikiroo/utils/serial/Server.java +++ b/src/be/nikiroo/utils/serial/Server.java @@ -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). *

* Can only be called once. + *

+ * This call is synchronous. */ public void start() { + start(true); + } + + /** + * Start the server (listen on the network for new connections). + *

+ * 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(); }