Search in sources :

Example 21 with StanzaListener

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

the class XMPPTCPConnection method processHandledCount.

private void processHandledCount(long handledCount) throws StreamManagementCounterError {
    long ackedStanzasCount = SMUtils.calculateDelta(handledCount, serverHandledStanzasCount);
    final List<Stanza> ackedStanzas = new ArrayList<Stanza>(ackedStanzasCount <= Integer.MAX_VALUE ? (int) ackedStanzasCount : Integer.MAX_VALUE);
    for (long i = 0; i < ackedStanzasCount; i++) {
        Stanza ackedStanza = unacknowledgedStanzas.poll();
        // unacknowledged stanza queue. There can be no exception.
        if (ackedStanza == null) {
            throw new StreamManagementCounterError(handledCount, serverHandledStanzasCount, ackedStanzasCount, ackedStanzas);
        }
        ackedStanzas.add(ackedStanza);
    }
    boolean atLeastOneStanzaAcknowledgedListener = false;
    if (!stanzaAcknowledgedListeners.isEmpty()) {
        // If stanzaAcknowledgedListeners is not empty, the we have at least one
        atLeastOneStanzaAcknowledgedListener = true;
    } else {
        // Otherwise we look for a matching id in the stanza *id* acknowledged listeners
        for (Stanza ackedStanza : ackedStanzas) {
            String id = ackedStanza.getStanzaId();
            if (id != null && stanzaIdAcknowledgedListeners.containsKey(id)) {
                atLeastOneStanzaAcknowledgedListener = true;
                break;
            }
        }
    }
    // Only spawn a new thread if there is a chance that some listener is invoked
    if (atLeastOneStanzaAcknowledgedListener) {
        asyncGo(new Runnable() {

            @Override
            public void run() {
                for (Stanza ackedStanza : ackedStanzas) {
                    for (StanzaListener listener : stanzaAcknowledgedListeners) {
                        try {
                            listener.processStanza(ackedStanza);
                        } catch (InterruptedException | NotConnectedException e) {
                            LOGGER.log(Level.FINER, "Received exception", e);
                        }
                    }
                    String id = ackedStanza.getStanzaId();
                    if (StringUtils.isNullOrEmpty(id)) {
                        continue;
                    }
                    StanzaListener listener = stanzaIdAcknowledgedListeners.remove(id);
                    if (listener != null) {
                        try {
                            listener.processStanza(ackedStanza);
                        } catch (InterruptedException | NotConnectedException e) {
                            LOGGER.log(Level.FINER, "Received exception", e);
                        }
                    }
                }
            }
        });
    }
    serverHandledStanzasCount = handledCount;
}
Also used : StreamManagementCounterError(org.jivesoftware.smack.sm.StreamManagementException.StreamManagementCounterError) Stanza(org.jivesoftware.smack.packet.Stanza) ArrayList(java.util.ArrayList) StanzaListener(org.jivesoftware.smack.StanzaListener)

Aggregations

StanzaListener (org.jivesoftware.smack.StanzaListener)21 InputStream (java.io.InputStream)12 DataPacketExtension (org.jivesoftware.smackx.bytestreams.ibb.packet.DataPacketExtension)12 Test (org.junit.Test)12 IQ (org.jivesoftware.smack.packet.IQ)10 Data (org.jivesoftware.smackx.bytestreams.ibb.packet.Data)9 Stanza (org.jivesoftware.smack.packet.Stanza)6 IOException (java.io.IOException)5 Random (java.util.Random)4 Message (org.jivesoftware.smack.packet.Message)3 JTabbedPane (javax.swing.JTabbedPane)2 SmackException (org.jivesoftware.smack.SmackException)2 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)2 XMPPException (org.jivesoftware.smack.XMPPException)2 StanzaFilter (org.jivesoftware.smack.filter.StanzaFilter)2 Jingle (org.jivesoftware.smackx.jingleold.packet.Jingle)2 Color (java.awt.Color)1 GridLayout (java.awt.GridLayout)1 Clipboard (java.awt.datatransfer.Clipboard)1 StringSelection (java.awt.datatransfer.StringSelection)1