RoutingTableImpl.java 10.2 KB
Newer Older
Matt Tucker's avatar
Matt Tucker committed
1 2 3 4 5
/**
 * $RCSfile$
 * $Revision$
 * $Date$
 *
Matt Tucker's avatar
Matt Tucker committed
6
 * Copyright (C) 2004 Jive Software. All rights reserved.
Matt Tucker's avatar
Matt Tucker committed
7
 *
Matt Tucker's avatar
Matt Tucker committed
8 9
 * This software is published under the terms of the GNU Public License (GPL),
 * a copy of which is included in this distribution.
Matt Tucker's avatar
Matt Tucker committed
10
 */
Matt Tucker's avatar
Matt Tucker committed
11

Matt Tucker's avatar
Matt Tucker committed
12 13 14
package org.jivesoftware.messenger.spi;

import org.jivesoftware.messenger.*;
15
import org.jivesoftware.messenger.server.OutgoingServerSession;
16
import org.jivesoftware.messenger.container.BasicModule;
17
import org.jivesoftware.util.Log;
18
import org.xmpp.packet.JID;
Matt Tucker's avatar
Matt Tucker committed
19

20 21 22 23
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
Matt Tucker's avatar
Matt Tucker committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * <p>Uses simple hashtables for table storage.</p>
 * <p>Leaves in the tree are indicated by a PacketHandler, while branches are stored in hashtables.
 * Traverse the tree according to an XMPPAddress' fields (host -> name -> resource) and when you
 * hit a PacketHandler, you have found the handler for that node and all sub-nodes. </p>
 *
 * @author Iain Shigeoka
 */
public class RoutingTableImpl extends BasicModule implements RoutingTable {

    /**
     * We need a three level tree built of hashtables: host -> name -> resource
     */
    private Hashtable routes = new Hashtable();

    /**
     * locks access to the routing tale
     */
    private ReadWriteLock routeLock = new ReentrantReadWriteLock();
46
    private String serverName;
Matt Tucker's avatar
Matt Tucker committed
47 48 49 50 51

    public RoutingTableImpl() {
        super("Routing table");
    }

52
    public void addRoute(JID node, RoutableChannelHandler destination) {
Matt Tucker's avatar
Matt Tucker committed
53

54 55
        String nodeJID = node.getNode() == null ? "" : node.getNode();
        String resourceJID = node.getResource() == null ? "" : node.getResource();
Matt Tucker's avatar
Matt Tucker committed
56 57 58

        routeLock.writeLock().lock();
        try {
59
            if (destination instanceof ClientSession) {
60
                Object nameRoutes = routes.get(node.getDomain());
61
                if (nameRoutes == null) {
62 63
                    nameRoutes = new Hashtable();
                    routes.put(node.getDomain(), nameRoutes);
Matt Tucker's avatar
Matt Tucker committed
64
                }
65 66 67 68
                Object resourceRoutes = ((Hashtable)nameRoutes).get(nodeJID);
                if (resourceRoutes == null) {
                    resourceRoutes = new Hashtable();
                    ((Hashtable)nameRoutes).put(nodeJID, resourceRoutes);
Matt Tucker's avatar
Matt Tucker committed
69
                }
70 71 72 73
                ((Hashtable)resourceRoutes).put(resourceJID, destination);
            }
            else {
                routes.put(node.getDomain(), destination);
Matt Tucker's avatar
Matt Tucker committed
74 75 76 77 78 79 80
            }
        }
        finally {
            routeLock.writeLock().unlock();
        }
    }

81
    public RoutableChannelHandler getRoute(JID node) throws NoSuchRouteException {
Matt Tucker's avatar
Matt Tucker committed
82
        RoutableChannelHandler route = null;
83 84 85
        String nodeJID = node.getNode() == null ? "" : node.getNode();
        String resourceJID = node.getResource() == null ? "" : node.getResource();

86 87 88 89 90 91 92
        // Check if the address belongs to a remote server
        if (!node.getDomain().contains(serverName)) {
            // Authenticate this hostname with the remote server so that the remote server can
            // accept packets from this server.
            OutgoingServerSession.authenticateDomain(serverName, node.getDomain());
        }

Matt Tucker's avatar
Matt Tucker committed
93 94
        routeLock.readLock().lock();
        try {
95
            Object nameRoutes = routes.get(node.getDomain());
Matt Tucker's avatar
Matt Tucker committed
96 97 98 99
            if (nameRoutes instanceof ChannelHandler) {
                route = (RoutableChannelHandler)nameRoutes;
            }
            else {
100
                Object resourceRoutes = ((Hashtable)nameRoutes).get(nodeJID);
Matt Tucker's avatar
Matt Tucker committed
101 102 103 104
                if (resourceRoutes instanceof ChannelHandler) {
                    route = (RoutableChannelHandler)resourceRoutes;
                }
                else if (resourceRoutes != null) {
105
                    route = (RoutableChannelHandler) ((Hashtable)resourceRoutes).get(resourceJID);
Matt Tucker's avatar
Matt Tucker committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
                }
                else {
                    throw new NoSuchRouteException(node.toString());
                }
            }
        }
        catch (Exception e) {
            throw new NoSuchRouteException(node == null ? "No node" : node.toString(), e);
        }
        finally {
            routeLock.readLock().unlock();
        }

        if (route == null) {
            throw new NoSuchRouteException(node == null ? "No node" : node.toString());
        }
        return route;
    }

125
    public Iterator getRoutes(JID node) {
126 127 128 129 130 131 132
        // Check if the address belongs to a remote server
        if (!node.getDomain().contains(serverName)) {
            // Authenticate this hostname with the remote server so that the remote server can
            // accept packets from this server.
            OutgoingServerSession.authenticateDomain(serverName, node.getDomain());
        }

Matt Tucker's avatar
Matt Tucker committed
133 134 135
        LinkedList list = null;
        routeLock.readLock().lock();
        try {
136
            if (node == null || node.getDomain() == null) {
Matt Tucker's avatar
Matt Tucker committed
137 138 139 140
                list = new LinkedList();
                getRoutes(list, routes);
            }
            else {
141
                Object nameRoutes = routes.get(node.getDomain());
Matt Tucker's avatar
Matt Tucker committed
142 143 144 145 146
                if (nameRoutes != null) {
                    if (nameRoutes instanceof ChannelHandler) {
                        list = new LinkedList();
                        list.add(nameRoutes);
                    }
147
                    else if (node.getNode() == null) {
Matt Tucker's avatar
Matt Tucker committed
148 149 150 151 152
                        list = new LinkedList();
                        getRoutes(list, (Hashtable)nameRoutes);
                    }
                    else {
                        Object resourceRoutes =
153
                                ((Hashtable)nameRoutes).get(node.getNode());
Matt Tucker's avatar
Matt Tucker committed
154 155 156 157 158 159 160 161 162 163 164
                        if (resourceRoutes != null) {
                            if (resourceRoutes instanceof ChannelHandler) {
                                list = new LinkedList();
                                list.add(resourceRoutes);
                            }
                            else if (node.getResource() == null || node.getResource().length() == 0) {
                                list = new LinkedList();
                                getRoutes(list, (Hashtable)resourceRoutes);
                            }
                            else {
                                Object entry =
165
                                        ((Hashtable)resourceRoutes).get(node.getResource());
Matt Tucker's avatar
Matt Tucker committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
                                if (entry != null) {
                                    list = new LinkedList();
                                    list.add(entry);
                                }
                            }
                        }
                    }
                }
            }
        }
        finally {
            routeLock.readLock().unlock();
        }
        if (list == null) {
            return Collections.EMPTY_LIST.iterator();
        }
        else {
            return list.iterator();
        }
    }

    /**
     * <p>Recursive method to iterate through the given table (and any embedded tables)
     * and stuff non-Hashtable values into the given list.</p>
     * <p>There should be no recursion problems since
     * the routing table is at most 3 levels deep.</p>
     *
     * @param list  The list to stuff entries into
     * @param table The hashtable who's values should be entered into the list
     */
    private void getRoutes(LinkedList list, Hashtable table) {
        Iterator entryIter = table.values().iterator();
        while (entryIter.hasNext()) {
            Object entry = entryIter.next();
            if (entry instanceof Hashtable) {
                getRoutes(list, (Hashtable)entry);
            }
            else {
                // Do not include the same entry many times. This could be the case when the same 
                // session is associated with the bareJID and with a given resource
                if (!list.contains(entry)) {
                    list.add(entry);
                }
            }
        }
    }

213
    public ChannelHandler getBestRoute(JID node) throws NoSuchRouteException {
Matt Tucker's avatar
Matt Tucker committed
214 215 216 217 218 219

        ChannelHandler route = null;
        try {
            route = getRoute(node);
        }
        catch (NoSuchRouteException e) {
220
            JID defaultNode = new JID(node.getNode(), node.getDomain(), "");
Matt Tucker's avatar
Matt Tucker committed
221 222 223 224 225 226 227 228
            route = getRoute(defaultNode);
        }
        if (route == null) {
            throw new NoSuchRouteException();
        }
        return route;
    }

229
    public ChannelHandler removeRoute(JID node) {
Matt Tucker's avatar
Matt Tucker committed
230 231

        ChannelHandler route = null;
232 233
        String nodeJID = node.getNode() == null ? "" : node.getNode();
        String resourceJID = node.getResource() == null ? "" : node.getResource();
Matt Tucker's avatar
Matt Tucker committed
234 235 236

        routeLock.writeLock().lock();
        try {
237 238 239 240 241 242 243 244 245 246
            Object nameRoutes = routes.get(node.getDomain());
            if (nameRoutes instanceof Hashtable) {
                Object resourceRoutes = ((Hashtable)nameRoutes).get(nodeJID);
                if (resourceRoutes instanceof Hashtable) {
                    // Remove the requested resource for this user
                    route = (ChannelHandler) ((Hashtable)resourceRoutes).remove(resourceJID);
                    if (((Hashtable)resourceRoutes).isEmpty()) {
                        ((Hashtable)nameRoutes).remove(nodeJID);
                        if (((Hashtable)nameRoutes).isEmpty()) {
                            routes.remove(node.getDomain());
Matt Tucker's avatar
Matt Tucker committed
247 248 249 250

                        }
                    }
                }
251 252 253 254 255
                else {
                    // Remove the unique route to this node
                    ((Hashtable)nameRoutes).remove(nodeJID);
                }
            }
Gaston Dombiak's avatar
Gaston Dombiak committed
256
            else if (nameRoutes != null) {
257 258 259 260 261
                // The retrieved route points to a RoutableChannelHandler
                if (((RoutableChannelHandler)nameRoutes).getAddress().equals(node)) {
                    // Remove the route to this domain
                    routes.remove(node.getDomain());
                }
Matt Tucker's avatar
Matt Tucker committed
262 263 264
            }
        }
        catch (Exception e) {
265
            Log.error("Error removing route", e);
Matt Tucker's avatar
Matt Tucker committed
266 267 268 269 270 271
        }
        finally {
            routeLock.writeLock().unlock();
        }
        return route;
    }
272 273 274 275 276

    public void initialize(XMPPServer server) {
        super.initialize(server);
        serverName = server.getServerInfo().getName();
    }
Matt Tucker's avatar
Matt Tucker committed
277
}