📄 routingtableimpl.java
字号:
return sessions;
}
public OutgoingServerSession getServerRoute(JID jid) {
// Check if this session is hosted by this cluster node
OutgoingServerSession session = (OutgoingServerSession) localRoutingTable.getRoute(jid.getDomain());
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(jid.getDomain());
if (nodeID != null) {
session = locator.getOutgoingServerSession(nodeID, jid);
}
}
}
return session;
}
public Collection<String> getServerHostnames() {
return serversCache.keySet();
}
public int getServerSessionsCount() {
return localRoutingTable.getServerRoutes().size();
}
public Collection<String> getComponentsDomains() {
return componentsCache.keySet();
}
public boolean hasClientRoute(JID jid) {
return usersCache.containsKey(jid.toString()) || isAnonymousRoute(jid);
}
public boolean isAnonymousRoute(JID jid) {
return anonymousUsersCache.containsKey(jid.toString());
}
public boolean isLocalRoute(JID jid) {
return localRoutingTable.isLocalRoute(jid);
}
public boolean hasServerRoute(JID jid) {
return serversCache.containsKey(jid.getDomain());
}
public boolean hasComponentRoute(JID jid) {
return componentsCache.containsKey(jid.getDomain());
}
public List<JID> getRoutes(JID route, JID requester) {
List<JID> jids = new ArrayList<JID>();
if (serverName.equals(route.getDomain())) {
// Address belongs to local user
if (route.getResource() != null) {
// Address is a full JID of a user
ClientRoute clientRoute = usersCache.get(route.toString());
if (clientRoute == null) {
clientRoute = anonymousUsersCache.get(route.toString());
}
if (clientRoute != null &&
(clientRoute.isAvailable() || presenceUpdateHandler.hasDirectPresence(route, requester))) {
jids.add(route);
}
}
else {
// Address is a bare JID so return all AVAILABLE resources of user
Collection<String> sessions = usersSessions.get(route.toBareJID());
if (sessions != null) {
// Select only available sessions
for (String jid : sessions) {
ClientRoute clientRoute = usersCache.get(jid);
if (clientRoute == null) {
clientRoute = anonymousUsersCache.get(jid);
}
if (clientRoute != null && (clientRoute.isAvailable() ||
presenceUpdateHandler.hasDirectPresence(new JID(jid), requester))) {
jids.add(new JID(jid));
}
}
}
}
}
else if (route.getDomain().contains(serverName)) {
// Packet sent to component hosted in this server
if (componentsCache.containsKey(route.getDomain())) {
jids.add(new JID(route.getDomain()));
}
}
else {
// Packet sent to remote server
jids.add(route);
}
return jids;
}
public boolean removeClientRoute(JID route) {
boolean anonymous = false;
String address = route.toString();
ClientRoute clientRoute = null;
Lock lockU = CacheFactory.getLock(address, usersCache);
try {
lockU.lock();
clientRoute = usersCache.remove(address);
}
finally {
lockU.unlock();
}
if (clientRoute == null) {
Lock lockA = CacheFactory.getLock(address, anonymousUsersCache);
try {
lockA.lock();
clientRoute = anonymousUsersCache.remove(address);
anonymous = true;
}
finally {
lockA.unlock();
}
}
if (clientRoute != null && route.getResource() != null) {
Lock lock = CacheFactory.getLock(route.toBareJID(), usersSessions);
try {
lock.lock();
if (anonymous) {
usersSessions.remove(route.toBareJID());
}
else {
Collection<String> jids = usersSessions.get(route.toBareJID());
if (jids != null) {
jids.remove(route.toString());
if (!jids.isEmpty()) {
usersSessions.put(route.toBareJID(), jids);
}
else {
usersSessions.remove(route.toBareJID());
}
}
}
}
finally {
lock.unlock();
}
}
localRoutingTable.removeRoute(address);
return clientRoute != null;
}
public boolean removeServerRoute(JID route) {
String address = route.getDomain();
boolean removed = false;
Lock lock = CacheFactory.getLock(address, serversCache);
try {
lock.lock();
removed = serversCache.remove(address) != null;
}
finally {
lock.unlock();
}
localRoutingTable.removeRoute(address);
return removed;
}
public boolean removeComponentRoute(JID route) {
String address = route.getDomain();
boolean removed = false;
Lock lock = CacheFactory.getLock(address, componentsCache);
try {
lock.lock();
Set<NodeID> nodes = componentsCache.get(address);
if (nodes != null) {
removed = nodes.remove(server.getNodeID());
if (nodes.isEmpty()) {
componentsCache.remove(address);
}
else {
componentsCache.put(address, nodes);
}
}
} finally {
lock.unlock();
}
localRoutingTable.removeRoute(address);
return removed;
}
public void setRemotePacketRouter(RemotePacketRouter remotePacketRouter) {
this.remotePacketRouter = remotePacketRouter;
}
public RemotePacketRouter getRemotePacketRouter() {
return remotePacketRouter;
}
public void initialize(XMPPServer server) {
super.initialize(server);
this.server = server;
serverName = server.getServerInfo().getXMPPDomain();
iqRouter = server.getIQRouter();
messageRouter = server.getMessageRouter();
presenceRouter = server.getPresenceRouter();
presenceUpdateHandler = server.getPresenceUpdateHandler();
// Listen to cluster events
ClusterManager.addListener(this);
}
public void start() throws IllegalStateException {
super.start();
localRoutingTable.start();
}
public void stop() {
super.stop();
localRoutingTable.stop();
}
public void joinedCluster() {
restoreCacheContent();
// Broadcast presence of local sessions to remote sessions when subscribed to presence
// Probe presences of remote sessions when subscribed to presence of local session
// Send pending subscription requests to local sessions from remote sessions
// Deliver offline messages sent to local sessions that were unavailable in other nodes
// Send available presences of local sessions to other resources of the same user
PresenceUpdateHandler presenceUpdateHandler = XMPPServer.getInstance().getPresenceUpdateHandler();
for (LocalClientSession session : localRoutingTable.getClientRoutes()) {
// Simulate that the local session has just became available
session.setInitialized(false);
// Simulate that current session presence has just been received
presenceUpdateHandler.process(session.getPresence());
}
}
public void joinedCluster(byte[] nodeID) {
// Do nothing
}
public void leftCluster() {
if (!XMPPServer.getInstance().isShuttingDown()) {
// Add local sessions to caches
restoreCacheContent();
}
}
public void leftCluster(byte[] nodeID) {
// Do nothing
}
public void markedAsSeniorClusterMember() {
// Do nothing
}
private void restoreCacheContent() {
// Add outgoing server sessions hosted locally to the cache (using new nodeID)
for (LocalOutgoingServerSession session : localRoutingTable.getServerRoutes()) {
addServerRoute(session.getAddress(), 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);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -