From 248c82cf50a20e605a18075f197950b85b58b8fe Mon Sep 17 00:00:00 2001 From: Emmanuel Bigeon Date: Tue, 6 Dec 2016 10:38:16 -0500 Subject: [PATCH] Systematic end of communication phase by sending 'Bye.'. modify tests. Signed-off-by: Emmanuel Bigeon --- .../socket/SocketConsoleApplicationShell.java | 37 ++++++------ .../socket/SocketConsoleApplicationTest.java | 57 +++++++++---------- 2 files changed, 44 insertions(+), 50 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 8705048..4a47e8e 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 @@ -95,12 +95,12 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable { @Override public void run() { try { - while (!socket.isOutputShutdown()) { - while (!socket.isOutputShutdown() && + while (!socket.isClosed()) { + while (!socket.isClosed() && !consoleManager.available()) { waitASec(); } - if (socket.isOutputShutdown()) { + if (socket.isClosed()) { return; } String m = consoleManager.readNextLine(); @@ -266,7 +266,6 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable { communicateOnce(socket, in); } else { communicateLoop(socket, in); - } } @@ -279,8 +278,21 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable { Thread th = new Thread(reading, "gclcToApp"); //$NON-NLS-1$ th.start(); communicationContent(reading); + doEndCommunication(reading); + } + + /** @param reading the reading runnable + * @throws IOException if the end of communication failed */ + private void doEndCommunication(ReadingRunnable reading) throws IOException { reading.setRunning(false); - socket.shutdownOutput(); + Thread wait = consoleManager.getWaitForDelivery("Bye."); //$NON-NLS-1$ + consoleManager.println("Bye."); //$NON-NLS-1$ + try { + wait.join(); + } catch (InterruptedException e) { + LOGGER.warning("The Bye wait was interrupted."); //$NON-NLS-1$ + LOGGER.log(Level.FINE, "An interruption occured", e); //$NON-NLS-1$ + } } /** @param socket the socket @@ -294,12 +306,7 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable { while (app.isRunning() && communicationContent(reading)) { // keep on going } - reading.setRunning(false); - while (consoleManager.available()) { - waitASec(); - } - socket.shutdownOutput(); - + doEndCommunication(reading); } /** @param reading the reading @@ -322,14 +329,6 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable { } String ln = reading.getMessage(); if (ln.equals(close)) { - Thread wait = consoleManager.getWaitForDelivery("Bye."); //$NON-NLS-1$ - consoleManager.println("Bye."); //$NON-NLS-1$ - try { - wait.join(); - } catch (InterruptedException e) { - LOGGER.warning("The Bye wait was interrupted."); //$NON-NLS-1$ - LOGGER.log(Level.FINE, "An interruption occured", e); //$NON-NLS-1$ - } return false; } // Pass command to application diff --git a/gclc-socket/src/test/java/fr/bigeon/gclc/socket/SocketConsoleApplicationTest.java b/gclc-socket/src/test/java/fr/bigeon/gclc/socket/SocketConsoleApplicationTest.java index a467c10..68fb45f 100644 --- a/gclc-socket/src/test/java/fr/bigeon/gclc/socket/SocketConsoleApplicationTest.java +++ b/gclc-socket/src/test/java/fr/bigeon/gclc/socket/SocketConsoleApplicationTest.java @@ -89,17 +89,10 @@ public class SocketConsoleApplicationTest { String[] cmds = {"help", "toto", "test", "bye"}; while ((fromServer = in.readLine()) != null) { i++; - LOGGER.fine("Server: \n" + fromServer); - if (fromServer.equals("Bye.")) { + fromServer = consumeToPrompt(fromServer, in); + if (fromServer == null || fromServer.equals("Bye.")) { break; } - while (fromServer != null && !fromServer.equals("> ")) { - fromServer = in.readLine(); - LOGGER.fine("Server: \n" + fromServer); - } - if (fromServer == null) { - fail("Null pointer"); - } final String fromUser = cmds[i]; if (fromUser != null) { @@ -123,19 +116,15 @@ public class SocketConsoleApplicationTest { String[] cmds = {"help", "toto", "test", ConsoleTestApplication.EXIT}; while ((fromServer = in.readLine()) != null) { - LOGGER.fine("Server: \n" + fromServer); - while (fromServer != null && !fromServer.equals("> ")) { - LOGGER.fine("Server: \n" + fromServer); - fromServer = in.readLine(); - } - if (fromServer == null) { + fromServer = consumeToPrompt(fromServer, in); + if (fromServer == null || fromServer.equals("Bye.")) { break; } - LOGGER.fine("Server: \n" + fromServer); + LOGGER.info("Server: \n" + fromServer); final String fromUser = cmds[i]; if (fromUser != null) { - LOGGER.fine("Client: " + fromUser); + LOGGER.info("Client: " + fromUser); out.println(fromUser); } i++; @@ -161,11 +150,8 @@ public class SocketConsoleApplicationTest { String[] cmds = {"help", "toto", "test", ConsoleTestApplication.EXIT}; while ((fromServer = in.readLine()) != null) { - while (fromServer != null && !fromServer.equals("> ")) { - fromServer = in.readLine(); - LOGGER.fine("Server: \n" + fromServer); - } - if (fromServer == null) { + fromServer = consumeToPrompt(fromServer, in); + if (fromServer == null || fromServer.equals("Bye.")) { break; } @@ -207,17 +193,11 @@ public class SocketConsoleApplicationTest { int i = 0; String[] cmds = {"help", "test", "close"}; while ((fromServer = in.readLine()) != null) { - assertTrue(i < 1); - while (fromServer != null && !fromServer.equals("> ") && - !fromServer.equals("See you")) { - fromServer = in.readLine(); - LOGGER.fine("Server: \n" + fromServer); - } - if (fromServer == null || fromServer.equals("Bye.") || - fromServer.equals("See you")) { + fromServer = consumeToPrompt(fromServer, in); + if (fromServer == null || fromServer.equals("Bye.")) { break; } - + assertTrue(i < 1); final String fromUser = cmds[i]; if (fromUser != null) { LOGGER.fine("Client: " + fromUser); @@ -243,4 +223,19 @@ public class SocketConsoleApplicationTest { e.printStackTrace(); } } + + /** @param in the input + * @return the string + * @throws IOException if the input reading failed */ + private String consumeToPrompt(String server, + BufferedReader in) throws IOException { + String fromServer = server; + LOGGER.fine("Server: \n" + fromServer); + while (fromServer != null && !fromServer.equals("Bye.") && + !fromServer.equals("> ")) { + fromServer = in.readLine(); + LOGGER.fine("Server: \n" + fromServer); + } + return fromServer; + } }