1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-12-09 22:51:07 +01:00

Improve packet send and result collecting API

Instead of repeating the same pattern, when sending an IQ get/set packet
and collecting the response

PacketFilter filter = new PacketIDFilter(request.getPacketID()),
PacketCollector collector = connection.createPacketCollector(filter);
connection.sendPacket(reg);
IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
collector.cancel();
if (result == null) {
    throw new XMPPException("No response from server.");
}
else if (result.getType() == IQ.Type.ERROR) {
    throw new XMPPException(result.getError());
}

the API got redesigned, so that the above code block can be replaced
with

Packet result = connection.createPacketCollectorAndSend(request).nextResultOrThrow();
This commit is contained in:
Florian Schmaus 2014-02-18 15:05:19 +01:00
parent e6d5385129
commit 7bd7b3d24c
50 changed files with 333 additions and 1489 deletions

View file

@ -17,10 +17,7 @@
package org.jivesoftware.smackx.sharedgroups;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smackx.sharedgroups.packet.SharedGroupsInfo;
@ -48,22 +45,7 @@ public class SharedGroupManager {
SharedGroupsInfo info = new SharedGroupsInfo();
info.setType(IQ.Type.GET);
// Create a packet collector to listen for a response.
PacketCollector collector =
connection.createPacketCollector(new PacketIDFilter(info.getPacketID()));
connection.sendPacket(info);
// Wait up to 5 seconds for a result.
IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
collector.cancel();
if (result == null) {
throw new XMPPException("No response from the server.");
}
if (result.getType() == IQ.Type.ERROR) {
throw new XMPPException(result.getError());
}
return ((SharedGroupsInfo) result).getGroups();
SharedGroupsInfo result = (SharedGroupsInfo) connection.createPacketCollectorAndSend(info).nextResultOrThrow();
return result.getGroups();
}
}