mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-06 05:01:12 +01:00
Convert Connection references to weak references
If a Manager is strong referenced from a gc root, usually the instances map, it should not hold itself a strong reference the connection in order to avoid a cycle that prevents the Connection instance from being gc'ed. SMACK-383
This commit is contained in:
parent
666f555733
commit
9c61c6c945
5 changed files with 46 additions and 35 deletions
|
|
@ -20,7 +20,6 @@
|
|||
|
||||
package org.jivesoftware.smackx;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.jivesoftware.smackx.ping;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
|
|
@ -65,7 +66,7 @@ public class PingManager {
|
|||
});
|
||||
}
|
||||
|
||||
private Connection connection;
|
||||
private WeakReference<Connection> weakRefConnection;
|
||||
|
||||
/**
|
||||
* Retrieves a {@link PingManager} for the specified {@link Connection}, creating one if it doesn't already
|
||||
|
|
@ -84,8 +85,8 @@ public class PingManager {
|
|||
return pingManager;
|
||||
}
|
||||
|
||||
private PingManager(Connection con) {
|
||||
this.connection = con;
|
||||
private PingManager(Connection connection) {
|
||||
weakRefConnection = new WeakReference<Connection>(connection);
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
|
||||
// The ServiceDiscoveryManager was not pre-initialized
|
||||
|
|
@ -101,6 +102,7 @@ public class PingManager {
|
|||
* Sends a Pong for every Ping
|
||||
*/
|
||||
public void processPacket(Packet packet) {
|
||||
Connection connection = weakRefConnection.get();
|
||||
IQ pong = IQ.createResultIQ((Ping) packet);
|
||||
connection.sendPacket(pong);
|
||||
}
|
||||
|
|
@ -121,7 +123,7 @@ public class PingManager {
|
|||
*/
|
||||
public boolean ping(String jid, long pingTimeout) {
|
||||
Ping ping = new Ping(jid);
|
||||
|
||||
Connection connection = weakRefConnection.get();
|
||||
try {
|
||||
SyncPacketSend.getReply(connection, ping);
|
||||
}
|
||||
|
|
@ -151,6 +153,7 @@ public class PingManager {
|
|||
* @throws XMPPException An XMPP related error occurred during the request
|
||||
*/
|
||||
public boolean isPingSupported(String jid) throws XMPPException {
|
||||
Connection connection = weakRefConnection.get();
|
||||
DiscoverInfo result = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(jid);
|
||||
return result.containsFeature(Ping.NAMESPACE);
|
||||
}
|
||||
|
|
@ -165,6 +168,7 @@ public class PingManager {
|
|||
* @return true if a reply was received from the server, false otherwise.
|
||||
*/
|
||||
public boolean pingMyServer() {
|
||||
Connection connection = weakRefConnection.get();
|
||||
return ping(connection.getServiceName());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.jivesoftware.smackx.receipts;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
|
|
@ -52,7 +53,7 @@ public class DeliveryReceiptManager implements PacketListener {
|
|||
});
|
||||
}
|
||||
|
||||
private Connection connection;
|
||||
private WeakReference<Connection> weakRefConnection;
|
||||
private boolean auto_receipts_enabled = false;
|
||||
private Set<ReceiptReceivedListener> receiptReceivedListeners = Collections
|
||||
.synchronizedSet(new HashSet<ReceiptReceivedListener>());
|
||||
|
|
@ -60,7 +61,7 @@ public class DeliveryReceiptManager implements PacketListener {
|
|||
private DeliveryReceiptManager(Connection connection) {
|
||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
sdm.addFeature(DeliveryReceipt.NAMESPACE);
|
||||
this.connection = connection;
|
||||
weakRefConnection = new WeakReference<Connection>(connection);
|
||||
instances.put(connection, this);
|
||||
|
||||
// register listener for delivery receipts and requests
|
||||
|
|
@ -91,6 +92,7 @@ public class DeliveryReceiptManager implements PacketListener {
|
|||
* @return true if supported
|
||||
*/
|
||||
public boolean isSupported(String jid) {
|
||||
Connection connection = weakRefConnection.get();
|
||||
try {
|
||||
DiscoverInfo result =
|
||||
ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(jid);
|
||||
|
|
@ -119,6 +121,7 @@ public class DeliveryReceiptManager implements PacketListener {
|
|||
DeliveryReceiptRequest drr = (DeliveryReceiptRequest)packet.getExtension(
|
||||
DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE);
|
||||
if (drr != null) {
|
||||
Connection connection = weakRefConnection.get();
|
||||
Message ack = new Message(packet.getFrom(), Message.Type.normal);
|
||||
ack.addExtension(new DeliveryReceipt(packet.getPacketID()));
|
||||
connection.sendPacket(ack);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue