mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2024-11-22 22:32:06 +01:00
Refactor Managers to subclass abstract Manager
Also use ServiceDiscoveryManager.supportsFeature() where possible.
This commit is contained in:
parent
93eea8ab8d
commit
f7fc38e1f4
9 changed files with 105 additions and 162 deletions
|
@ -16,13 +16,13 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.carbons;
|
package org.jivesoftware.smackx.carbons;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
import org.jivesoftware.smack.Connection;
|
import org.jivesoftware.smack.Connection;
|
||||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||||
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.PacketListener;
|
import org.jivesoftware.smack.PacketListener;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.filter.IQReplyFilter;
|
import org.jivesoftware.smack.filter.IQReplyFilter;
|
||||||
|
@ -31,7 +31,6 @@ import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
|
import org.jivesoftware.smackx.carbons.packet.CarbonExtension;
|
||||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Packet extension for XEP-0280: Message Carbons. This class implements
|
* Packet extension for XEP-0280: Message Carbons. This class implements
|
||||||
|
@ -43,7 +42,7 @@ import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
||||||
*
|
*
|
||||||
* @author Georg Lukas
|
* @author Georg Lukas
|
||||||
*/
|
*/
|
||||||
public class CarbonManager {
|
public class CarbonManager extends Manager {
|
||||||
|
|
||||||
private static Map<Connection, CarbonManager> instances =
|
private static Map<Connection, CarbonManager> instances =
|
||||||
Collections.synchronizedMap(new WeakHashMap<Connection, CarbonManager>());
|
Collections.synchronizedMap(new WeakHashMap<Connection, CarbonManager>());
|
||||||
|
@ -56,13 +55,12 @@ public class CarbonManager {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private WeakReference<Connection> weakRefConnection;
|
|
||||||
private volatile boolean enabled_state = false;
|
private volatile boolean enabled_state = false;
|
||||||
|
|
||||||
private CarbonManager(Connection connection) {
|
private CarbonManager(Connection connection) {
|
||||||
|
super(connection);
|
||||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
sdm.addFeature(CarbonExtension.NAMESPACE);
|
sdm.addFeature(CarbonExtension.NAMESPACE);
|
||||||
weakRefConnection = new WeakReference<Connection>(connection);
|
|
||||||
instances.put(connection, this);
|
instances.put(connection, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,11 +97,9 @@ public class CarbonManager {
|
||||||
* @return true if supported
|
* @return true if supported
|
||||||
*/
|
*/
|
||||||
public boolean isSupportedByServer() {
|
public boolean isSupportedByServer() {
|
||||||
Connection connection = weakRefConnection.get();
|
|
||||||
try {
|
try {
|
||||||
DiscoverInfo result = ServiceDiscoveryManager
|
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(
|
||||||
.getInstanceFor(connection).discoverInfo(connection.getServiceName());
|
connection().getServiceName(), CarbonExtension.NAMESPACE);
|
||||||
return result.containsFeature(CarbonExtension.NAMESPACE);
|
|
||||||
}
|
}
|
||||||
catch (XMPPException e) {
|
catch (XMPPException e) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -119,20 +115,19 @@ public class CarbonManager {
|
||||||
* @param new_state whether carbons should be enabled or disabled
|
* @param new_state whether carbons should be enabled or disabled
|
||||||
*/
|
*/
|
||||||
public void sendCarbonsEnabled(final boolean new_state) {
|
public void sendCarbonsEnabled(final boolean new_state) {
|
||||||
final Connection connection = weakRefConnection.get();
|
|
||||||
IQ setIQ = carbonsEnabledIQ(new_state);
|
IQ setIQ = carbonsEnabledIQ(new_state);
|
||||||
|
|
||||||
connection.addPacketListener(new PacketListener() {
|
connection().addPacketListener(new PacketListener() {
|
||||||
public void processPacket(Packet packet) {
|
public void processPacket(Packet packet) {
|
||||||
IQ result = (IQ)packet;
|
IQ result = (IQ)packet;
|
||||||
if (result.getType() == IQ.Type.RESULT) {
|
if (result.getType() == IQ.Type.RESULT) {
|
||||||
enabled_state = new_state;
|
enabled_state = new_state;
|
||||||
}
|
}
|
||||||
connection.removePacketListener(this);
|
connection().removePacketListener(this);
|
||||||
}
|
}
|
||||||
}, new IQReplyFilter(setIQ, connection));
|
}, new IQReplyFilter(setIQ, connection()));
|
||||||
|
|
||||||
connection.sendPacket(setIQ);
|
connection().sendPacket(setIQ);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -150,10 +145,9 @@ public class CarbonManager {
|
||||||
public void setCarbonsEnabled(final boolean new_state) throws XMPPException {
|
public void setCarbonsEnabled(final boolean new_state) throws XMPPException {
|
||||||
if (enabled_state == new_state) return;
|
if (enabled_state == new_state) return;
|
||||||
|
|
||||||
Connection connection = weakRefConnection.get();
|
|
||||||
IQ setIQ = carbonsEnabledIQ(new_state);
|
IQ setIQ = carbonsEnabledIQ(new_state);
|
||||||
|
|
||||||
connection.createPacketCollectorAndSend(setIQ).nextResultOrThrow();
|
connection().createPacketCollectorAndSend(setIQ).nextResultOrThrow();
|
||||||
enabled_state = new_state;
|
enabled_state = new_state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* Copyright © 2009 Jonas Ådahl, 2011-2013 Florian Schmaus
|
* Copyright © 2009 Jonas Ådahl, 2011-2014 Florian Schmaus
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -19,6 +19,7 @@ package org.jivesoftware.smackx.caps;
|
||||||
import org.jivesoftware.smack.Connection;
|
import org.jivesoftware.smack.Connection;
|
||||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||||
import org.jivesoftware.smack.ConnectionListener;
|
import org.jivesoftware.smack.ConnectionListener;
|
||||||
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.PacketInterceptor;
|
import org.jivesoftware.smack.PacketInterceptor;
|
||||||
import org.jivesoftware.smack.PacketListener;
|
import org.jivesoftware.smack.PacketListener;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
|
@ -58,7 +59,6 @@ import java.util.TreeSet;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ import java.security.NoSuchAlgorithmException;
|
||||||
*
|
*
|
||||||
* @author Florian Schmaus
|
* @author Florian Schmaus
|
||||||
*/
|
*/
|
||||||
public class EntityCapsManager {
|
public class EntityCapsManager extends Manager {
|
||||||
|
|
||||||
public static final String NAMESPACE = "http://jabber.org/protocol/caps";
|
public static final String NAMESPACE = "http://jabber.org/protocol/caps";
|
||||||
public static final String ELEMENT = "c";
|
public static final String ELEMENT = "c";
|
||||||
|
@ -110,7 +110,6 @@ public class EntityCapsManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private WeakReference<Connection> weakRefConnection;
|
|
||||||
private ServiceDiscoveryManager sdm;
|
private ServiceDiscoveryManager sdm;
|
||||||
private boolean entityCapsEnabled;
|
private boolean entityCapsEnabled;
|
||||||
private String currentCapsVersion;
|
private String currentCapsVersion;
|
||||||
|
@ -221,7 +220,7 @@ public class EntityCapsManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
private EntityCapsManager(Connection connection) {
|
private EntityCapsManager(Connection connection) {
|
||||||
this.weakRefConnection = new WeakReference<Connection>(connection);
|
super(connection);
|
||||||
this.sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
this.sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
instances.put(connection, this);
|
instances.put(connection, this);
|
||||||
|
|
||||||
|
@ -384,16 +383,8 @@ public class EntityCapsManager {
|
||||||
* @param jid
|
* @param jid
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean areEntityCapsSupported(String jid) {
|
public boolean areEntityCapsSupported(String jid) throws XMPPException {
|
||||||
if (jid == null)
|
return sdm.supportsFeature(jid, NAMESPACE);
|
||||||
return false;
|
|
||||||
|
|
||||||
try {
|
|
||||||
DiscoverInfo result = sdm.discoverInfo(jid);
|
|
||||||
return result.containsFeature(NAMESPACE);
|
|
||||||
} catch (XMPPException e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -401,8 +392,8 @@ public class EntityCapsManager {
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean areEntityCapsSupportedByServer() {
|
public boolean areEntityCapsSupportedByServer() throws XMPPException {
|
||||||
return areEntityCapsSupported(weakRefConnection.get().getServiceName());
|
return areEntityCapsSupported(connection().getServiceName());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -422,7 +413,7 @@ public class EntityCapsManager {
|
||||||
* the local users extended info
|
* the local users extended info
|
||||||
*/
|
*/
|
||||||
public void updateLocalEntityCaps() {
|
public void updateLocalEntityCaps() {
|
||||||
Connection connection = weakRefConnection.get();
|
Connection connection = connection();
|
||||||
|
|
||||||
DiscoverInfo discoverInfo = new DiscoverInfo();
|
DiscoverInfo discoverInfo = new DiscoverInfo();
|
||||||
discoverInfo.setType(IQ.Type.RESULT);
|
discoverInfo.setType(IQ.Type.RESULT);
|
||||||
|
|
|
@ -17,13 +17,13 @@
|
||||||
|
|
||||||
package org.jivesoftware.smackx.chatstates;
|
package org.jivesoftware.smackx.chatstates;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
import org.jivesoftware.smack.Chat;
|
import org.jivesoftware.smack.Chat;
|
||||||
import org.jivesoftware.smack.ChatManagerListener;
|
import org.jivesoftware.smack.ChatManagerListener;
|
||||||
import org.jivesoftware.smack.Connection;
|
import org.jivesoftware.smack.Connection;
|
||||||
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.MessageListener;
|
import org.jivesoftware.smack.MessageListener;
|
||||||
import org.jivesoftware.smack.PacketInterceptor;
|
import org.jivesoftware.smack.PacketInterceptor;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
|
@ -50,13 +50,13 @@ import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
* @see org.jivesoftware.smackx.chatstates.ChatState
|
* @see org.jivesoftware.smackx.chatstates.ChatState
|
||||||
* @see org.jivesoftware.smackx.chatstates.packet.ChatStateExtension
|
* @see org.jivesoftware.smackx.chatstates.packet.ChatStateExtension
|
||||||
*/
|
*/
|
||||||
public class ChatStateManager {
|
public class ChatStateManager extends Manager {
|
||||||
|
public static final String NAMESPACE = "http://jabber.org/protocol/chatstates";
|
||||||
|
|
||||||
private static final Map<Connection, WeakReference<ChatStateManager>> managers =
|
private static final Map<Connection, ChatStateManager> INSTANCES =
|
||||||
new WeakHashMap<Connection, WeakReference<ChatStateManager>>();
|
new WeakHashMap<Connection, ChatStateManager>();
|
||||||
|
|
||||||
private static final PacketFilter filter = new NotFilter(
|
private static final PacketFilter filter = new NotFilter(new PacketExtensionFilter(NAMESPACE));
|
||||||
new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the ChatStateManager related to the Connection and it will create one if it does
|
* Returns the ChatStateManager related to the Connection and it will create one if it does
|
||||||
|
@ -65,28 +65,14 @@ public class ChatStateManager {
|
||||||
* @param connection the connection to return the ChatStateManager
|
* @param connection the connection to return the ChatStateManager
|
||||||
* @return the ChatStateManager related the the connection.
|
* @return the ChatStateManager related the the connection.
|
||||||
*/
|
*/
|
||||||
public static ChatStateManager getInstance(final Connection connection) {
|
public static synchronized ChatStateManager getInstance(final Connection connection) {
|
||||||
if(connection == null) {
|
ChatStateManager manager = INSTANCES.get(connection);
|
||||||
return null;
|
if (manager == null) {
|
||||||
}
|
|
||||||
synchronized (managers) {
|
|
||||||
ChatStateManager manager;
|
|
||||||
WeakReference<ChatStateManager> ref = managers.get(connection);
|
|
||||||
|
|
||||||
if (ref == null) {
|
|
||||||
manager = new ChatStateManager(connection);
|
manager = new ChatStateManager(connection);
|
||||||
manager.init();
|
|
||||||
managers.put(connection, new WeakReference<ChatStateManager>(manager));
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
manager = ref.get();
|
|
||||||
|
|
||||||
return manager;
|
return manager;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Connection connection;
|
|
||||||
|
|
||||||
private final OutgoingMessageInterceptor outgoingInterceptor = new OutgoingMessageInterceptor();
|
private final OutgoingMessageInterceptor outgoingInterceptor = new OutgoingMessageInterceptor();
|
||||||
|
|
||||||
private final IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor();
|
private final IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor();
|
||||||
|
@ -98,18 +84,15 @@ public class ChatStateManager {
|
||||||
new ReferenceMap<Chat, ChatState>(ReferenceMap.WEAK, ReferenceMap.HARD);
|
new ReferenceMap<Chat, ChatState>(ReferenceMap.WEAK, ReferenceMap.HARD);
|
||||||
|
|
||||||
private ChatStateManager(Connection connection) {
|
private ChatStateManager(Connection connection) {
|
||||||
this.connection = connection;
|
super(connection);
|
||||||
}
|
connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor, filter);
|
||||||
|
|
||||||
private void init() {
|
|
||||||
connection.getChatManager().addOutgoingMessageInterceptor(outgoingInterceptor,
|
|
||||||
filter);
|
|
||||||
connection.getChatManager().addChatListener(incomingInterceptor);
|
connection.getChatManager().addChatListener(incomingInterceptor);
|
||||||
|
|
||||||
ServiceDiscoveryManager.getInstanceFor(connection)
|
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(NAMESPACE);
|
||||||
.addFeature("http://jabber.org/protocol/chatstates");
|
INSTANCES.put(connection, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the current state of the provided chat. This method will send an empty bodied Message
|
* Sets the current state of the provided chat. This method will send an empty bodied Message
|
||||||
* packet with the state attached as a {@link org.jivesoftware.smack.packet.PacketExtension}, if
|
* packet with the state attached as a {@link org.jivesoftware.smack.packet.PacketExtension}, if
|
||||||
|
@ -142,12 +125,12 @@ public class ChatStateManager {
|
||||||
|
|
||||||
ChatStateManager that = (ChatStateManager) o;
|
ChatStateManager that = (ChatStateManager) o;
|
||||||
|
|
||||||
return connection.equals(that.connection);
|
return connection().equals(that.connection());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
return connection.hashCode();
|
return connection().hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean updateChatState(Chat chat, ChatState newState) {
|
private boolean updateChatState(Chat chat, ChatState newState) {
|
||||||
|
@ -171,7 +154,7 @@ public class ChatStateManager {
|
||||||
|
|
||||||
public void interceptPacket(Packet packet) {
|
public void interceptPacket(Packet packet) {
|
||||||
Message message = (Message) packet;
|
Message message = (Message) packet;
|
||||||
Chat chat = connection.getChatManager().getThreadChat(message.getThread());
|
Chat chat = connection().getChatManager().getThreadChat(message.getThread());
|
||||||
if (chat == null) {
|
if (chat == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -188,8 +171,7 @@ public class ChatStateManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processMessage(Chat chat, Message message) {
|
public void processMessage(Chat chat, Message message) {
|
||||||
PacketExtension extension
|
PacketExtension extension = message.getExtension(NAMESPACE);
|
||||||
= message.getExtension("http://jabber.org/protocol/chatstates");
|
|
||||||
if (extension == null) {
|
if (extension == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,6 @@ import org.jivesoftware.smackx.disco.packet.DiscoverItems;
|
||||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
|
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
|
||||||
import org.jivesoftware.smackx.xdata.Form;
|
import org.jivesoftware.smackx.xdata.Form;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -54,10 +53,8 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
*
|
*
|
||||||
* @author Gabriel Guardincerri
|
* @author Gabriel Guardincerri
|
||||||
*/
|
*/
|
||||||
public class AdHocCommandManager {
|
public class AdHocCommandManager extends Manager {
|
||||||
private static final String DISCO_NAMESPACE = "http://jabber.org/protocol/commands";
|
public static final String NAMESPACE = "http://jabber.org/protocol/commands";
|
||||||
|
|
||||||
private static final String discoNode = DISCO_NAMESPACE;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The session time out in seconds.
|
* The session time out in seconds.
|
||||||
|
@ -97,11 +94,6 @@ public class AdHocCommandManager {
|
||||||
return ahcm;
|
return ahcm;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The Connection that this instances of AdHocCommandManager manages
|
|
||||||
*/
|
|
||||||
private final WeakReference<Connection> connection;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map a command node with its AdHocCommandInfo. Note: Key=command node,
|
* Map a command node with its AdHocCommandInfo. Note: Key=command node,
|
||||||
* Value=command. Command node matches the node attribute sent by command
|
* Value=command. Command node matches the node attribute sent by command
|
||||||
|
@ -125,7 +117,7 @@ public class AdHocCommandManager {
|
||||||
private Thread sessionsSweeper;
|
private Thread sessionsSweeper;
|
||||||
|
|
||||||
private AdHocCommandManager(Connection connection) {
|
private AdHocCommandManager(Connection connection) {
|
||||||
this.connection = new WeakReference<Connection>(connection);
|
super(connection);
|
||||||
this.serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
|
this.serviceDiscoveryManager = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
|
|
||||||
// Register the new instance and associate it with the connection
|
// Register the new instance and associate it with the connection
|
||||||
|
@ -136,13 +128,13 @@ public class AdHocCommandManager {
|
||||||
// This information will be used when another client tries to
|
// This information will be used when another client tries to
|
||||||
// discover whether this client supports AdHoc-Commands or not.
|
// discover whether this client supports AdHoc-Commands or not.
|
||||||
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(
|
ServiceDiscoveryManager.getInstanceFor(connection).addFeature(
|
||||||
DISCO_NAMESPACE);
|
NAMESPACE);
|
||||||
|
|
||||||
// Set the NodeInformationProvider that will provide information about
|
// Set the NodeInformationProvider that will provide information about
|
||||||
// which AdHoc-Commands are registered, whenever a disco request is
|
// which AdHoc-Commands are registered, whenever a disco request is
|
||||||
// received
|
// received
|
||||||
ServiceDiscoveryManager.getInstanceFor(connection)
|
ServiceDiscoveryManager.getInstanceFor(connection)
|
||||||
.setNodeInformationProvider(discoNode,
|
.setNodeInformationProvider(NAMESPACE,
|
||||||
new NodeInformationProvider() {
|
new NodeInformationProvider() {
|
||||||
public List<DiscoverItems.Item> getNodeItems() {
|
public List<DiscoverItems.Item> getNodeItems() {
|
||||||
|
|
||||||
|
@ -221,7 +213,7 @@ public class AdHocCommandManager {
|
||||||
* @param factory a factory to create new instances of the command.
|
* @param factory a factory to create new instances of the command.
|
||||||
*/
|
*/
|
||||||
public void registerCommand(String node, final String name, LocalCommandFactory factory) {
|
public void registerCommand(String node, final String name, LocalCommandFactory factory) {
|
||||||
AdHocCommandInfo commandInfo = new AdHocCommandInfo(node, name, connection.get().getUser(), factory);
|
AdHocCommandInfo commandInfo = new AdHocCommandInfo(node, name, connection().getUser(), factory);
|
||||||
|
|
||||||
commands.put(node, commandInfo);
|
commands.put(node, commandInfo);
|
||||||
// Set the NodeInformationProvider that will provide information about
|
// Set the NodeInformationProvider that will provide information about
|
||||||
|
@ -234,7 +226,7 @@ public class AdHocCommandManager {
|
||||||
|
|
||||||
public List<String> getNodeFeatures() {
|
public List<String> getNodeFeatures() {
|
||||||
List<String> answer = new ArrayList<String>();
|
List<String> answer = new ArrayList<String>();
|
||||||
answer.add(DISCO_NAMESPACE);
|
answer.add(NAMESPACE);
|
||||||
// TODO: check if this service is provided by the
|
// TODO: check if this service is provided by the
|
||||||
// TODO: current connection.
|
// TODO: current connection.
|
||||||
answer.add("jabber:x:data");
|
answer.add("jabber:x:data");
|
||||||
|
@ -266,7 +258,7 @@ public class AdHocCommandManager {
|
||||||
* @throws XMPPException if the operation failed for some reason.
|
* @throws XMPPException if the operation failed for some reason.
|
||||||
*/
|
*/
|
||||||
public DiscoverItems discoverCommands(String jid) throws XMPPException {
|
public DiscoverItems discoverCommands(String jid) throws XMPPException {
|
||||||
return serviceDiscoveryManager.discoverItems(jid, discoNode);
|
return serviceDiscoveryManager.discoverItems(jid, NAMESPACE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -287,7 +279,7 @@ public class AdHocCommandManager {
|
||||||
discoverItems.addItem(item);
|
discoverItems.addItem(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
serviceDiscoveryManager.publishItems(jid, discoNode, discoverItems);
|
serviceDiscoveryManager.publishItems(jid, NAMESPACE, discoverItems);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -301,7 +293,7 @@ public class AdHocCommandManager {
|
||||||
* @return a local instance equivalent to the remote command.
|
* @return a local instance equivalent to the remote command.
|
||||||
*/
|
*/
|
||||||
public RemoteCommand getRemoteCommand(String jid, String node) {
|
public RemoteCommand getRemoteCommand(String jid, String node) {
|
||||||
return new RemoteCommand(connection.get(), node, jid);
|
return new RemoteCommand(connection(), node, jid);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -449,7 +441,7 @@ public class AdHocCommandManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sends the response packet
|
// Sends the response packet
|
||||||
connection.get().sendPacket(response);
|
connection().sendPacket(response);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (XMPPException e) {
|
catch (XMPPException e) {
|
||||||
|
@ -564,7 +556,7 @@ public class AdHocCommandManager {
|
||||||
executingCommands.remove(sessionId);
|
executingCommands.remove(sessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
connection.get().sendPacket(response);
|
connection().sendPacket(response);
|
||||||
}
|
}
|
||||||
catch (XMPPException e) {
|
catch (XMPPException e) {
|
||||||
// If there is an exception caused by the next, complete,
|
// If there is an exception caused by the next, complete,
|
||||||
|
@ -620,7 +612,7 @@ public class AdHocCommandManager {
|
||||||
private void respondError(AdHocCommandData response, XMPPError error) {
|
private void respondError(AdHocCommandData response, XMPPError error) {
|
||||||
response.setType(IQ.Type.ERROR);
|
response.setType(IQ.Type.ERROR);
|
||||||
response.setError(error);
|
response.setError(error);
|
||||||
connection.get().sendPacket(response);
|
connection().sendPacket(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,7 +17,11 @@
|
||||||
|
|
||||||
package org.jivesoftware.smackx.disco;
|
package org.jivesoftware.smackx.disco;
|
||||||
|
|
||||||
import org.jivesoftware.smack.*;
|
import org.jivesoftware.smack.Connection;
|
||||||
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||||
|
import org.jivesoftware.smack.Manager;
|
||||||
|
import org.jivesoftware.smack.PacketListener;
|
||||||
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.filter.PacketFilter;
|
import org.jivesoftware.smack.filter.PacketFilter;
|
||||||
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
import org.jivesoftware.smack.filter.PacketTypeFilter;
|
||||||
import org.jivesoftware.smack.packet.IQ;
|
import org.jivesoftware.smack.packet.IQ;
|
||||||
|
@ -30,8 +34,15 @@ import org.jivesoftware.smackx.disco.packet.DiscoverItems;
|
||||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
|
import org.jivesoftware.smackx.disco.packet.DiscoverInfo.Identity;
|
||||||
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
import org.jivesoftware.smackx.xdata.packet.DataForm;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
import java.util.ArrayList;
|
||||||
import java.util.*;
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.WeakHashMap;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -45,7 +56,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||||
*
|
*
|
||||||
* @author Gaston Dombiak
|
* @author Gaston Dombiak
|
||||||
*/
|
*/
|
||||||
public class ServiceDiscoveryManager {
|
public class ServiceDiscoveryManager extends Manager {
|
||||||
|
|
||||||
private static final String DEFAULT_IDENTITY_NAME = "Smack";
|
private static final String DEFAULT_IDENTITY_NAME = "Smack";
|
||||||
private static final String DEFAULT_IDENTITY_CATEGORY = "client";
|
private static final String DEFAULT_IDENTITY_CATEGORY = "client";
|
||||||
|
@ -61,7 +72,6 @@ public class ServiceDiscoveryManager {
|
||||||
private static Map<Connection, ServiceDiscoveryManager> instances =
|
private static Map<Connection, ServiceDiscoveryManager> instances =
|
||||||
Collections.synchronizedMap(new WeakHashMap<Connection, ServiceDiscoveryManager>());
|
Collections.synchronizedMap(new WeakHashMap<Connection, ServiceDiscoveryManager>());
|
||||||
|
|
||||||
private WeakReference<Connection> connection;
|
|
||||||
private final Set<String> features = new HashSet<String>();
|
private final Set<String> features = new HashSet<String>();
|
||||||
private DataForm extendedInfo = null;
|
private DataForm extendedInfo = null;
|
||||||
private Map<String, NodeInformationProvider> nodeInformationProviders =
|
private Map<String, NodeInformationProvider> nodeInformationProviders =
|
||||||
|
@ -94,8 +104,7 @@ public class ServiceDiscoveryManager {
|
||||||
* @param connection the connection to which a ServiceDiscoveryManager is going to be created.
|
* @param connection the connection to which a ServiceDiscoveryManager is going to be created.
|
||||||
*/
|
*/
|
||||||
private ServiceDiscoveryManager(Connection connection) {
|
private ServiceDiscoveryManager(Connection connection) {
|
||||||
this.connection = new WeakReference<Connection>(connection);
|
super(connection);
|
||||||
|
|
||||||
// Register the new instance and associate it with the connection
|
// Register the new instance and associate it with the connection
|
||||||
instances.put(connection, this);
|
instances.put(connection, this);
|
||||||
|
|
||||||
|
@ -106,7 +115,7 @@ public class ServiceDiscoveryManager {
|
||||||
PacketFilter packetFilter = new PacketTypeFilter(DiscoverItems.class);
|
PacketFilter packetFilter = new PacketTypeFilter(DiscoverItems.class);
|
||||||
PacketListener packetListener = new PacketListener() {
|
PacketListener packetListener = new PacketListener() {
|
||||||
public void processPacket(Packet packet) {
|
public void processPacket(Packet packet) {
|
||||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
Connection connection = connection();
|
||||||
if (connection == null) return;
|
if (connection == null) return;
|
||||||
DiscoverItems discoverItems = (DiscoverItems) packet;
|
DiscoverItems discoverItems = (DiscoverItems) packet;
|
||||||
// Send back the items defined in the client if the request is of type GET
|
// Send back the items defined in the client if the request is of type GET
|
||||||
|
@ -143,7 +152,7 @@ public class ServiceDiscoveryManager {
|
||||||
packetFilter = new PacketTypeFilter(DiscoverInfo.class);
|
packetFilter = new PacketTypeFilter(DiscoverInfo.class);
|
||||||
packetListener = new PacketListener() {
|
packetListener = new PacketListener() {
|
||||||
public void processPacket(Packet packet) {
|
public void processPacket(Packet packet) {
|
||||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
Connection connection = connection();
|
||||||
if (connection == null) return;
|
if (connection == null) return;
|
||||||
DiscoverInfo discoverInfo = (DiscoverInfo) packet;
|
DiscoverInfo discoverInfo = (DiscoverInfo) packet;
|
||||||
// Answer the client's supported features if the request is of the GET type
|
// Answer the client's supported features if the request is of the GET type
|
||||||
|
@ -530,16 +539,13 @@ public class ServiceDiscoveryManager {
|
||||||
* @throws XMPPException if the operation failed for some reason.
|
* @throws XMPPException if the operation failed for some reason.
|
||||||
*/
|
*/
|
||||||
public DiscoverInfo discoverInfo(String entityID, String node) throws XMPPException {
|
public DiscoverInfo discoverInfo(String entityID, String node) throws XMPPException {
|
||||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
|
||||||
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
|
|
||||||
|
|
||||||
// Discover the entity's info
|
// Discover the entity's info
|
||||||
DiscoverInfo disco = new DiscoverInfo();
|
DiscoverInfo disco = new DiscoverInfo();
|
||||||
disco.setType(IQ.Type.GET);
|
disco.setType(IQ.Type.GET);
|
||||||
disco.setTo(entityID);
|
disco.setTo(entityID);
|
||||||
disco.setNode(node);
|
disco.setNode(node);
|
||||||
|
|
||||||
Packet result = connection.createPacketCollectorAndSend(disco).nextResultOrThrow();
|
Packet result = connection().createPacketCollectorAndSend(disco).nextResultOrThrow();
|
||||||
|
|
||||||
return (DiscoverInfo) result;
|
return (DiscoverInfo) result;
|
||||||
}
|
}
|
||||||
|
@ -566,16 +572,13 @@ public class ServiceDiscoveryManager {
|
||||||
* @throws XMPPException if the operation failed for some reason.
|
* @throws XMPPException if the operation failed for some reason.
|
||||||
*/
|
*/
|
||||||
public DiscoverItems discoverItems(String entityID, String node) throws XMPPException {
|
public DiscoverItems discoverItems(String entityID, String node) throws XMPPException {
|
||||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
|
||||||
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
|
|
||||||
|
|
||||||
// Discover the entity's items
|
// Discover the entity's items
|
||||||
DiscoverItems disco = new DiscoverItems();
|
DiscoverItems disco = new DiscoverItems();
|
||||||
disco.setType(IQ.Type.GET);
|
disco.setType(IQ.Type.GET);
|
||||||
disco.setTo(entityID);
|
disco.setTo(entityID);
|
||||||
disco.setNode(node);
|
disco.setNode(node);
|
||||||
|
|
||||||
Packet result = connection.createPacketCollectorAndSend(disco).nextResultOrThrow();
|
Packet result = connection().createPacketCollectorAndSend(disco).nextResultOrThrow();
|
||||||
return (DiscoverItems) result;
|
return (DiscoverItems) result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -635,14 +638,11 @@ public class ServiceDiscoveryManager {
|
||||||
*/
|
*/
|
||||||
public void publishItems(String entityID, String node, DiscoverItems discoverItems)
|
public void publishItems(String entityID, String node, DiscoverItems discoverItems)
|
||||||
throws XMPPException {
|
throws XMPPException {
|
||||||
Connection connection = ServiceDiscoveryManager.this.connection.get();
|
|
||||||
if (connection == null) throw new XMPPException("Connection instance already gc'ed");
|
|
||||||
|
|
||||||
discoverItems.setType(IQ.Type.SET);
|
discoverItems.setType(IQ.Type.SET);
|
||||||
discoverItems.setTo(entityID);
|
discoverItems.setTo(entityID);
|
||||||
discoverItems.setNode(node);
|
discoverItems.setNode(node);
|
||||||
|
|
||||||
connection.createPacketCollectorAndSend(discoverItems).nextResultOrThrow();
|
connection().createPacketCollectorAndSend(discoverItems).nextResultOrThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -17,12 +17,12 @@
|
||||||
|
|
||||||
package org.jivesoftware.smackx.iqversion;
|
package org.jivesoftware.smackx.iqversion;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
import org.jivesoftware.smack.Connection;
|
import org.jivesoftware.smack.Connection;
|
||||||
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.PacketListener;
|
import org.jivesoftware.smack.PacketListener;
|
||||||
import org.jivesoftware.smack.filter.AndFilter;
|
import org.jivesoftware.smack.filter.AndFilter;
|
||||||
import org.jivesoftware.smack.filter.IQTypeFilter;
|
import org.jivesoftware.smack.filter.IQTypeFilter;
|
||||||
|
@ -48,15 +48,14 @@ import org.jivesoftware.smackx.iqversion.packet.Version;
|
||||||
*
|
*
|
||||||
* @author Georg Lukas
|
* @author Georg Lukas
|
||||||
*/
|
*/
|
||||||
public class VersionManager {
|
public class VersionManager extends Manager {
|
||||||
private static final Map<Connection, VersionManager> instances =
|
private static final Map<Connection, VersionManager> instances =
|
||||||
Collections.synchronizedMap(new WeakHashMap<Connection, VersionManager>());
|
Collections.synchronizedMap(new WeakHashMap<Connection, VersionManager>());
|
||||||
|
|
||||||
private Version own_version;
|
private Version own_version;
|
||||||
private WeakReference<Connection> weakRefConnection;
|
|
||||||
|
|
||||||
private VersionManager(final Connection connection) {
|
private VersionManager(final Connection connection) {
|
||||||
this.weakRefConnection = new WeakReference<Connection>(connection);
|
super(connection);
|
||||||
instances.put(connection, this);
|
instances.put(connection, this);
|
||||||
|
|
||||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
|
@ -73,7 +72,7 @@ public class VersionManager {
|
||||||
Version reply = new Version(own_version);
|
Version reply = new Version(own_version);
|
||||||
reply.setPacketID(packet.getPacketID());
|
reply.setPacketID(packet.getPacketID());
|
||||||
reply.setTo(packet.getFrom());
|
reply.setTo(packet.getFrom());
|
||||||
weakRefConnection.get().sendPacket(reply);
|
connection().sendPacket(reply);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
, new AndFilter(new PacketTypeFilter(Version.class), new IQTypeFilter(Type.GET)));
|
, new AndFilter(new PacketTypeFilter(Version.class), new IQTypeFilter(Type.GET)));
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.ping;
|
package org.jivesoftware.smackx.ping;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -29,6 +28,7 @@ import java.util.logging.Logger;
|
||||||
import org.jivesoftware.smack.Connection;
|
import org.jivesoftware.smack.Connection;
|
||||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||||
import org.jivesoftware.smack.ConnectionListener;
|
import org.jivesoftware.smack.ConnectionListener;
|
||||||
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.PacketListener;
|
import org.jivesoftware.smack.PacketListener;
|
||||||
import org.jivesoftware.smack.SmackError;
|
import org.jivesoftware.smack.SmackError;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
|
@ -39,7 +39,6 @@ import org.jivesoftware.smack.filter.PacketTypeFilter;
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.jivesoftware.smack.packet.IQ.Type;
|
import org.jivesoftware.smack.packet.IQ.Type;
|
||||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
|
||||||
import org.jivesoftware.smackx.ping.packet.Ping;
|
import org.jivesoftware.smackx.ping.packet.Ping;
|
||||||
import org.jivesoftware.smackx.ping.packet.Pong;
|
import org.jivesoftware.smackx.ping.packet.Pong;
|
||||||
|
|
||||||
|
@ -52,7 +51,7 @@ import org.jivesoftware.smackx.ping.packet.Pong;
|
||||||
* @author Florian Schmaus
|
* @author Florian Schmaus
|
||||||
* @see <a href="http://www.xmpp.org/extensions/xep-0199.html">XEP-0199:XMPP Ping</a>
|
* @see <a href="http://www.xmpp.org/extensions/xep-0199.html">XEP-0199:XMPP Ping</a>
|
||||||
*/
|
*/
|
||||||
public class PingManager {
|
public class PingManager extends Manager {
|
||||||
public static final String NAMESPACE = "urn:xmpp:ping";
|
public static final String NAMESPACE = "urn:xmpp:ping";
|
||||||
|
|
||||||
private static final Logger LOGGER = Logger.getLogger(PingManager.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(PingManager.class.getName());
|
||||||
|
@ -118,10 +117,8 @@ public class PingManager {
|
||||||
*/
|
*/
|
||||||
private long lastSuccessfulManualPing = -1;
|
private long lastSuccessfulManualPing = -1;
|
||||||
|
|
||||||
private WeakReference<Connection> weakRefConnection;
|
|
||||||
|
|
||||||
private PingManager(Connection connection) {
|
private PingManager(Connection connection) {
|
||||||
weakRefConnection = new WeakReference<Connection>(connection);
|
super(connection);
|
||||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
sdm.addFeature(PingManager.NAMESPACE);
|
sdm.addFeature(PingManager.NAMESPACE);
|
||||||
INSTANCES.put(connection, this);
|
INSTANCES.put(connection, this);
|
||||||
|
@ -130,9 +127,8 @@ public class PingManager {
|
||||||
// Send a Pong for every Ping
|
// Send a Pong for every Ping
|
||||||
@Override
|
@Override
|
||||||
public void processPacket(Packet packet) {
|
public void processPacket(Packet packet) {
|
||||||
Connection connection = weakRefConnection.get();
|
|
||||||
Pong pong = new Pong(packet);
|
Pong pong = new Pong(packet);
|
||||||
connection.sendPacket(pong);
|
connection().sendPacket(pong);
|
||||||
}
|
}
|
||||||
}, PING_PACKET_FILTER);
|
}, PING_PACKET_FILTER);
|
||||||
connection.addConnectionListener(new ConnectionListener() {
|
connection.addConnectionListener(new ConnectionListener() {
|
||||||
|
@ -170,12 +166,11 @@ public class PingManager {
|
||||||
*/
|
*/
|
||||||
public boolean ping(String jid, long pingTimeout) {
|
public boolean ping(String jid, long pingTimeout) {
|
||||||
Ping ping = new Ping(jid);
|
Ping ping = new Ping(jid);
|
||||||
Connection connection = weakRefConnection.get();
|
|
||||||
try {
|
try {
|
||||||
connection.createPacketCollectorAndSend(ping).nextResultOrThrow();
|
connection().createPacketCollectorAndSend(ping).nextResultOrThrow();
|
||||||
}
|
}
|
||||||
catch (XMPPException exc) {
|
catch (XMPPException exc) {
|
||||||
return (jid.equals(connection.getServiceName()) && (exc.getSmackError() != SmackError.NO_RESPONSE_FROM_SERVER));
|
return (jid.equals(connection().getServiceName()) && (exc.getSmackError() != SmackError.NO_RESPONSE_FROM_SERVER));
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -188,8 +183,7 @@ public class PingManager {
|
||||||
* @return true if a reply was received from the entity, false otherwise.
|
* @return true if a reply was received from the entity, false otherwise.
|
||||||
*/
|
*/
|
||||||
public boolean ping(String jid) {
|
public boolean ping(String jid) {
|
||||||
Connection connection = weakRefConnection.get();
|
return ping(jid, connection().getPacketReplyTimeout());
|
||||||
return ping(jid, connection.getPacketReplyTimeout());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -200,9 +194,7 @@ public class PingManager {
|
||||||
* @throws XMPPException An XMPP related error occurred during the request
|
* @throws XMPPException An XMPP related error occurred during the request
|
||||||
*/
|
*/
|
||||||
public boolean isPingSupported(String jid) throws XMPPException {
|
public boolean isPingSupported(String jid) throws XMPPException {
|
||||||
Connection connection = weakRefConnection.get();
|
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid, PingManager.NAMESPACE);
|
||||||
DiscoverInfo result = ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(jid);
|
|
||||||
return result.containsFeature(PingManager.NAMESPACE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -229,8 +221,7 @@ public class PingManager {
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public boolean pingMyServer(boolean notifyListeners) {
|
public boolean pingMyServer(boolean notifyListeners) {
|
||||||
Connection connection = weakRefConnection.get();
|
boolean res = ping(connection().getServiceName());
|
||||||
boolean res = ping(connection.getServiceName());
|
|
||||||
if (res) {
|
if (res) {
|
||||||
pongReceived();
|
pongReceived();
|
||||||
} else if (notifyListeners) {
|
} else if (notifyListeners) {
|
||||||
|
@ -296,8 +287,7 @@ public class PingManager {
|
||||||
maybeStopPingServerTask();
|
maybeStopPingServerTask();
|
||||||
if (pingInterval > 0) {
|
if (pingInterval > 0) {
|
||||||
LOGGER.fine("Scheduling ServerPingTask in " + pingInterval + " seconds");
|
LOGGER.fine("Scheduling ServerPingTask in " + pingInterval + " seconds");
|
||||||
Connection connection = weakRefConnection.get();
|
nextAutomaticPing = connection().schedule(pingServerRunnable, pingInterval, TimeUnit.SECONDS);
|
||||||
nextAutomaticPing = connection.schedule(pingServerRunnable, pingInterval, TimeUnit.SECONDS);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -318,7 +308,7 @@ public class PingManager {
|
||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
LOGGER.fine("ServerPingTask run()");
|
LOGGER.fine("ServerPingTask run()");
|
||||||
Connection connection = weakRefConnection.get();
|
Connection connection = connection();
|
||||||
if (connection == null) {
|
if (connection == null) {
|
||||||
// connection has been collected by GC
|
// connection has been collected by GC
|
||||||
// which means we can stop the thread by breaking the loop
|
// which means we can stop the thread by breaking the loop
|
||||||
|
|
|
@ -16,8 +16,16 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.privacy;
|
package org.jivesoftware.smackx.privacy;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
import java.util.WeakHashMap;
|
||||||
|
|
||||||
import org.jivesoftware.smack.Connection;
|
import org.jivesoftware.smack.Connection;
|
||||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||||
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.PacketListener;
|
import org.jivesoftware.smack.PacketListener;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.filter.*;
|
import org.jivesoftware.smack.filter.*;
|
||||||
|
@ -26,9 +34,6 @@ import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.jivesoftware.smackx.privacy.packet.Privacy;
|
import org.jivesoftware.smackx.privacy.packet.Privacy;
|
||||||
import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
|
import org.jivesoftware.smackx.privacy.packet.PrivacyItem;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A PrivacyListManager is used by XMPP clients to block or allow communications from other
|
* A PrivacyListManager is used by XMPP clients to block or allow communications from other
|
||||||
* users. Use the manager to: <ul>
|
* users. Use the manager to: <ul>
|
||||||
|
@ -42,13 +47,12 @@ import java.util.*;
|
||||||
*
|
*
|
||||||
* @author Francisco Vives
|
* @author Francisco Vives
|
||||||
*/
|
*/
|
||||||
public class PrivacyListManager {
|
public class PrivacyListManager extends Manager {
|
||||||
|
|
||||||
// Keep the list of instances of this class.
|
// Keep the list of instances of this class.
|
||||||
private static Map<Connection, PrivacyListManager> instances = Collections
|
private static Map<Connection, PrivacyListManager> instances = Collections
|
||||||
.synchronizedMap(new WeakHashMap<Connection, PrivacyListManager>());
|
.synchronizedMap(new WeakHashMap<Connection, PrivacyListManager>());
|
||||||
|
|
||||||
private WeakReference<Connection> connection;
|
|
||||||
private final List<PrivacyListListener> listeners = new ArrayList<PrivacyListListener>();
|
private final List<PrivacyListListener> listeners = new ArrayList<PrivacyListListener>();
|
||||||
PacketFilter packetFilter = new AndFilter(new IQTypeFilter(IQ.Type.SET),
|
PacketFilter packetFilter = new AndFilter(new IQTypeFilter(IQ.Type.SET),
|
||||||
new PacketExtensionFilter("query", "jabber:iq:privacy"));
|
new PacketExtensionFilter("query", "jabber:iq:privacy"));
|
||||||
|
@ -71,7 +75,7 @@ public class PrivacyListManager {
|
||||||
* @param connection the XMPP connection.
|
* @param connection the XMPP connection.
|
||||||
*/
|
*/
|
||||||
private PrivacyListManager(final Connection connection) {
|
private PrivacyListManager(final Connection connection) {
|
||||||
this.connection = new WeakReference<Connection>(connection);
|
super(connection);
|
||||||
// Register the new instance and associate it with the connection
|
// Register the new instance and associate it with the connection
|
||||||
instances.put(connection, this);
|
instances.put(connection, this);
|
||||||
|
|
||||||
|
@ -121,7 +125,7 @@ public class PrivacyListManager {
|
||||||
* @return the userJID that owns the privacy
|
* @return the userJID that owns the privacy
|
||||||
*/
|
*/
|
||||||
private String getUser() {
|
private String getUser() {
|
||||||
return connection.get().getUser();
|
return connection().getUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -146,13 +150,11 @@ public class PrivacyListManager {
|
||||||
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
||||||
*/
|
*/
|
||||||
private Privacy getRequest(Privacy requestPrivacy) throws XMPPException {
|
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
|
// The request is a get iq type
|
||||||
requestPrivacy.setType(Privacy.Type.GET);
|
requestPrivacy.setType(Privacy.Type.GET);
|
||||||
requestPrivacy.setFrom(this.getUser());
|
requestPrivacy.setFrom(this.getUser());
|
||||||
|
|
||||||
Privacy privacyAnswer = (Privacy) connection.createPacketCollectorAndSend(requestPrivacy).nextResultOrThrow();
|
Privacy privacyAnswer = (Privacy) connection().createPacketCollectorAndSend(requestPrivacy).nextResultOrThrow();
|
||||||
return privacyAnswer;
|
return privacyAnswer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,14 +168,11 @@ public class PrivacyListManager {
|
||||||
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
* @exception XMPPException if the request or the answer failed, it raises an exception.
|
||||||
*/
|
*/
|
||||||
private Packet setRequest(Privacy requestPrivacy) throws XMPPException {
|
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
|
// The request is a get iq type
|
||||||
requestPrivacy.setType(Privacy.Type.SET);
|
requestPrivacy.setType(Privacy.Type.SET);
|
||||||
requestPrivacy.setFrom(this.getUser());
|
requestPrivacy.setFrom(this.getUser());
|
||||||
|
|
||||||
return connection.createPacketCollectorAndSend(requestPrivacy).nextResultOrThrow();
|
return connection().createPacketCollectorAndSend(requestPrivacy).nextResultOrThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -16,7 +16,6 @@
|
||||||
*/
|
*/
|
||||||
package org.jivesoftware.smackx.receipts;
|
package org.jivesoftware.smackx.receipts;
|
||||||
|
|
||||||
import java.lang.ref.WeakReference;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -25,13 +24,13 @@ import java.util.WeakHashMap;
|
||||||
|
|
||||||
import org.jivesoftware.smack.Connection;
|
import org.jivesoftware.smack.Connection;
|
||||||
import org.jivesoftware.smack.ConnectionCreationListener;
|
import org.jivesoftware.smack.ConnectionCreationListener;
|
||||||
|
import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.PacketListener;
|
import org.jivesoftware.smack.PacketListener;
|
||||||
import org.jivesoftware.smack.XMPPException;
|
import org.jivesoftware.smack.XMPPException;
|
||||||
import org.jivesoftware.smack.filter.PacketExtensionFilter;
|
import org.jivesoftware.smack.filter.PacketExtensionFilter;
|
||||||
import org.jivesoftware.smack.packet.Message;
|
import org.jivesoftware.smack.packet.Message;
|
||||||
import org.jivesoftware.smack.packet.Packet;
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||||
import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manager for XEP-0184: Message Delivery Receipts. This class implements
|
* Manager for XEP-0184: Message Delivery Receipts. This class implements
|
||||||
|
@ -40,7 +39,7 @@ import org.jivesoftware.smackx.disco.packet.DiscoverInfo;
|
||||||
*
|
*
|
||||||
* @author Georg Lukas
|
* @author Georg Lukas
|
||||||
*/
|
*/
|
||||||
public class DeliveryReceiptManager implements PacketListener {
|
public class DeliveryReceiptManager extends Manager implements PacketListener {
|
||||||
|
|
||||||
private static Map<Connection, DeliveryReceiptManager> instances =
|
private static Map<Connection, DeliveryReceiptManager> instances =
|
||||||
Collections.synchronizedMap(new WeakHashMap<Connection, DeliveryReceiptManager>());
|
Collections.synchronizedMap(new WeakHashMap<Connection, DeliveryReceiptManager>());
|
||||||
|
@ -53,15 +52,14 @@ public class DeliveryReceiptManager implements PacketListener {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private WeakReference<Connection> weakRefConnection;
|
|
||||||
private boolean auto_receipts_enabled = false;
|
private boolean auto_receipts_enabled = false;
|
||||||
private Set<ReceiptReceivedListener> receiptReceivedListeners = Collections
|
private Set<ReceiptReceivedListener> receiptReceivedListeners = Collections
|
||||||
.synchronizedSet(new HashSet<ReceiptReceivedListener>());
|
.synchronizedSet(new HashSet<ReceiptReceivedListener>());
|
||||||
|
|
||||||
private DeliveryReceiptManager(Connection connection) {
|
private DeliveryReceiptManager(Connection connection) {
|
||||||
|
super(connection);
|
||||||
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
ServiceDiscoveryManager sdm = ServiceDiscoveryManager.getInstanceFor(connection);
|
||||||
sdm.addFeature(DeliveryReceipt.NAMESPACE);
|
sdm.addFeature(DeliveryReceipt.NAMESPACE);
|
||||||
weakRefConnection = new WeakReference<Connection>(connection);
|
|
||||||
instances.put(connection, this);
|
instances.put(connection, this);
|
||||||
|
|
||||||
// register listener for delivery receipts and requests
|
// register listener for delivery receipts and requests
|
||||||
|
@ -92,11 +90,9 @@ public class DeliveryReceiptManager implements PacketListener {
|
||||||
* @return true if supported
|
* @return true if supported
|
||||||
*/
|
*/
|
||||||
public boolean isSupported(String jid) {
|
public boolean isSupported(String jid) {
|
||||||
Connection connection = weakRefConnection.get();
|
|
||||||
try {
|
try {
|
||||||
DiscoverInfo result =
|
return ServiceDiscoveryManager.getInstanceFor(connection()).supportsFeature(jid,
|
||||||
ServiceDiscoveryManager.getInstanceFor(connection).discoverInfo(jid);
|
DeliveryReceipt.NAMESPACE);
|
||||||
return result.containsFeature(DeliveryReceipt.NAMESPACE);
|
|
||||||
}
|
}
|
||||||
catch (XMPPException e) {
|
catch (XMPPException e) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -121,7 +117,7 @@ public class DeliveryReceiptManager implements PacketListener {
|
||||||
DeliveryReceiptRequest drr = (DeliveryReceiptRequest)packet.getExtension(
|
DeliveryReceiptRequest drr = (DeliveryReceiptRequest)packet.getExtension(
|
||||||
DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE);
|
DeliveryReceiptRequest.ELEMENT, DeliveryReceipt.NAMESPACE);
|
||||||
if (drr != null) {
|
if (drr != null) {
|
||||||
Connection connection = weakRefConnection.get();
|
Connection connection = connection();
|
||||||
Message ack = new Message(packet.getFrom(), Message.Type.normal);
|
Message ack = new Message(packet.getFrom(), Message.Type.normal);
|
||||||
ack.addExtension(new DeliveryReceipt(packet.getPacketID()));
|
ack.addExtension(new DeliveryReceipt(packet.getPacketID()));
|
||||||
connection.sendPacket(ack);
|
connection.sendPacket(ack);
|
||||||
|
|
Loading…
Reference in a new issue