Search in sources :

Example 16 with Stanza

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

the class AbstractXMPPConnection method firePacketSendingListeners.

/**
 * Process all stanza listeners for sending stanzas.
 * <p>
 * Compared to {@link #firePacketInterceptors(Stanza)}, the listeners will be invoked in a new thread.
 * </p>
 *
 * @param sendTopLevelStreamElement the top level stream element which just got send.
 */
// TODO: Rename to fireElementSendingListeners().
@SuppressWarnings("javadoc")
protected void firePacketSendingListeners(final TopLevelStreamElement sendTopLevelStreamElement) {
    if (debugger != null) {
        debugger.onOutgoingStreamElement(sendTopLevelStreamElement);
    }
    if (!(sendTopLevelStreamElement instanceof Stanza)) {
        return;
    }
    Stanza packet = (Stanza) sendTopLevelStreamElement;
    final List<StanzaListener> listenersToNotify = new LinkedList<>();
    synchronized (sendListeners) {
        for (ListenerWrapper listenerWrapper : sendListeners.values()) {
            if (listenerWrapper.filterMatches(packet)) {
                listenersToNotify.add(listenerWrapper.getListener());
            }
        }
    }
    if (listenersToNotify.isEmpty()) {
        return;
    }
    // Notify in a new thread, because we can
    asyncGo(new Runnable() {

        @Override
        public void run() {
            for (StanzaListener listener : listenersToNotify) {
                try {
                    listener.processStanza(packet);
                } catch (Exception e) {
                    LOGGER.log(Level.WARNING, "Sending listener threw exception", e);
                    continue;
                }
            }
        }
    });
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) LinkedList(java.util.LinkedList) SmackSaslException(org.jivesoftware.smack.SmackException.SmackSaslException) XMPPErrorException(org.jivesoftware.smack.XMPPException.XMPPErrorException) FailedNonzaException(org.jivesoftware.smack.XMPPException.FailedNonzaException) XmppStringprepException(org.jxmpp.stringprep.XmppStringprepException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) StreamErrorException(org.jivesoftware.smack.XMPPException.StreamErrorException) XmlPullParserException(org.jivesoftware.smack.xml.XmlPullParserException) NoResponseException(org.jivesoftware.smack.SmackException.NoResponseException) IOException(java.io.IOException) SecurityRequiredException(org.jivesoftware.smack.SmackException.SecurityRequiredException) SmackParsingException(org.jivesoftware.smack.parsing.SmackParsingException) NotLoggedInException(org.jivesoftware.smack.SmackException.NotLoggedInException) AlreadyLoggedInException(org.jivesoftware.smack.SmackException.AlreadyLoggedInException) SASLErrorException(org.jivesoftware.smack.sasl.SASLErrorException) AlreadyConnectedException(org.jivesoftware.smack.SmackException.AlreadyConnectedException) SmackWrappedException(org.jivesoftware.smack.SmackException.SmackWrappedException) SecurityRequiredByClientException(org.jivesoftware.smack.SmackException.SecurityRequiredByClientException) ResourceBindingNotOfferedException(org.jivesoftware.smack.SmackException.ResourceBindingNotOfferedException)

Example 17 with Stanza

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

the class StanzaCollectorTest method verifyRollover.

@Test
public void verifyRollover() throws InterruptedException {
    StanzaCollector collector = createTestStanzaCollector(null, new OKEverything(), 5);
    for (int i = 0; i < 6; i++) {
        Stanza testPacket = new TestPacket(i);
        collector.processStanza(testPacket);
    }
    // Assert that '0' has rolled off
    assertEquals("1", collector.nextResultBlockForever().getStanzaId());
    assertEquals("2", collector.nextResultBlockForever().getStanzaId());
    assertEquals("3", collector.nextResultBlockForever().getStanzaId());
    assertEquals("4", collector.nextResultBlockForever().getStanzaId());
    assertEquals("5", collector.pollResult().getStanzaId());
    assertNull(collector.pollResult());
    for (int i = 10; i < 15; i++) {
        Stanza testPacket = new TestPacket(i);
        collector.processStanza(testPacket);
    }
    assertEquals("10", collector.nextResultBlockForever().getStanzaId());
    assertEquals("11", collector.nextResultBlockForever().getStanzaId());
    assertEquals("12", collector.nextResultBlockForever().getStanzaId());
    assertEquals("13", collector.nextResultBlockForever().getStanzaId());
    assertEquals("14", collector.pollResult().getStanzaId());
    assertNull(collector.pollResult());
    assertNull(collector.nextResult(10));
}
Also used : Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 18 with Stanza

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

the class FromMatchesFilterTest method fullCompareMatchingEntityFullJid.

@Test
public void fullCompareMatchingEntityFullJid() {
    FromMatchesFilter filter = FromMatchesFilter.createFull(FULL_JID1_R1);
    Stanza packet = StanzaBuilder.buildMessage().build();
    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 : Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 19 with Stanza

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

the class FromMatchesFilterTest method bareCompareMatchingBaseJid.

@Test
public void bareCompareMatchingBaseJid() {
    FromMatchesFilter filter = FromMatchesFilter.createBare(BASE_JID1);
    Stanza packet = StanzaBuilder.buildMessage().build();
    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 : Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Example 20 with Stanza

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

the class FromMatchesFilterTest method bareCompareMatchingEntityFullJid.

@Test
public void bareCompareMatchingEntityFullJid() {
    FromMatchesFilter filter = FromMatchesFilter.createBare(FULL_JID1_R1);
    Stanza packet = StanzaBuilder.buildMessage().build();
    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 : Stanza(org.jivesoftware.smack.packet.Stanza) Test(org.junit.Test)

Aggregations

Stanza (org.jivesoftware.smack.packet.Stanza)101 StanzaListener (org.jivesoftware.smack.StanzaListener)24 Test (org.junit.Test)22 IQ (org.jivesoftware.smack.packet.IQ)20 Test (org.junit.jupiter.api.Test)18 XMPPConnection (org.jivesoftware.smack.XMPPConnection)14 Message (org.jivesoftware.smack.packet.Message)14 ArrayList (java.util.ArrayList)11 SmackException (org.jivesoftware.smack.SmackException)11 Jid (org.jxmpp.jid.Jid)11 IOException (java.io.IOException)9 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)8 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)8 Bytestream (org.jivesoftware.smackx.bytestreams.socks5.packet.Bytestream)7 DelayInformation (org.jivesoftware.smackx.delay.packet.DelayInformation)7 XMPPErrorException (org.jivesoftware.smack.XMPPException.XMPPErrorException)6 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)6 Protocol (org.jivesoftware.util.Protocol)6 EntityFullJid (org.jxmpp.jid.EntityFullJid)6 LinkedList (java.util.LinkedList)5