mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-29 09:42:06 +01:00
Added ConnectionListener support.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1983 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
ed14b2a204
commit
b25380282c
3 changed files with 121 additions and 3 deletions
77
source/org/jivesoftware/smack/ConnectionListener.java
Normal file
77
source/org/jivesoftware/smack/ConnectionListener.java
Normal file
|
@ -0,0 +1,77 @@
|
||||||
|
/**
|
||||||
|
* $RCSfile$
|
||||||
|
* $Revision$
|
||||||
|
* $Date$
|
||||||
|
*
|
||||||
|
* Copyright (C) 2002-2003 Jive Software. All rights reserved.
|
||||||
|
* ====================================================================
|
||||||
|
* The Jive Software License (based on Apache Software License, Version 1.1)
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions
|
||||||
|
* are met:
|
||||||
|
*
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in
|
||||||
|
* the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
*
|
||||||
|
* 3. The end-user documentation included with the redistribution,
|
||||||
|
* if any, must include the following acknowledgment:
|
||||||
|
* "This product includes software developed by
|
||||||
|
* Jive Software (http://www.jivesoftware.com)."
|
||||||
|
* Alternately, this acknowledgment may appear in the software itself,
|
||||||
|
* if and wherever such third-party acknowledgments normally appear.
|
||||||
|
*
|
||||||
|
* 4. The names "Smack" and "Jive Software" must not be used to
|
||||||
|
* endorse or promote products derived from this software without
|
||||||
|
* prior written permission. For written permission, please
|
||||||
|
* contact webmaster@jivesoftware.com.
|
||||||
|
*
|
||||||
|
* 5. Products derived from this software may not be called "Smack",
|
||||||
|
* nor may "Smack" appear in their name, without prior written
|
||||||
|
* permission of Jive Software.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||||
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||||
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
|
||||||
|
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||||
|
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||||
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||||
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||||
|
* SUCH DAMAGE.
|
||||||
|
* ====================================================================
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface that allows for implementing classes to listen for connection closing
|
||||||
|
* events. Listeners are reigstered with XMPPConnection objects.
|
||||||
|
*
|
||||||
|
* @see XMPPConnection#addConnectionListener
|
||||||
|
* @see XMPPConnection#removeConnectionListener
|
||||||
|
*
|
||||||
|
* @author Matt Tucker
|
||||||
|
*/
|
||||||
|
public interface ConnectionListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notification that the connection was closed normally.
|
||||||
|
*/
|
||||||
|
public void connectionClosed();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Notification that the connection was closed due to an exception.
|
||||||
|
*
|
||||||
|
* @param e the exception.
|
||||||
|
*/
|
||||||
|
public void connectionClosedOnError(Exception e);
|
||||||
|
}
|
|
@ -90,6 +90,7 @@ class PacketReader {
|
||||||
private boolean done = false;
|
private boolean done = false;
|
||||||
protected List collectors = new ArrayList();
|
protected List collectors = new ArrayList();
|
||||||
private List listeners = new ArrayList();
|
private List listeners = new ArrayList();
|
||||||
|
protected List connectionListeners = new ArrayList();
|
||||||
|
|
||||||
private String connectionID = null;
|
private String connectionID = null;
|
||||||
private Object connectionIDLock = new Object();
|
private Object connectionIDLock = new Object();
|
||||||
|
@ -271,17 +272,29 @@ class PacketReader {
|
||||||
else if (eventType == XmlPullParser.END_TAG) {
|
else if (eventType == XmlPullParser.END_TAG) {
|
||||||
if (parser.getName().equals("stream")) {
|
if (parser.getName().equals("stream")) {
|
||||||
connection.close();
|
connection.close();
|
||||||
|
done = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
eventType = parser.next();
|
eventType = parser.next();
|
||||||
} while (eventType != XmlPullParser.END_DOCUMENT && !done);
|
} while (eventType != XmlPullParser.END_DOCUMENT && !done);
|
||||||
|
synchronized (connectionListeners) {
|
||||||
|
for (Iterator i=connectionListeners.iterator(); i.hasNext(); ) {
|
||||||
|
ConnectionListener listener = (ConnectionListener)i.next();
|
||||||
|
listener.connectionClosed();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
if (!done) {
|
if (!done) {
|
||||||
// An exception occurred while parsing. Print the error an close the
|
// An exception occurred while parsing. Notify connection listeners of
|
||||||
// connection.
|
// the error and close the connection.
|
||||||
e.printStackTrace();
|
|
||||||
connection.close();
|
connection.close();
|
||||||
|
synchronized (connectionListeners) {
|
||||||
|
for (Iterator i=connectionListeners.iterator(); i.hasNext(); ) {
|
||||||
|
ConnectionListener listener = (ConnectionListener)i.next();
|
||||||
|
listener.connectionClosedOnError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -485,6 +485,34 @@ public class XMPPConnection {
|
||||||
return packetReader.createPacketCollector(packetFilter);
|
return packetReader.createPacketCollector(packetFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a connection listener to this connection that will be notified when
|
||||||
|
* the connection closes or fails.
|
||||||
|
*
|
||||||
|
* @param connectionListener a connection listener.
|
||||||
|
*/
|
||||||
|
public void addConnectionListener(ConnectionListener connectionListener) {
|
||||||
|
if (connectionListener == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
synchronized (packetReader.connectionListeners) {
|
||||||
|
if (!packetReader.connectionListeners.contains(connectionListener)) {
|
||||||
|
packetReader.connectionListeners.add(connectionListener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a connection listener from this connection.
|
||||||
|
*
|
||||||
|
* @param connectionListener a connection listener.
|
||||||
|
*/
|
||||||
|
public void removeConnectionListener(ConnectionListener connectionListener) {
|
||||||
|
synchronized (packetReader.connectionListeners) {
|
||||||
|
packetReader.connectionListeners.remove(connectionListener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the connection by creating a packet reader and writer and opening a
|
* Initializes the connection by creating a packet reader and writer and opening a
|
||||||
* XMPP stream to the server.
|
* XMPP stream to the server.
|
||||||
|
|
Loading…
Reference in a new issue