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