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

Improve OfflineMessages.getMessages()

poll the result before waiting for a message (with nextResult()), in
order to prevent waiting unnecessarily.

Add PacketCollector.pollResultOrThrow()
This commit is contained in:
Florian Schmaus 2014-12-07 23:41:17 +01:00
parent 5dd97a363c
commit 187e158260
2 changed files with 30 additions and 0 deletions

View file

@ -112,6 +112,25 @@ public class PacketCollector {
return (P) resultQueue.poll();
}
/**
* Polls to see if a packet is currently available and returns it, or
* immediately returns <tt>null</tt> if no packets are currently in the
* result queue.
* <p>
* Throws an XMPPErrorException in case the polled stanzas did contain an XMPPError.
* </p>
*
* @return the next available packet.
* @throws XMPPErrorException in case an error response.
*/
public <P extends Packet> P pollResultOrThrow() throws XMPPErrorException {
P result = pollResult();
if (result != null) {
XMPPErrorException.ifHasErrorThenThrow(result);
}
return result;
}
/**
* Returns the next available packet. The method call will block (not return) until a packet is
* available.

View file

@ -194,6 +194,17 @@ public class OfflineMessageManager {
Message message = messageCollector.nextResult();
while (message != null) {
messages.add(message);
// It is important that we query the resultCollector before the messageCollector
Packet result = resultCollector.pollResultOrThrow();
message = messageCollector.pollResult();
if (message == null && result != null) {
// No new messages, but we have a non-error IQ response, we are done
return messages;
} else if (message != null) {
// We have received a message without waiting, great, continue to add this message and proceed with
// the loop
continue;
}
message = messageCollector.nextResult();
}
resultCollector.nextResultOrThrow();