use of org.jivesoftware.smack.filter.StanzaFilter in project Smack by igniterealtime.
the class IoTDataManager method requestMomentaryValuesReadOut.
/**
* Try to read out a things momentary values.
*
* @param jid the full JID of the thing to read data from.
* @return a list with the read out data.
* @throws NoResponseException
* @throws XMPPErrorException
* @throws NotConnectedException
* @throws InterruptedException
*/
public List<IoTFieldsExtension> requestMomentaryValuesReadOut(EntityFullJid jid) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
final XMPPConnection connection = connection();
final int seqNr = nextSeqNr.incrementAndGet();
IoTDataRequest iotDataRequest = new IoTDataRequest(seqNr, true);
iotDataRequest.setTo(jid);
StanzaFilter doneFilter = new IoTFieldsExtensionFilter(seqNr, true);
StanzaFilter dataFilter = new IoTFieldsExtensionFilter(seqNr, false);
// Setup the IoTFieldsExtension message collectors before sending the IQ to avoid a data race.
StanzaCollector doneCollector = connection.createStanzaCollector(doneFilter);
StanzaCollector.Configuration dataCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(dataFilter).setCollectorToReset(doneCollector);
StanzaCollector dataCollector = connection.createStanzaCollector(dataCollectorConfiguration);
try {
connection.createStanzaCollectorAndSend(iotDataRequest).nextResultOrThrow();
// Wait until a message with an IoTFieldsExtension and the done flag comes in.
doneCollector.nextResult();
} finally {
// Ensure that the two collectors are canceled in any case.
dataCollector.cancel();
doneCollector.cancel();
}
int collectedCount = dataCollector.getCollectedCount();
List<IoTFieldsExtension> res = new ArrayList<>(collectedCount);
for (int i = 0; i < collectedCount; i++) {
Message message = dataCollector.pollResult();
IoTFieldsExtension iotFieldsExtension = IoTFieldsExtension.from(message);
res.add(iotFieldsExtension);
}
return res;
}
use of org.jivesoftware.smack.filter.StanzaFilter in project Smack by igniterealtime.
the class AbstractXMPPConnection method createStanzaCollectorAndSend.
@Override
public StanzaCollector createStanzaCollectorAndSend(IQ packet) throws NotConnectedException, InterruptedException {
StanzaFilter packetFilter = new IQReplyFilter(packet, this);
// Create the packet collector before sending the packet
StanzaCollector packetCollector = createStanzaCollectorAndSend(packetFilter, packet);
return packetCollector;
}
use of org.jivesoftware.smack.filter.StanzaFilter in project Smack by igniterealtime.
the class AbstractXMPPConnection method sendIqWithResponseCallback.
@Override
public void sendIqWithResponseCallback(IQ iqRequest, final StanzaListener callback, final ExceptionCallback exceptionCallback, long timeout) throws NotConnectedException, InterruptedException {
StanzaFilter replyFilter = new IQReplyFilter(iqRequest, this);
sendStanzaWithResponseCallback(iqRequest, replyFilter, callback, exceptionCallback, timeout);
}
use of org.jivesoftware.smack.filter.StanzaFilter in project Smack by igniterealtime.
the class OfflineMessageManager method getMessages.
/**
* Returns a List of the offline <tt>Messages</tt> whose stamp matches the specified
* request. The request will include the list of stamps that uniquely identifies
* the offline messages to retrieve. The returned offline messages will not be deleted
* from the server. Use {@link #deleteMessages(java.util.List)} to delete the messages.
*
* @param nodes the list of stamps that uniquely identifies offline message.
* @return a List with the offline <tt>Messages</tt> that were received as part of
* this request.
* @throws XMPPErrorException If the user is not allowed to make this request or the server does
* not support offline message retrieval.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
*/
public List<Message> getMessages(final List<String> nodes) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
List<Message> messages = new ArrayList<Message>();
OfflineMessageRequest request = new OfflineMessageRequest();
for (String node : nodes) {
OfflineMessageRequest.Item item = new OfflineMessageRequest.Item(node);
item.setAction("view");
request.addItem(item);
}
// Filter offline messages that were requested by this request
StanzaFilter messageFilter = new AndFilter(PACKET_FILTER, new StanzaFilter() {
@Override
public boolean accept(Stanza packet) {
OfflineMessageInfo info = (OfflineMessageInfo) packet.getExtension("offline", namespace);
return nodes.contains(info.getNode());
}
});
int pendingNodes = nodes.size();
StanzaCollector messageCollector = connection.createStanzaCollector(messageFilter);
try {
connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
// Collect the received offline messages
Message message = messageCollector.nextResult();
while (message != null && pendingNodes > 0) {
pendingNodes--;
messages.add(message);
message = messageCollector.nextResult();
}
} finally {
// Stop queuing offline messages
messageCollector.cancel();
}
return messages;
}
use of org.jivesoftware.smack.filter.StanzaFilter in project Smack by igniterealtime.
the class MultiUserChat method changeNickname.
/**
* Changes the occupant's nickname to a new nickname within the room. Each room occupant
* will receive two presence packets. One of type "unavailable" for the old nickname and one
* indicating availability for the new nickname. The unavailable presence will contain the new
* nickname and an appropriate status code (namely 303) as extended presence information. The
* status code 303 indicates that the occupant is changing his/her nickname.
*
* @param nickname the new nickname within the room.
* @throws XMPPErrorException if the new nickname is already in use by another occupant.
* @throws NoResponseException if there was no response from the server.
* @throws NotConnectedException
* @throws InterruptedException
* @throws MucNotJoinedException
*/
public synchronized void changeNickname(Resourcepart nickname) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, MucNotJoinedException {
StringUtils.requireNotNullOrEmpty(nickname, "Nickname must not be null or blank.");
// nickname.
if (!joined) {
throw new MucNotJoinedException(this);
}
final EntityFullJid jid = JidCreate.fullFrom(room, nickname);
// We change the nickname by sending a presence packet where the "to"
// field is in the form "roomName@service/nickname"
// We don't have to signal the MUC support again
Presence joinPresence = new Presence(Presence.Type.available);
joinPresence.setTo(jid);
// Wait for a presence packet back from the server.
StanzaFilter responseFilter = new AndFilter(FromMatchesFilter.createFull(jid), new StanzaTypeFilter(Presence.class));
StanzaCollector response = connection.createStanzaCollectorAndSend(responseFilter, joinPresence);
// Wait up to a certain number of seconds for a reply. If there is a negative reply, an
// exception will be thrown
response.nextResultOrThrow();
this.nickname = nickname;
}
Aggregations