Added piped stream manager
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
@@ -193,26 +193,7 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
return;
|
||||
}
|
||||
do {
|
||||
try {
|
||||
final String cmd = manager.prompt();
|
||||
if (cmd == null || cmd.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
for (final CommandRequestListener listener : listeners) {
|
||||
listener.commandRequest(cmd);
|
||||
}
|
||||
interpretCommand(cmd);
|
||||
} catch (InterruptedIOException e) {
|
||||
LOGGER.log(Level.INFO,
|
||||
"Prompt interrupted. It is likely the application is closing.", //$NON-NLS-1$
|
||||
e);
|
||||
} catch (IOException e) {
|
||||
// The manager was closed
|
||||
running = false;
|
||||
LOGGER.log(Level.WARNING,
|
||||
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
|
||||
e);
|
||||
}
|
||||
runLoop();
|
||||
} while (running);
|
||||
if (footer != null) {
|
||||
try {
|
||||
@@ -229,6 +210,33 @@ public class ConsoleApplication implements ICommandProvider {
|
||||
LOGGER.fine("Exiting application."); //$NON-NLS-1$
|
||||
}
|
||||
|
||||
/** The running loop content.
|
||||
* <p>
|
||||
* This consisting in getting the command, executing it and exiting
|
||||
* (restarting the loop). */
|
||||
private void runLoop() {
|
||||
try {
|
||||
final String cmd = manager.prompt();
|
||||
if (cmd == null || cmd.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
for (final CommandRequestListener listener : listeners) {
|
||||
listener.commandRequest(cmd);
|
||||
}
|
||||
interpretCommand(cmd);
|
||||
} catch (InterruptedIOException e) {
|
||||
LOGGER.log(Level.INFO,
|
||||
"Prompt interrupted. It is likely the application is closing.", //$NON-NLS-1$
|
||||
e);
|
||||
} catch (IOException e) {
|
||||
// The manager was closed
|
||||
running = false;
|
||||
LOGGER.log(Level.WARNING,
|
||||
"The console manager was closed. Closing the application as no one can reach it.", //$NON-NLS-1$
|
||||
e);
|
||||
}
|
||||
}
|
||||
|
||||
/** @return the running status */
|
||||
public boolean isRunning() {
|
||||
return running;
|
||||
|
||||
@@ -72,13 +72,13 @@ public interface ConsoleManager {
|
||||
/** @return the user inputed string
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt() throws IOException, InterruptedIOException;
|
||||
String prompt() throws IOException;
|
||||
|
||||
/** @param message the message to prompt the user
|
||||
* @return the user inputed string
|
||||
* @throws IOException if the manager is closed or could not read the prompt
|
||||
* @throws InterruptedIOException if the prompt was interrupted */
|
||||
String prompt(String message) throws IOException, InterruptedIOException;
|
||||
String prompt(String message) throws IOException;
|
||||
|
||||
/** <p>
|
||||
* Set a prompting prefix.
|
||||
@@ -94,6 +94,7 @@ public interface ConsoleManager {
|
||||
/** @return if the manager is closed. */
|
||||
boolean isClosed();
|
||||
|
||||
/** Indicate to the manager that is should interrompt the prompting */
|
||||
/** Indicate to the manager that is should interrompt the prompting, if
|
||||
* possible. */
|
||||
void interruptPrompt();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* 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-test:fr.bigeon.gclc.test.TestConsoleManager.java
|
||||
* Created on: Nov 18, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PipedInputStream;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
/** This console manager allows to enter commands and retrieve the output as an
|
||||
* input.
|
||||
* <p>
|
||||
* This console manager is used to internally pilot an application. This can be
|
||||
* used to test application behavior.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public final class PipedConsoleManager
|
||||
implements ConsoleManager, AutoCloseable {
|
||||
|
||||
/** THe inner manager */
|
||||
private final SystemConsoleManager innerManager;
|
||||
/** The stream to pipe commands into */
|
||||
private final PipedOutputStream commandInput;
|
||||
/** The reader to get application return from */
|
||||
private final BufferedReader commandBuffOutput;
|
||||
/** The stream to get application return from */
|
||||
private final PipedInputStream commandOutput;
|
||||
/** The print writer for application to write return to */
|
||||
private final PrintStream outPrint;
|
||||
/** The stream for the application to read commands from */
|
||||
private final PipedInputStream in;
|
||||
/** The writing thread */
|
||||
private final WritingRunnable writing;
|
||||
/** The reading thread */
|
||||
private final ReadingRunnable reading;
|
||||
|
||||
/** @throws IOException if the piping failed for streams */
|
||||
public PipedConsoleManager() throws IOException {
|
||||
commandInput = new PipedOutputStream();
|
||||
in = new PipedInputStream(commandInput);
|
||||
commandOutput = new PipedInputStream();
|
||||
PipedOutputStream out = new PipedOutputStream(commandOutput);
|
||||
commandBuffOutput = new BufferedReader(
|
||||
new InputStreamReader(commandOutput, Charset.forName("UTF-8")));
|
||||
outPrint = new PrintStream(out, true, "UTF-8");
|
||||
innerManager = new SystemConsoleManager(outPrint, in,
|
||||
Charset.forName("UTF-8")); //$NON-NLS-1$
|
||||
writing = new WritingRunnable(commandInput, Charset.forName("UTF-8"));
|
||||
reading = new ReadingRunnable(commandBuffOutput);
|
||||
Thread th = new Thread(writing, "write"); //$NON-NLS-1$
|
||||
th.start();
|
||||
th = new Thread(reading, "read"); //$NON-NLS-1$
|
||||
th.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPrompt() {
|
||||
return innerManager.getPrompt();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void print(String object) throws IOException {
|
||||
innerManager.print(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println() throws IOException {
|
||||
innerManager.println();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void println(String object) throws IOException {
|
||||
innerManager.println(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String prompt() throws IOException {
|
||||
return innerManager
|
||||
.prompt(innerManager.getPrompt() + System.lineSeparator());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String prompt(String message) throws IOException {
|
||||
return innerManager.prompt(message + System.lineSeparator());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPrompt(String prompt) {
|
||||
innerManager.setPrompt(prompt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
innerManager.close();
|
||||
reading.setRunning(false);
|
||||
writing.setRunning(false);
|
||||
outPrint.close();
|
||||
commandBuffOutput.close();
|
||||
commandOutput.close();
|
||||
in.close();
|
||||
commandInput.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return innerManager.isClosed();
|
||||
}
|
||||
|
||||
/** @param content the content to type to the application
|
||||
* @throws IOException if the typing failed */
|
||||
public void type(String content) throws IOException {
|
||||
writing.addMessage(content);
|
||||
}
|
||||
|
||||
/** @return the content of the next line written by the application
|
||||
* @throws IOException if the reading failed */
|
||||
public String readNextLine() throws IOException {
|
||||
return reading.getMessage();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
||||
@Override
|
||||
public void interruptPrompt() {
|
||||
innerManager.interruptPrompt();
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
/**
|
||||
* gclc:fr.bigeon.gclc.manager.ReadRunnable.java
|
||||
* Created on: Nov 21, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon
|
||||
*
|
||||
*/
|
||||
public class ReadRunnable implements Runnable {
|
||||
|
||||
/** The logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(ReadRunnable.class.getName());
|
||||
/** The result */
|
||||
private String result = "";
|
||||
/** The input buffer */
|
||||
private final BufferedReader in;
|
||||
|
||||
/** @param in the input buffer */
|
||||
protected ReadRunnable(BufferedReader in) {
|
||||
super();
|
||||
this.in = in;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
result = in.readLine();
|
||||
while (result != null && result.length() > 0 &&
|
||||
result.charAt(0) == 0) {
|
||||
result = result.substring(1);
|
||||
}
|
||||
} catch (final IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Unable to read prompt", e); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
|
||||
/** @return the result */
|
||||
public String getResult() {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
154
gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java
Normal file
154
gclc/src/main/java/fr/bigeon/gclc/manager/ReadingRunnable.java
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* 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.test.utils.WritingRunnable.java
|
||||
* Created on: Nov 29, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InterruptedIOException;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/** A runnable to read the piped output.
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class ReadingRunnable implements Runnable {
|
||||
|
||||
/** Wait timeout */
|
||||
private static final long TIMEOUT = 1000;
|
||||
/** Class logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(ReadingRunnable.class.getName());
|
||||
/** Read messages */
|
||||
private final Deque<String> messages = new ArrayDeque<>();
|
||||
/** the reader */
|
||||
private final BufferedReader reader;
|
||||
/** the state of this runnable */
|
||||
private boolean running = true;
|
||||
|
||||
/** Synchro object */
|
||||
private final Object lock = new Object();
|
||||
|
||||
/** @param reader the input to read from */
|
||||
public ReadingRunnable(BufferedReader reader) {
|
||||
super();
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while (running) {
|
||||
try {
|
||||
String line = reader.readLine();
|
||||
if (line == null) {
|
||||
// Buffer end
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
LOGGER.finer("Read: " + line); //$NON-NLS-1$
|
||||
line = stripNull(line);
|
||||
synchronized (lock) {
|
||||
messages.add(line);
|
||||
lock.notify();
|
||||
}
|
||||
} catch (InterruptedIOException e) {
|
||||
LOGGER.log(Level.INFO, "Reading interrupted", e); //$NON-NLS-1$
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Unable to read from stream", e); //$NON-NLS-1$
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Strip the string from head NULL characters.
|
||||
*
|
||||
* @param line the line to strip the null character from
|
||||
* @return the resulting string */
|
||||
private String stripNull(String line) {
|
||||
String res = line;
|
||||
while (res.length() > 0 && res.charAt(0) == 0) {
|
||||
LOGGER.severe(
|
||||
"NULL character heading the result of the read. This is a stream problem...");
|
||||
res = res.substring(1);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/** @return the next read message
|
||||
* @throws IOException if the pipe is closed */
|
||||
public String getMessage() throws IOException {
|
||||
synchronized (lock) {
|
||||
if (!running) {
|
||||
throw new IOException("Closed pipe"); //$NON-NLS-1$
|
||||
}
|
||||
while (messages.isEmpty()) {
|
||||
try {
|
||||
lock.wait(TIMEOUT);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.log(Level.SEVERE, "Thread interruption exception.", //$NON-NLS-1$
|
||||
e);
|
||||
}
|
||||
if (messages.isEmpty() && !running) {
|
||||
throw new IOException("Closed pipe"); //$NON-NLS-1$
|
||||
}
|
||||
}
|
||||
LOGGER.finest("Polled: " + messages.peek()); //$NON-NLS-1$
|
||||
return messages.poll();
|
||||
}
|
||||
}
|
||||
|
||||
/** @param running the running to set */
|
||||
public void setRunning(boolean running) {
|
||||
synchronized (lock) {
|
||||
this.running = running;
|
||||
}
|
||||
}
|
||||
|
||||
/** @return the running */
|
||||
public boolean isRunning() {
|
||||
synchronized (lock) {
|
||||
return running;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -44,25 +44,17 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/** A console using the input stream and print stream.
|
||||
* <p>
|
||||
* The default constructor will use the system standart input and output.
|
||||
*
|
||||
* @author Emmanuel BIGEON */
|
||||
public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
|
||||
/** The default prompt */
|
||||
public static final String DEFAULT_PROMPT = "> "; //$NON-NLS-1$
|
||||
|
||||
/** The logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(SystemConsoleManager.class.getName());
|
||||
|
||||
/** The empty string constant */
|
||||
private static final String EMPTY = ""; //$NON-NLS-1$
|
||||
|
||||
/** The command prompt. It can be changed. */
|
||||
private String prompt = DEFAULT_PROMPT;
|
||||
|
||||
@@ -74,7 +66,11 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
/** If the manager is closed */
|
||||
private boolean closed = false;
|
||||
|
||||
private Thread reading;
|
||||
/** The prompting thread */
|
||||
private final Thread promptThread;
|
||||
|
||||
/** The reading runnable */
|
||||
private final ReadingRunnable reading;
|
||||
|
||||
/** This default constructor relies on the system defined standart output
|
||||
* and input stream. */
|
||||
@@ -90,6 +86,9 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
super();
|
||||
this.out = out;
|
||||
this.in = new BufferedReader(new InputStreamReader(in, charset));
|
||||
reading = new ReadingRunnable(this.in);
|
||||
promptThread = new Thread(reading, "prompt"); //$NON-NLS-1$
|
||||
promptThread.start();
|
||||
}
|
||||
|
||||
/** @return the prompt */
|
||||
@@ -142,23 +141,7 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
public String prompt(String message) throws IOException {
|
||||
checkOpen();
|
||||
out.print(message);
|
||||
// ReadRunnable rr = new ReadRunnable(in);
|
||||
// reading = new Thread(rr, "prompt"); //$NON-NLS-1$
|
||||
// reading.start();
|
||||
// try {
|
||||
// reading.join();
|
||||
// } catch (final InterruptedException e) {
|
||||
// LOGGER.log(Level.SEVERE, "Prompt reading interrupted", e); //$NON-NLS-1$
|
||||
// throw new InterruptedIOException("Prompt interruption"); //$NON-NLS-1$
|
||||
// }
|
||||
String result = EMPTY;
|
||||
result = in.readLine();
|
||||
while (result != null && result.length() > 0 &&
|
||||
result.charAt(0) == 0) {
|
||||
result = result.substring(1);
|
||||
}
|
||||
return result;
|
||||
// return rr.getResult();
|
||||
return reading.getMessage();
|
||||
}
|
||||
|
||||
/** @param prompt the prompt to set */
|
||||
@@ -172,6 +155,7 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
closed = true;
|
||||
reading.setRunning(false);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
@@ -181,13 +165,12 @@ public class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
||||
return closed;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
/** Beware, in this implementation this is the same as closing the manager.
|
||||
*
|
||||
* @see fr.bigeon.gclc.manager.ConsoleManager#interruptPrompt() */
|
||||
@Override
|
||||
public void interruptPrompt() {
|
||||
if (reading != null) {
|
||||
reading.interrupt();
|
||||
}
|
||||
promptThread.interrupt();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
138
gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java
Normal file
138
gclc/src/main/java/fr/bigeon/gclc/manager/WritingRunnable.java
Normal file
@@ -0,0 +1,138 @@
|
||||
/*
|
||||
* 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.test.utils.WritingRunnable.java
|
||||
* Created on: Nov 29, 2016
|
||||
*/
|
||||
package fr.bigeon.gclc.manager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PipedOutputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Deque;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
/** <p>
|
||||
* TODO
|
||||
*
|
||||
* @author Emmanuel Bigeon */
|
||||
public class WritingRunnable implements Runnable {
|
||||
|
||||
/** Wait timeout */
|
||||
private static final long TIMEOUT = 1000;
|
||||
/** Class logger */
|
||||
private static final Logger LOGGER = Logger
|
||||
.getLogger(WritingRunnable.class.getName());
|
||||
/** Messages to write */
|
||||
private final Deque<String> messages = new ArrayDeque<>();
|
||||
/** Stream to write to */
|
||||
private final PipedOutputStream outPrint;
|
||||
/** The charset */
|
||||
private final Charset charset;
|
||||
/** Runnable state */
|
||||
private boolean running = true;
|
||||
|
||||
/** Synchro object */
|
||||
private final Object lock = new Object();
|
||||
|
||||
/** @param outPrint the output to print to */
|
||||
public WritingRunnable(PipedOutputStream outPrint, Charset charset) {
|
||||
super();
|
||||
this.outPrint = outPrint;
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see java.lang.Runnable#run() */
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
while (running) {
|
||||
synchronized (lock) {
|
||||
while (messages.isEmpty()) {
|
||||
try {
|
||||
lock.wait(TIMEOUT);
|
||||
} catch (InterruptedException e) {
|
||||
LOGGER.log(Level.SEVERE,
|
||||
"Thread interruption exception.", e); //$NON-NLS-1$
|
||||
}
|
||||
if (!running) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
String message = messages.poll();
|
||||
ByteBuffer buff = charset
|
||||
.encode(message + System.lineSeparator());
|
||||
if (buff.hasArray()) {
|
||||
try {
|
||||
outPrint.write(buff.array());
|
||||
} catch (IOException e) {
|
||||
LOGGER.log(Level.SEVERE, "Unable to write to stream", //$NON-NLS-1$
|
||||
e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** @param message the message
|
||||
* @throws IOException if the pipe is closed */
|
||||
public void addMessage(String message) throws IOException {
|
||||
synchronized (lock) {
|
||||
if (!running) {
|
||||
throw new IOException("Closed pipe"); //$NON-NLS-1$
|
||||
}
|
||||
messages.offer(message);
|
||||
lock.notify();
|
||||
}
|
||||
}
|
||||
|
||||
/** @param running the running to set */
|
||||
public void setRunning(boolean running) {
|
||||
synchronized (lock) {
|
||||
this.running = running;
|
||||
}
|
||||
}
|
||||
|
||||
/** @return the running */
|
||||
public boolean isRunning() {
|
||||
synchronized (lock) {
|
||||
return running;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user