1
0
Fork 0
mirror of https://codeberg.org/Mercury-IM/Smack synced 2024-11-25 15:52:06 +01:00

Make InBandBytestreamManager use weak references to XMPPConnection

This commit is contained in:
Florian Schmaus 2017-03-15 15:07:41 +01:00
parent 525feaa161
commit a592a12229

View file

@ -17,15 +17,16 @@
package org.jivesoftware.smackx.bytestreams.ibb; package org.jivesoftware.smackx.bytestreams.ibb;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smack.AbstractConnectionClosedListener; import org.jivesoftware.smack.AbstractConnectionClosedListener;
import org.jivesoftware.smack.ConnectionCreationListener; import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.Manager;
import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException; import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException; import org.jivesoftware.smack.SmackException.NotConnectedException;
@ -79,7 +80,7 @@ import org.jxmpp.jid.Jid;
* *
* @author Henning Staib * @author Henning Staib
*/ */
public final class InBandBytestreamManager implements BytestreamManager { public final class InBandBytestreamManager extends Manager implements BytestreamManager {
/** /**
* Stanzas that can be used to encapsulate In-Band Bytestream data packets. * Stanzas that can be used to encapsulate In-Band Bytestream data packets.
@ -140,10 +141,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
private final static Random randomGenerator = new Random(); private final static Random randomGenerator = new Random();
/* stores one InBandBytestreamManager for each XMPP connection */ /* stores one InBandBytestreamManager for each XMPP connection */
private final static Map<XMPPConnection, InBandBytestreamManager> managers = new HashMap<XMPPConnection, InBandBytestreamManager>(); private final static Map<XMPPConnection, InBandBytestreamManager> managers = new WeakHashMap<>();
/* XMPP connection */
private final XMPPConnection connection;
/* /*
* assigns a user to a listener that is informed if an In-Band Bytestream request for this user * assigns a user to a listener that is informed if an In-Band Bytestream request for this user
@ -208,7 +206,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
* @param connection the XMPP connection * @param connection the XMPP connection
*/ */
private InBandBytestreamManager(XMPPConnection connection) { private InBandBytestreamManager(XMPPConnection connection) {
this.connection = connection; super(connection);
// register bytestream open packet listener // register bytestream open packet listener
this.initiationListener = new InitiationListener(this); this.initiationListener = new InitiationListener(this);
@ -434,11 +432,13 @@ public final class InBandBytestreamManager implements BytestreamManager {
Open byteStreamRequest = new Open(sessionID, this.defaultBlockSize, this.stanza); Open byteStreamRequest = new Open(sessionID, this.defaultBlockSize, this.stanza);
byteStreamRequest.setTo(targetJID); byteStreamRequest.setTo(targetJID);
final XMPPConnection connection = connection();
// sending packet will throw exception on timeout or error reply // sending packet will throw exception on timeout or error reply
connection.createStanzaCollectorAndSend(byteStreamRequest).nextResultOrThrow(); connection.createStanzaCollectorAndSend(byteStreamRequest).nextResultOrThrow();
InBandBytestreamSession inBandBytestreamSession = new InBandBytestreamSession( InBandBytestreamSession inBandBytestreamSession = new InBandBytestreamSession(
this.connection, byteStreamRequest, targetJID); connection, byteStreamRequest, targetJID);
this.sessions.put(sessionID, inBandBytestreamSession); this.sessions.put(sessionID, inBandBytestreamSession);
return inBandBytestreamSession; return inBandBytestreamSession;
@ -454,7 +454,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
*/ */
protected void replyRejectPacket(IQ request) throws NotConnectedException, InterruptedException { protected void replyRejectPacket(IQ request) throws NotConnectedException, InterruptedException {
IQ error = IQ.createErrorResponse(request, XMPPError.Condition.not_acceptable); IQ error = IQ.createErrorResponse(request, XMPPError.Condition.not_acceptable);
this.connection.sendStanza(error); connection().sendStanza(error);
} }
/** /**
@ -467,7 +467,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
*/ */
protected void replyResourceConstraintPacket(IQ request) throws NotConnectedException, InterruptedException { protected void replyResourceConstraintPacket(IQ request) throws NotConnectedException, InterruptedException {
IQ error = IQ.createErrorResponse(request, XMPPError.Condition.resource_constraint); IQ error = IQ.createErrorResponse(request, XMPPError.Condition.resource_constraint);
this.connection.sendStanza(error); connection().sendStanza(error);
} }
/** /**
@ -480,7 +480,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
*/ */
protected void replyItemNotFoundPacket(IQ request) throws NotConnectedException, InterruptedException { protected void replyItemNotFoundPacket(IQ request) throws NotConnectedException, InterruptedException {
IQ error = IQ.createErrorResponse(request, XMPPError.Condition.item_not_found); IQ error = IQ.createErrorResponse(request, XMPPError.Condition.item_not_found);
this.connection.sendStanza(error); connection().sendStanza(error);
} }
/** /**
@ -501,7 +501,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
* @return the XMPP connection * @return the XMPP connection
*/ */
protected XMPPConnection getConnection() { protected XMPPConnection getConnection() {
return this.connection; return connection();
} }
/** /**
@ -548,6 +548,7 @@ public final class InBandBytestreamManager implements BytestreamManager {
* internal status, which includes removing this instance from the managers map. * internal status, which includes removing this instance from the managers map.
*/ */
private void disableService() { private void disableService() {
final XMPPConnection connection = connection();
// remove manager from static managers map // remove manager from static managers map
managers.remove(connection); managers.remove(connection);