Search in sources :

Example 1 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector 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 if there was no response from the remote entity.
 * @throws XMPPErrorException if there was an XMPP error returned.
 * @throws NotConnectedException if the XMPP connection is not connected.
 * @throws InterruptedException if the calling thread was interrupted.
 */
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.sendIqRequestAndWaitForResponse(iotDataRequest);
        // Wait until a message with an IoTFieldsExtension and the done flag comes in.
        doneCollector.nextResult();
    } finally {
        // Canceling dataCollector will also cancel the doneCollector since it is configured as dataCollector's
        // collector to reset.
        dataCollector.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 StanzaCollector

use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.

the class MamManager method queryArchivePage.

private MamQueryPage queryArchivePage(MamQueryIQ mamQueryIq) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException, NotLoggedInException {
    final XMPPConnection connection = getAuthenticatedConnectionOrThrow();
    MamFinIQ mamFinIQ;
    StanzaCollector mamFinIQCollector = connection.createStanzaCollector(new IQReplyFilter(mamQueryIq, connection));
    StanzaCollector.Configuration resultCollectorConfiguration = StanzaCollector.newConfiguration().setStanzaFilter(new MamResultFilter(mamQueryIq)).setCollectorToReset(mamFinIQCollector);
    StanzaCollector cancelledResultCollector;
    try (StanzaCollector resultCollector = connection.createStanzaCollector(resultCollectorConfiguration)) {
        connection.sendStanza(mamQueryIq);
        mamFinIQ = mamFinIQCollector.nextResultOrThrow();
        cancelledResultCollector = resultCollector;
    }
    return new MamQueryPage(cancelledResultCollector, mamFinIQ);
}
Also used : IQReplyFilter(org.jivesoftware.smack.filter.IQReplyFilter) MamFinIQ(org.jivesoftware.smackx.mam.element.MamFinIQ) MamResultFilter(org.jivesoftware.smackx.mam.filter.MamResultFilter) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 3 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.

the class Socks5ByteStreamTest method testRespondWithErrorOnSocks5BytestreamRequest.

/**
 * Target should respond with not-acceptable error if no listeners for incoming Socks5
 * bytestream requests are registered.
 *
 * @throws XMPPException should not happen
 */
public void testRespondWithErrorOnSocks5BytestreamRequest() throws XMPPException {
    XMPPConnection targetConnection = getConnection(0);
    XMPPConnection initiatorConnection = getConnection(1);
    Bytestream bytestreamInitiation = Socks5PacketUtils.createBytestreamInitiation(initiatorConnection.getUser(), targetConnection.getUser(), "session_id");
    bytestreamInitiation.addStreamHost("proxy.localhost", "127.0.0.1", 7777);
    StanzaCollector collector = initiatorConnection.createStanzaCollector(new PacketIDFilter(bytestreamInitiation.getStanzaId()));
    initiatorConnection.sendStanza(bytestreamInitiation);
    Packet result = collector.nextResult();
    assertNotNull(result.getError());
    assertEquals(XMPPError.Condition.no_acceptable.toString(), result.getError().getCondition());
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Bytestream(org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) StanzaCollector(org.jivesoftware.smack.StanzaCollector) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 4 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.

the class MultipleRecipientManagerTest method testReplying.

/**
 * Ensures that replying to packets is ok.
 */
public void testReplying() throws XMPPException {
    StanzaCollector collector0 = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector1 = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector2 = getConnection(2).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    StanzaCollector collector3 = getConnection(3).createStanzaCollector(new MessageTypeFilter(Message.Type.normal));
    // Send the intial message with multiple recipients
    Message message = new Message();
    message.setBody("Hola");
    List<String> to = Arrays.asList(new String[] { getBareJID(1) });
    List<String> cc = Arrays.asList(new String[] { getBareJID(2) });
    List<String> bcc = Arrays.asList(new String[] { getBareJID(3) });
    MultipleRecipientManager.send(getConnection(0), message, to, cc, bcc);
    // Get the message and ensure it's ok
    Message message1 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 1 never received the message", message1);
    MultipleRecipientInfo info = MultipleRecipientManager.getMultipleRecipientInfo(message1);
    assertNotNull("Message 1 does not contain MultipleRecipientInfo", info);
    assertFalse("Message 1 should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Prepare and send the reply
    Message reply1 = new Message();
    reply1.setBody("This is my reply");
    MultipleRecipientManager.reply(getConnection(1), message1, reply1);
    // Get the reply and ensure it's ok
    reply1 = (Message) collector0.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 0 never received the reply", reply1);
    info = MultipleRecipientManager.getMultipleRecipientInfo(reply1);
    assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
    assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Send a reply to the reply
    Message reply2 = new Message();
    reply2.setBody("This is my reply to your reply");
    reply2.setFrom(getBareJID(0));
    MultipleRecipientManager.reply(getConnection(0), reply1, reply2);
    // Get the reply and ensure it's ok
    reply2 = (Message) collector1.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection 1 never received the reply", reply2);
    info = MultipleRecipientManager.getMultipleRecipientInfo(reply2);
    assertNotNull("Replied message does not contain MultipleRecipientInfo", info);
    assertFalse("Replied message should be 'replyable'", info.shouldNotReply());
    assertEquals("Incorrect number of TO addresses", 1, info.getTOAddresses().size());
    assertEquals("Incorrect number of CC addresses", 1, info.getCCAddresses().size());
    // Check that connection2 recevied 3 messages
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 1 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 2 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection2 didn't receive the 3 message", message1);
    message1 = (Message) collector2.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNull("XMPPConnection2 received 4 messages", message1);
    // Check that connection3 recevied only 1 message (was BCC in the first message)
    message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNotNull("XMPPConnection3 didn't receive the 1 message", message1);
    message1 = (Message) collector3.nextResult(SmackConfiguration.getPacketReplyTimeout());
    assertNull("XMPPConnection2 received 2 messages", message1);
    collector0.cancel();
    collector1.cancel();
    collector2.cancel();
    collector3.cancel();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Example 5 with StanzaCollector

use of org.jivesoftware.smack.StanzaCollector in project Smack by igniterealtime.

the class OfflineMessageManagerTest method testReadAndDelete.

/**
 * While user2 is connected but unavailable, user1 sends 2 messages to user1. User2 then
 * performs some "Flexible Offline Message Retrieval" checking the number of offline messages,
 * retriving the headers, then the real messages of the headers and finally removing the
 * loaded messages.
 */
public void testReadAndDelete() {
    // Make user2 unavailable
    getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
    try {
        Thread.sleep(500);
        // User1 sends some messages to User2 which is not available at the moment
        Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
        chat.sendMessage("Test 1");
        chat.sendMessage("Test 2");
        Thread.sleep(500);
        // User2 checks the number of offline messages
        OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
        assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
        // Check the message headers
        Iterator<OfflineMessageHeader> headers = offlineManager.getHeaders();
        assertTrue("No message header was found", headers.hasNext());
        List<String> stamps = new ArrayList<String>();
        while (headers.hasNext()) {
            OfflineMessageHeader header = headers.next();
            assertEquals("Incorrect sender", getFullJID(0), header.getJid());
            assertNotNull("No stamp was found in the message header", header.getStamp());
            stamps.add(header.getStamp());
        }
        assertEquals("Wrong number of headers", 2, stamps.size());
        // Get the offline messages
        Iterator<Message> messages = offlineManager.getMessages(stamps);
        assertTrue("No message was found", messages.hasNext());
        stamps = new ArrayList<String>();
        while (messages.hasNext()) {
            Message message = messages.next();
            OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", "http://jabber.org/protocol/offline");
            assertNotNull("No offline information was included in the offline message", info);
            assertNotNull("No stamp was found in the message header", info.getNode());
            stamps.add(info.getNode());
        }
        assertEquals("Wrong number of messages", 2, stamps.size());
        // Check that the offline messages have not been deleted
        assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
        // User2 becomes available again
        StanzaCollector collector = getConnection(1).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
        getConnection(1).sendStanza(new Presence(Presence.Type.available));
        // Check that no offline messages was sent to the user
        Message message = (Message) collector.nextResult(2500);
        assertNull("An offline message was sent from the server", message);
        // Delete the retrieved offline messages
        offlineManager.deleteMessages(stamps);
        // Check that there are no offline message for this user
        assertEquals("Wrong number of offline messages", 0, offlineManager.getMessageCount());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.packet.OfflineMessageInfo) ArrayList(java.util.ArrayList) XMPPException(org.jivesoftware.smack.XMPPException) MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Chat(org.jivesoftware.smack.Chat) Presence(org.jivesoftware.smack.packet.Presence) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Aggregations

StanzaCollector (org.jivesoftware.smack.StanzaCollector)28 Message (org.jivesoftware.smack.packet.Message)14 AndFilter (org.jivesoftware.smack.filter.AndFilter)8 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)8 Presence (org.jivesoftware.smack.packet.Presence)8 XMPPConnection (org.jivesoftware.smack.XMPPConnection)7 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)6 ArrayList (java.util.ArrayList)5 Chat (org.jivesoftware.smack.Chat)5 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)5 Packet (org.jivesoftware.smack.packet.Packet)5 XMPPException (org.jivesoftware.smack.XMPPException)4 IQ (org.jivesoftware.smack.packet.IQ)4 Stanza (org.jivesoftware.smack.packet.Stanza)4 ThreadFilter (org.jivesoftware.smack.filter.ThreadFilter)3 IQReplyFilter (org.jivesoftware.smack.filter.IQReplyFilter)2 PacketFilter (org.jivesoftware.smack.filter.PacketFilter)2 PacketIDFilter (org.jivesoftware.smack.filter.PacketIDFilter)2 PresenceBuilder (org.jivesoftware.smack.packet.PresenceBuilder)2 StandardExtensionElement (org.jivesoftware.smack.packet.StandardExtensionElement)2