1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-22 06:12:05 +01:00

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 * 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()} * 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 * 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(); final long deadline = System.currentTimeMillis() + getReplyTimeout();
synchronized (internalMonitor) { synchronized (internalMonitor) {
while (!condition.get().booleanValue() && !hasCurrentConnectionException()) { while (!condition.get().booleanValue()) {
final long now = System.currentTimeMillis(); final long now = System.currentTimeMillis();
if (now >= deadline) { if (now >= deadline) {
return false; return false;
@ -716,15 +716,19 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
return true; return true;
} }
protected final void waitForCondition(Supplier<Boolean> condition, String waitFor) throws InterruptedException, NoResponseException { protected final boolean waitForConditionOrConnectionException(Supplier<Boolean> condition) throws InterruptedException {
boolean success = waitForCondition(condition); return waitFor(() -> condition.get().booleanValue() || hasCurrentConnectionException());
}
protected final void waitForConditionOrConnectionException(Supplier<Boolean> condition, String waitFor) throws InterruptedException, NoResponseException {
boolean success = waitForConditionOrConnectionException(condition);
if (!success) { if (!success) {
throw NoResponseException.newWith(this, waitFor); throw NoResponseException.newWith(this, waitFor);
} }
} }
protected final void waitForConditionOrThrowConnectionException(Supplier<Boolean> condition, String waitFor) throws InterruptedException, SmackException, XMPPException { protected final void waitForConditionOrThrowConnectionException(Supplier<Boolean> condition, String waitFor) throws InterruptedException, SmackException, XMPPException {
waitForCondition(condition, waitFor); waitForConditionOrConnectionException(condition, waitFor);
if (hasCurrentConnectionException()) { if (hasCurrentConnectionException()) {
throwCurrentConnectionException(); throwCurrentConnectionException();
} }

View file

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