Corrections on the socket closing.
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
d07795cb6a
commit
efa492570e
@ -35,12 +35,9 @@
|
|||||||
package fr.bigeon.gclc.socket;
|
package fr.bigeon.gclc.socket;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.BufferedWriter;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.io.PipedInputStream;
|
|
||||||
import java.io.PipedOutputStream;
|
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
@ -72,7 +69,7 @@ import fr.bigeon.gclc.manager.ReadingRunnable;
|
|||||||
* end of the execution.
|
* end of the execution.
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public class SocketConsoleApplicationShell implements Runnable {
|
public class SocketConsoleApplicationShell implements Runnable, AutoCloseable {
|
||||||
|
|
||||||
/** The runnable to forward output of application to socket.
|
/** 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;
|
protected static final long ONE_TENTH_OF_SECOND = 100;
|
||||||
/** The listening port */
|
/** The listening port */
|
||||||
private final int port;
|
private final int port;
|
||||||
/** The input */
|
|
||||||
private final PipedInputStream consoleInput = new PipedInputStream();
|
|
||||||
/** The application */
|
/** The application */
|
||||||
private ConsoleApplication app;
|
private ConsoleApplication app;
|
||||||
/** The session closing command */
|
/** The session closing command */
|
||||||
@ -195,18 +190,8 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
this.serverSocket = actualServerSocket;
|
this.serverSocket = actualServerSocket;
|
||||||
running = true;
|
running = true;
|
||||||
// Create the streams
|
// 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();
|
runSokectServer();
|
||||||
}
|
} catch (final IOException e) {
|
||||||
} catch (
|
|
||||||
|
|
||||||
final IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE,
|
LOGGER.log(Level.SEVERE,
|
||||||
"Communication error between client and server", e); //$NON-NLS-1$
|
"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$
|
Thread appThNext = new Thread(runnable, "gclc-ctrl"); //$NON-NLS-1$
|
||||||
appThNext.start();
|
appThNext.start();
|
||||||
while (running) {
|
while (running) {
|
||||||
LOGGER.info("Opening client"); //$NON-NLS-1$
|
LOGGER.info("Waiting client"); //$NON-NLS-1$
|
||||||
try (Socket clientSocket = serverSocket.accept();
|
try (Socket clientSocket = serverSocket.accept();
|
||||||
PrintWriter out = new PrintWriter(new OutputStreamWriter(
|
PrintWriter out = new PrintWriter(new OutputStreamWriter(
|
||||||
clientSocket.getOutputStream(), charset), true);
|
clientSocket.getOutputStream(), charset), true);
|
||||||
@ -227,6 +212,7 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
BufferedReader in = new BufferedReader(isr);) {
|
BufferedReader in = new BufferedReader(isr);) {
|
||||||
// this is not threaded to avoid several clients at the same
|
// this is not threaded to avoid several clients at the same
|
||||||
// time
|
// time
|
||||||
|
LOGGER.info("Opening client"); //$NON-NLS-1$
|
||||||
|
|
||||||
// Initiate application
|
// Initiate application
|
||||||
if (!runnable.isApplicationRunning()) {
|
if (!runnable.isApplicationRunning()) {
|
||||||
@ -248,7 +234,8 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
}
|
}
|
||||||
runnable.setRunning(false);
|
runnable.setRunning(false);
|
||||||
consoleManager.type(applicationShutdown);
|
consoleManager.type(applicationShutdown);
|
||||||
LOGGER.info("Out client"); //$NON-NLS-1$
|
consoleManager.close();
|
||||||
|
LOGGER.info("Closing Server"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param runnable the runnable */
|
/** @param runnable the runnable */
|
||||||
@ -279,6 +266,7 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
communicateOnce(socket, in);
|
communicateOnce(socket, in);
|
||||||
} else {
|
} else {
|
||||||
communicateLoop(socket, in);
|
communicateLoop(socket, in);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -315,16 +303,23 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
* @return if the communication should be stopped.
|
* @return if the communication should be stopped.
|
||||||
* @throws IOException if the reading failed */
|
* @throws IOException if the reading failed */
|
||||||
private boolean communicationContent(ReadingRunnable reading) throws IOException {
|
private boolean communicationContent(ReadingRunnable reading) throws IOException {
|
||||||
|
try {
|
||||||
while (app.isRunning() && !reading.hasMessage()) {
|
while (app.isRunning() && !reading.hasMessage()) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
waitASec();
|
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()) {
|
if (!app.isRunning()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
String ln = reading.getMessage();
|
String ln = reading.getMessage();
|
||||||
if (ln.equals(close)) {
|
if (ln.equals(close)) {
|
||||||
|
consoleManager.println("Bye.");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// Pass command to application
|
// Pass command to application
|
||||||
@ -368,4 +363,11 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
return;
|
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("> ")) {
|
while (fromServer != null && !fromServer.equals("> ")) {
|
||||||
fromServer = in.readLine();
|
fromServer = in.readLine();
|
||||||
System.out
|
LOGGER.fine("Server: \n" + fromServer);
|
||||||
.println("Server: \n" + fromServer);
|
|
||||||
}
|
}
|
||||||
if (fromServer == null) {
|
if (fromServer == null) {
|
||||||
fail("Null pointer");
|
fail("Null pointer");
|
||||||
@ -126,8 +125,7 @@ public class SocketConsoleApplicationTest {
|
|||||||
while ((fromServer = in.readLine()) != null) {
|
while ((fromServer = in.readLine()) != null) {
|
||||||
LOGGER.fine("Server: \n" + fromServer);
|
LOGGER.fine("Server: \n" + fromServer);
|
||||||
while (fromServer != null && !fromServer.equals("> ")) {
|
while (fromServer != null && !fromServer.equals("> ")) {
|
||||||
System.out
|
LOGGER.fine("Server: \n" + fromServer);
|
||||||
.println("Server: \n" + fromServer);
|
|
||||||
fromServer = in.readLine();
|
fromServer = in.readLine();
|
||||||
}
|
}
|
||||||
if (fromServer == null) {
|
if (fromServer == null) {
|
||||||
@ -165,8 +163,7 @@ public class SocketConsoleApplicationTest {
|
|||||||
while ((fromServer = in.readLine()) != null) {
|
while ((fromServer = in.readLine()) != null) {
|
||||||
while (fromServer != null && !fromServer.equals("> ")) {
|
while (fromServer != null && !fromServer.equals("> ")) {
|
||||||
fromServer = in.readLine();
|
fromServer = in.readLine();
|
||||||
System.out
|
LOGGER.fine("Server: \n" + fromServer);
|
||||||
.println("Server: \n" + fromServer);
|
|
||||||
}
|
}
|
||||||
if (fromServer == null) {
|
if (fromServer == null) {
|
||||||
break;
|
break;
|
||||||
@ -214,8 +211,7 @@ public class SocketConsoleApplicationTest {
|
|||||||
while (fromServer != null && !fromServer.equals("> ") &&
|
while (fromServer != null && !fromServer.equals("> ") &&
|
||||||
!fromServer.equals("See you")) {
|
!fromServer.equals("See you")) {
|
||||||
fromServer = in.readLine();
|
fromServer = in.readLine();
|
||||||
System.out
|
LOGGER.fine("Server: \n" + fromServer);
|
||||||
.println("Server: \n" + fromServer);
|
|
||||||
}
|
}
|
||||||
if (fromServer == null || fromServer.equals("Bye.") ||
|
if (fromServer == null || fromServer.equals("Bye.") ||
|
||||||
fromServer.equals("See you")) {
|
fromServer.equals("See you")) {
|
||||||
|
@ -1,3 +1,37 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
/**
|
/**
|
||||||
* gclc:fr.bigeon.gclc.tools.AOutputForwardRunnable.java
|
* gclc:fr.bigeon.gclc.tools.AOutputForwardRunnable.java
|
||||||
* Created on: Dec 1, 2016
|
* Created on: Dec 1, 2016
|
||||||
|
@ -0,0 +1,132 @@
|
|||||||
|
/*
|
||||||
|
* Copyright Bigeon Emmanuel (2014)
|
||||||
|
*
|
||||||
|
* emmanuel@bigeon.fr
|
||||||
|
*
|
||||||
|
* This software is a computer program whose purpose is to
|
||||||
|
* provide a generic framework for console applications.
|
||||||
|
*
|
||||||
|
* This software is governed by the CeCILL license under French law and
|
||||||
|
* abiding by the rules of distribution of free software. You can use,
|
||||||
|
* modify and/or redistribute the software under the terms of the CeCILL
|
||||||
|
* license as circulated by CEA, CNRS and INRIA at the following URL
|
||||||
|
* "http://www.cecill.info".
|
||||||
|
*
|
||||||
|
* As a counterpart to the access to the source code and rights to copy,
|
||||||
|
* modify and redistribute granted by the license, users are provided only
|
||||||
|
* with a limited warranty and the software's author, the holder of the
|
||||||
|
* economic rights, and the successive licensors have only limited
|
||||||
|
* liability.
|
||||||
|
*
|
||||||
|
* In this respect, the user's attention is drawn to the risks associated
|
||||||
|
* with loading, using, modifying and/or developing or reproducing the
|
||||||
|
* software by the user in light of its specific status of free software,
|
||||||
|
* that may mean that it is complicated to manipulate, and that also
|
||||||
|
* therefore means that it is reserved for developers and experienced
|
||||||
|
* professionals having in-depth computer knowledge. Users are therefore
|
||||||
|
* encouraged to load and test the software's suitability as regards their
|
||||||
|
* requirements in conditions enabling the security of their systems and/or
|
||||||
|
* data to be ensured and, more generally, to use and operate it in the
|
||||||
|
* same conditions as regards security.
|
||||||
|
*
|
||||||
|
* The fact that you are presently reading this means that you have had
|
||||||
|
* knowledge of the CeCILL license and that you accept its terms.
|
||||||
|
*/
|
||||||
|
/**
|
||||||
|
* gclc:fr.bigeon.gclc.tools.AOutputForwardRunnableTest.java
|
||||||
|
* Created on: Dec 3, 2016
|
||||||
|
*/
|
||||||
|
package fr.bigeon.gclc.tools;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
|
|
||||||
|
/** <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
public class AOutputForwardRunnableTest {
|
||||||
|
|
||||||
|
/** <p>
|
||||||
|
* TODO
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
private final class AOutputForwardTestRunnable
|
||||||
|
extends AOutputForwardRunnable {
|
||||||
|
private int count = 1;
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
/** @param manager */
|
||||||
|
private AOutputForwardTestRunnable(PipedConsoleManager manager) {
|
||||||
|
super(manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean isRunning() {
|
||||||
|
return count != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void forwardLine(String m) {
|
||||||
|
// Do nothing
|
||||||
|
message = m;
|
||||||
|
synchronized (this) {
|
||||||
|
notify();
|
||||||
|
try {
|
||||||
|
wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @return the message */
|
||||||
|
public String getMessage() {
|
||||||
|
if (message == null) {
|
||||||
|
synchronized (this) {
|
||||||
|
try {
|
||||||
|
wait();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String m = message;
|
||||||
|
count--;
|
||||||
|
message = null;
|
||||||
|
synchronized (this) {
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Test method for
|
||||||
|
* {@link fr.bigeon.gclc.tools.AOutputForwardRunnable#run()}. */
|
||||||
|
@Test
|
||||||
|
public final void testRun() {
|
||||||
|
try (PipedConsoleManager manager = new PipedConsoleManager()) {
|
||||||
|
AOutputForwardTestRunnable runnable = new AOutputForwardTestRunnable(
|
||||||
|
manager);
|
||||||
|
|
||||||
|
Thread th = new Thread(runnable, "forward");
|
||||||
|
th.start();
|
||||||
|
manager.println("before");
|
||||||
|
|
||||||
|
assertEquals("before", runnable.getMessage());
|
||||||
|
|
||||||
|
th.join();
|
||||||
|
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user