Commit 5dcee39c authored by Gaston Dombiak's avatar Gaston Dombiak Committed by gaston

Fixed concurrency problem. JM-276


git-svn-id: http://svn.igniterealtime.org/svn/repos/messenger/trunk@1307 b35dd754-fafc-0310-a699-88a17e54d16e
parent 4ebb97be
...@@ -135,9 +135,9 @@ public class SessionManager extends BasicModule { ...@@ -135,9 +135,9 @@ public class SessionManager extends BasicModule {
*/ */
void addSession(ClientSession session) { void addSession(ClientSession session) {
String resource = session.getAddress().getResource(); String resource = session.getAddress().getResource();
resources.put(resource, session);
Presence presence = session.getPresence(); Presence presence = session.getPresence();
int priority = presence == null ? 0 : presence.getPriority(); int priority = presence == null ? 0 : presence.getPriority();
resources.put(resource, session);
sortSession(resource, priority); sortSession(resource, priority);
} }
...@@ -148,12 +148,12 @@ public class SessionManager extends BasicModule { ...@@ -148,12 +148,12 @@ public class SessionManager extends BasicModule {
* @param priority The priority to use for sorting * @param priority The priority to use for sorting
*/ */
private void sortSession(String resource, int priority) { private void sortSession(String resource, int priority) {
synchronized (priorityList) {
if (priorityList.size() > 0) { if (priorityList.size() > 0) {
Iterator iter = priorityList.iterator(); Iterator iter = priorityList.iterator();
for (int i = 0; iter.hasNext(); i++) { for (int i = 0; iter.hasNext(); i++) {
ClientSession sess = resources.get(iter.next()); ClientSession sess = resources.get(iter.next());
if (sess.getPresence().getPriority() <= priority) { if (sess != null && sess.getPresence().getPriority() <= priority) {
priorityList.add(i, resource); priorityList.add(i, resource);
break; break;
} }
...@@ -163,6 +163,7 @@ public class SessionManager extends BasicModule { ...@@ -163,6 +163,7 @@ public class SessionManager extends BasicModule {
priorityList.addLast(resource); priorityList.addLast(resource);
} }
} }
}
/** /**
* Change the priority of a session associated with the sender. * Change the priority of a session associated with the sender.
...@@ -173,10 +174,12 @@ public class SessionManager extends BasicModule { ...@@ -173,10 +174,12 @@ public class SessionManager extends BasicModule {
public void changePriority(JID sender, int priority) { public void changePriority(JID sender, int priority) {
String resource = sender.getResource(); String resource = sender.getResource();
if (resources.containsKey(resource)) { if (resources.containsKey(resource)) {
synchronized (priorityList) {
priorityList.remove(resource); priorityList.remove(resource);
sortSession(resource, priority); sortSession(resource, priority);
} }
} }
}
/** /**
* Remove a session from the manager. * Remove a session from the manager.
...@@ -186,8 +189,10 @@ public class SessionManager extends BasicModule { ...@@ -186,8 +189,10 @@ public class SessionManager extends BasicModule {
void removeSession(Session session) { void removeSession(Session session) {
String resource = session.getAddress().getResource(); String resource = session.getAddress().getResource();
resources.remove(resource); resources.remove(resource);
synchronized (priorityList) {
priorityList.remove(resource); priorityList.remove(resource);
} }
}
/** /**
* Gets the session for the given resource. * Gets the session for the given resource.
...@@ -227,12 +232,14 @@ public class SessionManager extends BasicModule { ...@@ -227,12 +232,14 @@ public class SessionManager extends BasicModule {
return resources.get(priorityList.getFirst()); return resources.get(priorityList.getFirst());
} }
else { else {
synchronized (priorityList) {
for (int i=0; i < priorityList.size(); i++) { for (int i=0; i < priorityList.size(); i++) {
ClientSession s = resources.get(priorityList.get(i)); ClientSession s = resources.get(priorityList.get(i));
if (s.getPresence().isAvailable()) { if (s != null && s.getPresence().isAvailable()) {
return s; return s;
} }
} }
}
return null; return null;
} }
} }
......
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