1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-12-22 10:37:59 +01:00

Fix OfflineMessageManager.getMessages(List<STring>)

by implementing a new logic how the messages are retrieved.

Previously in case the node list has exactly one item, the method
would perform an unnecessary call to nextResult() causing a delay.

Fixes SMACK-785.
This commit is contained in:
Florian Schmaus 2017-11-18 15:49:57 +01:00
parent 809ab6f42f
commit 52bd680bb5

View file

@ -19,6 +19,8 @@ package org.jivesoftware.smackx.offline;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
@ -58,6 +60,8 @@ import org.jivesoftware.smackx.xdata.Form;
*/
public class OfflineMessageManager {
private static final Logger LOGGER = Logger.getLogger(OfflineMessageManager.class.getName());
private final static String namespace = "http://jabber.org/protocol/offline";
private final XMPPConnection connection;
@ -144,7 +148,7 @@ public class OfflineMessageManager {
* @throws InterruptedException
*/
public List<Message> getMessages(final List<String> nodes) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
List<Message> messages = new ArrayList<Message>();
List<Message> messages = new ArrayList<Message>(nodes.size());
OfflineMessageRequest request = new OfflineMessageRequest();
for (String node : nodes) {
OfflineMessageRequest.Item item = new OfflineMessageRequest.Item(node);
@ -165,12 +169,17 @@ public class OfflineMessageManager {
try {
connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
// Collect the received offline messages
Message message = messageCollector.nextResult();
while (message != null && pendingNodes > 0) {
pendingNodes--;
messages.add(message);
Message message;
do {
message = messageCollector.nextResult();
}
if (message != null) {
messages.add(message);
pendingNodes--;
} else if (message == null && pendingNodes > 0) {
LOGGER.log(Level.WARNING,
"Did not receive all expected offline messages. " + pendingNodes + " are missing.");
}
} while (message != null && pendingNodes > 0);
}
finally {
// Stop queuing offline messages