From ddc39030d77d8319be613c60fbf88492276f4030 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Wed, 17 Jun 2020 20:27:02 +0200 Subject: [PATCH] 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. --- .../smack/AbstractXMPPConnection.java | 16 ++++++++++------ .../smack/tcp/XMPPTCPConnection.java | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java index 6e90e35b2..bbb104fe8 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java @@ -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 condition) throws InterruptedException { + protected final boolean waitFor(Supplier 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 condition, String waitFor) throws InterruptedException, NoResponseException { - boolean success = waitForCondition(condition); + protected final boolean waitForConditionOrConnectionException(Supplier condition) throws InterruptedException { + return waitFor(() -> condition.get().booleanValue() || hasCurrentConnectionException()); + } + + protected final void waitForConditionOrConnectionException(Supplier condition, String waitFor) throws InterruptedException, NoResponseException { + boolean success = waitForConditionOrConnectionException(condition); if (!success) { throw NoResponseException.newWith(this, waitFor); } } protected final void waitForConditionOrThrowConnectionException(Supplier condition, String waitFor) throws InterruptedException, SmackException, XMPPException { - waitForCondition(condition, waitFor); + waitForConditionOrConnectionException(condition, waitFor); if (hasCurrentConnectionException()) { throwCurrentConnectionException(); } diff --git a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java index 99c6d2953..91bedc8e4 100644 --- a/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java +++ b/smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java @@ -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);