KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > PacketRouter


1 /**
2  * $RCSfile: PacketRouter.java,v $
3  * $Revision: 1.8 $
4  * $Date: 2005/02/04 04:53:20 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger;
13
14 import org.xmpp.packet.Packet;
15 import org.xmpp.packet.Message;
16 import org.xmpp.packet.Presence;
17 import org.xmpp.packet.IQ;
18 import org.jivesoftware.messenger.container.BasicModule;
19
20 /**
21  * <p>An uber router that can handle any packet type.</p>
22  * <p>The interface is provided primarily as a convenience for services
23  * that must route all packet types (e.g. s2s routing, e2e encryption, etc).</p>
24  *
25  * @author Iain Shigeoka
26  */

27 public class PacketRouter extends BasicModule {
28
29     private IQRouter iqRouter;
30     private PresenceRouter presenceRouter;
31     private MessageRouter messageRouter;
32
33     /**
34      * Constructs a packet router.
35      */

36     public PacketRouter() {
37         super("XMPP Packet Router");
38     }
39
40     /**
41      * Routes the given packet based on packet recipient and sender. The
42      * router defers actual routing decisions to other classes.
43      * <h2>Warning</h2>
44      * Be careful to enforce concurrency DbC of concurrent by synchronizing
45      * any accesses to class resources.
46      *
47      * @param packet The packet to route
48      */

49     public void route(Packet packet) {
50         if (packet instanceof Message) {
51             route((Message)packet);
52         }
53         else if (packet instanceof Presence) {
54             route((Presence)packet);
55         }
56         else if (packet instanceof IQ) {
57             route((IQ)packet);
58         }
59         else {
60             throw new IllegalArgumentException JavaDoc();
61         }
62     }
63
64     public void route(IQ packet) {
65         iqRouter.route(packet);
66     }
67
68     public void route(Message packet) {
69         messageRouter.route(packet);
70     }
71
72     public void route(Presence packet) {
73         presenceRouter.route(packet);
74     }
75
76     public void initialize(XMPPServer server) {
77         super.initialize(server);
78         iqRouter = server.getIQRouter();
79         messageRouter = server.getMessageRouter();
80         presenceRouter = server.getPresenceRouter();
81     }
82 }
Popular Tags