Search in sources :

Example 66 with Presence

use of org.jivesoftware.smack.packet.Presence in project xabber-android by redsolution.

the class PresenceManager method sendVCardUpdatePresence.

public void sendVCardUpdatePresence(AccountJid account, String hash) throws NetworkException {
    LogManager.i(this, "sendVCardUpdatePresence: " + account);
    AccountItem accountItem = AccountManager.getInstance().getAccount(account);
    if (accountItem == null) {
        return;
    }
    final Presence presence = accountItem.getPresence();
    final VCardUpdate vCardUpdate = new VCardUpdate();
    vCardUpdate.setPhotoHash(hash);
    presence.addExtension(vCardUpdate);
    StanzaSender.sendStanza(account, presence);
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) Presence(org.jivesoftware.smack.packet.Presence) VCardUpdate(com.xabber.xmpp.vcardupdate.VCardUpdate)

Example 67 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class PacketParserUtils method parsePresence.

/**
     * Parses a presence packet.
     *
     * @param parser the XML parser, positioned at the start of a presence packet.
     * @return a Presence packet.
     * @throws Exception 
     */
public static Presence parsePresence(XmlPullParser parser) throws Exception {
    ParserUtils.assertAtStartTag(parser);
    final int initialDepth = parser.getDepth();
    Presence.Type type = Presence.Type.available;
    String typeString = parser.getAttributeValue("", "type");
    if (typeString != null && !typeString.equals("")) {
        type = Presence.Type.fromString(typeString);
    }
    Presence presence = new Presence(type);
    presence.setTo(ParserUtils.getJidAttribute(parser, "to"));
    presence.setFrom(ParserUtils.getJidAttribute(parser, "from"));
    presence.setStanzaId(parser.getAttributeValue("", "id"));
    String language = getLanguageAttribute(parser);
    if (language != null && !"".equals(language.trim())) {
        // CHECKSTYLE:OFF
        presence.setLanguage(language);
    // CHECKSTYLE:ON
    }
    // Parse sub-elements
    outerloop: while (true) {
        int eventType = parser.next();
        switch(eventType) {
            case XmlPullParser.START_TAG:
                String elementName = parser.getName();
                String namespace = parser.getNamespace();
                switch(elementName) {
                    case "status":
                        presence.setStatus(parser.nextText());
                        break;
                    case "priority":
                        int priority = Integer.parseInt(parser.nextText());
                        presence.setPriority(priority);
                        break;
                    case "show":
                        String modeText = parser.nextText();
                        if (StringUtils.isNotEmpty(modeText)) {
                            presence.setMode(Presence.Mode.fromString(modeText));
                        } else {
                            // Some implementations send presence stanzas with a
                            // '<show />' element, which is a invalid XMPP presence
                            // stanza according to RFC 6121 4.7.2.1
                            LOGGER.warning("Empty or null mode text in presence show element form " + presence.getFrom() + " with id '" + presence.getStanzaId() + "' which is invalid according to RFC6121 4.7.2.1");
                        }
                        break;
                    case "error":
                        presence.setError(parseError(parser));
                        break;
                    default:
                        // failing completely here. See SMACK-390 for more information.
                        try {
                            PacketParserUtils.addExtensionElement(presence, parser, elementName, namespace);
                        } catch (Exception e) {
                            LOGGER.warning("Failed to parse extension element in Presence stanza: \"" + e + "\" from: '" + presence.getFrom() + " id: '" + presence.getStanzaId() + "'");
                        }
                        break;
                }
                break;
            case XmlPullParser.END_TAG:
                if (parser.getDepth() == initialDepth) {
                    break outerloop;
                }
                break;
        }
    }
    return presence;
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) IOException(java.io.IOException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

Example 68 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class PresenceTest method testMessageToHighestPriority.

/**
     * XMPPConnection(0) will send messages to the bareJID of XMPPConnection(1) where the user of
     * XMPPConnection(1) has logged from two different places with different presence priorities.
     */
public void testMessageToHighestPriority() {
    XMPPTCPConnection conn = null;
    try {
        // 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).sendStanza(new Presence(Presence.Type.available, null, 1, Presence.Mode.available));
        conn.sendStanza(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).sendStanza(new Presence(Presence.Type.available, null, 2, Presence.Mode.available));
        conn.sendStanza(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).sendStanza(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), getPassword(1), "OtherPlace");
        conn.sendStanza(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).sendStanza(new Presence(Presence.Type.available, null, 1, Presence.Mode.available));
        conn.sendStanza(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");
    /*assertNotNull("Resource with highest priority didn't receive the message",
                    chat2.nextMessage(2000));
            assertNull("Resource with lowest priority received the message",
                    chat1.nextMessage(1000));*/
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 69 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class RosterSmackTest method testMultipleResources.

/**
     * User1 is connected from 2 resources. User1 adds User0 to his roster. Ensure
     * that both resources of user1 get the available presence of User0. Remove User0
     * from User1's roster and check presences again.
     */
public void testMultipleResources() throws Exception {
    // Create another connection for the same user of connection 1
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
    conn4.connect();
    conn4.login(getUsername(1), getPassword(1), "Home");
    // Add a new roster entry
    Roster roster = conn4.getRoster();
    roster.createEntry(getBareJID(0), "gato11", null);
    // Wait up to 2 seconds
    long initial = System.currentTimeMillis();
    while (System.currentTimeMillis() - initial < 2000 && (!roster.getPresence(getBareJID(0)).isAvailable() || !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());
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 70 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class RosterSmackTest method testDeleteAllRosterGroupEntries.

/**
     * 1. Create entries in roster groups
     * 2. Iterate on the groups and remove the entry from each group
     * 3. Check that the entries are kept as unfiled entries
     */
public void testDeleteAllRosterGroupEntries() {
    try {
        // Add a new roster entry
        Roster roster = getConnection(0).getRoster();
        CountDownLatch latch = new CountDownLatch(2);
        setupCountdown(latch, roster);
        roster.createEntry(getBareJID(1), "gato11", new String[] { "Friends", "Family" });
        roster.createEntry(getBareJID(2), "gato12", new String[] { "Family" });
        waitForCountdown(latch, roster, 2);
        final CountDownLatch removeLatch = new CountDownLatch(3);
        RosterListener latchCounter = new RosterListener() {

            @Override
            public void presenceChanged(Presence presence) {
            }

            @Override
            public void entriesUpdated(Collection<String> addresses) {
                removeLatch.countDown();
            }

            @Override
            public void entriesDeleted(Collection<String> addresses) {
            }

            @Override
            public void entriesAdded(Collection<String> addresses) {
            }
        };
        roster.addRosterListener(latchCounter);
        for (RosterEntry entry : roster.getEntries()) {
            for (RosterGroup rosterGroup : entry.getGroups()) {
                rosterGroup.removeEntry(entry);
            }
        }
        removeLatch.await(5, TimeUnit.SECONDS);
        roster.removeRosterListener(latchCounter);
        assertEquals("The number of entries in connection 1 should be 1", 1, getConnection(1).getRoster().getEntryCount());
        assertEquals("The number of groups in connection 1 should be 0", 0, getConnection(1).getRoster().getGroupCount());
        assertEquals("The number of entries in connection 2 should be 1", 1, getConnection(2).getRoster().getEntryCount());
        assertEquals("The number of groups in connection 2 should be 0", 0, getConnection(2).getRoster().getGroupCount());
        assertEquals("The number of entries in connection 0 should be 2", 2, roster.getEntryCount());
        assertEquals("The number of groups in connection 0 should be 0", 0, roster.getGroupCount());
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) Collection(java.util.Collection) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)103 Message (org.jivesoftware.smack.packet.Message)21 Resourcepart (org.jxmpp.jid.parts.Resourcepart)12 Jid (org.jxmpp.jid.Jid)11 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)10 AccountItem (com.xabber.android.data.account.AccountItem)9 UserJid (com.xabber.android.data.entity.UserJid)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 StanzaCollector (org.jivesoftware.smack.StanzaCollector)7 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 UserPresence (jetbrains.communicator.core.users.UserPresence)6 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)6 BareJid (org.jxmpp.jid.BareJid)6 ClientInfo (com.xabber.android.data.extension.capability.ClientInfo)5 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)5 Test (org.junit.Test)5 EntityFullJid (org.jxmpp.jid.EntityFullJid)5