1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 01:29:38 +02:00

Make XMPPConnection an interface

create AbstractXMPPConnection.
This commit is contained in:
Florian Schmaus 2014-05-25 12:28:08 +02:00
parent 6dd180e9d3
commit beecb8a675
33 changed files with 1264 additions and 1090 deletions

File diff suppressed because it is too large Load diff

View file

@ -20,7 +20,7 @@ package org.jivesoftware.smack;
/**
* Implementors of this interface will be notified when a new {@link XMPPConnection}
* has been created. The newly created connection will not be actually connected to
* the server. Use {@link XMPPConnection#addConnectionCreationListener(ConnectionCreationListener)}
* the server. Use {@link XMPPConnectionRegistry#addConnectionCreationListener(ConnectionCreationListener)}
* to add new listeners.
*
* @author Gaston Dombiak

View file

@ -17,8 +17,6 @@
package org.jivesoftware.smack;
import java.lang.ref.WeakReference;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
public abstract class Manager {
@ -31,8 +29,4 @@ public abstract class Manager {
protected final XMPPConnection connection() {
return weakConnection.get();
}
protected ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
return connection().schedule(command, delay, unit);
}
}

View file

@ -39,7 +39,7 @@ public class ReconnectionManager extends AbstractConnectionListener {
private static final Logger LOGGER = Logger.getLogger(ReconnectionManager.class.getName());
// Holds the connection to the server
private XMPPConnection connection;
private final AbstractXMPPConnection connection;
private Thread reconnectionThread;
private int randomBase = new Random().nextInt(11) + 5; // between 5 and 15 seconds
@ -50,14 +50,16 @@ public class ReconnectionManager extends AbstractConnectionListener {
// Create a new PrivacyListManager on every established connection. In the init()
// method of PrivacyListManager, we'll add a listener that will delete the
// instance when the connection is closed.
XMPPConnection.addConnectionCreationListener(new ConnectionCreationListener() {
XMPPConnectionRegistry.addConnectionCreationListener(new ConnectionCreationListener() {
public void connectionCreated(XMPPConnection connection) {
connection.addConnectionListener(new ReconnectionManager(connection));
if (connection instanceof AbstractXMPPConnection) {
connection.addConnectionListener(new ReconnectionManager((AbstractXMPPConnection) connection));
}
}
});
}
private ReconnectionManager(XMPPConnection connection) {
private ReconnectionManager(AbstractXMPPConnection connection) {
this.connection = connection;
}

View file

@ -0,0 +1,64 @@
/**
*
* Copyright 2014 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.smack;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
public class XMPPConnectionRegistry {
/**
* A set of listeners which will be invoked if a new connection is created.
*/
private final static Set<ConnectionCreationListener> connectionEstablishedListeners =
new CopyOnWriteArraySet<ConnectionCreationListener>();
/**
* Adds a new listener that will be notified when new Connections are created. Note
* that newly created connections will not be actually connected to the server.
*
* @param connectionCreationListener a listener interested on new connections.
*/
public static void addConnectionCreationListener(
ConnectionCreationListener connectionCreationListener) {
connectionEstablishedListeners.add(connectionCreationListener);
}
/**
* Removes a listener that was interested in connection creation events.
*
* @param connectionCreationListener a listener interested on new connections.
*/
public static void removeConnectionCreationListener(
ConnectionCreationListener connectionCreationListener) {
connectionEstablishedListeners.remove(connectionCreationListener);
}
/**
* Get the collection of listeners that are interested in connection creation events.
*
* @return a collection of listeners interested on new connections.
*/
protected static Collection<ConnectionCreationListener> getConnectionCreationListeners() {
return Collections.unmodifiableCollection(connectionEstablishedListeners);
}
}

View file

@ -49,7 +49,7 @@ import org.jivesoftware.smack.packet.Packet;
* @see XMPPConnection
* @author Guenther Niess
*/
public class DummyConnection extends XMPPConnection {
public class DummyConnection extends AbstractXMPPConnection {
private boolean authenticated = false;
private boolean anonymous = false;
@ -68,9 +68,9 @@ public class DummyConnection extends XMPPConnection {
public DummyConnection(ConnectionConfiguration configuration) {
super(configuration);
for (ConnectionCreationListener listener : getConnectionCreationListeners()) {
listener.connectionCreated(this);
}
for (ConnectionCreationListener listener : XMPPConnectionRegistry.getConnectionCreationListeners()) {
listener.connectionCreated(this);
}
}
@Override