mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-26 05:52:06 +01:00
Wait for server response before returning roster.
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@1867 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
d77787f67c
commit
af4ea681ee
4 changed files with 42 additions and 11 deletions
|
@ -68,6 +68,9 @@ public class Roster {
|
||||||
|
|
||||||
private XMPPConnection connection;
|
private XMPPConnection connection;
|
||||||
private Map groups;
|
private Map groups;
|
||||||
|
// The roster is marked as initialized when at least a single roster packet
|
||||||
|
// has been recieved and processed.
|
||||||
|
boolean rosterInitialized = false;
|
||||||
|
|
||||||
Roster(final XMPPConnection connection) {
|
Roster(final XMPPConnection connection) {
|
||||||
this.connection = connection;
|
this.connection = connection;
|
||||||
|
@ -163,8 +166,7 @@ public class Roster {
|
||||||
RosterPacket rosterPacket = (RosterPacket)packet;
|
RosterPacket rosterPacket = (RosterPacket)packet;
|
||||||
for (Iterator i=rosterPacket.getRosterItems(); i.hasNext(); ) {
|
for (Iterator i=rosterPacket.getRosterItems(); i.hasNext(); ) {
|
||||||
RosterPacket.Item item = (RosterPacket.Item)i.next();
|
RosterPacket.Item item = (RosterPacket.Item)i.next();
|
||||||
RosterEntry entry = new RosterEntry(item.getUser(), item.getName(),
|
RosterEntry entry = new RosterEntry(item.getUser(), item.getName(), connection);
|
||||||
connection);
|
|
||||||
// Find the list of groups that the user currently belongs to.
|
// Find the list of groups that the user currently belongs to.
|
||||||
List currentGroupNames = new ArrayList();
|
List currentGroupNames = new ArrayList();
|
||||||
for (Iterator j = entry.getGroups(); j.hasNext(); ) {
|
for (Iterator j = entry.getGroups(); j.hasNext(); ) {
|
||||||
|
@ -206,6 +208,9 @@ public class Roster {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Mark the roster as initialized.
|
||||||
|
rosterInitialized = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,11 +55,10 @@ public class RosterEntry {
|
||||||
* @return an iterator for the groups this entry belongs to.
|
* @return an iterator for the groups this entry belongs to.
|
||||||
*/
|
*/
|
||||||
public Iterator getGroups() {
|
public Iterator getGroups() {
|
||||||
Roster roster = connection.getRoster();
|
|
||||||
List results = new ArrayList();
|
List results = new ArrayList();
|
||||||
// Loop through all roster groups and find the ones that contain this
|
// Loop through all roster groups and find the ones that contain this
|
||||||
// entry. This algorithm should be fine
|
// entry. This algorithm should be fine
|
||||||
for (Iterator i=roster.getGroups(); i.hasNext(); ) {
|
for (Iterator i=connection.roster.getGroups(); i.hasNext(); ) {
|
||||||
RosterGroup group = (RosterGroup)i.next();
|
RosterGroup group = (RosterGroup)i.next();
|
||||||
if (group.contains(this)) {
|
if (group.contains(this)) {
|
||||||
results.add(group);
|
results.add(group);
|
||||||
|
|
|
@ -131,10 +131,10 @@ public class RosterGroup {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if an entry is part of the group.
|
* Returns true if an entry is part of this group.
|
||||||
*
|
*
|
||||||
* @param entry
|
* @param entry a roster entry.
|
||||||
* @return
|
* @return true if the entry is part of this group.
|
||||||
*/
|
*/
|
||||||
public boolean contains(RosterEntry entry) {
|
public boolean contains(RosterEntry entry) {
|
||||||
synchronized (entries) {
|
synchronized (entries) {
|
||||||
|
@ -142,6 +142,11 @@ public class RosterGroup {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a roster entry to this group.
|
||||||
|
*
|
||||||
|
* @param entry a roster entry.
|
||||||
|
*/
|
||||||
public void addEntry(RosterEntry entry) {
|
public void addEntry(RosterEntry entry) {
|
||||||
// Only add the entry if it isn't already in the list.
|
// Only add the entry if it isn't already in the list.
|
||||||
synchronized (entries) {
|
synchronized (entries) {
|
||||||
|
@ -155,6 +160,11 @@ public class RosterGroup {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a roster entry from this group.
|
||||||
|
*
|
||||||
|
* @param entry a roster entry.
|
||||||
|
*/
|
||||||
public void removeEntry(RosterEntry entry) {
|
public void removeEntry(RosterEntry entry) {
|
||||||
// Only remove the entry if it's in the entry list.
|
// Only remove the entry if it's in the entry list.
|
||||||
synchronized (entries) {
|
synchronized (entries) {
|
||||||
|
|
|
@ -53,8 +53,7 @@
|
||||||
package org.jivesoftware.smack;
|
package org.jivesoftware.smack;
|
||||||
|
|
||||||
import org.jivesoftware.smack.packet.*;
|
import org.jivesoftware.smack.packet.*;
|
||||||
import org.jivesoftware.smack.filter.PacketIDFilter;
|
import org.jivesoftware.smack.filter.*;
|
||||||
import org.jivesoftware.smack.filter.PacketFilter;
|
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
|
@ -109,7 +108,7 @@ public class XMPPConnection {
|
||||||
private PacketWriter packetWriter;
|
private PacketWriter packetWriter;
|
||||||
private PacketReader packetReader;
|
private PacketReader packetReader;
|
||||||
|
|
||||||
private Roster roster = null;
|
Roster roster = null;
|
||||||
private AccountManager accountManager = null;
|
private AccountManager accountManager = null;
|
||||||
|
|
||||||
Writer writer;
|
Writer writer;
|
||||||
|
@ -330,6 +329,24 @@ public class XMPPConnection {
|
||||||
* @return the user's roster, or <tt>null</tt> if the user has not logged in yet.
|
* @return the user's roster, or <tt>null</tt> if the user has not logged in yet.
|
||||||
*/
|
*/
|
||||||
public Roster getRoster() {
|
public Roster getRoster() {
|
||||||
|
if (roster == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// If this is the first time the user has asked for the roster after calling
|
||||||
|
// login, we want to wait up to 2 seconds for the server to send back the
|
||||||
|
// user's roster. This behavior shields API users from having to worry about the
|
||||||
|
// fact that roster operations are asynchronous, although they'll still have to
|
||||||
|
// listen for changes to the roster. Note: because of this waiting logic, internal
|
||||||
|
// Smack code should be wary about calling the getRoster method, and may need to
|
||||||
|
// access the roster object directly.
|
||||||
|
int elapsed = 0;
|
||||||
|
while (!roster.rosterInitialized && elapsed <= 2000) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(500);
|
||||||
|
}
|
||||||
|
catch (Exception e) { }
|
||||||
|
elapsed += 500;
|
||||||
|
}
|
||||||
return roster;
|
return roster;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue