Corrections on the socket closing.
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
@@ -35,12 +35,9 @@
|
||||
package fr.bigeon.gclc.socket;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
@@ -72,7 +69,7 @@ import fr.bigeon.gclc.manager.ReadingRunnable;
|
||||
* end of the execution.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class SocketConsoleApplicationShell implements Runnable {
|
||||
public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
|
||||
|
||||
/** The runnable to forward output of application to socket.
|
||||
*
|
||||
@@ -126,8 +123,6 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
protected static final long ONE_TENTH_OF_SECOND = 100;
|
||||
/** The listening port */
|
||||
private final int port;
|
||||
/** The input */
|
||||
private final PipedInputStream consoleInput = new PipedInputStream();
|
||||
/** The application */
|
||||
private ConsoleApplication app;
|
||||
/** The session closing command */
|
||||
@@ -195,18 +190,8 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
this.serverSocket = actualServerSocket;
|
||||
running = true;
|
||||
// Create the streams
|
||||
try (PipedOutputStream outStream = new PipedOutputStream();
|
||||
BufferedWriter writer = new BufferedWriter(
|
||||
new OutputStreamWriter(outStream, charset));
|
||||
InputStreamReader isr = new InputStreamReader(consoleInput,
|
||||
charset);
|
||||
BufferedReader inBuf = new BufferedReader(isr)) {
|
||||
consoleInput.connect(outStream);
|
||||
runSokectServer();
|
||||
}
|
||||
} catch (
|
||||
|
||||
final IOException e) {
|
||||
runSokectServer();
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.SEVERE,
|
||||
"Communication error between client and server", e); //$NON-NLS-1$
|
||||
}
|
||||
@@ -218,7 +203,7 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
Thread appThNext = new Thread(runnable, "gclc-ctrl"); //$NON-NLS-1$
|
||||
appThNext.start();
|
||||
while (running) {
|
||||
LOGGER.info("Opening client"); //$NON-NLS-1$
|
||||
LOGGER.info("Waiting client"); //$NON-NLS-1$
|
||||
try (Socket clientSocket = serverSocket.accept();
|
||||
PrintWriter out = new PrintWriter(new OutputStreamWriter(
|
||||
clientSocket.getOutputStream(), charset), true);
|
||||
@@ -227,6 +212,7 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
BufferedReader in = new BufferedReader(isr);) {
|
||||
// this is not threaded to avoid several clients at the same
|
||||
// time
|
||||
LOGGER.info("Opening client"); //$NON-NLS-1$
|
||||
|
||||
// Initiate application
|
||||
if (!runnable.isApplicationRunning()) {
|
||||
@@ -248,7 +234,8 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
}
|
||||
runnable.setRunning(false);
|
||||
consoleManager.type(applicationShutdown);
|
||||
LOGGER.info("Out client"); //$NON-NLS-1$
|
||||
consoleManager.close();
|
||||
LOGGER.info("Closing Server"); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/** @param runnable the runnable */
|
||||
@@ -279,6 +266,7 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
communicateOnce(socket, in);
|
||||
} else {
|
||||
communicateLoop(socket, in);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -315,16 +303,23 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
* @return if the communication should be stopped.
|
||||
* @throws IOException if the reading failed */
|
||||
private boolean communicationContent(ReadingRunnable reading) throws IOException {
|
||||
while (app.isRunning() && !reading.hasMessage()) {
|
||||
synchronized (this) {
|
||||
waitASec();
|
||||
try {
|
||||
while (app.isRunning() && !reading.hasMessage()) {
|
||||
synchronized (this) {
|
||||
waitASec();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.warning("Client seems dead. Closing communication"); //$NON-NLS-1$
|
||||
LOGGER.log(Level.FINE, "Wait on message from client failed", e); //$NON-NLS-1$
|
||||
return false;
|
||||
}
|
||||
if (!app.isRunning()) {
|
||||
return false;
|
||||
}
|
||||
String ln = reading.getMessage();
|
||||
if (ln.equals(close)) {
|
||||
consoleManager.println("Bye.");
|
||||
return false;
|
||||
}
|
||||
// Pass command to application
|
||||
@@ -368,4 +363,11 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.AutoCloseable#close() */
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
consoleManager.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,8 +94,7 @@ public class SocketConsoleApplicationTest {
|
||||
}
|
||||
while (fromServer != null && !fromServer.equals("> ")) {
|
||||
fromServer = in.readLine();
|
||||
System.out
|
||||
.println("Server: \n" + fromServer);
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
}
|
||||
if (fromServer == null) {
|
||||
fail("Null pointer");
|
||||
@@ -126,8 +125,7 @@ public class SocketConsoleApplicationTest {
|
||||
while ((fromServer = in.readLine()) != null) {
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
while (fromServer != null && !fromServer.equals("> ")) {
|
||||
System.out
|
||||
.println("Server: \n" + fromServer);
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
fromServer = in.readLine();
|
||||
}
|
||||
if (fromServer == null) {
|
||||
@@ -165,8 +163,7 @@ public class SocketConsoleApplicationTest {
|
||||
while ((fromServer = in.readLine()) != null) {
|
||||
while (fromServer != null && !fromServer.equals("> ")) {
|
||||
fromServer = in.readLine();
|
||||
System.out
|
||||
.println("Server: \n" + fromServer);
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
}
|
||||
if (fromServer == null) {
|
||||
break;
|
||||
@@ -214,8 +211,7 @@ public class SocketConsoleApplicationTest {
|
||||
while (fromServer != null && !fromServer.equals("> ") &&
|
||||
!fromServer.equals("See you")) {
|
||||
fromServer = in.readLine();
|
||||
System.out
|
||||
.println("Server: \n" + fromServer);
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
}
|
||||
if (fromServer == null || fromServer.equals("Bye.") ||
|
||||
fromServer.equals("See you")) {
|
||||
|
||||
Reference in New Issue
Block a user