diff --git a/gclc-process/src/main/java/net/bigeon/gclc/process/io/ConnectingConsoleInput.java b/gclc-process/src/main/java/net/bigeon/gclc/process/io/ConnectingConsoleInput.java index c77d719..e0c75b2 100644 --- a/gclc-process/src/main/java/net/bigeon/gclc/process/io/ConnectingConsoleInput.java +++ b/gclc-process/src/main/java/net/bigeon/gclc/process/io/ConnectingConsoleInput.java @@ -194,14 +194,8 @@ public final class ConnectingConsoleInput implements ConsoleInput { prompting = true; } while (true) { - synchronized (promptLock) { - if (!prompting) { - if (interrupting) { - interrupting = false; - throw new InterruptedIOException("Prompt was interrupted"); - } - return null; - } + if (!checkPrompt()) { + return null; } getConnection(0); boolean connect; @@ -210,25 +204,26 @@ public final class ConnectingConsoleInput implements ConsoleInput { connect = connected != null; actualConnected = connected; } - if (connect) { - 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; + 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; + } } } } @@ -245,14 +240,8 @@ public final class ConnectingConsoleInput implements ConsoleInput { } final long end = System.currentTimeMillis() + timeout; do { - synchronized (promptLock) { - if (!prompting) { - if (interrupting) { - interrupting = false; - throw new InterruptedIOException("Prompt was interrupted"); - } - break; - } + if (!checkPrompt()) { + break; } getConnection(timeout); boolean connect; @@ -261,31 +250,48 @@ public final class ConnectingConsoleInput implements ConsoleInput { connect = connected != null; actualConnected = connected; } - if (connect) { - try { - final String res = actualConnected.prompt(message, - end - System.currentTimeMillis()); - 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; + if (!connect) { + continue; + } + try { + final String res = actualConnected.prompt(message, + end - System.currentTimeMillis()); + 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; + } } } while (System.currentTimeMillis() < end); return null; } + /** Test if we are in prompting state. + * + * @return if the process is currently in prompting state. + * @throws InterruptedIOException if the prompting state has been interrupted */ + private boolean checkPrompt() throws InterruptedIOException { + synchronized (promptLock) { + if (!prompting) { + if (interrupting) { + interrupting = false; + throw new InterruptedIOException("Prompt was interrupted"); + } + } + return prompting; + } + } + private void getConnection(final long timeout) throws InterruptedIOException { boolean connect; synchronized (connectionLock) {