From d463d2663337bb11aaeab48a06d2b3091c3b7830 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Tue, 28 Nov 2017 08:34:36 +0100 Subject: [PATCH] Revert Server behaviour to what it was : - old code will now work again - we now can use run() directly if we want a synchronous call, which used to be for internal uses only (but public!) --- src/be/nikiroo/utils/serial/Server.java | 161 +++++++++++----------- src/be/nikiroo/utils/test/SerialTest.java | 10 +- 2 files changed, 87 insertions(+), 84 deletions(-) diff --git a/src/be/nikiroo/utils/serial/Server.java b/src/be/nikiroo/utils/serial/Server.java index b2771863..934be337 100644 --- a/src/be/nikiroo/utils/serial/Server.java +++ b/src/be/nikiroo/utils/serial/Server.java @@ -126,43 +126,104 @@ abstract public class Server implements Runnable { *

* Can only be called once. *

- * This call is synchronous. + * This call is asynchronous, and will just start a new {@link Thread} on + * itself (see {@link Server#run()}). */ public void start() { - start(true); + new Thread(this).start(); } /** * Start the server (listen on the network for new connections). *

* Can only be called once. - * - * @param wait - * TRUE for synchronous, FALSE for asynchronous + *

+ * You may call it via {@link Server#start()} for an asynchronous call, too. */ - public void start(boolean wait) { - boolean ok = false; + @Override + public void run() { + ServerSocket ss = null; + boolean alreadyStarted = false; synchronized (lock) { + ss = this.ss; if (!started && ss != null) { - ok = true; started = true; - if (!wait) { - new Thread(this).start(); - } + } else { + alreadyStarted = started; } } - 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"); - } else { + if (alreadyStarted) { tracer.error(name + ": cannot start server on port " + port + ", it is already started"); + return; + } + + if (ss == null) { + tracer.error(name + ": cannot start server on port " + port + + ", it has already been used"); + return; + } + + try { + tracer.trace(name + ": server starting on port " + port); + + while (started && !exiting) { + count(1); + Socket s = ss.accept(); + new ConnectActionServer(s) { + @Override + public void action(Version clientVersion) throws Exception { + try { + for (Object data = rec(); true; data = rec()) { + Object rep = null; + try { + rep = onRequest(this, clientVersion, data); + } catch (Exception e) { + onError(e); + } + send(rep); + } + } catch (NullPointerException e) { + // Client has no data any more, we quit + tracer.trace(name + + ": client has data no more, stopping connection"); + } + } + + @Override + public void connect() { + try { + super.connect(); + } finally { + count(-1); + } + } + }.connectAsync(); + } + + // Will be covered by @link{Server#stop(long)} for timeouts + while (counter > 0) { + Thread.sleep(10); + } + } catch (Exception e) { + if (counter > 0) { + onError(e); + } + } finally { + try { + ss.close(); + } catch (Exception e) { + onError(e); + } + + this.ss = null; + + started = false; + exiting = false; + counter = 0; + + tracer.trace(name + ": client terminated on port " + port); } } @@ -205,6 +266,7 @@ abstract public class Server implements Runnable { * or 0 for "no timeout" */ private void stop(long timeout) { + tracer.trace(name + ": server stopping on port " + port); synchronized (lock) { if (started && !exiting) { exiting = true; @@ -235,65 +297,6 @@ abstract public class Server implements Runnable { } } - @Override - public void run() { - try { - while (started && !exiting) { - count(1); - Socket s = ss.accept(); - new ConnectActionServer(s) { - @Override - public void action(Version clientVersion) throws Exception { - try { - for (Object data = rec(); true; data = rec()) { - Object rep = null; - try { - rep = onRequest(this, clientVersion, data); - } catch (Exception e) { - onError(e); - } - send(rep); - } - } catch (NullPointerException e) { - // Client has no data any more, we quit - tracer.trace("Client has data no more, stopping connection"); - } - } - - @Override - public void connect() { - try { - super.connect(); - } finally { - count(-1); - } - } - }.connectAsync(); - } - - // Will be covered by @link{Server#stop(long)} for timeouts - while (counter > 0) { - Thread.sleep(10); - } - } catch (Exception e) { - if (counter > 0) { - onError(e); - } - } finally { - try { - ss.close(); - } catch (Exception e) { - onError(e); - } - - ss = null; - - started = false; - exiting = false; - counter = 0; - } - } - /** * This is the method that is called on each client request. *

diff --git a/src/be/nikiroo/utils/test/SerialTest.java b/src/be/nikiroo/utils/test/SerialTest.java index e5bafa9c..e3171c21 100644 --- a/src/be/nikiroo/utils/test/SerialTest.java +++ b/src/be/nikiroo/utils/test/SerialTest.java @@ -38,7 +38,7 @@ class SerialTest extends TestLauncher { // TODO: remove server.setTraceHandler(new TraceHandler(true, true, true)); - server.start(false); + server.start(); try { new ConnectActionClient(null, server.getPort(), ssl) { @@ -80,7 +80,7 @@ class SerialTest extends TestLauncher { } }; - server.start(false); + server.start(); try { new ConnectActionClient(null, server.getPort(), ssl) { @@ -117,7 +117,7 @@ class SerialTest extends TestLauncher { throws Exception { sent[0] = data; action.send("pong"); - sent[1] = action.flush(); + sent[1] = action.rec(); return "pong2"; } @@ -128,7 +128,7 @@ class SerialTest extends TestLauncher { } }; - server.start(false); + server.start(); try { new ConnectActionClient(null, server.getPort(), ssl) { @@ -177,7 +177,7 @@ class SerialTest extends TestLauncher { } }; - server.start(false); + server.start(); try { new ConnectActionClient(null, server.getPort(), ssl) { -- 2.27.0