public RoutingTableImpl() { super("Routing table"); serversCache = CacheFactory.createCache(S2S_CACHE_NAME); componentsCache = CacheFactory.createCache(COMPONENT_CACHE_NAME); usersCache = CacheFactory.createCache(C2S_CACHE_NAME); anonymousUsersCache = CacheFactory.createCache(ANONYMOUS_C2S_CACHE_NAME); usersSessions = CacheFactory.createCache(C2S_SESSION_NAME); localRoutingTable = new LocalRoutingTable(); }
@Override public void addServerRoute(DomainPair address, LocalOutgoingServerSession destination) { localRoutingTable.addRoute(address, destination); Lock lock = CacheFactory.getLock(address, serversCache); try { lock.lock(); serversCache.put(address, server.getNodeID().toByteArray()); } finally { lock.unlock(); } }
private void restoreCacheContent() { // Add outgoing server sessions hosted locally to the cache (using new nodeID) for (LocalOutgoingServerSession session : localRoutingTable.getServerRoutes()) { for (DomainPair pair : session.getOutgoingDomainPairs()) { addServerRoute(pair, session); } } // Add component sessions hosted locally to the cache (using new nodeID) and remove traces to old nodeID for (RoutableChannelHandler route : localRoutingTable.getComponentRoute()) { addComponentRoute(route.getAddress(), route); } // Add client sessions hosted locally to the cache (using new nodeID) for (LocalClientSession session : localRoutingTable.getClientRoutes()) { addClientRoute(session.getAddress(), session); } }
@Override public OutgoingServerSession getServerRoute(DomainPair jids) { // Check if this session is hosted by this cluster node OutgoingServerSession session = (OutgoingServerSession) localRoutingTable.getRoute(jids); if (session == null) { // The session is not in this JVM so assume remote RemoteSessionLocator locator = server.getRemoteSessionLocator(); if (locator != null) { // Check if the session is hosted by other cluster node byte[] nodeID = serversCache.get(jids); if (nodeID != null) { session = locator.getOutgoingServerSession(nodeID, jids); } } } return session; }
routed = false; } else { if (localRoutingTable.isLocalRoute(jid)) { if (packet instanceof Message) { Message message = (Message) packet; localRoutingTable.getRoute(route).process(carbon); } catch (UnauthorizedException e) { Log.error("Unable to route packet " + packet.toXML(), e); localRoutingTable.getRoute(jid).process(packet); routed = true; } catch (UnauthorizedException e) {
@Override public void broadcastPacket(Message packet, boolean onlyLocal) { // Send the message to client sessions connected to this JVM for(ClientSession session : localRoutingTable.getClientRoutes()) { session.process(packet); } // Check if we need to broadcast the message to client sessions connected to remote cluter nodes if (!onlyLocal && remotePacketRouter != null) { remotePacketRouter.broadcastPacket(packet); } }
@Override public boolean removeServerRoute(DomainPair route) { String address = route.toString(); boolean removed = false; Lock lock = CacheFactory.getLock(route, serversCache); try { lock.lock(); removed = serversCache.remove(route) != null; } finally { lock.unlock(); } localRoutingTable.removeRoute(route); return removed; }
@Override public void start() throws IllegalStateException { super.start(); localRoutingTable.start(); }
@Override public void stop() { super.stop(); localRoutingTable.stop(); }
@Override public int getServerSessionsCount() { return localRoutingTable.getServerRoutes().size(); }
@Override public boolean isLocalRoute(JID jid) { return localRoutingTable.isLocalRoute(jid); }
@Override public ClientSession getClientRoute(JID jid) { // Check if this session is hosted by this cluster node ClientSession session = (ClientSession) localRoutingTable.getRoute(jid); if (session == null) { // The session is not in this JVM so assume remote RemoteSessionLocator locator = server.getRemoteSessionLocator(); if (locator != null) { // Check if the session is hosted by other cluster node ClientRoute route = usersCache.get(jid.toString()); if (route == null) { route = anonymousUsersCache.get(jid.toString()); } if (route != null) { session = locator.getClientSession(route.getNodeID().toByteArray(), jid); } } } return session; }
routed = false; } else { if (localRoutingTable.isLocalRoute(jid)) { if (packet instanceof Message) { Message message = (Message) packet; localRoutingTable.getRoute(route).process(carbon); } catch (UnauthorizedException e) { Log.error("Unable to route packet " + packet.toXML(), e); localRoutingTable.getRoute(jid).process(packet); routed = true; } catch (UnauthorizedException e) {
@Override public Collection<ClientSession> getClientsRoutes(boolean onlyLocal) { // Add sessions hosted by this cluster node Collection<ClientSession> sessions = new ArrayList<ClientSession>(localRoutingTable.getClientRoutes()); if (!onlyLocal) { // Add sessions not hosted by this JVM RemoteSessionLocator locator = server.getRemoteSessionLocator(); if (locator != null) { // Add sessions of non-anonymous users hosted by other cluster nodes for (Map.Entry<String, ClientRoute> entry : usersCache.entrySet()) { ClientRoute route = entry.getValue(); if (!server.getNodeID().equals(route.getNodeID())) { sessions.add(locator.getClientSession(route.getNodeID().toByteArray(), new JID(entry.getKey()))); } } // Add sessions of anonymous users hosted by other cluster nodes for (Map.Entry<String, ClientRoute> entry : anonymousUsersCache.entrySet()) { ClientRoute route = entry.getValue(); if (!server.getNodeID().equals(route.getNodeID())) { sessions.add(locator.getClientSession(route.getNodeID().toByteArray(), new JID(entry.getKey()))); } } } } return sessions; }
/** * Remove local or remote component route. * * @param route the route of the component to be removed. * @param nodeID The node to which the to-be-removed component was connected to. */ private boolean removeComponentRoute(JID route, NodeID nodeID) { String address = route.getDomain(); boolean removed = false; Lock lock = CacheFactory.getLock(address, componentsCache); try { lock.lock(); HashSet<NodeID> nodes = componentsCache.get(address); if (nodes != null) { removed = nodes.remove(nodeID); if (nodes.isEmpty()) { componentsCache.remove(address); } else { componentsCache.put(address, nodes); } } } finally { lock.unlock(); } localRoutingTable.removeRoute(new DomainPair("", address)); return removed; }
@Override public void start() throws IllegalStateException { super.start(); localRoutingTable.start(); }
@Override public void stop() { super.stop(); localRoutingTable.stop(); }
@Override public int getServerSessionsCount() { return localRoutingTable.getServerRoutes().size(); }
@Override public boolean isLocalRoute(JID jid) { return localRoutingTable.isLocalRoute(jid); }
private void restoreCacheContent() { // Add outgoing server sessions hosted locally to the cache (using new nodeID) for (LocalOutgoingServerSession session : localRoutingTable.getServerRoutes()) { for (DomainPair pair : session.getOutgoingDomainPairs()) { addServerRoute(pair, session); } } // Add component sessions hosted locally to the cache (using new nodeID) and remove traces to old nodeID for (RoutableChannelHandler route : localRoutingTable.getComponentRoute()) { addComponentRoute(route.getAddress(), route); } // Add client sessions hosted locally to the cache (using new nodeID) for (LocalClientSession session : localRoutingTable.getClientRoutes()) { addClientRoute(session.getAddress(), session); } }