Package org.jivesoftware.smack.packet

Examples of org.jivesoftware.smack.packet.Presence


     * performs some "Flexible Offline Message Retrieval" by fetching all the offline messages
     * and then removing all the offline messages.
     */
    public void testFetchAndPurge() {
        // Make user2 unavailable
        getConnection(1).sendPacket(new Presence(Presence.Type.unavailable));

        try {
            Thread.sleep(500);

            // User1 sends some messages to User2 which is not available at the moment
            Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
            chat.sendMessage("Test 1");
            chat.sendMessage("Test 2");

            Thread.sleep(500);

            // User2 checks the number of offline messages
            OfflineMessageManager offlineManager = new OfflineMessageManager(getConnection(1));
            assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());
            // Get all offline messages
            Iterator messages = offlineManager.getMessages();
            assertTrue("No message was found", messages.hasNext());
            List<String> stamps = new ArrayList<String>();
            while (messages.hasNext()) {
                Message message = (Message) messages.next();
                OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline",
                        "http://jabber.org/protocol/offline");
                assertNotNull("No offline information was included in the offline message", info);
                assertNotNull("No stamp was found in the message header", info.getNode());
                stamps.add(info.getNode());
            }
            assertEquals("Wrong number of messages", 2, stamps.size());
            // Check that the offline messages have not been deleted
            assertEquals("Wrong number of offline messages", 2, offlineManager.getMessageCount());

            // User2 becomes available again
            PacketCollector collector = getConnection(1).createPacketCollector(
                    new MessageTypeFilter(Message.Type.chat));
            getConnection(1).sendPacket(new Presence(Presence.Type.available));

            // Check that no offline messages was sent to the user
            Message message = (Message) collector.nextResult(2500);
            assertNull("An offline message was sent from the server", message);

View Full Code Here


     * 5. Check that presence for each connected resource is correct
     */
    public void testRosterPresences() throws Exception {
        Thread.sleep(200);
        try {
            Presence presence;

            // Create another connection for the same user of connection 1
            ConnectionConfiguration connectionConfiguration =
                    new ConnectionConfiguration(getHost(), getPort(), getServiceName());
            XMPPConnection conn4 = new XMPPConnection(connectionConfiguration);
            conn4.connect();
            conn4.login(getUsername(1), getUsername(1), "Home");

            // Add a new roster entry
            Roster roster = getConnection(0).getRoster();
            roster.createEntry(getBareJID(1), "gato11", null);

            // Wait up to 2 seconds
            long initial = System.currentTimeMillis();
            while (System.currentTimeMillis() - initial < 2000 &&
                    (roster.getPresence(getBareJID(1)).getType() == Presence.Type.unavailable)) {
                Thread.sleep(100);
            }

            // Check that a presence is returned for a user
            presence = roster.getPresence(getBareJID(1));
            assertTrue("Returned a null Presence for an existing user", presence.isAvailable());

            // Check that the right presence is returned for a user+resource
            presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getServiceName() + "/Home");
            assertEquals("Returned the wrong Presence", "Home",
                    StringUtils.parseResource(presence.getFrom()));

            // Check that the right presence is returned for a user+resource
            presence = roster.getPresenceResource(getFullJID(1));
            assertTrue("Presence not found for user " + getFullJID(1), presence.isAvailable());
            assertEquals("Returned the wrong Presence", "Smack",
                    StringUtils.parseResource(presence.getFrom()));

            // Check the returned presence for a non-existent user+resource
            presence = roster.getPresenceResource("noname@" + getServiceName() + "/Smack");
            assertFalse("Available presence was returned for a non-existing user", presence.isAvailable());
            assertEquals("Returned Presence for a non-existing user has the incorrect type",
                    Presence.Type.unavailable, presence.getType());

            // Check that the returned presences are correct
            Iterator<Presence> presences = roster.getPresences(getBareJID(1));
            int count = 0;
            while (presences.hasNext()) {
View Full Code Here

                        !getConnection(1).getRoster().getPresence(getBareJID(0)).isAvailable())) {
            Thread.sleep(100);
        }

        // Check that a presence is returned for the new contact
        Presence presence = roster.getPresence(getBareJID(0));
        assertTrue("Returned a null Presence for an existing user", presence.isAvailable());

        // Check that a presence is returned for the new contact
        presence = getConnection(1).getRoster().getPresence(getBareJID(0));
        assertTrue("Returned a null Presence for an existing user", presence.isAvailable());

        // Delete user from roster
        roster.removeEntry(roster.getEntry(getBareJID(0)));

        // Wait up to 2 seconds
        initial = System.currentTimeMillis();
        while (System.currentTimeMillis() - initial < 2000 && (
                roster.getPresence(getBareJID(0)).getType() != Presence.Type.unavailable ||
                        getConnection(1).getRoster().getPresence(getBareJID(0)).getType() !=
                                Presence.Type.unavailable)) {
            Thread.sleep(100);
        }

        // Check that no presence is returned for the removed contact
        presence = roster.getPresence(getBareJID(0));
        assertFalse("Available presence was returned for removed contact", presence.isAvailable());
        assertEquals("Returned Presence for removed contact has incorrect type",
                Presence.Type.unavailable, presence.getType());

        // Check that no presence is returned for the removed contact
        presence = getConnection(1).getRoster().getPresence(getBareJID(0));
        assertFalse("Available presence was returned for removed contact", presence.isAvailable());
        assertEquals("Returned Presence for removed contact has incorrect type",
                Presence.Type.unavailable, presence.getType());
    }
View Full Code Here

        Thread.sleep(200);

        // Break the connection
        getConnection(0).packetReader.notifyConnectionError(new Exception("Simulated Error"));

        Presence presence = roster.getPresence(getBareJID(1));
        assertFalse("Unavailable presence not found for offline user", presence.isAvailable());
        assertEquals("Unavailable presence not found for offline user", Presence.Type.unavailable,
                presence.getType());
        // Reconnection should occur in 10 seconds
        Thread.sleep(12200);
        presence = roster.getPresence(getBareJID(1));
        assertTrue("Presence not found for user", presence.isAvailable());
        assertEquals("Presence should be online after a connection reconnection",
                Presence.Type.available, presence.getType());
    }
View Full Code Here

            // User_1 will log in again using another resource
            conn = createConnection();
            conn.connect();
            conn.login(getUsername(1), getUsername(1), "OtherPlace");
            // Change the presence priorities of User_1
            getConnection(1).sendPacket(new Presence(Presence.Type.available, null, 1,
                    Presence.Mode.available));
            conn.sendPacket(new Presence(Presence.Type.available, null, 2,
                    Presence.Mode.available));
            Thread.sleep(150);
            // Create the chats between the participants
            Chat chat0 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
            Chat chat1 = getConnection(1).getChatManager().createChat(getBareJID(0), chat0.getThreadID(), null);
            Chat chat2 = conn.getChatManager().createChat(getBareJID(0), chat0.getThreadID(), null);

            // Test delivery of message to the presence with highest priority
            chat0.sendMessage("Hello");
            /*assertNotNull("Resource with highest priority didn't receive the message",
                    chat2.nextMessage(2000));
            assertNull("Resource with lowest priority received the message",
                    chat1.nextMessage(1000));*/

            // Invert the presence priorities of User_1
            getConnection(1).sendPacket(new Presence(Presence.Type.available, null, 2,
                    Presence.Mode.available));
            conn.sendPacket(new Presence(Presence.Type.available, null, 1,
                    Presence.Mode.available));

            Thread.sleep(150);
            // Test delivery of message to the presence with highest priority
            chat0.sendMessage("Hello");
            /*assertNotNull("Resource with highest priority didn't receive the message",
                    chat1.nextMessage(2000));
            assertNull("Resource with lowest priority received the message",
                    chat2.nextMessage(1000));*/

            // User_1 closes his connection
            conn.disconnect();
            Thread.sleep(150);

            // Test delivery of message to the unique presence of the user_1
            chat0.sendMessage("Hello");
            /*assertNotNull("Resource with highest priority didn't receive the message",
                    chat1.nextMessage(2000));*/

            getConnection(1).sendPacket(new Presence(Presence.Type.available, null, 2,
                    Presence.Mode.available));

            // User_1 will log in again using another resource
            conn = createConnection();
            conn.connect();
            conn.login(getUsername(1), getUsername(1), "OtherPlace");
            conn.sendPacket(new Presence(Presence.Type.available, null, 1,
                    Presence.Mode.available));
            chat2 = conn.getChatManager().createChat(getBareJID(0), chat0.getThreadID(), null);

            Thread.sleep(150);
            // Test delivery of message to the presence with highest priority
            chat0.sendMessage("Hello");
            /*assertNotNull("Resource with highest priority didn't receive the message",
                    chat1.nextMessage(2000));
            assertNull("Resource with lowest priority received the message",
                    chat2.nextMessage(1000));*/

            // Invert the presence priorities of User_1
            getConnection(1).sendPacket(new Presence(Presence.Type.available, null, 1,
                    Presence.Mode.available));
            conn.sendPacket(new Presence(Presence.Type.available, null, 2,
                    Presence.Mode.available));

            Thread.sleep(150);
            // Test delivery of message to the presence with highest priority
            chat0.sendMessage("Hello");
View Full Code Here

     * should receive the message.
     * TODO Fix this in Wildfire but before check if XMPP spec requests this feature
     */
    public void testNotAvailablePresence() throws XMPPException {
        // Change the presence to unavailable of User_1
        getConnection(1).sendPacket(new Presence(Presence.Type.unavailable));

        // User_1 will log in again using another resource (that is going to be available)
        XMPPConnection conn = createConnection();
        conn.connect();
        conn.login(getUsername(1), getUsername(1), "OtherPlace");
View Full Code Here

        {
            Thread.sleep(100);
        }

        // Check that a presence is returned for the new contact
        Presence presence = roster.getPresence(getBareJID(1));
        assertTrue("Returned an offline Presence for an existing user", presence.isAvailable());

        presence = roster.getPresenceResource(getBareJID(1) + "/Home");
        assertTrue("Returned an offline Presence for Home resource", presence.isAvailable());

        presence = roster.getPresenceResource(getFullJID(1));
        assertTrue("Returned an offline Presence for Smack resource", presence.isAvailable());

        Iterator<Presence> presences = roster.getPresences(getBareJID(1));
        assertTrue("Returned an offline Presence for an existing user", presence.isAvailable());
        assertNotNull("No presence was found for user1", presences);
        assertTrue("No presence was found for user1", presences.hasNext());
        presences.next();
        assertTrue("Only one presence was found for user1", presences.hasNext());

        // User1 logs out from one resource
        conn4.disconnect();

        // Wait up to 1 second
        Thread.sleep(700);

        // Check that a presence is returned for the new contact
        presence = roster.getPresence(getBareJID(1));
        assertTrue("Returned a null Presence for an existing user", presence.isAvailable());

        presence = roster.getPresenceResource(getFullJID(1));
        assertTrue("Returned a null Presence for Smack resource", presence.isAvailable());

        presence = roster.getPresenceResource(getBareJID(1) + "/Home");
        assertTrue("Returned a Presence for no longer connected resource", !presence.isAvailable());

        presences = roster.getPresences(getBareJID(1));
        assertNotNull("No presence was found for user1", presences);
        Presence value = presences.next();
        assertTrue("No presence was found for user1", value.isAvailable());
        assertFalse("More than one presence was found for user1", presences.hasNext());
    }
View Full Code Here

                roster.getPresence(getBareJID(1)).getType().equals(Presence.Type.unavailable))) {
            Thread.sleep(100);
        }

        // Sign out of conn1 with status
        Presence offlinePresence = new Presence(Presence.Type.unavailable);
        offlinePresence.setStatus("Offline test");
        getConnection(1).disconnect(offlinePresence);

        // Wait 500 ms
        Thread.sleep(500);
        Presence presence = getConnection(0).getRoster().getPresence(getBareJID(1));
        assertEquals("Offline presence status not received.", "Offline test", presence.getStatus());

        // Sign out of conn0.
        getConnection(0).disconnect();

        // See if conneciton 0 can get offline status.
        XMPPConnection con0 = getConnection(0);
        con0.connect();
        con0.login(getUsername(0), getUsername(0));

        // Wait 500 ms
        Thread.sleep(500);
        presence = con0.getRoster().getPresence(getBareJID(1));
        assertTrue("Offline presence status not received after logout.",
                "Offline test".equals(presence.getStatus()));
    }
View Full Code Here

     * on this connection again. This is unlike the behavior during unexpected disconnects
     * (and subsequent connections). In that case, all state is preserved to allow for
     * more seamless error recovery.
     */
    public void disconnect() {
        disconnect(new Presence(Presence.Type.unavailable));
    }
View Full Code Here

* @author Gaston Dombiak
*/
public class DeafOccupantInterceptor implements PacketInterceptor {

    public void interceptPacket(Packet packet) {
        Presence presence = (Presence) packet;
        // Check if user is joining a room
        if (Presence.Type.available == presence.getType() &&
                presence.getExtension("x", "http://jabber.org/protocol/muc") != null) {
            // Add extension that indicates that user wants to be a deaf occupant
            packet.addExtension(new DeafExtension());
        }
    }
View Full Code Here

TOP

Related Classes of org.jivesoftware.smack.packet.Presence

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.