Search in sources :

Example 1 with Presence

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

the class LowLevelRosterIntegrationTest method testPresenceEventListenersOffline.

@SmackIntegrationTest
public void testPresenceEventListenersOffline(final XMPPTCPConnection conOne, final XMPPTCPConnection conTwo) throws TimeoutException, Exception {
    RosterIntegrationTest.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
    final Roster rosterOne = Roster.getInstanceFor(conOne);
    final Roster rosterTwo = Roster.getInstanceFor(conTwo);
    // TODO create Roster.createEntry() with boolean flag for subscribe or not.
    rosterOne.createEntry(conTwo.getUser().asBareJid(), "Con Two", null);
    rosterTwo.createEntry(conOne.getUser().asBareJid(), "Con One", null);
    // TODO Change timeout form '5000' to something configurable.
    final long timeout = 5000;
    RosterIntegrationTest.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);
    final SimpleResultSyncPoint offlineTriggered = new SimpleResultSyncPoint();
    rosterOne.addPresenceEventListener(new AbstractPresenceEventListener() {

        @Override
        public void presenceUnavailable(FullJid jid, Presence presence) {
            if (!jid.equals(conTwo.getUser())) {
                return;
            }
            offlineTriggered.signal();
        }
    });
    // Disconnect conTwo, this should cause an 'unavilable' presence to be send from conTwo to
    // conOne.
    conTwo.disconnect();
    Boolean result = offlineTriggered.waitForResult(timeout);
    if (!result) {
        throw new Exception("presenceUnavailable() was not called");
    }
}
Also used : FullJid(org.jxmpp.jid.FullJid) Presence(org.jivesoftware.smack.packet.Presence) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint) TimeoutException(java.util.concurrent.TimeoutException) SmackIntegrationTest(org.igniterealtime.smack.inttest.SmackIntegrationTest)

Example 2 with Presence

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

the class RosterIntegrationTest method ensureSubscribedTo.

private static void ensureSubscribedTo(final XMPPConnection conOne, final XMPPConnection conTwo, long timeout) throws TimeoutException, Exception {
    Roster rosterOne = Roster.getInstanceFor(conOne);
    Roster rosterTwo = Roster.getInstanceFor(conTwo);
    if (rosterOne.isSubscribedToMyPresence(conTwo.getUser())) {
        return;
    }
    final SubscribeListener subscribeListener = new SubscribeListener() {

        @Override
        public SubscribeAnswer processSubscribe(Jid from, Presence subscribeRequest) {
            if (from.equals(conTwo.getUser().asBareJid())) {
                return SubscribeAnswer.Approve;
            }
            return SubscribeAnswer.Deny;
        }
    };
    rosterOne.addSubscribeListener(subscribeListener);
    final SimpleResultSyncPoint syncPoint = new SimpleResultSyncPoint();
    rosterTwo.addPresenceEventListener(new AbstractPresenceEventListener() {

        @Override
        public void presenceSubscribed(BareJid address, Presence subscribedPresence) {
            if (!address.equals(conOne.getUser().asBareJid())) {
                return;
            }
            syncPoint.signal();
        }
    });
    rosterTwo.sendSubscriptionRequest(conOne.getUser().asBareJid());
    try {
        syncPoint.waitForResult(timeout);
    } finally {
        rosterOne.removeSubscribeListener(subscribeListener);
    }
}
Also used : Jid(org.jxmpp.jid.Jid) BareJid(org.jxmpp.jid.BareJid) BareJid(org.jxmpp.jid.BareJid) Presence(org.jivesoftware.smack.packet.Presence) SimpleResultSyncPoint(org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint)

Example 3 with Presence

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

the class XMPPSession method sendChatState.

/**
     * @see net.sf.kraken.session.TransportSession#sendChatState(org.xmpp.packet.JID, net.sf.kraken.type.ChatStateType)
     */
@Override
public void sendChatState(JID jid, ChatStateType chatState) {
    final Presence presence = conn.getRoster().getPresence(jid.toString());
    if (presence == null || presence.getType().equals(Presence.Type.unavailable)) {
        // don't send chat state to contacts that are offline.
        return;
    }
    Chat chat = conn.getChatManager().createChat(getTransport().convertJIDToID(jid), listener);
    try {
        ChatState state = ChatState.active;
        switch(chatState) {
            case active:
                state = ChatState.active;
                break;
            case composing:
                state = ChatState.composing;
                break;
            case paused:
                state = ChatState.paused;
                break;
            case inactive:
                state = ChatState.inactive;
                break;
            case gone:
                state = ChatState.gone;
                break;
        }
        Message message = new Message();
        message.addExtension(new ChatStateExtension(state));
        chat.sendMessage(message);
    } catch (XMPPException e) {
    // Ignore
    }
}
Also used : ChatState(org.jivesoftware.smackx.ChatState) Message(org.jivesoftware.smack.packet.Message) ChatStateExtension(org.jivesoftware.smackx.packet.ChatStateExtension) Presence(org.jivesoftware.smack.packet.Presence)

Example 4 with Presence

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

the class XMPPSession method updateStatus.

/**
     * @see net.sf.kraken.session.TransportSession#updateStatus(net.sf.kraken.type.PresenceType, String)
     */
@Override
public void updateStatus(PresenceType presenceType, String verboseStatus) {
    setPresenceAndStatus(presenceType, verboseStatus);
    final org.jivesoftware.smack.packet.Presence presence = constructCurrentLegacyPresencePacket();
    try {
        conn.sendPacket(presence);
    } catch (IllegalStateException e) {
        Log.debug("XMPP: Not connected while trying to change status.");
    }
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 5 with Presence

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

the class XMPPSession method acceptAddContact.

/**
     * @see net.sf.kraken.session.TransportSession#acceptAddContact(JID)
     */
@Override
public void acceptAddContact(JID jid) {
    final String userID = getTransport().convertJIDToID(jid);
    Log.debug("XMPP: accept-add contact: " + userID);
    final Presence accept = new Presence(Type.subscribed);
    accept.setTo(userID);
    conn.sendPacket(accept);
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)222 Message (org.jivesoftware.smack.packet.Message)37 Jid (org.jxmpp.jid.Jid)18 Resourcepart (org.jxmpp.jid.parts.Resourcepart)16 AndFilter (org.jivesoftware.smack.filter.AndFilter)15 Roster (org.jivesoftware.smack.roster.Roster)15 XMPPConnection (org.jivesoftware.smack.XMPPConnection)14 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)14 SwingWorker (org.jivesoftware.spark.util.SwingWorker)14 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)13 XMPPException (org.jivesoftware.smack.XMPPException)13 RosterEntry (org.jivesoftware.smack.roster.RosterEntry)13 EntityFullJid (org.jxmpp.jid.EntityFullJid)13 Date (java.util.Date)12 SmackException (org.jivesoftware.smack.SmackException)12 UserJid (com.xabber.android.data.entity.UserJid)11 IQ (org.jivesoftware.smack.packet.IQ)11 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)11 AccountItem (com.xabber.android.data.account.AccountItem)10