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);
}
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;
}
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();
}
}
}
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());
}
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());
}
}
Aggregations