1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2025-09-10 18:59:41 +02:00

SMACK-458 Managers should be kept on disconnects

Smack's Managers should not remove itself when the connection is closed
or should re-add themselves if the connection get reconnected.

This should also fix some NPE's.

We are currently going with two different designs of Manager: 1. The one
with WeakReferences/WeakHashMaps (SDM, EntityCapsManager) and 2. the one
where the managers remove their listeners on connectionClosed() *and*
connectionClosedOnError(), and later add their listeners on
reconnectionSuccessful(). The first design has the Connection instance
only weak referenced. The other design does reference Connection
strongly (e.g. the 'managers' map in IBBManager/S5BManager), but removes
this references when connectionClosed(onError)() is called.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_2@13788 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Florian Schmaus 2013-10-26 11:17:16 +00:00 committed by flow
parent 032fc8626e
commit b16f34f61e
12 changed files with 298 additions and 369 deletions

View file

@ -25,6 +25,7 @@ import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Privacy;
import org.jivesoftware.smack.packet.PrivacyItem;
import java.lang.ref.WeakReference;
import java.util.*;
/**
@ -43,9 +44,10 @@ import java.util.*;
public class PrivacyListManager {
// Keep the list of instances of this class.
private static Map<Connection, PrivacyListManager> instances = new Hashtable<Connection, PrivacyListManager>();
private static Map<Connection, PrivacyListManager> instances = Collections
.synchronizedMap(new WeakHashMap<Connection, PrivacyListManager>());
private Connection connection;
private WeakReference<Connection> connection;
private final List<PrivacyListListener> listeners = new ArrayList<PrivacyListListener>();
PacketFilter packetFilter = new AndFilter(new IQTypeFilter(IQ.Type.SET),
new PacketExtensionFilter("query", "jabber:iq:privacy"));
@ -67,49 +69,10 @@ public class PrivacyListManager {
*
* @param connection the XMPP connection.
*/
private PrivacyListManager(Connection connection) {
this.connection = connection;
this.init();
}
/** Answer the connection userJID that owns the privacy.
* @return the userJID that owns the privacy
*/
private String getUser() {
return connection.getUser();
}
/**
* Initializes the packet listeners of the connection that will notify for any set privacy
* package.
*/
private void init() {
private PrivacyListManager(final Connection connection) {
this.connection = new WeakReference<Connection>(connection);
// Register the new instance and associate it with the connection
instances.put(connection, this);
// Add a listener to the connection that removes the registered instance when
// the connection is closed
connection.addConnectionListener(new ConnectionListener() {
public void connectionClosed() {
// Unregister this instance since the connection has been closed
instances.remove(connection);
}
public void connectionClosedOnError(Exception e) {
// ignore
}
public void reconnectionFailed(Exception e) {
// ignore
}
public void reconnectingIn(int seconds) {
// ignore
}
public void reconnectionSuccessful() {
// ignore
}
});
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
@ -151,8 +114,14 @@ public class PrivacyListManager {
// Send create & join packet.
connection.sendPacket(iq);
}
}, packetFilter);
}
}, packetFilter); }
/** Answer the connection userJID that owns the privacy.
* @return the userJID that owns the privacy
*/
private String getUser() {
return connection.get().getUser();
}
/**
* Returns the PrivacyListManager instance associated with a given Connection.
@ -174,6 +143,8 @@ public class PrivacyListManager {
* @exception XMPPException if the request or the answer failed, it raises an exception.
*/
private Privacy getRequest(Privacy requestPrivacy) throws XMPPException {
Connection connection = PrivacyListManager.this.connection.get();
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
// The request is a get iq type
requestPrivacy.setType(Privacy.Type.GET);
requestPrivacy.setFrom(this.getUser());
@ -212,7 +183,8 @@ public class PrivacyListManager {
* @exception XMPPException if the request or the answer failed, it raises an exception.
*/
private Packet setRequest(Privacy requestPrivacy) throws XMPPException {
Connection connection = PrivacyListManager.this.connection.get();
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
// The request is a get iq type
requestPrivacy.setType(Privacy.Type.SET);
requestPrivacy.setFrom(this.getUser());