Search in sources :

Example 1 with StanzaFilter

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;
}
Also used : IoTDataRequest(org.jivesoftware.smackx.iot.data.element.IoTDataRequest) IoTFieldsExtension(org.jivesoftware.smackx.iot.data.element.IoTFieldsExtension) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Message(org.jivesoftware.smack.packet.Message) ArrayList(java.util.ArrayList) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaCollector(org.jivesoftware.smack.StanzaCollector) IoTFieldsExtensionFilter(org.jivesoftware.smackx.iot.data.filter.IoTFieldsExtensionFilter)

Example 2 with StanzaFilter

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;
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) IQReplyFilter(org.jivesoftware.smack.filter.IQReplyFilter)

Example 3 with StanzaFilter

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);
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) IQReplyFilter(org.jivesoftware.smack.filter.IQReplyFilter)

Example 4 with StanzaFilter

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;
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.offline.packet.OfflineMessageInfo) Stanza(org.jivesoftware.smack.packet.Stanza) ArrayList(java.util.ArrayList) AndFilter(org.jivesoftware.smack.filter.AndFilter) OfflineMessageRequest(org.jivesoftware.smackx.offline.packet.OfflineMessageRequest) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 5 with StanzaFilter

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;
}
Also used : AndFilter(org.jivesoftware.smack.filter.AndFilter) StanzaTypeFilter(org.jivesoftware.smack.filter.StanzaTypeFilter) StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) EntityFullJid(org.jxmpp.jid.EntityFullJid) Presence(org.jivesoftware.smack.packet.Presence) MUCInitialPresence(org.jivesoftware.smackx.muc.packet.MUCInitialPresence) MucNotJoinedException(org.jivesoftware.smackx.muc.MultiUserChatException.MucNotJoinedException) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Aggregations

StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)12 StanzaCollector (org.jivesoftware.smack.StanzaCollector)5 AndFilter (org.jivesoftware.smack.filter.AndFilter)5 Stanza (org.jivesoftware.smack.packet.Stanza)4 IQReplyFilter (org.jivesoftware.smack.filter.IQReplyFilter)3 IQ (org.jivesoftware.smack.packet.IQ)3 Message (org.jivesoftware.smack.packet.Message)3 Presence (org.jivesoftware.smack.packet.Presence)3 ArrayList (java.util.ArrayList)2 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)2 StanzaListener (org.jivesoftware.smack.StanzaListener)2 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)2 Jingle (org.jivesoftware.smackx.jingleold.packet.Jingle)2 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)2 SmackException (org.jivesoftware.smack.SmackException)1 NoResponseException (org.jivesoftware.smack.SmackException.NoResponseException)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 XMPPException (org.jivesoftware.smack.XMPPException)1 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)1 NotFilter (org.jivesoftware.smack.filter.NotFilter)1