曹妃甸核酸检测平台.apk(点击下载) / PacketReader.java


package asmack.org.jivesoftware.smack;

import asmack.org.jivesoftware.smack.Connection;
import asmack.org.jivesoftware.smack.ConnectionConfiguration;
import asmack.org.jivesoftware.smack.packet.Packet;
import asmack.org.jivesoftware.smack.packet.Presence;
import asmack.org.jivesoftware.smack.packet.PrivacyItem;
import asmack.org.jivesoftware.smack.packet.XMPPError;
import asmack.org.jivesoftware.smack.provider.PacketProvider;
import asmack.org.jivesoftware.smack.provider.ProviderManager;
import asmack.org.jivesoftware.smack.sasl.SASLMechanism;
import asmack.org.jivesoftware.smack.util.PacketParserUtils;
import com.umeng.analytics.pro.ai;
import com.umeng.analytics.pro.c;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

/* access modifiers changed from: package-private */
public class PacketReader {
    private AtomicLong atomicTimeLastReadPacket;
    private XMPPConnection connection;
    private String connectionID = null;
    private Semaphore connectionSemaphore;
    private boolean done;
    private ExecutorService listenerExecutor;
    private XmlPullParser parser;
    private Thread readerThread;
    private Thread threadCheckTime;

    /* access modifiers changed from: private */
    public class ListenerNotification implements Runnable {
        private Packet packet;

        public ListenerNotification(Packet packet2) {
            this.packet = packet2;
        }

        public void run() {
            for (Connection.ListenerWrapper listenerWrapper : PacketReader.this.connection.recvListeners.values()) {
                listenerWrapper.notifyListener(this.packet);
            }
        }
    }

    protected PacketReader(XMPPConnection xMPPConnection) {
        this.connection = xMPPConnection;
        init();
    }

    private void parseFeatures(XmlPullParser xmlPullParser) throws Exception {
        boolean z = false;
        boolean z2 = false;
        boolean z3 = false;
        while (!z) {
            int next = xmlPullParser.next();
            if (next == 2) {
                if (xmlPullParser.getName().equals("starttls")) {
                    z2 = true;
                } else if (xmlPullParser.getName().equals("mechanisms")) {
                    this.connection.getSASLAuthentication().setAvailableSASLMethods(PacketParserUtils.parseMechanisms(xmlPullParser));
                } else if (xmlPullParser.getName().equals("bind")) {
                    this.connection.getSASLAuthentication().bindingRequired();
                } else if (xmlPullParser.getName().equals("ver")) {
                    this.connection.getConfiguration().setRosterVersioningAvailable(true);
                } else if (xmlPullParser.getName().equals("c")) {
                    String attributeValue = xmlPullParser.getAttributeValue(null, "node");
                    String attributeValue2 = xmlPullParser.getAttributeValue(null, "ver");
                    this.connection.getConfiguration().setCapsNode(String.valueOf(attributeValue) + "#" + attributeValue2);
                } else if (xmlPullParser.getName().equals(c.aw)) {
                    this.connection.getSASLAuthentication().sessionsSupported();
                } else if (xmlPullParser.getName().equals("compression")) {
                    this.connection.setAvailableCompressionMethods(PacketParserUtils.parseCompressionMethods(xmlPullParser));
                } else if (xmlPullParser.getName().equals("register")) {
                    this.connection.getAccountManager().setSupportsAccountCreation(true);
                }
            } else if (next == 3) {
                if (xmlPullParser.getName().equals("starttls")) {
                    this.connection.startTLSReceived(z3);
                } else if (xmlPullParser.getName().equals("required") && z2) {
                    z3 = true;
                } else if (xmlPullParser.getName().equals("features")) {
                    z = true;
                }
            }
        }
        if (!this.connection.isSecureConnection() && !z2 && this.connection.getConfiguration().getSecurityMode() == ConnectionConfiguration.SecurityMode.required) {
            throw new XMPPException("Server does not support security (TLS), but security required by connection configuration.", new XMPPError(XMPPError.Condition.forbidden));
        } else if (!z2 || this.connection.getConfiguration().getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) {
            releaseConnectionIDLock();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void parsePackets(Thread thread) {
        Packet parsePacket;
        try {
            int eventType = this.parser.getEventType();
            do {
                if (eventType == 2) {
                    if (this.parser.getName().equals("message")) {
                        parsePacket = PacketParserUtils.parseMessage(this.parser);
                    } else if (this.parser.getName().equals("iq")) {
                        parsePacket = PacketParserUtils.parseIQ(this.parser, this.connection);
                    } else if (this.parser.getName().equals("presence")) {
                        parsePacket = PacketParserUtils.parsePresence(this.parser);
                    } else if (this.parser.getName().equals(ai.av)) {
                        this.connection.sendPacket(new Packet() {
                            /* class asmack.org.jivesoftware.smack.PacketReader.AnonymousClass4 */

                            @Override // asmack.org.jivesoftware.smack.packet.Packet
                            public String toXML() {
                                return "<p/>";
                            }
                        });
                    } else if (this.parser.getName().equals("stream")) {
                        if ("jabber:client".equals(this.parser.getNamespace(null))) {
                            for (int i = 0; i < this.parser.getAttributeCount(); i++) {
                                if (this.parser.getAttributeName(i).equals("id")) {
                                    this.connectionID = this.parser.getAttributeValue(i);
                                    if (!"1.0".equals(this.parser.getAttributeValue("", "version"))) {
                                        releaseConnectionIDLock();
                                    }
                                } else if (this.parser.getAttributeName(i).equals(PrivacyItem.PrivacyRule.SUBSCRIPTION_FROM)) {
                                    this.connection.config.setServiceName(this.parser.getAttributeValue(i));
                                }
                            }
                        }
                    } else if (this.parser.getName().equals(c.O)) {
                        throw new XMPPException(PacketParserUtils.parseStreamError(this.parser));
                    } else if (this.parser.getName().equals("features")) {
                        parseFeatures(this.parser);
                    } else {
                        if (this.parser.getName().equals("proceed")) {
                            this.connection.proceedTLSReceived();
                        } else if (this.parser.getName().equals("failure")) {
                            String namespace = this.parser.getNamespace(null);
                            if ("urn:ietf:params:xml:ns:xmpp-tls".equals(namespace)) {
                                throw new Exception("TLS negotiation has failed");
                            } else if ("http://jabber.org/protocol/compress".equals(namespace)) {
                                this.connection.streamCompressionDenied();
                            } else {
                                processPacket(PacketParserUtils.parseSASLFailure(this.parser));
                                this.connection.getSASLAuthentication().authenticationFailed();
                            }
                        } else if (this.parser.getName().equals("challenge")) {
                            String nextText = this.parser.nextText();
                            processPacket(new SASLMechanism.Challenge(nextText));
                            this.connection.getSASLAuthentication().challengeReceived(nextText);
                        } else if (this.parser.getName().equals("success")) {
                            processPacket(new SASLMechanism.Success(this.parser.nextText()));
                            this.connection.packetWriter.openStream();
                            resetParser();
                            this.connection.getSASLAuthentication().authenticated();
                        } else if (this.parser.getName().equals("compressed")) {
                            this.connection.startStreamCompression();
                        } else {
                            PacketProvider packetProvider = ProviderManager.getInstance().getPacketProvider(this.parser.getName());
                            if (packetProvider != null) {
                                parsePacket = packetProvider.parsePacket(this.parser);
                            }
                        }
                        resetParser();
                    }
                    processPacket(parsePacket);
                } else if (eventType == 3 && this.parser.getName().equals("stream")) {
                    this.connection.disconnect();
                }
                eventType = this.parser.next();
                this.atomicTimeLastReadPacket.set(System.currentTimeMillis());
                if (this.done || eventType == 1) {
                    return;
                }
            } while (thread == this.readerThread);
        } catch (Exception e2) {
            if (!this.done) {
                notifyConnectionError(e2);
            }
        }
    }

    private void processPacket(Packet packet) {
        if (packet != null) {
            for (PacketCollector packetCollector : this.connection.getPacketCollectors()) {
                packetCollector.processPacket(packet);
            }
            this.listenerExecutor.submit(new ListenerNotification(packet));
        }
    }

    private void releaseConnectionIDLock() {
        this.connectionSemaphore.release();
    }

    private void resetParser() {
        try {
            this.parser = XmlPullParserFactory.newInstance().newPullParser();
            this.parser.setFeature("http://xmlpull.org/v1/doc/features.html#process-namespaces", true);
            this.parser.setInput(this.connection.reader);
        } catch (XmlPullParserException e2) {
            e2.printStackTrace();
        }
    }

    /* access modifiers changed from: package-private */
    public void cleanup() {
        this.connection.recvListeners.clear();
        this.connection.collectors.clear();
    }

    /* access modifiers changed from: protected */
    public void init() {
        this.done = false;
        this.connectionID = null;
        this.readerThread = new Thread() {
            /* class asmack.org.jivesoftware.smack.PacketReader.AnonymousClass1 */

            public void run() {
                PacketReader.this.parsePackets(this);
            }
        };
        Thread thread = this.readerThread;
        thread.setName("Smack Packet Reader (" + this.connection.connectionCounterValue + ")");
        this.readerThread.setDaemon(true);
        this.listenerExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
            /* class asmack.org.jivesoftware.smack.PacketReader.AnonymousClass2 */

            public Thread newThread(Runnable runnable) {
                Thread thread = new Thread(runnable, "Smack Listener Processor (" + PacketReader.this.connection.connectionCounterValue + ")");
                thread.setDaemon(true);
                return thread;
            }
        });
        this.atomicTimeLastReadPacket = new AtomicLong(System.currentTimeMillis());
        if (SmackConfiguration.getKeepAliveInterval() > 0) {
            this.threadCheckTime = new Thread() {
                /* class asmack.org.jivesoftware.smack.PacketReader.AnonymousClass3 */

                public void run() {
                    while (!PacketReader.this.done && !isInterrupted()) {
                        long currentTimeMillis = System.currentTimeMillis() - PacketReader.this.atomicTimeLastReadPacket.get();
                        if (currentTimeMillis >= 60000) {
                            PacketReader.this.notifyConnectionError(new Exception("no receive packet in 30s"));
                            return;
                        }
                        long j = 60000 - currentTimeMillis;
                        long j2 = 10000;
                        if (j >= 10000) {
                            j2 = j;
                        }
                        try {
                            Thread.sleep(j2);
                        } catch (InterruptedException e2) {
                            e2.printStackTrace();
                        }
                    }
                }
            };
            this.threadCheckTime.setDaemon(true);
        }
        resetParser();
    }

    /* access modifiers changed from: package-private */
    public void notifyConnectionError(Exception exc) {
        this.done = true;
        this.connection.shutdown(new Presence(Presence.Type.unavailable));
        Thread thread = this.threadCheckTime;
        if (thread != null) {
            thread.interrupt();
        }
        exc.printStackTrace();
        for (ConnectionListener connectionListener : this.connection.getConnectionListeners()) {
            try {
                connectionListener.connectionClosedOnError(exc);
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    /* access modifiers changed from: protected */
    public void notifyReconnection() {
        for (ConnectionListener connectionListener : this.connection.getConnectionListeners()) {
            try {
                connectionListener.reconnectionSuccessful();
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    public void shutdown() {
        if (!this.done) {
            for (ConnectionListener connectionListener : this.connection.getConnectionListeners()) {
                try {
                    connectionListener.connectionClosed();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
        this.done = true;
        this.listenerExecutor.shutdown();
    }

    public void startup() throws XMPPException {
        this.connectionSemaphore = new Semaphore(1);
        this.readerThread.start();
        Thread thread = this.threadCheckTime;
        if (thread != null) {
            thread.start();
        }
        try {
            this.connectionSemaphore.acquire();
            this.connectionSemaphore.tryAcquire((long) (SmackConfiguration.getPacketReplyTimeout() * 3), TimeUnit.MILLISECONDS);
        } catch (InterruptedException unused) {
        }
        String str = this.connectionID;
        if (str != null) {
            this.connection.connectionID = str;
            return;
        }
        throw new XMPPException("Connection failed. No response from server.");
    }
}