Search in sources :

Example 1 with AbstractAcknowledgeableStanza

use of de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza in project Pix-Art-Messenger by kriztan.

the class XmppConnection method sendPacket.

private synchronized void sendPacket(final AbstractStanza packet, final boolean force) {
    if (stanzasSent == Integer.MAX_VALUE) {
        resetStreamId();
        disconnect(true);
        return;
    }
    synchronized (this.mStanzaQueue) {
        if (force || isBound) {
            tagWriter.writeStanzaAsync(packet);
        } else {
            Log.d(Config.LOGTAG, account.getJid().toBareJid() + " do not write stanza to unbound stream " + packet.toString());
        }
        if (packet instanceof AbstractAcknowledgeableStanza) {
            AbstractAcknowledgeableStanza stanza = (AbstractAcknowledgeableStanza) packet;
            if (this.mStanzaQueue.size() != 0) {
                int currentHighestKey = this.mStanzaQueue.keyAt(this.mStanzaQueue.size() - 1);
                if (currentHighestKey != stanzasSent) {
                    throw new AssertionError("Stanza count messed up");
                }
            }
            ++stanzasSent;
            this.mStanzaQueue.append(stanzasSent, stanza);
            if (stanza instanceof MessagePacket && stanza.getId() != null && inSmacksSession) {
                if (Config.EXTENDED_SM_LOGGING) {
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": requesting ack for message stanza #" + stanzasSent);
                }
                tagWriter.writeStanzaAsync(new RequestPacket(this.smVersion));
            }
        }
    }
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) AbstractAcknowledgeableStanza(de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza) RequestPacket(de.pixart.messenger.xmpp.stanzas.streammgmt.RequestPacket)

Example 2 with AbstractAcknowledgeableStanza

use of de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza in project Pix-Art-Messenger by kriztan.

the class XmppConnection method processStream.

private void processStream() throws XmlPullParserException, IOException, NoSuchAlgorithmException {
    final CountDownLatch streamCountDownLatch = new CountDownLatch(1);
    this.mStreamCountDownLatch = streamCountDownLatch;
    Tag nextTag = tagReader.readTag();
    while (nextTag != null && !nextTag.isEnd("stream")) {
        if (nextTag.isStart("error")) {
            processStreamError(nextTag);
        } else if (nextTag.isStart("features")) {
            processStreamFeatures(nextTag);
        } else if (nextTag.isStart("proceed")) {
            switchOverToTls(nextTag);
        } else if (nextTag.isStart("success")) {
            final String challenge = tagReader.readElement(nextTag).getContent();
            try {
                saslMechanism.getResponse(challenge);
            } catch (final SaslMechanism.AuthenticationException e) {
                Log.e(Config.LOGTAG, String.valueOf(e));
                throw new StateChangingException(Account.State.UNAUTHORIZED);
            }
            Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": logged in");
            account.setKey(Account.PINNED_MECHANISM_KEY, String.valueOf(saslMechanism.getPriority()));
            tagReader.reset();
            sendStartStream();
            final Tag tag = tagReader.readTag();
            if (tag != null && tag.isStart("stream")) {
                processStream();
            } else {
                throw new IOException("server didn't restart stream after successful auth");
            }
            break;
        } else if (nextTag.isStart("failure")) {
            final Element failure = tagReader.readElement(nextTag);
            if (Namespace.SASL.equals(failure.getNamespace())) {
                final String text = failure.findChildContent("text");
                if (failure.hasChild("account-disabled") && text != null) {
                    Matcher matcher = Patterns.AUTOLINK_WEB_URL.matcher(text);
                    if (matcher.find()) {
                        try {
                            URL url = new URL(text.substring(matcher.start(), matcher.end()));
                            if (url.getProtocol().equals("https")) {
                                this.redirectionUrl = url;
                                throw new StateChangingException(Account.State.PAYMENT_REQUIRED);
                            }
                        } catch (MalformedURLException e) {
                            throw new StateChangingException(Account.State.UNAUTHORIZED);
                        }
                    }
                }
                throw new StateChangingException(Account.State.UNAUTHORIZED);
            } else if (Namespace.TLS.equals(failure.getNamespace())) {
                throw new StateChangingException(Account.State.TLS_ERROR);
            } else {
                throw new StateChangingException(Account.State.INCOMPATIBLE_SERVER);
            }
        } else if (nextTag.isStart("challenge")) {
            final String challenge = tagReader.readElement(nextTag).getContent();
            final Element response = new Element("response", Namespace.SASL);
            try {
                response.setContent(saslMechanism.getResponse(challenge));
            } catch (final SaslMechanism.AuthenticationException e) {
                // TODO: Send auth abort tag.
                Log.e(Config.LOGTAG, e.toString());
            }
            tagWriter.writeElement(response);
        } else if (nextTag.isStart("enabled")) {
            final Element enabled = tagReader.readElement(nextTag);
            if ("true".equals(enabled.getAttribute("resume"))) {
                this.streamId = enabled.getAttribute("id");
                Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": stream management(" + smVersion + ") enabled (resumable)");
            } else {
                Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": stream management(" + smVersion + ") enabled");
            }
            this.stanzasReceived = 0;
            this.inSmacksSession = true;
            final RequestPacket r = new RequestPacket(smVersion);
            tagWriter.writeStanzaAsync(r);
        } else if (nextTag.isStart("resumed")) {
            this.inSmacksSession = true;
            this.isBound = true;
            this.tagWriter.writeStanzaAsync(new RequestPacket(smVersion));
            lastPacketReceived = SystemClock.elapsedRealtime();
            final Element resumed = tagReader.readElement(nextTag);
            final String h = resumed.getAttribute("h");
            try {
                ArrayList<AbstractAcknowledgeableStanza> failedStanzas = new ArrayList<>();
                synchronized (this.mStanzaQueue) {
                    final int serverCount = Integer.parseInt(h);
                    if (serverCount < stanzasSent) {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed with lost packages");
                        stanzasSent = serverCount;
                    } else {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": session resumed");
                    }
                    acknowledgeStanzaUpTo(serverCount);
                    for (int i = 0; i < this.mStanzaQueue.size(); ++i) {
                        failedStanzas.add(mStanzaQueue.valueAt(i));
                    }
                    mStanzaQueue.clear();
                }
                Log.d(Config.LOGTAG, "resending " + failedStanzas.size() + " stanzas");
                for (AbstractAcknowledgeableStanza packet : failedStanzas) {
                    if (packet instanceof MessagePacket) {
                        MessagePacket message = (MessagePacket) packet;
                        mXmppConnectionService.markMessage(account, message.getTo().toBareJid(), message.getId(), Message.STATUS_UNSEND);
                    }
                    sendPacket(packet);
                }
            } catch (final NumberFormatException ignored) {
            }
            Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": online with resource " + account.getResource());
            changeStatus(Account.State.ONLINE);
        } else if (nextTag.isStart("r")) {
            tagReader.readElement(nextTag);
            if (Config.EXTENDED_SM_LOGGING) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": acknowledging stanza #" + this.stanzasReceived);
            }
            final AckPacket ack = new AckPacket(this.stanzasReceived, smVersion);
            tagWriter.writeStanzaAsync(ack);
        } else if (nextTag.isStart("a")) {
            boolean accountUiNeedsRefresh = false;
            synchronized (NotificationService.CATCHUP_LOCK) {
                if (mWaitingForSmCatchup.compareAndSet(true, false)) {
                    int count = mSmCatchupMessageCounter.get();
                    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": SM catchup complete (" + count + ")");
                    accountUiNeedsRefresh = true;
                    if (count > 0) {
                        mXmppConnectionService.getNotificationService().finishBacklog(true, account);
                    }
                }
            }
            if (accountUiNeedsRefresh) {
                mXmppConnectionService.updateAccountUi();
            }
            final Element ack = tagReader.readElement(nextTag);
            lastPacketReceived = SystemClock.elapsedRealtime();
            try {
                synchronized (this.mStanzaQueue) {
                    final int serverSequence = Integer.parseInt(ack.getAttribute("h"));
                    acknowledgeStanzaUpTo(serverSequence);
                }
            } catch (NumberFormatException | NullPointerException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server send ack without sequence number");
            }
        } else if (nextTag.isStart("failed")) {
            Element failed = tagReader.readElement(nextTag);
            try {
                final int serverCount = Integer.parseInt(failed.getAttribute("h"));
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed but server acknowledged stanza #" + serverCount);
                synchronized (this.mStanzaQueue) {
                    acknowledgeStanzaUpTo(serverCount);
                }
            } catch (NumberFormatException | NullPointerException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": resumption failed");
            }
            resetStreamId();
            sendBindRequest();
        } else if (nextTag.isStart("iq")) {
            processIq(nextTag);
        } else if (nextTag.isStart("message")) {
            processMessage(nextTag);
        } else if (nextTag.isStart("presence")) {
            processPresence(nextTag);
        }
        nextTag = tagReader.readTag();
    }
    if (nextTag != null && nextTag.isEnd("stream")) {
        streamCountDownLatch.countDown();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) RequestPacket(de.pixart.messenger.xmpp.stanzas.streammgmt.RequestPacket) Matcher(java.util.regex.Matcher) Element(de.pixart.messenger.xml.Element) ArrayList(java.util.ArrayList) AckPacket(de.pixart.messenger.xmpp.stanzas.streammgmt.AckPacket) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) URL(java.net.URL) MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) AbstractAcknowledgeableStanza(de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza) Tag(de.pixart.messenger.xml.Tag)

Example 3 with AbstractAcknowledgeableStanza

use of de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza in project Pix-Art-Messenger by kriztan.

the class XmppConnection method acknowledgeStanzaUpTo.

private void acknowledgeStanzaUpTo(int serverCount) {
    if (serverCount > stanzasSent) {
        Log.e(Config.LOGTAG, "server acknowledged more stanzas than we sent. serverCount=" + serverCount + ", ourCount=" + stanzasSent);
    }
    for (int i = 0; i < mStanzaQueue.size(); ++i) {
        if (serverCount >= mStanzaQueue.keyAt(i)) {
            if (Config.EXTENDED_SM_LOGGING) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": server acknowledged stanza #" + mStanzaQueue.keyAt(i));
            }
            AbstractAcknowledgeableStanza stanza = mStanzaQueue.valueAt(i);
            if (stanza instanceof MessagePacket && acknowledgedListener != null) {
                MessagePacket packet = (MessagePacket) stanza;
                acknowledgedListener.onMessageAcknowledged(account, packet.getId());
            }
            mStanzaQueue.removeAt(i);
            i--;
        }
    }
}
Also used : MessagePacket(de.pixart.messenger.xmpp.stanzas.MessagePacket) AbstractAcknowledgeableStanza(de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza)

Aggregations

AbstractAcknowledgeableStanza (de.pixart.messenger.xmpp.stanzas.AbstractAcknowledgeableStanza)3 MessagePacket (de.pixart.messenger.xmpp.stanzas.MessagePacket)3 RequestPacket (de.pixart.messenger.xmpp.stanzas.streammgmt.RequestPacket)2 Element (de.pixart.messenger.xml.Element)1 Tag (de.pixart.messenger.xml.Tag)1 AckPacket (de.pixart.messenger.xmpp.stanzas.streammgmt.AckPacket)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matcher (java.util.regex.Matcher)1