Commit dcdfc697 authored by Derek DeMoro's avatar Derek DeMoro Committed by derek

Added Packet Delivery mechanism.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@450 b35dd754-fafc-0310-a699-88a17e54d16e
parent 10c3814a
package org.jivesoftware.messenger; package org.jivesoftware.messenger;
import java.util.Map; import org.jivesoftware.messenger.container.ServiceLookupFactory;
import java.util.concurrent.ConcurrentHashMap; import org.jivesoftware.messenger.spi.PacketDelivererImpl;
import org.jivesoftware.messenger.spi.PresenceImpl; import org.jivesoftware.messenger.spi.PresenceImpl;
import org.jivesoftware.util.Log;
import org.jivesoftware.util.StringUtils; import org.jivesoftware.util.StringUtils;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* <p>Manages the registration and delegation of Components.</p>
* <p/>
* <p>The ComponentManager is responsible for managing registration and delegation of <code>Components</code>,
* as well as offering a facade around basic server functionallity such as sending and receiving of
* packets.
*
* @author Derek DeMoro
*/
public class ComponentManager { public class ComponentManager {
private Map<String, Component> components = new ConcurrentHashMap<String, Component>(); private Map<String, Component> components = new ConcurrentHashMap<String, Component>();
private Map<XMPPAddress, XMPPAddress> presenceMap = new ConcurrentHashMap<XMPPAddress, XMPPAddress>(); private Map<XMPPAddress, XMPPAddress> presenceMap = new ConcurrentHashMap<XMPPAddress, XMPPAddress>();
...@@ -36,7 +50,14 @@ public class ComponentManager { ...@@ -36,7 +50,14 @@ public class ComponentManager {
private ComponentManager() { private ComponentManager() {
} }
public void addComponent(String jid, Component component){ /**
* Registers a <code>Component</code> with the server and maps
* to particular jid.
*
* @param jid the jid to map to.
* @param component the <code>Component</code> to register.
*/
public void addComponent(String jid, Component component) {
jid = validateJID(jid); jid = validateJID(jid);
components.put(jid, component); components.put(jid, component);
...@@ -44,18 +65,30 @@ public class ComponentManager { ...@@ -44,18 +65,30 @@ public class ComponentManager {
checkPresences(); checkPresences();
} }
public void removeComponent(String jid){ /**
* Removes a <code>Component</code> from the server.
*
* @param jid the jid mapped to the particular component.
*/
public void removeComponent(String jid) {
components.remove(validateJID(jid)); components.remove(validateJID(jid));
} }
public Component getComponent(String jid){ /**
if(components.containsKey(validateJID(jid))){ * Retrieves the <code>Component</code> which is mapped
* to the specified JID.
*
* @param jid the jid mapped to the component.
* @return
*/
public Component getComponent(String jid) {
if (components.containsKey(validateJID(jid))) {
return components.get(validateJID(jid)); return components.get(validateJID(jid));
} }
else { else {
String serverName = StringUtils.parseServer(validateJID(jid)); String serverName = StringUtils.parseServer(validateJID(jid));
int index = serverName.indexOf("."); int index = serverName.indexOf(".");
if(index != -1){ if (index != -1) {
String serviceName = serverName.substring(0, index); String serviceName = serverName.substring(0, index);
jid = serviceName; jid = serviceName;
} }
...@@ -63,21 +96,46 @@ public class ComponentManager { ...@@ -63,21 +96,46 @@ public class ComponentManager {
return components.get(validateJID(jid)); return components.get(validateJID(jid));
} }
private String validateJID(String jid){ /**
* Registers Probeers who have not yet been serviced.
* @param prober the jid probing.
* @param probee the presence being probed.
*/
public void addPresenceRequest(XMPPAddress prober, XMPPAddress probee) {
presenceMap.put(prober, probee);
}
/**
* Send a packet to the specified recipient. Please note that this sends packets only
* to outgoing jids and does to the incoming server reader.
* @param packet the packet to send.
*/
public void sendPacket(XMPPPacket packet) {
try {
PacketDelivererImpl deliverer = (PacketDelivererImpl) ServiceLookupFactory.getLookup().lookup(PacketDelivererImpl.class);
if (deliverer != null) {
// Hand off to the delegated deliverer
deliverer.deliver(packet);
}
}
catch (Exception e) {
Log.error("Unable to deliver packet", e);
}
}
private String validateJID(String jid) {
jid = jid.trim().toLowerCase(); jid = jid.trim().toLowerCase();
return jid; return jid;
} }
public void addPresenceRequest(XMPPAddress prober, XMPPAddress probee){
presenceMap.put(prober, probee);
}
private void checkPresences(){ private void checkPresences() {
for(XMPPAddress prober : presenceMap.keySet()){ for (XMPPAddress prober : presenceMap.keySet()) {
XMPPAddress probee = presenceMap.get(prober); XMPPAddress probee = presenceMap.get(prober);
Component component = getComponent(probee.toBareStringPrep()); Component component = getComponent(probee.toBareStringPrep());
if(component != null){ if (component != null) {
Presence presence = new PresenceImpl(); Presence presence = new PresenceImpl();
presence.setSender(prober); presence.setSender(prober);
presence.setRecipient(probee); presence.setRecipient(probee);
...@@ -88,4 +146,5 @@ public class ComponentManager { ...@@ -88,4 +146,5 @@ public class ComponentManager {
} }
} }
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment