mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-23 04:22:05 +01:00
Make ServiceDiscoveryManager use constant PacketFilters
This commit is contained in:
parent
191c4429da
commit
5d159c4be2
1 changed files with 55 additions and 63 deletions
|
@ -24,6 +24,8 @@ import org.jivesoftware.smack.Manager;
|
||||||
import org.jivesoftware.smack.PacketListener;
|
import org.jivesoftware.smack.PacketListener;
|
||||||
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
import org.jivesoftware.smack.XMPPConnectionRegistry;
|
||||||
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
||||||
|
import org.jivesoftware.smack.filter.AndFilter;
|
||||||
|
import org.jivesoftware.smack.filter.IQTypeFilter;
|
||||||
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;
|
||||||
|
@ -63,9 +65,14 @@ import java.util.logging.Logger;
|
||||||
public class ServiceDiscoveryManager extends Manager {
|
public class ServiceDiscoveryManager extends Manager {
|
||||||
|
|
||||||
private static final Logger LOGGER = Logger.getLogger(ServiceDiscoveryManager.class.getName());
|
private static final Logger LOGGER = Logger.getLogger(ServiceDiscoveryManager.class.getName());
|
||||||
|
|
||||||
|
private static final PacketFilter GET_DISCOVER_ITEMS = new AndFilter(IQTypeFilter.GET, new PacketTypeFilter(DiscoverItems.class));
|
||||||
|
private static final PacketFilter GET_DISCOVER_INFO = new AndFilter(IQTypeFilter.GET, new PacketTypeFilter(DiscoverInfo.class));
|
||||||
|
|
||||||
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";
|
||||||
private static final String DEFAULT_IDENTITY_TYPE = "pc";
|
private static final String DEFAULT_IDENTITY_TYPE = "pc";
|
||||||
|
|
||||||
private static DiscoverInfo.Identity defaultIdentity = new Identity(DEFAULT_IDENTITY_CATEGORY,
|
private static DiscoverInfo.Identity defaultIdentity = new Identity(DEFAULT_IDENTITY_CATEGORY,
|
||||||
DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE);
|
DEFAULT_IDENTITY_NAME, DEFAULT_IDENTITY_TYPE);
|
||||||
|
|
||||||
|
@ -117,14 +124,9 @@ public class ServiceDiscoveryManager extends Manager {
|
||||||
addFeature(DiscoverItems.NAMESPACE);
|
addFeature(DiscoverItems.NAMESPACE);
|
||||||
|
|
||||||
// Listen for disco#items requests and answer with an empty result
|
// Listen for disco#items requests and answer with an empty result
|
||||||
PacketFilter packetFilter = new PacketTypeFilter(DiscoverItems.class);
|
|
||||||
PacketListener packetListener = new PacketListener() {
|
PacketListener packetListener = new PacketListener() {
|
||||||
public void processPacket(Packet packet) throws NotConnectedException {
|
public void processPacket(Packet packet) throws NotConnectedException {
|
||||||
XMPPConnection connection = connection();
|
|
||||||
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
|
|
||||||
if (discoverItems != null && discoverItems.getType() == IQ.Type.get) {
|
|
||||||
DiscoverItems response = new DiscoverItems();
|
DiscoverItems response = new DiscoverItems();
|
||||||
response.setType(IQ.Type.result);
|
response.setType(IQ.Type.result);
|
||||||
response.setTo(discoverItems.getFrom());
|
response.setTo(discoverItems.getFrom());
|
||||||
|
@ -133,8 +135,7 @@ public class ServiceDiscoveryManager extends Manager {
|
||||||
|
|
||||||
// Add the defined items related to the requested node. Look for
|
// Add the defined items related to the requested node. Look for
|
||||||
// the NodeInformationProvider associated with the requested node.
|
// the NodeInformationProvider associated with the requested node.
|
||||||
NodeInformationProvider nodeInformationProvider =
|
NodeInformationProvider nodeInformationProvider = getNodeInformationProvider(discoverItems.getNode());
|
||||||
getNodeInformationProvider(discoverItems.getNode());
|
|
||||||
if (nodeInformationProvider != null) {
|
if (nodeInformationProvider != null) {
|
||||||
// Specified node was found, add node items
|
// Specified node was found, add node items
|
||||||
response.addItems(nodeInformationProvider.getNodeItems());
|
response.addItems(nodeInformationProvider.getNodeItems());
|
||||||
|
@ -146,22 +147,17 @@ public class ServiceDiscoveryManager extends Manager {
|
||||||
response.setType(IQ.Type.error);
|
response.setType(IQ.Type.error);
|
||||||
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
|
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
|
||||||
}
|
}
|
||||||
connection.sendPacket(response);
|
connection().sendPacket(response);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
connection.addPacketListener(packetListener, packetFilter);
|
connection.addPacketListener(packetListener, GET_DISCOVER_ITEMS);
|
||||||
|
|
||||||
// Listen for disco#info requests and answer the client's supported features
|
// Listen for disco#info requests and answer the client's supported features
|
||||||
// To add a new feature as supported use the #addFeature message
|
// To add a new feature as supported use the #addFeature message
|
||||||
packetFilter = new PacketTypeFilter(DiscoverInfo.class);
|
|
||||||
packetListener = new PacketListener() {
|
packetListener = new PacketListener() {
|
||||||
public void processPacket(Packet packet) throws NotConnectedException {
|
public void processPacket(Packet packet) throws NotConnectedException {
|
||||||
XMPPConnection connection = connection();
|
|
||||||
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
|
||||||
if (discoverInfo != null && discoverInfo.getType() == IQ.Type.get) {
|
|
||||||
DiscoverInfo response = new DiscoverInfo();
|
DiscoverInfo response = new DiscoverInfo();
|
||||||
response.setType(IQ.Type.result);
|
response.setType(IQ.Type.result);
|
||||||
response.setTo(discoverInfo.getFrom());
|
response.setTo(discoverInfo.getFrom());
|
||||||
|
@ -173,12 +169,10 @@ public class ServiceDiscoveryManager extends Manager {
|
||||||
// if the right node is chosen
|
// if the right node is chosen
|
||||||
if (discoverInfo.getNode() == null) {
|
if (discoverInfo.getNode() == null) {
|
||||||
addDiscoverInfoTo(response);
|
addDiscoverInfoTo(response);
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Disco#info was sent to a node. Check if we have information of the
|
// Disco#info was sent to a node. Check if we have information of the
|
||||||
// specified node
|
// specified node
|
||||||
NodeInformationProvider nodeInformationProvider =
|
NodeInformationProvider nodeInformationProvider = getNodeInformationProvider(discoverInfo.getNode());
|
||||||
getNodeInformationProvider(discoverInfo.getNode());
|
|
||||||
if (nodeInformationProvider != null) {
|
if (nodeInformationProvider != null) {
|
||||||
// Node was found. Add node features
|
// Node was found. Add node features
|
||||||
response.addFeatures(nodeInformationProvider.getNodeFeatures());
|
response.addFeatures(nodeInformationProvider.getNodeFeatures());
|
||||||
|
@ -186,18 +180,16 @@ public class ServiceDiscoveryManager extends Manager {
|
||||||
response.addIdentities(nodeInformationProvider.getNodeIdentities());
|
response.addIdentities(nodeInformationProvider.getNodeIdentities());
|
||||||
// Add packet extensions
|
// Add packet extensions
|
||||||
response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
|
response.addExtensions(nodeInformationProvider.getNodePacketExtensions());
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// Return <item-not-found/> error since specified node was not found
|
// Return <item-not-found/> error since specified node was not found
|
||||||
response.setType(IQ.Type.error);
|
response.setType(IQ.Type.error);
|
||||||
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
|
response.setError(new XMPPError(XMPPError.Condition.item_not_found));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
connection.sendPacket(response);
|
connection().sendPacket(response);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
connection.addPacketListener(packetListener, packetFilter);
|
connection.addPacketListener(packetListener, GET_DISCOVER_INFO);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue