Code compliance
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
9e040d80c4
commit
ef708c3291
@ -48,14 +48,18 @@ import fr.bigeon.gclc.ConsoleApplication;
|
|||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public class ConsoleRunnable implements Runnable {
|
public class ConsoleRunnable implements Runnable {
|
||||||
|
|
||||||
|
/** The wait timeout */
|
||||||
private static final long TIMEOUT = 100;
|
private static final long TIMEOUT = 100;
|
||||||
|
/** The logger */
|
||||||
private static final Logger LOGGER = Logger
|
private static final Logger LOGGER = Logger
|
||||||
.getLogger(ConsoleRunnable.class.getName());
|
.getLogger(ConsoleRunnable.class.getName());
|
||||||
/** The actual application */
|
/** The actual application */
|
||||||
private final ConsoleApplication app;
|
private final ConsoleApplication app;
|
||||||
|
/** The synchro object */
|
||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
/** the state of this runnable */
|
/** the state of this runnable */
|
||||||
private boolean running = true;
|
private boolean running = true;
|
||||||
|
/** If a start is required */
|
||||||
private boolean startReq;
|
private boolean startReq;
|
||||||
|
|
||||||
/** @param app the application */
|
/** @param app the application */
|
||||||
@ -75,7 +79,7 @@ public class ConsoleRunnable implements Runnable {
|
|||||||
lock.wait(TIMEOUT);
|
lock.wait(TIMEOUT);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
LOGGER.log(Level.SEVERE,
|
LOGGER.log(Level.SEVERE,
|
||||||
"Console application runnable interrupted wildly!",
|
"Console application runnable interrupted wildly!", //$NON-NLS-1$
|
||||||
e);
|
e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -114,6 +118,7 @@ public class ConsoleRunnable implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Request a restart of application */
|
||||||
public void restart() {
|
public void restart() {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
startReq = true;
|
startReq = true;
|
||||||
@ -121,7 +126,7 @@ public class ConsoleRunnable implements Runnable {
|
|||||||
try {
|
try {
|
||||||
lock.wait(TIMEOUT);
|
lock.wait(TIMEOUT);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
LOGGER.log(Level.SEVERE, "Restart wait interrupted!", e);
|
LOGGER.log(Level.SEVERE, "Restart wait interrupted!", e); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,7 +46,6 @@ import java.net.ServerSocket;
|
|||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
@ -54,7 +53,6 @@ import fr.bigeon.gclc.ConsoleApplication;
|
|||||||
import fr.bigeon.gclc.manager.ConsoleManager;
|
import fr.bigeon.gclc.manager.ConsoleManager;
|
||||||
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
import fr.bigeon.gclc.manager.PipedConsoleManager;
|
||||||
import fr.bigeon.gclc.manager.ReadingRunnable;
|
import fr.bigeon.gclc.manager.ReadingRunnable;
|
||||||
import fr.bigeon.smu.StringEncoder;
|
|
||||||
|
|
||||||
/** This is a socket communicating console consoleManager
|
/** This is a socket communicating console consoleManager
|
||||||
* <p>
|
* <p>
|
||||||
@ -76,20 +74,56 @@ import fr.bigeon.smu.StringEncoder;
|
|||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
public class SocketConsoleApplicationShell implements Runnable {
|
public class SocketConsoleApplicationShell implements Runnable {
|
||||||
|
|
||||||
|
/** The runnable to forward output of application to socket.
|
||||||
|
*
|
||||||
|
* @author Emmanuel Bigeon */
|
||||||
|
private final class OutputForwardRunnable implements Runnable {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static final String INTERRUPTION_WHILE_WORKING = "Interruption while application was working"; //$NON-NLS-1$
|
private final PrintWriter writer;
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private final Socket socket;
|
||||||
|
|
||||||
|
/** @param writer the writer
|
||||||
|
* @param socket the socket */
|
||||||
|
protected OutputForwardRunnable(PrintWriter writer, Socket socket) {
|
||||||
|
this.writer = writer;
|
||||||
|
this.socket = socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("synthetic-access")
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
while (!socket.isOutputShutdown()) {
|
||||||
|
while (!socket.isOutputShutdown() &&
|
||||||
|
!consoleManager.available()) {
|
||||||
|
waitASec();
|
||||||
|
}
|
||||||
|
if (socket.isOutputShutdown()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String m = consoleManager.readNextLine();
|
||||||
|
writer.println(m);
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Unexpected problem in manager", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/** The end of line character */
|
/** The end of line character */
|
||||||
protected static final String EOL = "\n"; //$NON-NLS-1$
|
protected static final String EOL = "\n"; //$NON-NLS-1$
|
||||||
/** The encoder */
|
|
||||||
private static final StringEncoder ENCODER = new StringEncoder("%", //$NON-NLS-1$
|
|
||||||
Arrays.asList(EOL));
|
|
||||||
/** The class logger */
|
/** The class logger */
|
||||||
private static final Logger LOGGER = Logger
|
private static final Logger LOGGER = Logger
|
||||||
.getLogger(SocketConsoleApplicationShell.class.getName());
|
.getLogger(SocketConsoleApplicationShell.class.getName());
|
||||||
|
/** Time of wait */
|
||||||
protected static final long ONE_TENTH_OF_SECOND = 100;
|
protected static final long ONE_TENTH_OF_SECOND = 100;
|
||||||
private static final long ONE_SEC = 100;
|
|
||||||
/** The listening port */
|
/** The listening port */
|
||||||
private final int port;
|
private final int port;
|
||||||
/** The input */
|
/** The input */
|
||||||
@ -101,9 +135,6 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
/** The running status */
|
/** The running status */
|
||||||
private boolean running;
|
private boolean running;
|
||||||
|
|
||||||
// /** The console manager implementation */
|
|
||||||
// private final ThreadedServerConsoleManager consoleManager = new ThreadedServerConsoleManager(
|
|
||||||
// ENCODER, promptingLock);
|
|
||||||
/** The console manager implementation */
|
/** The console manager implementation */
|
||||||
private final PipedConsoleManager consoleManager;
|
private final PipedConsoleManager consoleManager;
|
||||||
/** The auto close flag. if this is true, every request closes the session
|
/** The auto close flag. if this is true, every request closes the session
|
||||||
@ -171,8 +202,7 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
charset);
|
charset);
|
||||||
BufferedReader inBuf = new BufferedReader(isr)) {
|
BufferedReader inBuf = new BufferedReader(isr)) {
|
||||||
consoleInput.connect(outStream);
|
consoleInput.connect(outStream);
|
||||||
// consoleManager.setInput(inBuf);
|
runSokectServer();
|
||||||
runSokectServer(writer);
|
|
||||||
// Close the application
|
// Close the application
|
||||||
// Pass command to application
|
// Pass command to application
|
||||||
if (app.isRunning()) {
|
if (app.isRunning()) {
|
||||||
@ -188,11 +218,10 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param writer the writer to the application
|
/** @throws IOException if the communication with the client failed */
|
||||||
* @throws IOException if the communication with the client failed */
|
private void runSokectServer() throws IOException {
|
||||||
private void runSokectServer(BufferedWriter writer) throws IOException {
|
|
||||||
final ConsoleRunnable runnable = new ConsoleRunnable(app);
|
final ConsoleRunnable runnable = new ConsoleRunnable(app);
|
||||||
Thread appThNext = new Thread(runnable, "gclc-ctrl");
|
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("Opening client"); //$NON-NLS-1$
|
||||||
@ -207,18 +236,10 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
|
|
||||||
// Initiate application
|
// Initiate application
|
||||||
if (!runnable.isApplicationRunning()) {
|
if (!runnable.isApplicationRunning()) {
|
||||||
LOGGER.info("Start application");
|
LOGGER.info("Start application"); //$NON-NLS-1$
|
||||||
runnable.restart();
|
startApplication(runnable);
|
||||||
synchronized (this) {
|
|
||||||
try {
|
|
||||||
wait(ONE_SEC);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
LOGGER.log(Level.SEVERE,
|
|
||||||
"Interruption in application start", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
LOGGER.info("Reconnect to application");
|
LOGGER.info("Reconnect to application"); //$NON-NLS-1$
|
||||||
out.println("Reconnected"); //$NON-NLS-1$
|
out.println("Reconnected"); //$NON-NLS-1$
|
||||||
out.println(consoleManager.getPrompt());
|
out.println(consoleManager.getPrompt());
|
||||||
}
|
}
|
||||||
@ -236,44 +257,28 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
LOGGER.info("Out client"); //$NON-NLS-1$
|
LOGGER.info("Out client"); //$NON-NLS-1$
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param runnable the runnable */
|
||||||
|
private void startApplication(ConsoleRunnable runnable) {
|
||||||
|
runnable.restart();
|
||||||
|
synchronized (this) {
|
||||||
|
try {
|
||||||
|
wait(ONE_TENTH_OF_SECOND);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Interruption in application start", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** active communication between server and client
|
/** active communication between server and client
|
||||||
*
|
*
|
||||||
|
* @param socket the socket
|
||||||
* @param writer the writer to the application
|
* @param writer the writer to the application
|
||||||
* @param in the input from the client
|
* @param in the input from the client
|
||||||
* @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 Runnable() {
|
Thread th = new Thread(new OutputForwardRunnable(writer, socket), "ClientComm"); //$NON-NLS-1$
|
||||||
|
|
||||||
@SuppressWarnings("synthetic-access")
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
try {
|
|
||||||
while (!socket.isOutputShutdown()) {
|
|
||||||
while (!socket.isOutputShutdown() &&
|
|
||||||
!consoleManager.available()) {
|
|
||||||
try {
|
|
||||||
synchronized (this) {
|
|
||||||
wait(ONE_TENTH_OF_SECOND);
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Interrupted wait", //$NON-NLS-1$
|
|
||||||
e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (socket.isOutputShutdown()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String m = consoleManager.readNextLine();
|
|
||||||
writer.println(m);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Unexpected problem in manager", //$NON-NLS-1$
|
|
||||||
e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, "ClientComm"); //$NON-NLS-1$
|
|
||||||
th.start();
|
th.start();
|
||||||
if (autoClose) {
|
if (autoClose) {
|
||||||
communicateOnce(socket, in);
|
communicateOnce(socket, in);
|
||||||
@ -282,71 +287,58 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param in the input from the client
|
/** @param socket the socket
|
||||||
|
* @param in the input from the client
|
||||||
* @throws IOException if the communication failed */
|
* @throws IOException if the communication failed */
|
||||||
private void communicateOnce(Socket socket,
|
private void communicateOnce(Socket socket,
|
||||||
BufferedReader in) throws IOException {
|
BufferedReader in) throws IOException {
|
||||||
ReadingRunnable reading = new ReadingRunnable(in);
|
ReadingRunnable reading = new ReadingRunnable(in);
|
||||||
Thread th = new Thread(reading, "gclcToApp");
|
Thread th = new Thread(reading, "gclcToApp"); //$NON-NLS-1$
|
||||||
th.start();
|
th.start();
|
||||||
String ln;
|
|
||||||
if (app.isRunning()) {
|
if (app.isRunning()) {
|
||||||
while (app.isRunning() && !reading.hasMessage()) {
|
communicationContent(reading);
|
||||||
synchronized (this) {
|
|
||||||
try {
|
|
||||||
wait(ONE_SEC);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Wait interrupted", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!app.isRunning()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ln = reading.getMessage();
|
|
||||||
if (ln.equals(close)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Pass command to application
|
|
||||||
consoleManager.type(ln);
|
|
||||||
}
|
}
|
||||||
reading.setRunning(false);
|
reading.setRunning(false);
|
||||||
socket.shutdownOutput();
|
socket.shutdownOutput();
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @param in the input from the client
|
/** @param socket the socket
|
||||||
|
* @param in the input from the client
|
||||||
* @throws IOException if the communication failed */
|
* @throws IOException if the communication failed */
|
||||||
private void communicateLoop(Socket socket,
|
private void communicateLoop(Socket socket,
|
||||||
BufferedReader in) throws IOException {
|
BufferedReader in) throws IOException {
|
||||||
ReadingRunnable reading = new ReadingRunnable(in);
|
ReadingRunnable reading = new ReadingRunnable(in);
|
||||||
Thread th = new Thread(reading, "gclcToApp");
|
Thread th = new Thread(reading, "gclcToApp"); //$NON-NLS-1$
|
||||||
th.start();
|
th.start();
|
||||||
String ln;
|
while (app.isRunning() && communicationContent(reading)) {
|
||||||
while (app.isRunning()) {
|
// keep on going
|
||||||
while (app.isRunning() && !reading.hasMessage()) {
|
|
||||||
synchronized (this) {
|
|
||||||
try {
|
|
||||||
wait(ONE_SEC);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
LOGGER.log(Level.SEVERE, "Wait interrupted", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!app.isRunning()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ln = reading.getMessage();
|
|
||||||
if (ln.equals(close)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// Pass command to application
|
|
||||||
consoleManager.type(ln);
|
|
||||||
}
|
}
|
||||||
reading.setRunning(false);
|
reading.setRunning(false);
|
||||||
socket.shutdownOutput();
|
socket.shutdownOutput();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param reading the reading
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!app.isRunning()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String ln = reading.getMessage();
|
||||||
|
if (ln.equals(close)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// Pass command to application
|
||||||
|
consoleManager.type(ln);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/** @return the consoleManager */
|
/** @return the consoleManager */
|
||||||
public synchronized ConsoleManager getConsoleManager() {
|
public synchronized ConsoleManager getConsoleManager() {
|
||||||
return consoleManager;
|
return consoleManager;
|
||||||
@ -371,4 +363,16 @@ public class SocketConsoleApplicationShell implements Runnable {
|
|||||||
app.exit();
|
app.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** a method to wait some time */
|
||||||
|
protected void waitASec() {
|
||||||
|
try {
|
||||||
|
synchronized (this) {
|
||||||
|
wait(ONE_TENTH_OF_SECOND);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, "Interrupted wait", //$NON-NLS-1$
|
||||||
|
e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,7 +47,7 @@ import fr.bigeon.gclc.manager.ConsoleManager;
|
|||||||
/** Test class for {@link ConsoleRunnable}
|
/** Test class for {@link ConsoleRunnable}
|
||||||
*
|
*
|
||||||
* @author Emmanuel Bigeon */
|
* @author Emmanuel Bigeon */
|
||||||
@SuppressWarnings({"static-method", "unused", "javadoc"})
|
@SuppressWarnings({"unused", "javadoc"})
|
||||||
public class ConsoleRunnableTest {
|
public class ConsoleRunnableTest {
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
|
@ -128,13 +128,14 @@ public class SocketConsoleApplicationTest {
|
|||||||
while ((fromServer = in.readLine()) != null) {
|
while ((fromServer = in.readLine()) != null) {
|
||||||
System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
||||||
while (fromServer != null && !fromServer.equals("> ")) {
|
while (fromServer != null && !fromServer.equals("> ")) {
|
||||||
fromServer = in.readLine();
|
|
||||||
System.out
|
System.out
|
||||||
.println("Server: \n" + ENCODER.decode(fromServer));
|
.println("Server: \n" + ENCODER.decode(fromServer));
|
||||||
|
fromServer = in.readLine();
|
||||||
}
|
}
|
||||||
if (fromServer == null) {
|
if (fromServer == null) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
System.out.println("Server: \n" + ENCODER.decode(fromServer));
|
||||||
|
|
||||||
final String fromUser = cmds[i];
|
final String fromUser = cmds[i];
|
||||||
if (fromUser != null) {
|
if (fromUser != null) {
|
||||||
|
Loading…
Reference in New Issue
Block a user