Add prompt with timeout
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
8322454f72
commit
b93c2b5220
@ -74,12 +74,25 @@ public interface ConsoleManager extends AutoCloseable {
|
|||||||
* @throws InterruptedIOException if the prompt was interrupted */
|
* @throws InterruptedIOException if the prompt was interrupted */
|
||||||
String prompt() throws IOException;
|
String prompt() throws IOException;
|
||||||
|
|
||||||
|
/** @param timeout the time to wait in milliseconds
|
||||||
|
* @return the user inputed string, null if the timeout was reached
|
||||||
|
* @throws IOException if the manager is closed or could not read the prompt
|
||||||
|
* @throws InterruptedIOException if the prompt was interrupted */
|
||||||
|
String prompt(long timeout) throws IOException;
|
||||||
|
|
||||||
/** @param message the message to prompt the user
|
/** @param message the message to prompt the user
|
||||||
* @return the user inputed string
|
* @return the user inputed string
|
||||||
* @throws IOException if the manager is closed or could not read the prompt
|
* @throws IOException if the manager is closed or could not read the prompt
|
||||||
* @throws InterruptedIOException if the prompt was interrupted */
|
* @throws InterruptedIOException if the prompt was interrupted */
|
||||||
String prompt(String message) throws IOException;
|
String prompt(String message) throws IOException;
|
||||||
|
|
||||||
|
/** @param timeout the time to wait in milliseconds
|
||||||
|
* @param message the message to prompt the user
|
||||||
|
* @return the user inputed string, null if the timeout was reached
|
||||||
|
* @throws IOException if the manager is closed or could not read the prompt
|
||||||
|
* @throws InterruptedIOException if the prompt was interrupted */
|
||||||
|
String prompt(String message, long timeout) throws IOException;
|
||||||
|
|
||||||
/** <p>
|
/** <p>
|
||||||
* Set a prompting prefix.
|
* Set a prompting prefix.
|
||||||
*
|
*
|
||||||
|
@ -121,11 +121,23 @@ public final class PipedConsoleManager
|
|||||||
.prompt(innerManager.getPrompt() + System.lineSeparator());
|
.prompt(innerManager.getPrompt() + System.lineSeparator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.manager.ConsoleManager#prompt(long) */
|
||||||
|
@Override
|
||||||
|
public String prompt(long timeout) throws IOException {
|
||||||
|
return innerManager.prompt(timeout);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String prompt(String message) throws IOException {
|
public String prompt(String message) throws IOException {
|
||||||
return innerManager.prompt(message + System.lineSeparator());
|
return innerManager.prompt(message + System.lineSeparator());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String prompt(String message, long timeout) throws IOException {
|
||||||
|
return innerManager.prompt(message + System.lineSeparator(), timeout);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPrompt(String prompt) {
|
public void setPrompt(String prompt) {
|
||||||
innerManager.setPrompt(prompt);
|
innerManager.setPrompt(prompt);
|
||||||
|
@ -69,8 +69,7 @@ public class ReadingRunnable implements Runnable {
|
|||||||
/** @param obj the object to lock on
|
/** @param obj the object to lock on
|
||||||
* @param start the object to notify when ready to wait
|
* @param start the object to notify when ready to wait
|
||||||
* @param message the message to wait for */
|
* @param message the message to wait for */
|
||||||
public ToWaitRunnable(Object obj, Object start,
|
public ToWaitRunnable(Object obj, Object start, String message) {
|
||||||
String message) {
|
|
||||||
this.obj = obj;
|
this.obj = obj;
|
||||||
this.start = start;
|
this.start = start;
|
||||||
this.message = message;
|
this.message = message;
|
||||||
@ -91,8 +90,8 @@ public class ReadingRunnable implements Runnable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
LOGGER.log(Level.SEVERE,
|
LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION,
|
||||||
THREAD_INTERRUPTION_EXCEPTION, e);
|
e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -126,13 +125,9 @@ public class ReadingRunnable implements Runnable {
|
|||||||
private final Object lock = new Object();
|
private final Object lock = new Object();
|
||||||
/** The waiting status for a message */
|
/** The waiting status for a message */
|
||||||
private boolean waiting;
|
private boolean waiting;
|
||||||
/**
|
/** The blocker for a given message */
|
||||||
* The blocker for a given message
|
|
||||||
*/
|
|
||||||
private final Map<String, Object> messageBlocker = new HashMap<>();
|
private final Map<String, Object> messageBlocker = new HashMap<>();
|
||||||
/**
|
/** The lock */
|
||||||
* The lock
|
|
||||||
*/
|
|
||||||
private final Object messageBlockerLock = new Object();
|
private final Object messageBlockerLock = new Object();
|
||||||
/** The message being delivered */
|
/** The message being delivered */
|
||||||
private String delivering;
|
private String delivering;
|
||||||
@ -202,8 +197,7 @@ public class ReadingRunnable implements Runnable {
|
|||||||
try {
|
try {
|
||||||
lock.wait(TIMEOUT);
|
lock.wait(TIMEOUT);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION,
|
LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION, e);
|
||||||
e);
|
|
||||||
}
|
}
|
||||||
if (messages.isEmpty() && !running) {
|
if (messages.isEmpty() && !running) {
|
||||||
throw new IOException(CLOSED_PIPE);
|
throw new IOException(CLOSED_PIPE);
|
||||||
@ -216,6 +210,28 @@ public class ReadingRunnable implements Runnable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getNextMessage(long timeout) throws IOException {
|
||||||
|
synchronized (lock) {
|
||||||
|
if (!running) {
|
||||||
|
throw new IOException(CLOSED_PIPE);
|
||||||
|
}
|
||||||
|
waiting = true;
|
||||||
|
try {
|
||||||
|
lock.wait(timeout);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LOGGER.log(Level.SEVERE, THREAD_INTERRUPTION_EXCEPTION, e);
|
||||||
|
}
|
||||||
|
if (messages.isEmpty() && !running) {
|
||||||
|
throw new IOException(CLOSED_PIPE);
|
||||||
|
}
|
||||||
|
waiting = false;
|
||||||
|
if (messages.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return messages.poll();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** @param running the running to set */
|
/** @param running the running to set */
|
||||||
public void setRunning(boolean running) {
|
public void setRunning(boolean running) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
|
@ -136,6 +136,13 @@ public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
return prompt(prompt);
|
return prompt(prompt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.manager.ConsoleManager#prompt(long) */
|
||||||
|
@Override
|
||||||
|
public String prompt(long timeout) throws IOException {
|
||||||
|
return prompt(prompt, timeout);
|
||||||
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
|
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
|
||||||
@Override
|
@Override
|
||||||
@ -145,6 +152,15 @@ public final class SystemConsoleManager implements ConsoleManager { // NOSONAR
|
|||||||
return reading.getMessage();
|
return reading.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* (non-Javadoc)
|
||||||
|
* @see fr.bigeon.gclc.ConsoleManager#prompt(java.lang.String) */
|
||||||
|
@Override
|
||||||
|
public String prompt(String message, long timeout) throws IOException {
|
||||||
|
checkOpen();
|
||||||
|
out.print(message);
|
||||||
|
return reading.getNextMessage(timeout);
|
||||||
|
}
|
||||||
|
|
||||||
/** @param prompt the prompt to set */
|
/** @param prompt the prompt to set */
|
||||||
@Override
|
@Override
|
||||||
public void setPrompt(String prompt) {
|
public void setPrompt(String prompt) {
|
||||||
|
Loading…
Reference in New Issue
Block a user