Compare commits

..

4 Commits

6 changed files with 212 additions and 57 deletions

View File

@@ -70,7 +70,7 @@ of Emmanuel Bigeon. -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>gclc-socket</artifactId> <artifactId>gclc-socket</artifactId>
<version>1.1.1-SNAPSHOT</version> <version>1.1.1</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<url>http://www.bigeon.fr/emmanuel</url> <url>http://www.bigeon.fr/emmanuel</url>
<properties> <properties>
@@ -104,6 +104,6 @@ of Emmanuel Bigeon. -->
<description>Socket implementation of GCLC</description> <description>Socket implementation of GCLC</description>
<scm> <scm>
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection> <developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
<tag>HEAD</tag> <tag>gclc-socket-1.1.1</tag>
</scm> </scm>
</project> </project>

View File

@@ -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,24 +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(); runSokectServer();
BufferedWriter writer = new BufferedWriter( } catch (final IOException e) {
new OutputStreamWriter(outStream, charset));
InputStreamReader isr = new InputStreamReader(consoleInput,
charset);
BufferedReader inBuf = new BufferedReader(isr)) {
consoleInput.connect(outStream);
runSokectServer();
// Close the application
// Pass command to application
if (app.isRunning()) {
writer.write(applicationShutdown + EOL);
writer.flush();
}
}
} 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$
} }
@@ -224,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);
@@ -233,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()) {
@@ -254,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 */
@@ -278,12 +259,14 @@ public class SocketConsoleApplicationShell implements Runnable {
* @throws IOException if the communication failed */ * @throws IOException if the communication failed */
private void communicate(final Socket socket, final PrintWriter writer, private void communicate(final Socket socket, final PrintWriter writer,
BufferedReader in) throws IOException { BufferedReader in) throws IOException {
Thread th = new Thread(new OutputForwardRunnable(writer, socket), "ClientComm"); //$NON-NLS-1$ Thread th = new Thread(new OutputForwardRunnable(writer, socket),
"ClientComm"); //$NON-NLS-1$
th.start(); th.start();
if (autoClose) { if (autoClose) {
communicateOnce(socket, in); communicateOnce(socket, in);
} else { } else {
communicateLoop(socket, in); communicateLoop(socket, in);
} }
} }
@@ -295,9 +278,7 @@ public class SocketConsoleApplicationShell implements Runnable {
ReadingRunnable reading = new ReadingRunnable(in); ReadingRunnable reading = new ReadingRunnable(in);
Thread th = new Thread(reading, "gclcToApp"); //$NON-NLS-1$ Thread th = new Thread(reading, "gclcToApp"); //$NON-NLS-1$
th.start(); th.start();
if (app.isRunning()) { communicationContent(reading);
communicationContent(reading);
}
reading.setRunning(false); reading.setRunning(false);
socket.shutdownOutput(); socket.shutdownOutput();
} }
@@ -322,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 {
while (app.isRunning() && !reading.hasMessage()) { try {
synchronized (this) { while (app.isRunning() && !reading.hasMessage()) {
waitASec(); 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()) { 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
@@ -375,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();
}
} }

View File

@@ -48,20 +48,18 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.Socket; import java.net.Socket;
import java.util.Arrays; import java.util.logging.Logger;
import org.junit.Test; import org.junit.Test;
import fr.bigeon.smu.StringEncoder;
/** Test class for {@link SocketConsoleApplicationShell} /** Test class for {@link SocketConsoleApplicationShell}
* *
* @author Emmanuel Bigeon */ * @author Emmanuel Bigeon */
@SuppressWarnings({"static-method", "unused", "javadoc", "nls"}) @SuppressWarnings({"static-method", "unused", "javadoc", "nls"})
public class SocketConsoleApplicationTest { public class SocketConsoleApplicationTest {
private static final StringEncoder ENCODER = new StringEncoder("%", private static final Logger LOGGER = Logger
Arrays.asList("\n")); //$NON-NLS-1$ .getLogger(SocketConsoleApplicationTest.class.getName());
@Test @Test
public void integrationTest() { public void integrationTest() {
@@ -90,14 +88,13 @@ public class SocketConsoleApplicationTest {
int i = 0; int i = 0;
String[] cmds = {"help", "toto", "test", "bye"}; String[] cmds = {"help", "toto", "test", "bye"};
while ((fromServer = in.readLine()) != null) { while ((fromServer = in.readLine()) != null) {
System.out.println("Server: \n" + ENCODER.decode(fromServer)); LOGGER.fine("Server: \n" + fromServer);
if (fromServer.equals("Bye.")) { if (fromServer.equals("Bye.")) {
break; break;
} }
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" + ENCODER.decode(fromServer));
} }
if (fromServer == null) { if (fromServer == null) {
fail("Null pointer"); fail("Null pointer");
@@ -105,7 +102,7 @@ public class SocketConsoleApplicationTest {
final String fromUser = cmds[i]; final String fromUser = cmds[i];
if (fromUser != null) { if (fromUser != null) {
System.out.println("Client: " + fromUser); LOGGER.fine("Client: " + fromUser);
out.println(fromUser); out.println(fromUser);
} }
i++; i++;
@@ -126,20 +123,19 @@ 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) {
System.out.println("Server: \n" + ENCODER.decode(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" + ENCODER.decode(fromServer));
fromServer = in.readLine(); fromServer = in.readLine();
} }
if (fromServer == null) { if (fromServer == null) {
break; break;
} }
System.out.println("Server: \n" + ENCODER.decode(fromServer)); LOGGER.fine("Server: \n" + fromServer);
final String fromUser = cmds[i]; final String fromUser = cmds[i];
if (fromUser != null) { if (fromUser != null) {
System.out.println("Client: " + fromUser); LOGGER.fine("Client: " + fromUser);
out.println(fromUser); out.println(fromUser);
} }
i++; i++;
@@ -167,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" + ENCODER.decode(fromServer));
} }
if (fromServer == null) { if (fromServer == null) {
break; break;
@@ -176,7 +171,7 @@ public class SocketConsoleApplicationTest {
final String fromUser = cmds[i]; final String fromUser = cmds[i];
if (fromUser != null) { if (fromUser != null) {
System.out.println("Client: " + fromUser); LOGGER.fine("Client: " + fromUser);
out.println(fromUser); out.println(fromUser);
} }
i++; i++;
@@ -216,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" + ENCODER.decode(fromServer));
} }
if (fromServer == null || fromServer.equals("Bye.") || if (fromServer == null || fromServer.equals("Bye.") ||
fromServer.equals("See you")) { fromServer.equals("See you")) {
@@ -226,7 +220,7 @@ public class SocketConsoleApplicationTest {
final String fromUser = cmds[i]; final String fromUser = cmds[i];
if (fromUser != null) { if (fromUser != null) {
System.out.println("Client: " + fromUser); LOGGER.fine("Client: " + fromUser);
out.println(fromUser); out.println(fromUser);
} }
i++; i++;

View File

@@ -35,7 +35,7 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>gclc-swt</artifactId> <artifactId>gclc-swt</artifactId>
<version>1.1.1</version> <version>1.1.2-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<url>http://www.bigeon.fr/emmanuel</url> <url>http://www.bigeon.fr/emmanuel</url>
<properties> <properties>
@@ -64,7 +64,7 @@
<description>provide a swt window for console applications</description> <description>provide a swt window for console applications</description>
<scm> <scm>
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection> <developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
<tag>gclc-swt-1.1.1</tag> <tag>HEAD</tag>
</scm> </scm>
<profiles> <profiles>
<profile> <profile>

View File

@@ -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

View File

@@ -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();
}
}
}