Commit 4908903b authored by guus's avatar guus

Now uses AbstractComponent, other small improvements. OF-379

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@11781 b35dd754-fafc-0310-a699-88a17e54d16e
parent 8852c1f1
...@@ -20,115 +20,82 @@ ...@@ -20,115 +20,82 @@
package org.jinglenodes; package org.jinglenodes;
import org.dom4j.Element; import org.dom4j.Element;
import org.jivesoftware.openfire.XMPPServer;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.xmpp.component.Component; import org.xmpp.component.AbstractComponent;
import org.xmpp.component.ComponentException;
import org.xmpp.component.ComponentManager;
import org.xmpp.jnodes.RelayChannel; import org.xmpp.jnodes.RelayChannel;
import org.xmpp.jnodes.smack.JingleChannelIQ;
import org.xmpp.jnodes.nio.LocalIPResolver; import org.xmpp.jnodes.nio.LocalIPResolver;
import org.xmpp.jnodes.smack.JingleChannelIQ;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
import org.xmpp.packet.PacketError; import org.xmpp.packet.PacketError;
class JingleNodesComponent implements Component { class JingleNodesComponent extends AbstractComponent {
private static final Logger Log = LoggerFactory.getLogger(JingleNodesComponent.class); private static final Logger Log = LoggerFactory.getLogger(JingleNodesComponent.class);
private final ComponentManager componentManager; private static final String UDP = "udp";
private static final String UDP = "udp"; private static final String PROTOCOL = "protocol";
private static final String PROTOCOL = "protocol"; private static final String HOST = "host";
private static final String HOST = "host"; private static final String LOCAL_PORT = "localport";
private static final String LOCAL_PORT = "localport"; private static final String REMOTE_PORT = "remoteport";
private static final String REMOTE_PORT = "remoteport";
private final JingleNodesPlugin plugin;
private final JingleNodesPlugin plugin;
public JingleNodesComponent(final JingleNodesPlugin plugin) {
public JingleNodesComponent(final ComponentManager componentManager, final JingleNodesPlugin plugin) { this.plugin = plugin;
this.componentManager = componentManager; }
this.plugin = plugin;
} public String getName() {
return "JingleRelayNode";
public String getName() { }
return "JingleRelayNode";
} public String getDescription() {
return "Jingle Relay Service";
public String getDescription() { }
return "Jingle Relay Service";
} @Override
protected String[] discoInfoFeatureNamespaces() {
public void processPacket(Packet packet) { return new String[] { JingleChannelIQ.NAMESPACE };
if (Log.isDebugEnabled()) { }
Log.debug("Processing packet: {}", packet.toXML());
} @Override
if (packet instanceof IQ) { protected String discoInfoIdentityCategoryType() {
// Handle disco packets return "relay";
IQ iq = (IQ) packet; }
// Ignore IQs of type ERROR or RESULT
if (IQ.Type.error == iq.getType() || IQ.Type.result == iq.getType()) { @Override
return; protected IQ handleIQGet(IQ iq) throws Exception {
} final IQ reply = IQ.createResultIQ(iq);
processIQ(iq);
} final Element element = iq.getChildElement();
} final String namespace = element.getNamespaceURI();
private void processIQ(final IQ iq) { if (JingleChannelIQ.NAME.equals(element.getName()) && JingleChannelIQ.NAMESPACE.equals(namespace)
final IQ reply = IQ.createResultIQ(iq); && UDP.equals(element.attributeValue(PROTOCOL))) {
final Element element = iq.getChildElement(); final Element childElement = iq.getChildElement().createCopy();
final RelayChannel channel = plugin.createRelayChannel();
if (element != null) {
final String namespace = element.getNamespaceURI(); if (channel != null) {
childElement.addAttribute(HOST, LocalIPResolver.getLocalIP());
if ("http://jabber.org/protocol/disco#info".equals(namespace)) { childElement.addAttribute(LOCAL_PORT, Integer.toString(channel.getPortA()));
if (iq.getTo().getNode() == null) { childElement.addAttribute(REMOTE_PORT, Integer.toString(channel.getPortB()));
// Return service identity and features reply.setChildElement(childElement);
Element identity = element.addElement("identity");
identity.addAttribute("category", "component"); Log.debug("Created relay channel {}:{}, {}:{}, {}:{}", new Object[] { HOST,
identity.addAttribute("type", "relay"); LocalIPResolver.getLocalIP(), LOCAL_PORT, Integer.toString(channel.getPortA()), REMOTE_PORT,
identity.addAttribute("name", getName()); Integer.toString(channel.getPortB()) });
element.addElement("feature").addAttribute("var", "http://jabber.org/protocol/disco#info");
element.addElement("feature").addAttribute("var", JingleChannelIQ.NAMESPACE); } else {
} reply.setError(PacketError.Condition.internal_server_error);
} else if (JingleChannelIQ.NAME.equals(element.getName()) && JingleChannelIQ.NAMESPACE.equals(namespace) && UDP.equals(element.attributeValue(PROTOCOL))) { }
final Element childElement = iq.getChildElement().createCopy(); return reply;
final RelayChannel channel = plugin.createRelayChannel(); }
if (channel != null) { return null; // feature not implemented.
childElement.addAttribute(HOST, LocalIPResolver.getLocalIP()); }
childElement.addAttribute(LOCAL_PORT, Integer.toString(channel.getPortA()));
childElement.addAttribute(REMOTE_PORT, Integer.toString(channel.getPortB())); @Override
reply.setChildElement(childElement); public String getDomain() {
} else { return XMPPServer.getInstance().getServerInfo().getXMPPDomain();
reply.setError(PacketError.Condition.internal_server_error); }
}
} else {
reply.setError(PacketError.Condition.feature_not_implemented);
}
} else {
reply.setError(PacketError.Condition.feature_not_implemented);
}
try {
componentManager.sendPacket(this, reply);
if (Log.isDebugEnabled()) {
Log.debug("Packet sent: {}", reply.toXML());
}
}
catch (Exception e) {
Log.error(e.getMessage(), e);
}
}
public void initialize(final JID jid, final ComponentManager componentManager) throws ComponentException {
}
public void start() {
}
public void shutdown() {
}
} }
...@@ -19,6 +19,13 @@ ...@@ -19,6 +19,13 @@
package org.jinglenodes; package org.jinglenodes;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.jivesoftware.openfire.container.Plugin; import org.jivesoftware.openfire.container.Plugin;
import org.jivesoftware.openfire.container.PluginManager; import org.jivesoftware.openfire.container.PluginManager;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -28,20 +35,11 @@ import org.xmpp.component.ComponentManager; ...@@ -28,20 +35,11 @@ import org.xmpp.component.ComponentManager;
import org.xmpp.component.ComponentManagerFactory; import org.xmpp.component.ComponentManagerFactory;
import org.xmpp.jnodes.RelayChannel; import org.xmpp.jnodes.RelayChannel;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class JingleNodesPlugin implements Plugin { public class JingleNodesPlugin implements Plugin {
private static final Logger Log = LoggerFactory.getLogger(JingleNodesPlugin.class); private static final Logger Log = LoggerFactory.getLogger(JingleNodesPlugin.class);
private ComponentManager componentManager; private ComponentManager componentManager;
private JingleNodesComponent component;
private final ConcurrentHashMap<String, RelayChannel> channels = new ConcurrentHashMap<String, RelayChannel>(); private final ConcurrentHashMap<String, RelayChannel> channels = new ConcurrentHashMap<String, RelayChannel>();
private final long timeout = 60000; private final long timeout = 60000;
private final AtomicInteger ids = new AtomicInteger(0); private final AtomicInteger ids = new AtomicInteger(0);
...@@ -51,7 +49,7 @@ public class JingleNodesPlugin implements Plugin { ...@@ -51,7 +49,7 @@ public class JingleNodesPlugin implements Plugin {
public void initializePlugin(PluginManager manager, File pluginDirectory) { public void initializePlugin(PluginManager manager, File pluginDirectory) {
componentManager = ComponentManagerFactory.getComponentManager(); componentManager = ComponentManagerFactory.getComponentManager();
component = new JingleNodesComponent(componentManager, this); JingleNodesComponent component = new JingleNodesComponent(this);
try { try {
componentManager.addComponent(serviceName, component); componentManager.addComponent(serviceName, component);
} catch (ComponentException e) { } catch (ComponentException e) {
...@@ -105,7 +103,6 @@ public class JingleNodesPlugin implements Plugin { ...@@ -105,7 +103,6 @@ public class JingleNodesPlugin implements Plugin {
} }
public void destroyPlugin() { public void destroyPlugin() {
component.shutdown();
try { try {
componentManager.removeComponent(serviceName); componentManager.removeComponent(serviceName);
} catch (ComponentException e) { } catch (ComponentException e) {
......
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