Search in sources :

Example 1 with BOSHClientRequestListener

use of org.igniterealtime.jbosh.BOSHClientRequestListener in project Smack by igniterealtime.

the class XMPPBOSHConnection method initDebugger.

/**
     * Initialize the SmackDebugger which allows to log and debug XML traffic.
     */
@Override
protected void initDebugger() {
    // TODO: Maybe we want to extend the SmackDebugger for simplification
    //       and a performance boost.
    // Initialize a empty writer which discards all data.
    writer = new Writer() {

        @Override
        public void write(char[] cbuf, int off, int len) {
        /* ignore */
        }

        @Override
        public void close() {
        /* ignore */
        }

        @Override
        public void flush() {
        /* ignore */
        }
    };
    // Initialize a pipe for received raw data.
    try {
        readerPipe = new PipedWriter();
        reader = new PipedReader(readerPipe);
    } catch (IOException e) {
    // Ignore
    }
    // Call the method from the parent class which initializes the debugger.
    super.initDebugger();
    // Add listeners for the received and sent raw data.
    client.addBOSHClientResponseListener(new BOSHClientResponseListener() {

        @Override
        public void responseReceived(BOSHMessageEvent event) {
            if (event.getBody() != null) {
                try {
                    readerPipe.write(event.getBody().toXML());
                    readerPipe.flush();
                } catch (Exception e) {
                // Ignore
                }
            }
        }
    });
    client.addBOSHClientRequestListener(new BOSHClientRequestListener() {

        @Override
        public void requestSent(BOSHMessageEvent event) {
            if (event.getBody() != null) {
                try {
                    writer.write(event.getBody().toXML());
                } catch (Exception e) {
                // Ignore
                }
            }
        }
    });
    // Create and start a thread which discards all read data.
    readerConsumer = new Thread() {

        private Thread thread = this;

        private int bufferLength = 1024;

        @Override
        public void run() {
            try {
                char[] cbuf = new char[bufferLength];
                while (readerConsumer == thread && !done) {
                    reader.read(cbuf, 0, bufferLength);
                }
            } catch (IOException e) {
            // Ignore
            }
        }
    };
    readerConsumer.setDaemon(true);
    readerConsumer.start();
}
Also used : PipedWriter(java.io.PipedWriter) BOSHClientRequestListener(org.igniterealtime.jbosh.BOSHClientRequestListener) BOSHClientResponseListener(org.igniterealtime.jbosh.BOSHClientResponseListener) IOException(java.io.IOException) BOSHMessageEvent(org.igniterealtime.jbosh.BOSHMessageEvent) PipedWriter(java.io.PipedWriter) Writer(java.io.Writer) PipedReader(java.io.PipedReader) SmackException(org.jivesoftware.smack.SmackException) ConnectionException(org.jivesoftware.smack.SmackException.ConnectionException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) StreamErrorException(org.jivesoftware.smack.XMPPException.StreamErrorException) BOSHException(org.igniterealtime.jbosh.BOSHException) IOException(java.io.IOException) XMPPException(org.jivesoftware.smack.XMPPException)

Aggregations

IOException (java.io.IOException)1 PipedReader (java.io.PipedReader)1 PipedWriter (java.io.PipedWriter)1 Writer (java.io.Writer)1 BOSHClientRequestListener (org.igniterealtime.jbosh.BOSHClientRequestListener)1 BOSHClientResponseListener (org.igniterealtime.jbosh.BOSHClientResponseListener)1 BOSHException (org.igniterealtime.jbosh.BOSHException)1 BOSHMessageEvent (org.igniterealtime.jbosh.BOSHMessageEvent)1 SmackException (org.jivesoftware.smack.SmackException)1 ConnectionException (org.jivesoftware.smack.SmackException.ConnectionException)1 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)1 XMPPException (org.jivesoftware.smack.XMPPException)1 StreamErrorException (org.jivesoftware.smack.XMPPException.StreamErrorException)1