powered by Jive Software

Smack/SWT: how to deal with threads?

Hi all,

I’‘m trying to write an IM java application using smack and SWT, and I’‘m just at the proof of concept time: i’‘m just trying to make smack and SWT work together inside only one java class. (I’'ve read posts on forum dealing whit smack and swt, but I did not found any clue to solve my problem…)

Well, here’‘s the way I’'ve tried:

I’‘ve decided to use SUBSCRIPTION_MANUAL mode for the roster subscription management; then to do this, I’'ve

registered a packet listener which filters all Presence type packets.

I’'m not using RosterLister#presenceChanged() method: all is done in the packet listener to keep control on update gui method calls (see code snippet below).

To update my swt gui with presence states I’'ve read from packets, I need to access the gui thread using code like:

private synchronized void updateSwtGui(){

display.asyncExec( //where “display” represents the swt gui thread accessed asynchronously

new Runnable() {

public void run(){

String guiBuddyName = buddyName;

String guiPresenceStatus = buddyPresenceStatus;

//update buddyTree object with smack datas…

//…

}

});

}

I call the #updateSwtGui method in the packetListener implementation when I detect an event about presence (subscribe, unsubscribe, online, away…):

At this time, I’‘m not able to send parameters from the packetListener to the #updateSwtGui method, then I’'m (awfully) using private static class variables: “buddyName” and “buddyPresenceStatus”.

Here’'s a code snippet of my packet listener:

packetFilterPresence = new AndFilter(new PacketTypeFilter(Presence.class), new BasicPacketFilter());//BasicPacketFilter: custom filter which just accepts.

packetListenerPresence = new PacketListener() {

public void processPacket(Packet packet) {

if (null != packet) {

String xmlPacket = packet.toXML();

System.out.println(">xmlPacket: " + xmlPacket);//dbg

if (packet.getClass().equals(Presence.class)) {

Presence p = (Presence)packet;

if(null != p){

String from = p.getFrom();

buddyName = from;

String status = p.getStatus();

buddyPresenceStatus =status;

updateSwtGui();

}

}

}…

//…

connection.addPacketListener(packetListenerPresence,

packetFilterPresence);

When a lots of new packets are recieved by the packet listener, and mainly at connection, the synchronized #updateSwtGui method will be called with the same rythm and then

static variables which carry informations about smack presence roster state are out of date, and then gui is updated wrong!!!

=>Here is my question:

How can I update my swt gui from smack data in a consistent and safe way?

Is it because of the static variables used to bind smack thread and SWT gui thread? if yes, how to do?

Smack seems to be a really good xmpp client api, and i belive in coupling with SWT for the gui part…

Thanks a lot for your help…really need…

Alex