Hey Phil,
you are getting slightly confused between the nature of someones Presence and there current online/offline status.
Below are four very simple classes that show a connection being made and a user logging in.
The users current Roster is then printed out to System.out. Each users presence is checked and depending on whether its null or not is then printed. The users current Presence mode is printed along side.
A very simple RosterListener is added to the Roster to listen out for presence changing events. When one occurs the roster is simply re-printed with the updated information.
1st off the Connection
public class TestXMPPConn {
private XMPPConnection conn;
private String host;
private int port;
private String username;
private String password;
public TestXMPPConn(String host, int port, String username, String password) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
}
public void connect() throws Exception {
try {
conn = new XMPPConnection(host, port);
}
catch (XMPPException e) {
throw new Exception(e.toString());
}
}
public void login() throws Exception{
if(conn.isConnected()) {
try {
conn.login(username, password);
}
catch (XMPPException e) {
throw new Exception(e.toString());
}
}
}
public XMPPConnection getConn() {
return this.conn;
}
}
/code
2nd the Roster
public class TestRoster {
private TestXMPPConn testConn;
private XMPPConnection conn;
private Roster roster;
public TestRoster() {}
public void setConnection(TestXMPPConn testConn) {
this.testConn = testConn;
conn = testConn.getConn();
roster = conn.getRoster();
roster.addRosterListener(new TestRosterListener(this));
}
public void displayRoster() {
Iterator rosterEntries = roster.getEntries();
while(rosterEntries.hasNext()) {
RosterEntry entry = (RosterEntry)rosterEntries.next();
Presence presence = roster.getPresence(entry.getUser());
if(presence != null) {
Presence.Mode mode = presence.getMode();
System.out.println(entry.getUser() + ": " + mode);
}
else {
System.out.println(entry.getUser() + “: OFFLINE”);
}
}
}
}
/code
3rd the RosterListenr
public class TestRosterListener implements RosterListener {
private TestRoster testRoster;
public TestRosterListener(TestRoster testRoster) {
this.testRoster = testRoster;
}
public void entriesAdded(Collection addresses) {}
public void entriesDeleted(Collection addresses) {}
public void entriesUpdated(Collection addresses) {}
public void presenceChanged(String user) {
testRoster.displayRoster();
}
}
/code
And finally a main to test it all out. The Thread.sleeps are just in there to allow the connection time to pull the rRoster from the server and then so the program doesnt just quit, but falls asleep allowing you time to test out the Roster updates.
public class TestAll {
public static void main(String [] args) {
TestXMPPConn testConn = new TestXMPPConn(“Your Server Here”, 5222, “Your Username Here”, “Your Password Here”);
TestRoster testRoster;
try {
testConn.connect();
}
catch(Exception e) {
System.out.println(e.toString());
}
try {
testConn.login();
Thread.sleep(1000);
testRoster = new TestRoster();
testRoster.setConnection(testConn);
testRoster.displayRoster();
}
catch(Exception e) {
System.out.println(e.toString());
}
try {
Thread.sleep(10000);
}
catch(Exception e) {}
}
}
/code
This should compile as is so hopefully will help you out but if you get stuck further just give me a shout.
Jon