Added prompting interruption, add exception in parameterized commands

Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
2016-11-29 16:37:20 -05:00
parent fd8dde32f1
commit 65b6be8283
11 changed files with 264 additions and 72 deletions

View File

@@ -44,7 +44,7 @@ import fr.bigeon.gclc.ConsoleApplication;
*
* @author Emmanuel Bigeon */
public class ConsoleRunnable implements Runnable {
/** The actual application */
private final ConsoleApplication app;
/** The synchronization object */
@@ -74,4 +74,9 @@ public class ConsoleRunnable implements Runnable {
app.exit();
}
/** @return if the application is running */
public boolean isRunning() {
return app.isRunning();
}
}

View File

@@ -110,7 +110,7 @@ public class SocketConsoleApplicationShell implements Runnable {
/** The application shutdown string */
private final String applicationShutdown;
/** The charset for the communication. */
private Charset charset;
private final Charset charset;
/** Create a socket application shell which will listen on the given port
* and close session upon the provided string reception by client
@@ -156,8 +156,7 @@ public class SocketConsoleApplicationShell implements Runnable {
// Create the streams
try (PipedOutputStream outStream = new PipedOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(outStream,
charset));
new OutputStreamWriter(outStream, charset));
InputStreamReader isr = new InputStreamReader(consoleInput,
charset);
BufferedReader inBuf = new BufferedReader(isr)) {
@@ -187,24 +186,24 @@ public class SocketConsoleApplicationShell implements Runnable {
Thread appThOld = null;
Thread appThNext = new Thread(runnable);
while (running) {
LOGGER.info("Opening client"); //$NON-NLS-1$
try (Socket clientSocket = serverSocket.accept();
PrintWriter out = new PrintWriter(
new OutputStreamWriter(clientSocket.getOutputStream(),
charset),
true);
PrintWriter out = new PrintWriter(new OutputStreamWriter(
clientSocket.getOutputStream(), charset), true);
InputStreamReader isr = new InputStreamReader(
clientSocket.getInputStream(),
charset);
clientSocket.getInputStream(), charset);
BufferedReader in = new BufferedReader(isr);) {
// this is not threaded to avoid several clients at the same
// time
consoleManager.setOutput(out);
// Initiate application
if (appThOld == null || !appThOld.isAlive()) {
if (appThOld == null || !appThOld.isAlive() ||
!runnable.isRunning()) {
appThNext.start();
// Prepare next start
appThOld = appThNext;
appThNext = new Thread(runnable);
appThNext = new Thread(runnable, "gclc-ctrl"); //$NON-NLS-1$
} else {
out.println("Reconnected"); //$NON-NLS-1$
}
@@ -215,7 +214,9 @@ public class SocketConsoleApplicationShell implements Runnable {
"Socket closed with exception (probably due to server interruption)", //$NON-NLS-1$
e);
}
LOGGER.info("Closing client"); //$NON-NLS-1$
}
LOGGER.info("Out client"); //$NON-NLS-1$
}
/** active communication between server and client

View File

@@ -38,6 +38,9 @@
*/
package fr.bigeon.gclc.socket;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@@ -60,7 +63,8 @@ public class SocketConsoleApplicationTest {
@Test
public void integrationTest() {
Thread server = TestServer.startServer(false);
Thread server;
server = TestServer.startServer("bye");
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
@@ -70,27 +74,89 @@ public class SocketConsoleApplicationTest {
final int portNumber = 3300;
try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));) {
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));) {
String fromServer;
int i = 0;
String[] cmds = {"help", "toto", "test", "close"};
String[] cmds = {"help", "toto", "test", "bye"};
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: \n" + ENCODER.decode(fromServer));
// System.out.println("Server: \n" + ENCODER.decode(fromServer));
if (fromServer.equals("Bye.")) {
break;
}
final String fromUser = cmds[i];
if (fromUser != null) {
System.out.println("Client: " + fromUser);
// System.out.println("Client: " + fromUser);
out.println(fromUser);
}
i++;
}
assertEquals(4, i);
} catch (final IOException e) {
e.printStackTrace();
}
try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));) {
String fromServer;
int i = 0;
String[] cmds = {"help", "toto", "test",
ConsoleTestApplication.EXIT};
while ((fromServer = in.readLine()) != null) {
// System.out.println("Server: \n" + ENCODER.decode(fromServer));
if (fromServer.equals("Bye.")) {
break;
}
final String fromUser = cmds[i];
if (fromUser != null) {
// System.out.println("Client: " + fromUser);
out.println(fromUser);
}
i++;
}
assertEquals(4, i);
} catch (final IOException e) {
e.printStackTrace();
}
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));) {
String fromServer;
int i = 0;
String[] cmds = {"help", "toto", "test",
ConsoleTestApplication.EXIT};
while ((fromServer = in.readLine()) != null) {
// System.out.println("Server: \n" + ENCODER.decode(fromServer));
if (fromServer.equals("Bye.")) {
break;
}
final String fromUser = cmds[i];
if (fromUser != null) {
// System.out.println("Client: " + fromUser);
out.println(fromUser);
}
i++;
}
assertEquals(4, i);
} catch (final IOException e) {
e.printStackTrace();
}
@@ -108,31 +174,39 @@ public class SocketConsoleApplicationTest {
}
try (Socket kkSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));) {
PrintWriter out = new PrintWriter(kkSocket.getOutputStream(),
true);
BufferedReader in = new BufferedReader(
new InputStreamReader(kkSocket.getInputStream()));) {
String fromServer;
int i = 0;
String[] cmds = {"help", "test", "close"};
while ((fromServer = in.readLine()) != null) {
// System.out.println("Server: \n" + ENCODER.decode(fromServer));
assertTrue(i < 2);
System.out.println("Server: \n" + ENCODER.decode(fromServer));
if (fromServer.equals("Bye.")) {
break;
}
final String fromUser = cmds[i];
if (fromUser != null) {
// System.out.println("Client: " + fromUser);
System.out.println("Client: " + fromUser);
out.println(fromUser);
}
i++;
}
assertEquals(2, i);
} catch (final IOException e) {
e.printStackTrace();
}
Thread srv = TestServer.getServer();
TestServer.closeServer();
try {
srv.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@@ -85,6 +85,18 @@ public class TestServer {
return getServer();
}
public static Thread startServer(String closeConnection) {
if (SHELL == null) {
SHELL = new SocketConsoleApplicationShell(3300, closeConnection,
ConsoleTestApplication.EXIT, Charset.forName("UTF-8"));
final ConsoleTestApplication app = new ConsoleTestApplication(
SHELL.getConsoleManager());
SHELL.setApplication(app);
server = null;
}
return getServer();
}
public static void closeServer() {
SHELL.stop();
SHELL = null;