Search in sources :

Example 1 with TagWriter

use of de.pixart.messenger.xml.TagWriter in project Pix-Art-Messenger by kriztan.

the class XmppConnection method startXmpp.

/**
 * Starts xmpp protocol, call after connecting to socket
 *
 * @return true if server returns with valid xmpp, false otherwise
 */
private boolean startXmpp(Socket socket) throws Exception {
    if (Thread.currentThread().isInterrupted()) {
        throw new InterruptedException();
    }
    this.socket = socket;
    tagReader = new XmlReader();
    if (tagWriter != null) {
        tagWriter.forceClose();
    }
    tagWriter = new TagWriter();
    tagWriter.setOutputStream(socket.getOutputStream());
    tagReader.setInputStream(socket.getInputStream());
    tagWriter.beginDocument();
    sendStartStream();
    final Tag tag = tagReader.readTag();
    return tag != null && tag.isStart("stream");
}
Also used : TagWriter(de.pixart.messenger.xml.TagWriter) XmlReader(de.pixart.messenger.xml.XmlReader) Tag(de.pixart.messenger.xml.Tag)

Example 2 with TagWriter

use of de.pixart.messenger.xml.TagWriter in project Pix-Art-Messenger by kriztan.

the class XmppConnection method disconnect.

public void disconnect(final boolean force) {
    interrupt();
    Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": disconnecting force=" + Boolean.valueOf(force));
    if (force) {
        forceCloseSocket();
    } else {
        final TagWriter currentTagWriter = this.tagWriter;
        if (currentTagWriter.isActive()) {
            currentTagWriter.finish();
            final Socket currentSocket = this.socket;
            final CountDownLatch streamCountDownLatch = this.mStreamCountDownLatch;
            try {
                currentTagWriter.await(1, TimeUnit.SECONDS);
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": closing stream");
                currentTagWriter.writeTag(Tag.end("stream:stream"));
                if (streamCountDownLatch != null) {
                    if (streamCountDownLatch.await(1, TimeUnit.SECONDS)) {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": remote ended stream");
                    } else {
                        Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": remote has not closed socket. force closing");
                    }
                }
            } catch (InterruptedException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": interrupted while gracefully closing stream");
            } catch (final IOException e) {
                Log.d(Config.LOGTAG, account.getJid().toBareJid() + ": io exception during disconnect (" + e.getMessage() + ")");
            } finally {
                FileBackend.close(currentSocket);
            }
        } else {
            forceCloseSocket();
        }
    }
}
Also used : TagWriter(de.pixart.messenger.xml.TagWriter) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SSLSocket(javax.net.ssl.SSLSocket) Socket(java.net.Socket)

Aggregations

TagWriter (de.pixart.messenger.xml.TagWriter)2 Tag (de.pixart.messenger.xml.Tag)1 XmlReader (de.pixart.messenger.xml.XmlReader)1 IOException (java.io.IOException)1 Socket (java.net.Socket)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 SSLSocket (javax.net.ssl.SSLSocket)1