Systematic end of communication phase by sending 'Bye.'. modify tests.
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
6289ca1db9
commit
248c82cf50
@ -95,12 +95,12 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
while (!socket.isOutputShutdown()) {
|
while (!socket.isClosed()) {
|
||||||
while (!socket.isOutputShutdown() &&
|
while (!socket.isClosed() &&
|
||||||
!consoleManager.available()) {
|
!consoleManager.available()) {
|
||||||
waitASec();
|
waitASec();
|
||||||
}
|
}
|
||||||
if (socket.isOutputShutdown()) {
|
if (socket.isClosed()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String m = consoleManager.readNextLine();
|
String m = consoleManager.readNextLine();
|
||||||
@ -266,7 +266,6 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
|
|||||||
communicateOnce(socket, in);
|
communicateOnce(socket, in);
|
||||||
} else {
|
} else {
|
||||||
communicateLoop(socket, in);
|
communicateLoop(socket, in);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,8 +278,21 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
|
|||||||
Thread th = new Thread(reading, "gclcToApp"); //$NON-NLS-1$
|
Thread th = new Thread(reading, "gclcToApp"); //$NON-NLS-1$
|
||||||
th.start();
|
th.start();
|
||||||
communicationContent(reading);
|
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);
|
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
|
/** @param socket the socket
|
||||||
@ -294,12 +306,7 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
|
|||||||
while (app.isRunning() && communicationContent(reading)) {
|
while (app.isRunning() && communicationContent(reading)) {
|
||||||
// keep on going
|
// keep on going
|
||||||
}
|
}
|
||||||
reading.setRunning(false);
|
doEndCommunication(reading);
|
||||||
while (consoleManager.available()) {
|
|
||||||
waitASec();
|
|
||||||
}
|
|
||||||
socket.shutdownOutput();
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param reading the reading
|
/** @param reading the reading
|
||||||
@ -322,14 +329,6 @@ public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
|
|||||||
}
|
}
|
||||||
String ln = reading.getMessage();
|
String ln = reading.getMessage();
|
||||||
if (ln.equals(close)) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
// Pass command to application
|
// Pass command to application
|
||||||
|
@ -89,17 +89,10 @@ public class SocketConsoleApplicationTest {
|
|||||||
String[] cmds = {"help", "toto", "test", "bye"};
|
String[] cmds = {"help", "toto", "test", "bye"};
|
||||||
while ((fromServer = in.readLine()) != null) {
|
while ((fromServer = in.readLine()) != null) {
|
||||||
i++;
|
i++;
|
||||||
LOGGER.fine("Server: \n" + fromServer);
|
fromServer = consumeToPrompt(fromServer, in);
|
||||||
if (fromServer.equals("Bye.")) {
|
if (fromServer == null || fromServer.equals("Bye.")) {
|
||||||
break;
|
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];
|
final String fromUser = cmds[i];
|
||||||
if (fromUser != null) {
|
if (fromUser != null) {
|
||||||
@ -123,19 +116,15 @@ public class SocketConsoleApplicationTest {
|
|||||||
String[] cmds = {"help", "toto", "test",
|
String[] cmds = {"help", "toto", "test",
|
||||||
ConsoleTestApplication.EXIT};
|
ConsoleTestApplication.EXIT};
|
||||||
while ((fromServer = in.readLine()) != null) {
|
while ((fromServer = in.readLine()) != null) {
|
||||||
LOGGER.fine("Server: \n" + fromServer);
|
fromServer = consumeToPrompt(fromServer, in);
|
||||||
while (fromServer != null && !fromServer.equals("> ")) {
|
if (fromServer == null || fromServer.equals("Bye.")) {
|
||||||
LOGGER.fine("Server: \n" + fromServer);
|
|
||||||
fromServer = in.readLine();
|
|
||||||
}
|
|
||||||
if (fromServer == null) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
LOGGER.fine("Server: \n" + fromServer);
|
LOGGER.info("Server: \n" + fromServer);
|
||||||
|
|
||||||
final String fromUser = cmds[i];
|
final String fromUser = cmds[i];
|
||||||
if (fromUser != null) {
|
if (fromUser != null) {
|
||||||
LOGGER.fine("Client: " + fromUser);
|
LOGGER.info("Client: " + fromUser);
|
||||||
out.println(fromUser);
|
out.println(fromUser);
|
||||||
}
|
}
|
||||||
i++;
|
i++;
|
||||||
@ -161,11 +150,8 @@ public class SocketConsoleApplicationTest {
|
|||||||
String[] cmds = {"help", "toto", "test",
|
String[] cmds = {"help", "toto", "test",
|
||||||
ConsoleTestApplication.EXIT};
|
ConsoleTestApplication.EXIT};
|
||||||
while ((fromServer = in.readLine()) != null) {
|
while ((fromServer = in.readLine()) != null) {
|
||||||
while (fromServer != null && !fromServer.equals("> ")) {
|
fromServer = consumeToPrompt(fromServer, in);
|
||||||
fromServer = in.readLine();
|
if (fromServer == null || fromServer.equals("Bye.")) {
|
||||||
LOGGER.fine("Server: \n" + fromServer);
|
|
||||||
}
|
|
||||||
if (fromServer == null) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -207,17 +193,11 @@ public class SocketConsoleApplicationTest {
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
String[] cmds = {"help", "test", "close"};
|
String[] cmds = {"help", "test", "close"};
|
||||||
while ((fromServer = in.readLine()) != null) {
|
while ((fromServer = in.readLine()) != null) {
|
||||||
assertTrue(i < 1);
|
fromServer = consumeToPrompt(fromServer, in);
|
||||||
while (fromServer != null && !fromServer.equals("> ") &&
|
if (fromServer == null || fromServer.equals("Bye.")) {
|
||||||
!fromServer.equals("See you")) {
|
|
||||||
fromServer = in.readLine();
|
|
||||||
LOGGER.fine("Server: \n" + fromServer);
|
|
||||||
}
|
|
||||||
if (fromServer == null || fromServer.equals("Bye.") ||
|
|
||||||
fromServer.equals("See you")) {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
assertTrue(i < 1);
|
||||||
final String fromUser = cmds[i];
|
final String fromUser = cmds[i];
|
||||||
if (fromUser != null) {
|
if (fromUser != null) {
|
||||||
LOGGER.fine("Client: " + fromUser);
|
LOGGER.fine("Client: " + fromUser);
|
||||||
@ -243,4 +223,19 @@ public class SocketConsoleApplicationTest {
|
|||||||
e.printStackTrace();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user