From 3ac978bdc1d6bedc7e1d96355db92711004a3988 Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Thu, 9 Jun 2016 19:09:17 -0400 Subject: [PATCH] Correction of message key test long run command --- .../socket/SocketConsoleApplicationShell.java | 32 +++++++++++-------- .../gclc/socket/ConsoleTestApplication.java | 17 ++++++++++ gclc-swt/pom.xml | 6 ++-- .../bigeon/gclc/command/CommandProvider.java | 2 +- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java b/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java index 47f620b..7f9522b 100644 --- a/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java +++ b/gclc-socket/src/main/java/fr/bigeon/gclc/socket/SocketConsoleApplicationShell.java @@ -144,9 +144,6 @@ public class SocketConsoleApplicationShell implements Runnable { public void run() { try (ServerSocket actualServerSocket = new ServerSocket(port)) { this.serverSocket = actualServerSocket; - final ConsoleRunnable runnable = new ConsoleRunnable(app, - promptingLock); - final Thread appTh = new Thread(runnable); running = true; try (PipedOutputStream outStream = new PipedOutputStream(); BufferedWriter writer = new BufferedWriter( @@ -156,11 +153,13 @@ public class SocketConsoleApplicationShell implements Runnable { consoleInput); BufferedReader inBuf = new BufferedReader(isr);) { consoleManager.setInput(inBuf); - runSokectServer(appTh, writer); + runSokectServer(writer); // Close the application // Pass command to application - writer.write(applicationShutdown + EOL); - writer.flush(); + if (app.isRunning()) { + writer.write(applicationShutdown + EOL); + writer.flush(); + } } } } catch (final IOException e) { @@ -172,20 +171,27 @@ public class SocketConsoleApplicationShell implements Runnable { /** @param appTh the application thread * @param writer the writer to the application * @throws IOException if the communication with the client failed */ - private void runSokectServer(Thread appTh, - BufferedWriter writer) throws IOException { + private void runSokectServer(BufferedWriter writer) throws IOException { + final ConsoleRunnable runnable = new ConsoleRunnable(app, + promptingLock); + Thread appThOld = null; + Thread appThNext = new Thread(runnable); while (running) { try (Socket clientSocket = serverSocket.accept(); PrintWriter out = new PrintWriter( clientSocket.getOutputStream(), true); - BufferedReader in = new BufferedReader(new InputStreamReader( - clientSocket.getInputStream()));) { + InputStreamReader isr = new InputStreamReader( + clientSocket.getInputStream()); + BufferedReader in = new BufferedReader(isr);) { // this is not threaded to avoid several clients at the same // time consoleManager.setOutput(out); // Initiate application - if (!appTh.isAlive()) { - appTh.start(); + if (appThOld == null || !appThOld.isAlive()) { + appThNext.start(); + // Prepare next start + appThOld = appThNext; + appThNext = new Thread(runnable); } else { out.println("Reconnected"); //$NON-NLS-1$ } @@ -249,7 +255,7 @@ public class SocketConsoleApplicationShell implements Runnable { private void communicateLoop(BufferedReader in, BufferedWriter writer) throws IOException { String ln; - while (running && (ln = in.readLine()) != null) { + while (app.isRunning() && (ln = in.readLine()) != null) { if (ln.equals(close)) { break; } diff --git a/gclc-socket/src/test/java/fr/bigeon/gclc/socket/ConsoleTestApplication.java b/gclc-socket/src/test/java/fr/bigeon/gclc/socket/ConsoleTestApplication.java index c6ea6bf..d1a7f97 100644 --- a/gclc-socket/src/test/java/fr/bigeon/gclc/socket/ConsoleTestApplication.java +++ b/gclc-socket/src/test/java/fr/bigeon/gclc/socket/ConsoleTestApplication.java @@ -76,6 +76,23 @@ public class ConsoleTestApplication extends ConsoleApplication { } } }); + add(new Command("long") { + + @Override + public String tip() { + return "A long run test command"; + } + + @Override + public void execute(String... args) throws CommandRunException { + try { + Thread.sleep(2000); + manager.println("Test command ran fine"); + } catch (IOException | InterruptedException e) { + throw new CommandRunException("manager closed", e); + } + } + }); } catch (final InvalidCommandName e) { e.printStackTrace(); } diff --git a/gclc-swt/pom.xml b/gclc-swt/pom.xml index 1e38a78..6207074 100644 --- a/gclc-swt/pom.xml +++ b/gclc-swt/pom.xml @@ -32,12 +32,10 @@ - + 4.0.0 gclc-swt - 1.0.2-SNAPSHOT + 1.0.3-SNAPSHOT jar http://www.bigeon.fr/emmanuel diff --git a/gclc/src/main/java/fr/bigeon/gclc/command/CommandProvider.java b/gclc/src/main/java/fr/bigeon/gclc/command/CommandProvider.java index 2f97d17..503bce9 100644 --- a/gclc/src/main/java/fr/bigeon/gclc/command/CommandProvider.java +++ b/gclc/src/main/java/fr/bigeon/gclc/command/CommandProvider.java @@ -83,7 +83,7 @@ public class CommandProvider implements ICommandProvider { } } throw new CommandRunException( - Messages.getString("CommandProvider.unrecognized0", cmd)); //$NON-NLS-1$ + Messages.getString("CommandProvider.unrecognized", cmd)); //$NON-NLS-1$ } /* (non-Javadoc)