1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-22 20:12:07 +01:00

Fix Roster.waitUntilLoaded and make it interruptable

waitTime and start need to be initialized outside of the while body.
This commit is contained in:
Florian Schmaus 2015-07-03 14:42:45 +02:00
parent 3bb06b8429
commit 0555297a6e

View file

@ -303,9 +303,10 @@ public class Roster extends Manager {
*
* @throws NotLoggedInException
* @throws NotConnectedException
* @throws InterruptedException
* @since 4.1
*/
public void reloadAndWait() throws NotLoggedInException, NotConnectedException {
public void reloadAndWait() throws NotLoggedInException, NotConnectedException, InterruptedException {
reload();
waitUntilLoaded();
}
@ -329,25 +330,18 @@ public class Roster extends Manager {
return true;
}
protected boolean waitUntilLoaded() {
final XMPPConnection connection = connection();
while (!loaded) {
long waitTime = connection.getPacketReplyTimeout();
protected boolean waitUntilLoaded() throws InterruptedException {
long waitTime = connection().getPacketReplyTimeout();
long start = System.currentTimeMillis();
while (!loaded) {
if (waitTime <= 0) {
break;
}
try {
synchronized (this) {
if (!loaded) {
wait(waitTime);
}
}
}
catch (InterruptedException e) {
LOGGER.log(Level.FINE, "interrupted", e);
break;
}
long now = System.currentTimeMillis();
waitTime -= now - start;
start = now;
@ -1183,7 +1177,14 @@ public class Roster extends Manager {
// Try to ensure that the roster is loaded when processing presence stanzas. While the
// presence listener is synchronous, the roster result listener is not, which means that
// the presence listener may be invoked with a not yet loaded roster.
boolean loaded = waitUntilLoaded();
boolean loaded;
try {
loaded = waitUntilLoaded();
}
catch (InterruptedException e) {
LOGGER.log(Level.INFO, "Presence listener was interrupted", e);
loaded = Roster.this.loaded;
}
if (loaded) {
LOGGER.warning("Roster not loaded while processing presence stanza");
}