Commit 860d9048 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gato

Refactoring work.

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@3985 b35dd754-fafc-0310-a699-88a17e54d16e
parent 0bec3a0d
/** /**
* $RCSfile$ * $RCSfile$
* $Revision: 943 $ * $Revision: $
* $Date: 2005-02-04 01:53:20 -0300 (Fri, 04 Feb 2005) $ * $Date: $
* *
* Copyright (C) 2004 Jive Software. All rights reserved. * Copyright (C) 2006 Jive Software. All rights reserved.
* *
* This software is published under the terms of the GNU Public License (GPL), * This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution. * a copy of which is included in this distribution.
*/ */
package org.jivesoftware.wildfire; package org.jivesoftware.wildfire;
import org.xmpp.packet.Packet; import org.xmpp.packet.IQ;
import org.xmpp.packet.Message; import org.xmpp.packet.Message;
import org.xmpp.packet.Presence; import org.xmpp.packet.Packet;
import org.xmpp.packet.IQ; import org.xmpp.packet.Presence;
import org.jivesoftware.wildfire.container.BasicModule;
/**
/** * A router that handles incoming packets. Packets will be routed to their
* <p>An uber router that can handle any packet type.</p> * corresponding handler. A router is much like a forwarded with some logic
* <p>The interface is provided primarily as a convenience for services * to figute out who is the target for each packet.
* that must route all packet types (e.g. s2s routing, e2e encryption, etc).</p> *
* * @author Gaston Dombiak
* @author Iain Shigeoka */
*/ public interface PacketRouter {
public class PacketRouter extends BasicModule {
/**
private IQRouter iqRouter; * Routes the given packet based on its type.
private PresenceRouter presenceRouter; *
private MessageRouter messageRouter; * @param packet The packet to route.
*/
/** void route(Packet packet);
* Constructs a packet router.
*/ /**
public PacketRouter() { * Routes the given IQ packet.
super("XMPP Packet Router"); *
} * @param packet The packet to route.
*/
/** void route(IQ packet);
* Routes the given packet based on packet recipient and sender. The
* router defers actual routing decisions to other classes. /**
* <h2>Warning</h2> * Routes the given Message packet.
* Be careful to enforce concurrency DbC of concurrent by synchronizing *
* any accesses to class resources. * @param packet The packet to route.
* */
* @param packet The packet to route void route(Message packet);
*/
public void route(Packet packet) { /**
if (packet instanceof Message) { * Routes the given Presence packet.
route((Message)packet); *
} * @param packet The packet to route.
else if (packet instanceof Presence) { */
route((Presence)packet); void route(Presence packet);
} }
else if (packet instanceof IQ) {
route((IQ)packet);
}
else {
throw new IllegalArgumentException();
}
}
public void route(IQ packet) {
iqRouter.route(packet);
}
public void route(Message packet) {
messageRouter.route(packet);
}
public void route(Presence packet) {
presenceRouter.route(packet);
}
public void initialize(XMPPServer server) {
super.initialize(server);
iqRouter = server.getIQRouter();
messageRouter = server.getMessageRouter();
presenceRouter = server.getPresenceRouter();
}
}
\ No newline at end of file
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.multiplex;
import org.dom4j.Element;
import org.xmpp.packet.Packet;
import java.util.Iterator;
import java.util.List;
/**
* Route packets identify target sessions by their stream ID and contain a single
* wrapped stanza that should be processed by the target session.
*
* @author Gaston Dombiak
*/
public class Route extends Packet {
/**
* Constructs a new Route.
*
* @param streamID the stream ID that identifies the connection that is actually sending
* the wrapped stanza.
*/
public Route(String streamID) {
this.element = docFactory.createDocument().addElement("route");
// Set the stream ID that identifies the target session
element.addAttribute("streamid", streamID);
}
/**
* Constructs a new Route using an existing Element. This is useful
* for parsing incoming route Elements into Route objects.
*
* @param element the route Element.
*/
public Route(Element element) {
super(element);
}
public Route(Route route) {
Element elementCopy = route.element.createCopy();
docFactory.createDocument().add(elementCopy);
this.element = elementCopy;
// Copy cached JIDs (for performance reasons)
this.toJID = route.toJID;
this.fromJID = route.fromJID;
}
/**
* Returns the wrapped stanza that is being routed. Route packets must have
* a single wrapped stanza. This is a convenience method to avoid manipulating
* the underlying packet's Element instance directly.
*
* @return the wrapped stanza.
*/
public Element getChildElement() {
List elements = element.elements();
if (elements.isEmpty()) {
return null;
}
else {
// Return the first child element
return (Element) elements.get(0);
}
}
/**
* Sets the wrapped stanza by this Route packet. Route packets may have a single child
* element. This is a convenience method to avoid manipulating this underlying packet's
* Element instance directly.
*
* @param childElement the child element.
*/
public void setChildElement(Element childElement) {
for (Iterator i=element.elementIterator(); i.hasNext(); ) {
element.remove((Element)i.next());
}
element.add(childElement);
}
/**
* Return the stream ID that identifies the connection that is actually sending
* the wrapped stanza.
*
* @return the stream ID that identifies the connection that is actually sending
* the wrapped stanza.
*/
public String getStreamID() {
return element.attributeValue("streamid");
}
/**
* Returns a deep copy of this route packet.
*
* @return a deep copy of this route packet.
*/
public Route createCopy() {
return new Route(this);
}
}
/**
* $RCSfile$
* $Revision: 943 $
* $Date: 2005-02-04 01:53:20 -0300 (Fri, 04 Feb 2005) $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
*/
package org.jivesoftware.wildfire.spi;
import org.jivesoftware.wildfire.*;
import org.jivesoftware.wildfire.container.BasicModule;
import org.xmpp.packet.IQ;
import org.xmpp.packet.Message;
import org.xmpp.packet.Packet;
import org.xmpp.packet.Presence;
/**
* An uber router that can handle any packet type.<p>
*
* The interface is provided primarily as a convenience for services
* that must route all packet types (e.g. s2s routing, e2e encryption, etc).
*
* @author Iain Shigeoka
*/
public class PacketRouterImpl extends BasicModule implements PacketRouter {
private IQRouter iqRouter;
private PresenceRouter presenceRouter;
private MessageRouter messageRouter;
/**
* Constructs a packet router.
*/
public PacketRouterImpl() {
super("XMPP Packet Router");
}
/**
* Routes the given packet based on packet recipient and sender. The
* router defers actual routing decisions to other classes.
* <h2>Warning</h2>
* Be careful to enforce concurrency DbC of concurrent by synchronizing
* any accesses to class resources.
*
* @param packet The packet to route
*/
public void route(Packet packet) {
if (packet instanceof Message) {
route((Message)packet);
}
else if (packet instanceof Presence) {
route((Presence)packet);
}
else if (packet instanceof IQ) {
route((IQ)packet);
}
else {
throw new IllegalArgumentException();
}
}
public void route(IQ packet) {
iqRouter.route(packet);
}
public void route(Message packet) {
messageRouter.route(packet);
}
public void route(Presence packet) {
presenceRouter.route(packet);
}
public void initialize(XMPPServer server) {
super.initialize(server);
iqRouter = server.getIQRouter();
messageRouter = server.getMessageRouter();
presenceRouter = server.getPresenceRouter();
}
}
\ No newline at end of file
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