Commit f2600087 authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Fixed memory leak when using anonymous users.


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@765 b35dd754-fafc-0310-a699-88a17e54d16e
parent cd70e1fa
...@@ -21,7 +21,6 @@ import java.util.Iterator; ...@@ -21,7 +21,6 @@ import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock;
...@@ -58,7 +57,6 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -58,7 +57,6 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
private JID serverAddress; private JID serverAddress;
private UserManager userManager; private UserManager userManager;
private int conflictLimit; private int conflictLimit;
private Random randomResource = new Random();
private Map<String, Session> preAuthenticatedSessions = new ConcurrentHashMap<String, Session>(); private Map<String, Session> preAuthenticatedSessions = new ConcurrentHashMap<String, Session>();
...@@ -296,9 +294,9 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -296,9 +294,9 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
*/ */
public boolean addSession(Session session) { public boolean addSession(Session session) {
boolean success = false; boolean success = false;
sessionLock.writeLock().lock();
String username = session.getAddress().getNode().toLowerCase(); String username = session.getAddress().getNode().toLowerCase();
SessionMap resources = null; SessionMap resources = null;
sessionLock.writeLock().lock();
try { try {
resources = sessions.get(username); resources = sessions.get(username);
if (resources == null) { if (resources == null) {
...@@ -324,7 +322,9 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -324,7 +322,9 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
if (success) { if (success) {
Session defaultSession = resources.getDefaultSession(); Session defaultSession = resources.getDefaultSession();
JID node = new JID(defaultSession.getAddress().getNode(), defaultSession.getAddress().getDomain(), null); JID node = new JID(defaultSession.getAddress().getNode(), defaultSession.getAddress().getDomain(), null);
// Add route to default session (used when no resource is specified)
routingTable.addRoute(node, defaultSession); routingTable.addRoute(node, defaultSession);
// Add route to the new session
routingTable.addRoute(session.getAddress(), session); routingTable.addRoute(session.getAddress(), session);
} }
return success; return success;
...@@ -841,8 +841,10 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -841,8 +841,10 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
router.route(offline); router.route(offline);
} }
if (session.getAddress() != null && routingTable != null && session.getAddress().toBareJID().trim().length() != 0) { if (session.getAddress() != null && routingTable != null && session.getAddress().toBareJID().trim().length() != 0) {
// Remove route to the removed session
routingTable.removeRoute(session.getAddress()); routingTable.removeRoute(session.getAddress());
if (sessionMap != null) { if (sessionMap != null) {
// If all the user sessions are gone then remove the route to the default session
if (sessionMap.isEmpty()) { if (sessionMap.isEmpty()) {
// Remove the route for the session's BARE address // Remove the route for the session's BARE address
routingTable.removeRoute(new JID(session.getAddress().getNode(), session.getAddress().getDomain(), "")); routingTable.removeRoute(new JID(session.getAddress().getNode(), session.getAddress().getDomain(), ""));
...@@ -859,17 +861,18 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen ...@@ -859,17 +861,18 @@ public class SessionManager extends BasicModule implements ConnectionCloseListen
public void addAnonymousSession(Session session) { public void addAnonymousSession(Session session) {
try { try {
JID newAddress = new JID(session.getAddress().getNode(), session.getAddress().getDomain(),
Integer.toHexString(randomResource.nextInt()));
session.setAddress(newAddress);
anonymousSessionLock.writeLock().lock(); anonymousSessionLock.writeLock().lock();
try { try {
anonymousSessions.put(session.getAddress().getResource(), session); anonymousSessions.put(session.getAddress().getResource(), session);
session.getConnection().registerCloseListener(this, session); session.getConnection().registerCloseListener(this, session);
// Remove the session from the pre-Authenticated sessions list
preAuthenticatedSessions.remove(session.getAddress().toString());
} }
finally { finally {
anonymousSessionLock.writeLock().unlock(); anonymousSessionLock.writeLock().unlock();
} }
// Anonymous session always have resources so we only need to add one route. That is
// the route to the anonymous session
routingTable.addRoute(session.getAddress(), session); routingTable.addRoute(session.getAddress(), session);
} }
catch (UnauthorizedException e) { catch (UnauthorizedException e) {
......
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