Compare commits
17 Commits
gclc-swt-1
...
gclc-socke
| Author | SHA1 | Date | |
|---|---|---|---|
| b2c98ae940 | |||
| 1d6569cf69 | |||
| 072653c944 | |||
| ad17c7d5d7 | |||
| e469313baf | |||
| 543b1ef605 | |||
| 24f1fba97e | |||
| 88fe564e24 | |||
| efa492570e | |||
| d07795cb6a | |||
| f3c3580855 | |||
| 62f59722a6 | |||
| 3e31a38eb9 | |||
| 549ddc3e6f | |||
| d300e896e8 | |||
| 16514f9c25 | |||
| bb9f9c515b |
@@ -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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>gclc-socket</artifactId>
|
||||
<version>1.1.1-SNAPSHOT</version>
|
||||
<version>1.1.2</version>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://www.bigeon.fr/emmanuel</url>
|
||||
<properties>
|
||||
@@ -87,7 +87,7 @@ of Emmanuel Bigeon. -->
|
||||
<dependency>
|
||||
<groupId>fr.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>1.3.1</version>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>fr.bigeon</groupId>
|
||||
@@ -104,6 +104,6 @@ of Emmanuel Bigeon. -->
|
||||
<description>Socket implementation of GCLC</description>
|
||||
<scm>
|
||||
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
|
||||
<tag>HEAD</tag>
|
||||
<tag>gclc-socket-1.1.2</tag>
|
||||
</scm>
|
||||
</project>
|
||||
|
||||
@@ -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,24 +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();
|
||||
// Close the application
|
||||
// Pass command to application
|
||||
if (app.isRunning()) {
|
||||
writer.write(applicationShutdown + EOL);
|
||||
writer.flush();
|
||||
}
|
||||
}
|
||||
} catch (
|
||||
|
||||
final IOException e) {
|
||||
runSokectServer();
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.SEVERE,
|
||||
"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$
|
||||
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);
|
||||
@@ -233,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()) {
|
||||
@@ -254,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 */
|
||||
@@ -278,12 +259,14 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
* @throws IOException if the communication failed */
|
||||
private void communicate(final Socket socket, final PrintWriter writer,
|
||||
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();
|
||||
if (autoClose) {
|
||||
communicateOnce(socket, in);
|
||||
} else {
|
||||
communicateLoop(socket, in);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -295,9 +278,7 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
ReadingRunnable reading = new ReadingRunnable(in);
|
||||
Thread th = new Thread(reading, "gclcToApp"); //$NON-NLS-1$
|
||||
th.start();
|
||||
if (app.isRunning()) {
|
||||
communicationContent(reading);
|
||||
}
|
||||
communicationContent(reading);
|
||||
reading.setRunning(false);
|
||||
socket.shutdownOutput();
|
||||
}
|
||||
@@ -322,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."); //$NON-NLS-1$
|
||||
return false;
|
||||
}
|
||||
// Pass command to application
|
||||
@@ -375,4 +363,11 @@ public class SocketConsoleApplicationShell implements Runnable {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.AutoCloseable#close() */
|
||||
@Override
|
||||
public void close() throws Exception {
|
||||
consoleManager.close();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,20 +48,18 @@ import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import fr.bigeon.smu.StringEncoder;
|
||||
|
||||
/** Test class for {@link SocketConsoleApplicationShell}
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
@SuppressWarnings({"static-method", "unused", "javadoc", "nls"})
|
||||
public class SocketConsoleApplicationTest {
|
||||
|
||||
private static final StringEncoder ENCODER = new StringEncoder("%",
|
||||
Arrays.asList("\n")); //$NON-NLS-1$
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(SocketConsoleApplicationTest.class.getName());
|
||||
|
||||
@Test
|
||||
public void integrationTest() {
|
||||
@@ -90,14 +88,13 @@ public class SocketConsoleApplicationTest {
|
||||
int i = 0;
|
||||
String[] cmds = {"help", "toto", "test", "bye"};
|
||||
while ((fromServer = in.readLine()) != null) {
|
||||
System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
if (fromServer.equals("Bye.")) {
|
||||
break;
|
||||
}
|
||||
while (fromServer != null && !fromServer.equals("> ")) {
|
||||
fromServer = in.readLine();
|
||||
System.out
|
||||
.println("Server: \n" + ENCODER.decode(fromServer));
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
}
|
||||
if (fromServer == null) {
|
||||
fail("Null pointer");
|
||||
@@ -105,7 +102,7 @@ public class SocketConsoleApplicationTest {
|
||||
|
||||
final String fromUser = cmds[i];
|
||||
if (fromUser != null) {
|
||||
System.out.println("Client: " + fromUser);
|
||||
LOGGER.fine("Client: " + fromUser);
|
||||
out.println(fromUser);
|
||||
}
|
||||
i++;
|
||||
@@ -126,20 +123,19 @@ public class SocketConsoleApplicationTest {
|
||||
String[] cmds = {"help", "toto", "test",
|
||||
ConsoleTestApplication.EXIT};
|
||||
while ((fromServer = in.readLine()) != null) {
|
||||
System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
while (fromServer != null && !fromServer.equals("> ")) {
|
||||
System.out
|
||||
.println("Server: \n" + ENCODER.decode(fromServer));
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
fromServer = in.readLine();
|
||||
}
|
||||
if (fromServer == null) {
|
||||
break;
|
||||
}
|
||||
System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
|
||||
final String fromUser = cmds[i];
|
||||
if (fromUser != null) {
|
||||
System.out.println("Client: " + fromUser);
|
||||
LOGGER.fine("Client: " + fromUser);
|
||||
out.println(fromUser);
|
||||
}
|
||||
i++;
|
||||
@@ -167,8 +163,7 @@ public class SocketConsoleApplicationTest {
|
||||
while ((fromServer = in.readLine()) != null) {
|
||||
while (fromServer != null && !fromServer.equals("> ")) {
|
||||
fromServer = in.readLine();
|
||||
System.out
|
||||
.println("Server: \n" + ENCODER.decode(fromServer));
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
}
|
||||
if (fromServer == null) {
|
||||
break;
|
||||
@@ -176,7 +171,7 @@ public class SocketConsoleApplicationTest {
|
||||
|
||||
final String fromUser = cmds[i];
|
||||
if (fromUser != null) {
|
||||
System.out.println("Client: " + fromUser);
|
||||
LOGGER.fine("Client: " + fromUser);
|
||||
out.println(fromUser);
|
||||
}
|
||||
i++;
|
||||
@@ -216,8 +211,7 @@ public class SocketConsoleApplicationTest {
|
||||
while (fromServer != null && !fromServer.equals("> ") &&
|
||||
!fromServer.equals("See you")) {
|
||||
fromServer = in.readLine();
|
||||
System.out
|
||||
.println("Server: \n" + ENCODER.decode(fromServer));
|
||||
LOGGER.fine("Server: \n" + fromServer);
|
||||
}
|
||||
if (fromServer == null || fromServer.equals("Bye.") ||
|
||||
fromServer.equals("See you")) {
|
||||
@@ -226,7 +220,7 @@ public class SocketConsoleApplicationTest {
|
||||
|
||||
final String fromUser = cmds[i];
|
||||
if (fromUser != null) {
|
||||
System.out.println("Client: " + fromUser);
|
||||
LOGGER.fine("Client: " + fromUser);
|
||||
out.println(fromUser);
|
||||
}
|
||||
i++;
|
||||
|
||||
@@ -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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>gclc-swt</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.1.2-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://www.bigeon.fr/emmanuel</url>
|
||||
<properties>
|
||||
@@ -51,7 +51,7 @@
|
||||
<dependency>
|
||||
<groupId>fr.bigeon</groupId>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>1.3.1</version>
|
||||
<version>1.3.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>fr.bigeon</groupId>
|
||||
@@ -64,7 +64,7 @@
|
||||
<description>provide a swt window for console applications</description>
|
||||
<scm>
|
||||
<developerConnection>scm:git:gogs@git.code.bigeon.net:emmanuel/gclc.git</developerConnection>
|
||||
<tag>gclc-swt-1.1.0</tag>
|
||||
<tag>HEAD</tag>
|
||||
</scm>
|
||||
<profiles>
|
||||
<profile>
|
||||
|
||||
@@ -1,99 +0,0 @@
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.tools.AOutputForwardRunnable.java
|
||||
* Created on: Dec 1, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc.swt;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||
|
||||
/** An incomplete implematation used to forward messages from a piped console.
|
||||
* <p>
|
||||
* This forwarding can be interrupted without closing the piped manager.
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
* @deprecated since version 1.3.2 of gclc, this class has been integrated in
|
||||
* the main content. */
|
||||
@Deprecated
|
||||
public abstract class AOutputForwardRunnable implements Runnable {
|
||||
|
||||
/** The class logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(AOutputForwardRunnable.class.getName());
|
||||
/** The default timeout (one tenth of second). */
|
||||
private static final long DEFAULT_TIMEOUT = 100;
|
||||
/** The manager. */
|
||||
private final PipedConsoleManager manager;
|
||||
/** The timeout */
|
||||
private final long timeout;
|
||||
|
||||
/** Create a forward runnable with the given timeout.
|
||||
* <p>
|
||||
* Short timeout will be very responsive to the application actual messages,
|
||||
* but may use computation time if the application is not verbose. Long
|
||||
* timeout will save computation time, but will read batches of messages at
|
||||
* once if the application is verbose. The right length for the timeout is
|
||||
* likely to depend on the application and the use of it.
|
||||
* <p>
|
||||
* If you do not know what timeout length to use, please use the
|
||||
* {@link #AOutputForwardRunnable(PipedConsoleManager)} constructor.
|
||||
*
|
||||
* @param manager the manager
|
||||
* @param timeout the timeout between message requests. */
|
||||
public AOutputForwardRunnable(PipedConsoleManager manager, long timeout) {
|
||||
super();
|
||||
this.manager = manager;
|
||||
this.timeout = timeout;
|
||||
}
|
||||
|
||||
/** Create a forwarding runnable.
|
||||
*
|
||||
* @param manager the manager */
|
||||
public AOutputForwardRunnable(PipedConsoleManager manager) {
|
||||
super();
|
||||
this.manager = manager;
|
||||
timeout = DEFAULT_TIMEOUT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (isRunning()) {
|
||||
while (isRunning() && !manager.available()) {
|
||||
waitASec();
|
||||
}
|
||||
if (!isRunning()) {
|
||||
return;
|
||||
}
|
||||
String m = manager.readNextLine();
|
||||
forwardLine(m);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Unexpected problem in manager", //$NON-NLS-1$
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
/** @param m the line to forward */
|
||||
protected abstract void forwardLine(String m);
|
||||
|
||||
/** @return if the thread should keep running */
|
||||
protected abstract boolean isRunning();
|
||||
|
||||
/** a method to wait some time */
|
||||
protected void waitASec() {
|
||||
try {
|
||||
synchronized (this) {
|
||||
wait(timeout);
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.log(Level.SEVERE, "Interrupted wait", //$NON-NLS-1$
|
||||
e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -175,7 +175,9 @@ public class SWTConsole extends Composite
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
consoleOutput.append(text);
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
consoleOutput.append(text);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -188,7 +190,9 @@ public class SWTConsole extends Composite
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
consoleOutput.append(System.lineSeparator());
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
consoleOutput.append(System.lineSeparator());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -201,7 +205,9 @@ public class SWTConsole extends Composite
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
consoleOutput.append(message + System.lineSeparator());
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
consoleOutput.append(message + System.lineSeparator());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -220,8 +226,10 @@ public class SWTConsole extends Composite
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
consoleInput.setEnabled(true);
|
||||
consoleInput.setFocus();
|
||||
if (!consoleInput.isDisposed()) {
|
||||
consoleInput.setEnabled(true);
|
||||
consoleInput.setFocus();
|
||||
}
|
||||
}
|
||||
});
|
||||
prompting = true;
|
||||
@@ -252,11 +260,13 @@ public class SWTConsole extends Composite
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
lblPromptlabel.setText(message);
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
consoleInput.setEnabled(true);
|
||||
consoleInput.setFocus();
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
lblPromptlabel.setText(message);
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
consoleInput.setEnabled(true);
|
||||
consoleInput.setFocus();
|
||||
}
|
||||
}
|
||||
});
|
||||
prompting = true;
|
||||
@@ -273,9 +283,11 @@ public class SWTConsole extends Composite
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
lblPromptlabel.setText(prompt);
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
lblPromptlabel.setText(prompt);
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -299,9 +311,11 @@ public class SWTConsole extends Composite
|
||||
@SuppressWarnings("synthetic-access")
|
||||
@Override
|
||||
public void run() {
|
||||
lblPromptlabel.setText(prompt);
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
if (!consoleOutput.isDisposed()) {
|
||||
lblPromptlabel.setText(prompt);
|
||||
// relayout
|
||||
SWTConsole.this.layout();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -53,6 +53,7 @@ import org.eclipse.swt.widgets.Text;
|
||||
|
||||
import fr.bigeon.gclc.ConsoleApplication;
|
||||
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||
import fr.bigeon.gclc.tools.AOutputForwardRunnable;
|
||||
|
||||
/** A SWT component to connect to gclc {@link ConsoleApplication}
|
||||
* <p>
|
||||
@@ -62,14 +63,13 @@ public class SWTConsoleView extends Composite implements ConsoleDelayIO {
|
||||
/** The local implementation of the forwarding runnable
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
@SuppressWarnings("deprecation")
|
||||
private final class ToSWTConsoleForwarRunnable
|
||||
private final class ToSWTConsoleForwardRunnable
|
||||
extends AOutputForwardRunnable {
|
||||
/** The running status */
|
||||
private boolean running = true;
|
||||
|
||||
/** @param manager the manager */
|
||||
public ToSWTConsoleForwarRunnable(PipedConsoleManager manager) {
|
||||
public ToSWTConsoleForwardRunnable(PipedConsoleManager manager) {
|
||||
super(manager);
|
||||
}
|
||||
|
||||
@@ -99,7 +99,7 @@ public class SWTConsoleView extends Composite implements ConsoleDelayIO {
|
||||
/** The actual manager */
|
||||
private PipedConsoleManager manager;
|
||||
/** The forwarding runnable */
|
||||
private ToSWTConsoleForwarRunnable forward;
|
||||
private ToSWTConsoleForwardRunnable forward;
|
||||
|
||||
/** Create the composite.
|
||||
*
|
||||
@@ -135,7 +135,7 @@ public class SWTConsoleView extends Composite implements ConsoleDelayIO {
|
||||
if (forward != null) {
|
||||
forward.setRunning(false);
|
||||
}
|
||||
forward = new ToSWTConsoleForwarRunnable(manager);
|
||||
forward = new ToSWTConsoleForwardRunnable(manager);
|
||||
Thread th = new Thread(forward, "gclcToSWT"); //$NON-NLS-1$
|
||||
th.start();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,9 @@
|
||||
package fr.bigeon.gclc.swt;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -60,7 +62,7 @@ import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
public class SWTConsoleShellTest {
|
||||
|
||||
protected static final long TWO_SECONDS = 2000;
|
||||
private static final Display DISPLAY = new Display();
|
||||
private static final Display DISPLAY = Display.getDefault();
|
||||
|
||||
@Test
|
||||
public void testConsoleClose() {
|
||||
@@ -171,6 +173,7 @@ public class SWTConsoleShellTest {
|
||||
shell.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
applThread.start();
|
||||
@@ -180,6 +183,13 @@ public class SWTConsoleShellTest {
|
||||
DISPLAY.sleep();
|
||||
}
|
||||
}
|
||||
swtConsole.setPrompt(">");
|
||||
try {
|
||||
swtConsole.prompt();
|
||||
fail("Prompting when closed should fail!");
|
||||
} catch (IOException e) {
|
||||
assertNotNull(e);
|
||||
}
|
||||
// DISPLAY.dispose();
|
||||
assertTrue(appl.getManager().isClosed());
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* Copyright E. Bigeon (2015)
|
||||
*
|
||||
* emmanuel@bigeon.fr
|
||||
*
|
||||
* This software is a computer program whose purpose is to
|
||||
* provide a swt window 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-swt:fr.bigeon.gclc.swt.SWTConsoleShellTest.java
|
||||
* Created on: Jun 8, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc.swt;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.eclipse.swt.SWT;
|
||||
import org.eclipse.swt.widgets.Display;
|
||||
import org.eclipse.swt.widgets.Shell;
|
||||
import org.junit.Test;
|
||||
|
||||
import fr.bigeon.gclc.ConsoleApplication;
|
||||
import fr.bigeon.gclc.command.Command;
|
||||
import fr.bigeon.gclc.command.ExitCommand;
|
||||
import fr.bigeon.gclc.exception.InvalidCommandName;
|
||||
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||
|
||||
/** <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
@SuppressWarnings({"javadoc", "static-method", "nls", "deprecation"})
|
||||
public class SWTConsoleViewTest {
|
||||
|
||||
protected static final long TWO_SECONDS = 2000;
|
||||
private static final Display DISPLAY = Display.getDefault();
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
final Shell shell = new Shell(DISPLAY);
|
||||
final SWTConsoleView swtConsole = new SWTConsoleView(shell, SWT.NONE);
|
||||
try (PipedConsoleManager manager = new PipedConsoleManager()) {
|
||||
swtConsole.setManager(manager);
|
||||
final ConsoleApplication appl = new ConsoleApplication(manager,
|
||||
"Hello", "See you");
|
||||
appl.add(new ExitCommand("exit", appl));
|
||||
appl.add(new Command("long") {
|
||||
|
||||
@Override
|
||||
public String tip() {
|
||||
return "a long running command";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(String... args) {
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
// shell.pack();
|
||||
shell.open();
|
||||
Thread applThread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
appl.start();
|
||||
}
|
||||
});
|
||||
Thread testThread = new Thread(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
swtConsole.setText("test"); //$NON-NLS-1$
|
||||
swtConsole.validateCommand();
|
||||
}
|
||||
});
|
||||
try {
|
||||
Thread.sleep(TWO_SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
Display.getDefault().syncExec(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
shell.dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
applThread.start();
|
||||
testThread.start();
|
||||
while (!shell.isDisposed()) {
|
||||
if (!DISPLAY.readAndDispatch()) {
|
||||
DISPLAY.sleep();
|
||||
}
|
||||
}
|
||||
} catch (InvalidCommandName e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IOException e1) {
|
||||
// TODO Auto-generated catch block
|
||||
e1.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>gclc</artifactId>
|
||||
<version>1.3.2-SNAPSHOT</version>
|
||||
<version>1.3.3-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://www.bigeon.fr/emmanuel</url>
|
||||
<properties>
|
||||
|
||||
@@ -48,7 +48,7 @@ import java.io.InterruptedIOException;
|
||||
* a message, in that case, the usual behavior is to prompt normally.
|
||||
*
|
||||
* @author Emmanuel BIGEON */
|
||||
public interface ConsoleManager {
|
||||
public interface ConsoleManager extends AutoCloseable {
|
||||
|
||||
/** @return the prompt prefix */
|
||||
String getPrompt();
|
||||
@@ -89,6 +89,7 @@ public interface ConsoleManager {
|
||||
/** Closes the manager.
|
||||
*
|
||||
* @throws IOException if the close raised an exception */
|
||||
@Override
|
||||
void close() throws IOException;
|
||||
|
||||
/** @return if the manager is closed. */
|
||||
|
||||
@@ -54,7 +54,7 @@ import java.nio.charset.Charset;
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class PipedConsoleManager
|
||||
implements ConsoleManager, AutoCloseable {
|
||||
implements ConsoleManager {
|
||||
|
||||
/** The encoding between streams */
|
||||
private static final String UTF_8 = "UTF-8"; //$NON-NLS-1$
|
||||
@@ -91,6 +91,7 @@ public final class PipedConsoleManager
|
||||
Thread th = new Thread(writing, "write"); //$NON-NLS-1$
|
||||
th.start();
|
||||
th = new Thread(reading, "read"); //$NON-NLS-1$
|
||||
th.setDaemon(true);
|
||||
th.start();
|
||||
}
|
||||
|
||||
|
||||
@@ -88,6 +88,7 @@ public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
this.in = new BufferedReader(new InputStreamReader(in, charset));
|
||||
reading = new ReadingRunnable(this.in);
|
||||
promptThread = new Thread(reading, "prompt"); //$NON-NLS-1$
|
||||
promptThread.setDaemon(true);
|
||||
promptThread.start();
|
||||
}
|
||||
|
||||
@@ -156,6 +157,7 @@ public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
public void close() throws IOException {
|
||||
closed = true;
|
||||
reading.setRunning(false);
|
||||
promptThread.interrupt();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
||||
@@ -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
|
||||
* Created on: Dec 1, 2016
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
/*
|
||||
* 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 = 2;
|
||||
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");
|
||||
manager.println("before");
|
||||
th.start();
|
||||
|
||||
assertEquals("before", runnable.getMessage());
|
||||
|
||||
synchronized (this) {
|
||||
wait(3000);
|
||||
}
|
||||
manager.println("after");
|
||||
assertEquals("after", runnable.getMessage());
|
||||
synchronized (this) {
|
||||
wait(3000);
|
||||
}
|
||||
|
||||
th.join();
|
||||
|
||||
} catch (IOException | InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user