mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-10 17:49:38 +02: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:
parent
e6d5385129
commit
7bd7b3d24c
50 changed files with 333 additions and 1489 deletions
|
@ -23,9 +23,7 @@ import java.util.WeakHashMap;
|
|||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||
import org.jivesoftware.smack.PacketCollector;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.SmackConfiguration;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.filter.PacketIDFilter;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
|
@ -147,43 +145,36 @@ public class CarbonManager {
|
|||
* @param new_state whether carbons should be enabled or disabled
|
||||
*
|
||||
* @return true if the operation was successful
|
||||
* @throws XMPPException
|
||||
*/
|
||||
public boolean setCarbonsEnabled(final boolean new_state) {
|
||||
if (enabled_state == new_state)
|
||||
return true;
|
||||
public void setCarbonsEnabled(final boolean new_state) throws XMPPException {
|
||||
if (enabled_state == new_state) return;
|
||||
|
||||
Connection connection = weakRefConnection.get();
|
||||
IQ setIQ = carbonsEnabledIQ(new_state);
|
||||
|
||||
PacketCollector collector =
|
||||
connection.createPacketCollector(new PacketIDFilter(setIQ.getPacketID()));
|
||||
connection.sendPacket(setIQ);
|
||||
IQ result = (IQ) collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
|
||||
collector.cancel();
|
||||
|
||||
if (result != null && result.getType() == IQ.Type.RESULT) {
|
||||
enabled_state = new_state;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
connection.createPacketCollectorAndSend(setIQ).nextResultOrThrow();
|
||||
enabled_state = new_state;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to enable carbons.
|
||||
*
|
||||
* @return true if the operation was successful
|
||||
* @throws XMPPException
|
||||
*/
|
||||
public boolean enableCarbons() {
|
||||
return setCarbonsEnabled(true);
|
||||
public void enableCarbons() throws XMPPException {
|
||||
setCarbonsEnabled(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to disable carbons.
|
||||
*
|
||||
* @return true if the operation was successful
|
||||
* @throws XMPPException
|
||||
*/
|
||||
public boolean disableCarbons() {
|
||||
return setCarbonsEnabled(false);
|
||||
public void disableCarbons() throws XMPPException {
|
||||
setCarbonsEnabled(false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue