Rename waitForCondition() to waitForConditionOrConnectionException()

To make it clear that this will either return if the condition is
true *or* if a connection exception happened.

Also introduce waitFor(), which is deliberately not named
waitForCondition() because it carries a different semantic.
This commit is contained in:
Florian Schmaus 2020-06-17 20:27:02 +02:00
parent f9292a23fb
commit ddc39030d7
2 changed files with 12 additions and 8 deletions

View File

@ -688,7 +688,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
}
/**
* We use an extra object for {@link #notifyWaitingThreads()} and {@link #waitForCondition(Supplier)}, because all state
* We use an extra object for {@link #notifyWaitingThreads()} and {@link #waitForConditionOrConnectionException(Supplier)}, because all state
* changing methods of the connection are synchronized using the connection instance as monitor. If we now would
* also use the connection instance for the internal process to wait for a condition, the {@link Object#wait()}
* would leave the monitor when it waites, which would allow for another potential call to a state changing function
@ -702,10 +702,10 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
}
}
protected final boolean waitForCondition(Supplier<Boolean> condition) throws InterruptedException {
protected final boolean waitFor(Supplier<Boolean> condition) throws InterruptedException {
final long deadline = System.currentTimeMillis() + getReplyTimeout();
synchronized (internalMonitor) {
while (!condition.get().booleanValue() && !hasCurrentConnectionException()) {
while (!condition.get().booleanValue()) {
final long now = System.currentTimeMillis();
if (now >= deadline) {
return false;
@ -716,15 +716,19 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return true;
}
protected final void waitForCondition(Supplier<Boolean> condition, String waitFor) throws InterruptedException, NoResponseException {
boolean success = waitForCondition(condition);
protected final boolean waitForConditionOrConnectionException(Supplier<Boolean> condition) throws InterruptedException {
return waitFor(() -> condition.get().booleanValue() || hasCurrentConnectionException());
}
protected final void waitForConditionOrConnectionException(Supplier<Boolean> condition, String waitFor) throws InterruptedException, NoResponseException {
boolean success = waitForConditionOrConnectionException(condition);
if (!success) {
throw NoResponseException.newWith(this, waitFor);
}
}
protected final void waitForConditionOrThrowConnectionException(Supplier<Boolean> condition, String waitFor) throws InterruptedException, SmackException, XMPPException {
waitForCondition(condition, waitFor);
waitForConditionOrConnectionException(condition, waitFor);
if (hasCurrentConnectionException()) {
throwCurrentConnectionException();
}

View File

@ -394,7 +394,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
if (isSmResumptionPossible()) {
smResumedSyncPoint = SyncPointState.request_sent;
sendNonza(new Resume(clientHandledStanzasCount, smSessionId));
waitForCondition(() -> smResumedSyncPoint == SyncPointState.successful || smResumptionFailed != null, "resume previous stream");
waitForConditionOrConnectionException(() -> smResumedSyncPoint == SyncPointState.successful || smResumptionFailed != null, "resume previous stream");
if (smResumedSyncPoint == SyncPointState.successful) {
// We successfully resumed the stream, be done here
afterSuccessfulLogin(true);
@ -518,7 +518,7 @@ public class XMPPTCPConnection extends AbstractXMPPConnection {
setWasAuthenticated();
try {
boolean readerAndWriterThreadsTermianted = waitForCondition(() -> !packetWriter.running && !packetReader.running);
boolean readerAndWriterThreadsTermianted = waitForConditionOrConnectionException(() -> !packetWriter.running && !packetReader.running);
if (!readerAndWriterThreadsTermianted) {
LOGGER.severe("Reader and/or writer threads did not terminate timely. Writer running: "
+ packetWriter.running + ", Reader running: " + packetReader.running);