001/**
002 *
003 * Copyright 2003-2007 Jive Software.
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.jivesoftware.smack;
019
020/**
021 * Interface that allows for implementing classes to listen for connection closing
022 * and reconnection events. Listeners are registered with XMPPConnection objects.
023 *
024 * @see XMPPConnection#addConnectionListener
025 * @see XMPPConnection#removeConnectionListener
026 * 
027 * @author Matt Tucker
028 */
029public interface ConnectionListener {
030
031    /**
032     * Notification that the connection has been successfully connected to the remote endpoint (e.g. the XMPP server).
033     * <p>
034     * Note that the connection is likely not yet authenticated and therefore only limited operations like registering
035     * an account may be possible.
036     * </p>
037     *
038     * @param connection the XMPPConnection which successfully connected to its endpoint.
039     */
040    public void connected(XMPPConnection connection);
041
042    /**
043     * Notification that the connection has been authenticated.
044     *
045     * @param connection the XMPPConnection which successfully authenticated.
046     * @param resumed true if a previous XMPP session's stream was resumed.
047     */
048    public void authenticated(XMPPConnection connection, boolean resumed);
049
050    /**
051     * Notification that the connection was closed normally.
052     */
053    public void connectionClosed();
054
055    /**
056     * Notification that the connection was closed due to an exception. When
057     * abruptly disconnected it is possible for the connection to try reconnecting
058     * to the server.
059     *
060     * @param e the exception.
061     */
062    public void connectionClosedOnError(Exception e);
063
064    /**
065     * The connection has reconnected successfully to the server. Connections will
066     * reconnect to the server when the previous socket connection was abruptly closed.
067     */
068    public void reconnectionSuccessful();
069
070    // The next two methods *must* only be invoked by ReconnectionManager
071
072    /**
073     * The connection will retry to reconnect in the specified number of seconds.
074     * <p>
075     * Note: This method is only called if {@link ReconnectionManager#isAutomaticReconnectEnabled()} returns true, i.e.
076     * only when the reconnection manager is enabled for the connection.
077     * </p>
078     * 
079     * @param seconds remaining seconds before attempting a reconnection.
080     */
081    public void reconnectingIn(int seconds);
082
083    /**
084     * An attempt to connect to the server has failed. The connection will keep trying reconnecting to the server in a
085     * moment.
086     * <p>
087     * Note: This method is only called if {@link ReconnectionManager#isAutomaticReconnectEnabled()} returns true, i.e.
088     * only when the reconnection manager is enabled for the connection.
089     * </p>
090     *
091     * @param e the exception that caused the reconnection to fail.
092     */
093    public void reconnectionFailed(Exception e);
094}