Factor prompting methods code
Signed-off-by: Emmanuel Bigeon <emmanuel@bigeon.fr>
This commit is contained in:
parent
0cef23e17b
commit
8436e8926c
@ -193,39 +193,7 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
synchronized (promptLock) {
|
synchronized (promptLock) {
|
||||||
prompting = true;
|
prompting = true;
|
||||||
}
|
}
|
||||||
while (true) {
|
return doPrompt(message, 0, 0);
|
||||||
if (!checkPrompt()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
getConnection(0);
|
|
||||||
boolean connect;
|
|
||||||
ConsoleInput actualConnected;
|
|
||||||
synchronized (connectionLock) {
|
|
||||||
connect = connected != null;
|
|
||||||
actualConnected = connected;
|
|
||||||
}
|
|
||||||
if (!connect) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
final String res = actualConnected.prompt(message);
|
|
||||||
synchronized (promptLock) {
|
|
||||||
if (prompting) {
|
|
||||||
prompting = false;
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (final InterruptedIOException e) {
|
|
||||||
// The inner console was interrupted. This can mean we are
|
|
||||||
// disconnecting or actually interrupted.
|
|
||||||
if (disconnection) {
|
|
||||||
disconnection = false;
|
|
||||||
} else {
|
|
||||||
interrupting = false;
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* (non-Javadoc)
|
/* (non-Javadoc)
|
||||||
@ -238,12 +206,17 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
synchronized (promptLock) {
|
synchronized (promptLock) {
|
||||||
prompting = true;
|
prompting = true;
|
||||||
}
|
}
|
||||||
final long end = System.currentTimeMillis() + timeout;
|
final long tic = System.currentTimeMillis();
|
||||||
|
return doPrompt(message, timeout, tic);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String doPrompt(final String message, final long timeout, final long tic)
|
||||||
|
throws InterruptedIOException, IOException {
|
||||||
do {
|
do {
|
||||||
if (!checkPrompt()) {
|
if (!checkPrompt()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
getConnection(timeout);
|
getConnection(getTimeoutLeft(tic, timeout));
|
||||||
boolean connect;
|
boolean connect;
|
||||||
ConsoleInput actualConnected;
|
ConsoleInput actualConnected;
|
||||||
synchronized (connectionLock) {
|
synchronized (connectionLock) {
|
||||||
@ -254,8 +227,13 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
final String res = actualConnected.prompt(message,
|
final long timeoutLeft = getTimeoutLeft(tic, timeout);
|
||||||
end - System.currentTimeMillis());
|
final String res;
|
||||||
|
if (timeoutLeft == 0) {
|
||||||
|
res = actualConnected.prompt(message);
|
||||||
|
} else {
|
||||||
|
res = actualConnected.prompt(message, timeoutLeft);
|
||||||
|
}
|
||||||
synchronized (promptLock) {
|
synchronized (promptLock) {
|
||||||
if (prompting) {
|
if (prompting) {
|
||||||
prompting = false;
|
prompting = false;
|
||||||
@ -272,22 +250,34 @@ public final class ConnectingConsoleInput implements ConsoleInput {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (System.currentTimeMillis() < end);
|
} while (checkTimeout(tic, timeout));
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean checkTimeout(final long tic, final long timeout) {
|
||||||
|
return timeout <= 0 || tic + timeout > System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long getTimeoutLeft(final long tic, final long timeout) {
|
||||||
|
if (timeout > 0) {
|
||||||
|
return Math.max(timeout + tic - System.currentTimeMillis(), 1);
|
||||||
|
}
|
||||||
|
if (timeout < 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/** Test if we are in prompting state.
|
/** Test if we are in prompting state.
|
||||||
*
|
*
|
||||||
* @return if the process is currently in prompting state.
|
* @return if the process is currently in prompting state.
|
||||||
* @throws InterruptedIOException if the prompting state has been interrupted */
|
* @throws InterruptedIOException if the prompting state has been interrupted */
|
||||||
private boolean checkPrompt() throws InterruptedIOException {
|
private boolean checkPrompt() throws InterruptedIOException {
|
||||||
synchronized (promptLock) {
|
synchronized (promptLock) {
|
||||||
if (!prompting) {
|
if (!prompting && interrupting) {
|
||||||
if (interrupting) {
|
|
||||||
interrupting = false;
|
interrupting = false;
|
||||||
throw new InterruptedIOException("Prompt was interrupted");
|
throw new InterruptedIOException("Prompt was interrupted");
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return prompting;
|
return prompting;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user