Search in sources :

Example 16 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class EnhancedDebugger method addReadPacketToTable.

/**
     * Adds the received stanza(/packet) detail to the messages table.
     *
     * @param dateFormatter the SimpleDateFormat to use to format Dates
     * @param packet        the read stanza(/packet) to add to the table
     */
private void addReadPacketToTable(final SimpleDateFormat dateFormatter, final Stanza packet) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            String messageType;
            Jid from = packet.getFrom();
            String type = "";
            Icon packetTypeIcon;
            receivedPackets++;
            if (packet instanceof IQ) {
                packetTypeIcon = iqPacketIcon;
                messageType = "IQ Received (class=" + packet.getClass().getName() + ")";
                type = ((IQ) packet).getType().toString();
                receivedIQPackets++;
            } else if (packet instanceof Message) {
                packetTypeIcon = messagePacketIcon;
                messageType = "Message Received";
                type = ((Message) packet).getType().toString();
                receivedMessagePackets++;
            } else if (packet instanceof Presence) {
                packetTypeIcon = presencePacketIcon;
                messageType = "Presence Received";
                type = ((Presence) packet).getType().toString();
                receivedPresencePackets++;
            } else {
                packetTypeIcon = unknownPacketTypeIcon;
                messageType = packet.getClass().getName() + " Received";
                receivedOtherPackets++;
            }
            // Check if we need to remove old rows from the table to keep memory consumption low
            if (EnhancedDebuggerWindow.MAX_TABLE_ROWS > 0 && messagesTable.getRowCount() >= EnhancedDebuggerWindow.MAX_TABLE_ROWS) {
                messagesTable.removeRow(0);
            }
            messagesTable.addRow(new Object[] { formatXML(packet.toXML().toString()), dateFormatter.format(new Date()), packetReceivedIcon, packetTypeIcon, messageType, packet.getStanzaId(), type, "", from });
            // Update the statistics table
            updateStatistics();
        }
    });
}
Also used : Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) Message(org.jivesoftware.smack.packet.Message) IQ(org.jivesoftware.smack.packet.IQ) Presence(org.jivesoftware.smack.packet.Presence) Icon(javax.swing.Icon) ImageIcon(javax.swing.ImageIcon) Date(java.util.Date)

Example 17 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class EnhancedDebugger method addSentPacketToTable.

/**
     * Adds the sent stanza(/packet) detail to the messages table.
     *
     * @param dateFormatter the SimpleDateFormat to use to format Dates
     * @param packet        the sent stanza(/packet) to add to the table
     */
private void addSentPacketToTable(final SimpleDateFormat dateFormatter, final Stanza packet) {
    SwingUtilities.invokeLater(new Runnable() {

        @Override
        public void run() {
            String messageType;
            Jid to = packet.getTo();
            String type = "";
            Icon packetTypeIcon;
            sentPackets++;
            if (packet instanceof IQ) {
                packetTypeIcon = iqPacketIcon;
                messageType = "IQ Sent (class=" + packet.getClass().getName() + ")";
                type = ((IQ) packet).getType().toString();
                sentIQPackets++;
            } else if (packet instanceof Message) {
                packetTypeIcon = messagePacketIcon;
                messageType = "Message Sent";
                type = ((Message) packet).getType().toString();
                sentMessagePackets++;
            } else if (packet instanceof Presence) {
                packetTypeIcon = presencePacketIcon;
                messageType = "Presence Sent";
                type = ((Presence) packet).getType().toString();
                sentPresencePackets++;
            } else {
                packetTypeIcon = unknownPacketTypeIcon;
                messageType = packet.getClass().getName() + " Sent";
                sentOtherPackets++;
            }
            // Check if we need to remove old rows from the table to keep memory consumption low
            if (EnhancedDebuggerWindow.MAX_TABLE_ROWS > 0 && messagesTable.getRowCount() >= EnhancedDebuggerWindow.MAX_TABLE_ROWS) {
                messagesTable.removeRow(0);
            }
            messagesTable.addRow(new Object[] { formatXML(packet.toXML().toString()), dateFormatter.format(new Date()), packetSentIcon, packetTypeIcon, messageType, packet.getStanzaId(), type, to, "" });
            // Update the statistics table
            updateStatistics();
        }
    });
}
Also used : Jid(org.jxmpp.jid.Jid) EntityFullJid(org.jxmpp.jid.EntityFullJid) Message(org.jivesoftware.smack.packet.Message) IQ(org.jivesoftware.smack.packet.IQ) Presence(org.jivesoftware.smack.packet.Presence) Icon(javax.swing.Icon) ImageIcon(javax.swing.ImageIcon) Date(java.util.Date)

Example 18 with Message

use of org.jivesoftware.smack.packet.Message 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 19 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class FromMatchesFilterTest method bareCompareMatchingEntityFullJid.

@Test
public void bareCompareMatchingEntityFullJid() {
    FromMatchesFilter filter = FromMatchesFilter.createBare(FULL_JID1_R1);
    Stanza packet = new Message();
    packet.setFrom(BASE_JID1);
    assertTrue(filter.accept(packet));
    packet.setFrom(FULL_JID1_R1);
    assertTrue(filter.accept(packet));
    packet.setFrom(FULL_JID1_R2);
    assertTrue(filter.accept(packet));
    packet.setFrom(BASE_JID2);
    assertFalse(filter.accept(packet));
    packet.setFrom(FULL_JID2);
    assertFalse(filter.accept(packet));
    packet.setFrom(BASE_JID3);
    assertFalse(filter.accept(packet));
}
Also used : Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 20 with Message

use of org.jivesoftware.smack.packet.Message in project Smack by igniterealtime.

the class FromMatchesFilterTest method fullCompareMatchingEntityFullJid.

@Test
public void fullCompareMatchingEntityFullJid() {
    FromMatchesFilter filter = FromMatchesFilter.createFull(FULL_JID1_R1);
    Stanza packet = new Message();
    packet.setFrom(FULL_JID1_R1);
    assertTrue(filter.accept(packet));
    packet.setFrom(BASE_JID1);
    assertFalse(filter.accept(packet));
    packet.setFrom(FULL_JID1_R2);
    assertFalse(filter.accept(packet));
    packet.setFrom(BASE_JID2);
    assertFalse(filter.accept(packet));
    packet.setFrom(FULL_JID2);
    assertFalse(filter.accept(packet));
    packet.setFrom(BASE_JID3);
    assertFalse(filter.accept(packet));
}
Also used : Message(org.jivesoftware.smack.packet.Message) Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Aggregations

Message (org.jivesoftware.smack.packet.Message)142 Test (org.junit.Test)57 Presence (org.jivesoftware.smack.packet.Presence)17 StanzaCollector (org.jivesoftware.smack.StanzaCollector)14 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)12 Stanza (org.jivesoftware.smack.packet.Stanza)12 XMPPException (org.jivesoftware.smack.XMPPException)11 XMPPConnection (org.jivesoftware.smack.XMPPConnection)9 Date (java.util.Date)8 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)8 AccountItem (com.xabber.android.data.account.AccountItem)7 Chat (org.jivesoftware.smack.Chat)7 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 ArrayList (java.util.ArrayList)6 Jid (org.jxmpp.jid.Jid)6 NetworkException (com.xabber.android.data.NetworkException)5 Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)5 GeoLocation (org.jivesoftware.smackx.geoloc.packet.GeoLocation)5 IOException (java.io.IOException)4