1

usera sends a subscription request to userb. After the following code is executed, only usera is able to see userb in his roster list but not userb. Why is that ?

Function userB is called first and from within this function userA is called. I have registered a packetListener for userb but to my surprise it doesn't work ! I tested it by trying to print a statement but I see no output. So the listener is not being executed. What could be the reason for this ?

Here is what is happening in the following code :

userB ---activates packet listener-----> userA
  |                                         |
  |                                         |
  |                                       sends request to userB
  |                                         |
 / \                                       \ /
  |--------------------<--------------------|                                          |


public void userB() {
    try {
        final Connection connection = new XMPPConnection("localhost");
        connection.connect();
        connection.login("userb", "passb");

        Roster r = connection.getRoster();
        r.setSubscriptionMode(Roster.SubscriptionMode.manual);

        PacketListener pListener = new PacketListener() {                
            @Override
            public void processPacket(Packet packet) {
                if(packet instanceof Presence) {
                    Presence presence =(Presence)packet;
                    if(presence.getType().equals(Presence.Type.subscribe)) {
                        System.out.println("Inside the first if statement");
                        Presence newP = new Presence(Presence.Type.subscribed);
                        newP.setMode(Presence.Mode.available);
                        newP.setTo(presence.getFrom());
                        connection.sendPacket(newP);

                        Presence subscription = new Presence(Presence.Type.subscribe);
                        subscription.setTo(presence.getFrom());
                        connection.sendPacket(subscription);
                    }
                }
            }
        };

        PacketFilter pFilter = new PacketFilter() {
            @Override
            public boolean accept(Packet packet) {
                if(packet instanceof Presence) {
                    Presence presence = (Presence)packet;
                    if(presence.getType().equals(Presence.Type.subscribe)) {
                        System.out.println("Inside if statement");
                        return true;
                    }
                }
                return false;
            }
        };

        connection.addPacketListener(pListener, pFilter);
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                userA();
            }
        };
        new Thread(runnable).start();
        //connection.disconnect();
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

public void userA() {
    try{
        Connection connection = new XMPPConnection("localhost");
        connection.connect();
        connection.login("usera","passa");

        Presence subscribe = new Presence(Presence.Type.subscribe);
        subscribe.setTo("userb@localhost");
        connection.sendPacket(subscribe);
        connection.disconnect();
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}
0

1 Answer 1

0

Your code seems to be fine. But you only get a subscribe Packet the first time (when you actually subscribe). Subsequent times, these packets are filtered out apparently because you are already subscribed.

Try running your code with two new users.

5
  • The same thing happens. User A is able to see User B in his roster but User B doesn't see User A in his roster Aug 21, 2013 at 10:47
  • The original question was why the listeners were not called. I guess they are called now, right? Aug 21, 2013 at 11:06
  • UserA sends a 'subscribe' packet to userB. UserB sends back a 'subscribed' packet and sends a 'subscribe' request to UserA. But UserA never sends back a 'subscribed' packet. Aug 21, 2013 at 11:08
  • I tested with 2 new users but what i see, even after creating a new user is that processPacket function never gets called. I try to print something just after the function begins but nothing gets printed..On the other hand accept function is being called. Aug 22, 2013 at 18:08
  • I tested your code here locally and it works on my side. One possible explanation though might be that the userB() function ended before the packet from userA arrived (thereby garbage collecting the connection from the userB function). Aug 23, 2013 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.