AbstractDebugger.java

  1. /**
  2.  *
  3.  * Copyright the original author or authors
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.debugger;

  18. import java.util.logging.Logger;

  19. import org.jivesoftware.smack.AbstractXMPPConnection;
  20. import org.jivesoftware.smack.ConnectionListener;
  21. import org.jivesoftware.smack.ReconnectionListener;
  22. import org.jivesoftware.smack.ReconnectionManager;
  23. import org.jivesoftware.smack.XMPPConnection;
  24. import org.jivesoftware.smack.packet.TopLevelStreamElement;
  25. import org.jivesoftware.smack.util.ObservableReader;
  26. import org.jivesoftware.smack.util.ObservableWriter;
  27. import org.jivesoftware.smack.util.ReaderListener;
  28. import org.jivesoftware.smack.util.WriterListener;

  29. import org.jxmpp.jid.EntityFullJid;

  30. public abstract class AbstractDebugger extends SmackDebugger {

  31.     private static final Logger LOGGER = Logger.getLogger(AbstractDebugger.class.getName());

  32.     public static boolean printInterpreted = false;

  33.     private final ConnectionListener connListener;
  34.     private final ReconnectionListener reconnectionListener;
  35.     private final ReaderListener readerListener;
  36.     private final WriterListener writerListener;

  37.     private ObservableWriter writer;
  38.     private ObservableReader reader;

  39.     public AbstractDebugger(final XMPPConnection connection) {
  40.         super(connection);

  41.         // Create a special Reader that wraps the main Reader and logs data to the GUI.
  42.         this.reader = new ObservableReader(reader);
  43.         readerListener = new ReaderListener() {
  44.             @Override
  45.             public void read(String str) {
  46.                 log("RECV (" + connection.getConnectionCounter() + "): " + str);
  47.             }
  48.         };
  49.         this.reader.addReaderListener(readerListener);

  50.         // Create a special Writer that wraps the main Writer and logs data to the GUI.
  51.         this.writer = new ObservableWriter(writer);
  52.         writerListener = new WriterListener() {
  53.             @Override
  54.             public void write(String str) {
  55.                 log("SENT (" + connection.getConnectionCounter() + "): " + str);
  56.             }
  57.         };
  58.         this.writer.addWriterListener(writerListener);

  59.         connListener = new ConnectionListener() {
  60.             @Override
  61.             public void connected(XMPPConnection connection) {
  62.                 log("XMPPConnection connected ("
  63.                                 + connection + ")");
  64.             }
  65.             @Override
  66.             public void authenticated(XMPPConnection connection, boolean resumed) {
  67.                 String logString = "XMPPConnection authenticated (" + connection + ")";
  68.                 if (resumed) {
  69.                     logString += " and resumed";
  70.                 }
  71.                 log(logString);
  72.             }
  73.             @Override
  74.             public void connectionClosed() {
  75.                 log(
  76.                        "XMPPConnection closed (" +
  77.                         connection +
  78.                         ")");
  79.             }

  80.             @Override
  81.             public void connectionClosedOnError(Exception e) {
  82.                 log(
  83.                         "XMPPConnection closed due to an exception (" +
  84.                         connection +
  85.                         ")", e);
  86.             }
  87.         };

  88.         reconnectionListener = new ReconnectionListener() {
  89.             @Override
  90.             public void reconnectionFailed(Exception e) {
  91.                 log(
  92.                         "Reconnection failed due to an exception (" +
  93.                         connection +
  94.                         ")", e);
  95.             }
  96.             @Override
  97.             public void reconnectingIn(int seconds) {
  98.                 log(
  99.                         "XMPPConnection (" +
  100.                         connection +
  101.                         ") will reconnect in " + seconds);
  102.             }
  103.         };

  104.         if (connection instanceof AbstractXMPPConnection) {
  105.             AbstractXMPPConnection abstractXmppConnection = (AbstractXMPPConnection) connection;
  106.             ReconnectionManager.getInstanceFor(abstractXmppConnection).addReconnectionListener(reconnectionListener);
  107.         } else {
  108.             LOGGER.info("The connection instance " + connection
  109.                             + " is not an instance of AbstractXMPPConnection, thus we can not install the ReconnectionListener");
  110.         }
  111.     }

  112.     protected abstract void log(String logMessage);

  113.     protected abstract void log(String logMessage, Throwable throwable);

  114.     @Override
  115.     public final void outgoingStreamSink(CharSequence outgoingCharSequence) {
  116.         log("SENT (" + connection.getConnectionCounter() + "): " + outgoingCharSequence);
  117.     }

  118.     @Override
  119.     public final void incomingStreamSink(CharSequence incomingCharSequence) {
  120.         log("RECV (" + connection.getConnectionCounter() + "): " + incomingCharSequence);
  121.     }

  122.     @Override
  123.     public void userHasLogged(EntityFullJid user) {
  124.         String localpart = user.getLocalpart().toString();
  125.         boolean isAnonymous = "".equals(localpart);
  126.         String title =
  127.                 "User logged (" + connection.getConnectionCounter() + "): "
  128.                 + (isAnonymous ? "" : localpart)
  129.                 + "@"
  130.                 + connection.getXMPPServiceDomain()
  131.                 + ":"
  132.                 + connection.getPort();
  133.         title += "/" + user.getResourcepart();
  134.         log(title);
  135.         // Add the connection listener to the connection so that the debugger can be notified
  136.         // whenever the connection is closed.
  137.         connection.addConnectionListener(connListener);
  138.     }

  139.     @Override
  140.     public void onIncomingStreamElement(TopLevelStreamElement streamElement) {
  141.         if (printInterpreted) {
  142.             log("RCV PKT (" + connection.getConnectionCounter() + "): " + streamElement.toXML());
  143.         }
  144.     }

  145.     @Override
  146.     public void onOutgoingStreamElement(TopLevelStreamElement streamElement) {
  147.         // Does nothing (yet).
  148.     }

  149. }