Package org.jivesoftware.smack.packet

Examples of org.jivesoftware.smack.packet.Presence


     * User1 becomes lines. User0 never sent an available presence to the server but
     * instead sent one to User1. User1 sends a message to User0. Should User0 get the
     * message?
     */
    public void testDirectPresence() {
        getConnection(1).sendPacket(new Presence(Presence.Type.available));

        Presence presence = new Presence(Presence.Type.available);
        presence.setTo(getBareJID(1));
        getConnection(0).sendPacket(presence);

        PacketCollector collector = getConnection(0)
                .createPacketCollector(new MessageTypeFilter(Message.Type.chat));
        try {
View Full Code Here


    /**
     * Check that when a client becomes unavailable all messages sent to the client are stored offline. So that when
     * the client becomes available again the offline messages are received.
     */
    public void testOfflineMessage() {
        getConnection(0).sendPacket(new Presence(Presence.Type.available));
        getConnection(1).sendPacket(new Presence(Presence.Type.available));
        // 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);
            PacketCollector collector = getConnection(1).createPacketCollector(
                    new MessageTypeFilter(Message.Type.chat));
            chat.sendMessage("Test 1");
            chat.sendMessage("Test 2");

            Thread.sleep(500);

            // User2 becomes available again

            getConnection(1).sendPacket(new Presence(Presence.Type.available));

            // Check that offline messages are retrieved by user2 which is now available
            Message message = (Message) collector.nextResult(2500);
            assertNotNull(message);
            message = (Message) collector.nextResult(2000);
View Full Code Here

    /**
     * Check that two clients are able to send messages with a body of 4K characters and their
     * connections are not being closed.
     */
    public void testHugeMessage() {
        getConnection(0).sendPacket(new Presence(Presence.Type.available));
        getConnection(1).sendPacket(new Presence(Presence.Type.available));
        // User2 becomes available again
        PacketCollector collector = getConnection(1).createPacketCollector(
                new MessageTypeFilter(Message.Type.chat));

        // Create message with a body of 4K characters
View Full Code Here

                new ConnectionConfiguration(getHost(), getPort(), getServiceName());
        XMPPConnection conn3 = new XMPPConnection(connectionConfiguration);
        conn3.connect();
        conn3.login(getUsername(0), getUsername(0), "Home");
        // Set this connection as highest priority
        Presence presence = new Presence(Presence.Type.available);
        presence.setPriority(10);
        conn3.sendPacket(presence);
        // Set this connection as highest priority
        presence = new Presence(Presence.Type.available);
        presence.setPriority(5);
        getConnection(0).sendPacket(presence);

        // Let the server process the change in presences
        Thread.sleep(200);
View Full Code Here

                new ConnectionConfiguration(getHost(), getPort(), getServiceName());
        XMPPConnection conn3 = new XMPPConnection(connectionConfiguration);
        conn3.connect();
        conn3.login(getUsername(0), getUsername(0), "Home");
        // Set this connection as highest priority
        Presence presence = new Presence(Presence.Type.available);
        presence.setMode(Presence.Mode.away);
        conn3.sendPacket(presence);
        // Set this connection as highest priority
        presence = new Presence(Presence.Type.available);
        presence.setMode(Presence.Mode.available);
        getConnection(0).sendPacket(presence);

        // Let the server process the change in presences
        Thread.sleep(200);
View Full Code Here

                new ConnectionConfiguration(getHost(), getPort(), getServiceName());
        XMPPConnection conn3 = new XMPPConnection(connectionConfiguration);
        conn3.connect();
        conn3.login(getUsername(0), getUsername(0), "Home");
        // Set this connection as highest priority
        Presence presence = new Presence(Presence.Type.available);
        presence.setMode(Presence.Mode.available);
        presence.setPriority(10);
        conn3.sendPacket(presence);
        // Set this connection as highest priority
        presence = new Presence(Presence.Type.available);
        presence.setMode(Presence.Mode.available);
        presence.setPriority(10);
        getConnection(0).sendPacket(presence);

        connectionConfiguration =
                new ConnectionConfiguration(getHost(), getPort(), getServiceName());
        XMPPConnection conn4 = new XMPPConnection(connectionConfiguration);
        conn4.connect();
        conn4.login(getUsername(0), getUsername(0), "Home2");
        presence = new Presence(Presence.Type.available);
        presence.setMode(Presence.Mode.available);
        presence.setPriority(4);
        getConnection(0).sendPacket(presence);


        // Let the server process the change in presences
        Thread.sleep(200);
View Full Code Here

     *
     * @throws Exception if an error occurs.
     */
    public void testOfflineStorageWithNegativePriority() throws Exception {
        // Set this connection with negative priority
        Presence presence = new Presence(Presence.Type.available);
        presence.setMode(Presence.Mode.available);
        presence.setPriority(-1);
        getConnection(0).sendPacket(presence);

        // Let the server process the change in presences
        Thread.sleep(200);

        // User0 listen for incoming traffic
        PacketCollector collector = getConnection(0).createPacketCollector(new MessageTypeFilter(Message.Type.chat));

        // User1 sends a message to the bare JID of User0
        Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
        chat.sendMessage("Test 1");
        chat.sendMessage("Test 2");

        // Check that messages were sent to resource with highest priority
        Message message = (Message) collector.nextResult(2000);
        assertNull("Messages were not stored offline", message);

        // Set this connection with positive priority
        presence = new Presence(Presence.Type.available);
        presence.setMode(Presence.Mode.available);
        presence.setPriority(1);
        getConnection(0).sendPacket(presence);

        // Let the server process the change in presences
        Thread.sleep(200);

View Full Code Here

        else if (response.getType() == IQ.Type.ERROR) {
            throw new XMPPException(response.getError());
        }

        // Create a presence subscription packet and send.
        Presence presencePacket = new Presence(Presence.Type.subscribe);
        presencePacket.setTo(user);
        connection.sendPacket(presencePacket);
    }
View Full Code Here

     */
    public Presence getPresence(String user) {
        String key = getPresenceMapKey(StringUtils.parseBareAddress(user));
        Map<String, Presence> userPresences = presenceMap.get(key);
        if (userPresences == null) {
            Presence presence = new Presence(Presence.Type.unavailable);
            presence.setFrom(user);
            return presence;
        }
        else {
            // Find the resource with the highest priority
            // Might be changed to use the resource with the highest availability instead.
            Presence presence = null;

            for (String resource : userPresences.keySet()) {
                Presence p = userPresences.get(resource);
                if (!p.isAvailable()) {
                    continue;
                }
                // Chose presence with highest priority first.
                if (presence == null || p.getPriority() > presence.getPriority()) {
                    presence = p;
                }
                // If equal priority, choose "most available" by the mode value.
                else if (p.getPriority() == presence.getPriority()) {
                    Presence.Mode pMode = p.getMode();
                    // Default to presence mode of available.
                    if (pMode == null) {
                        pMode = Presence.Mode.available;
                    }
                    Presence.Mode presenceMode = presence.getMode();
                    // Default to presence mode of available.
                    if (presenceMode == null) {
                        presenceMode = Presence.Mode.available;
                    }
                    if (pMode.compareTo(presenceMode) < 0) {
                        presence = p;
                    }
                }
            }
            if (presence == null) {
                presence = new Presence(Presence.Type.unavailable);
                presence.setFrom(user);
                return presence;
            }
            else {
                return presence;
View Full Code Here

    public Presence getPresenceResource(String userWithResource) {
        String key = getPresenceMapKey(userWithResource);
        String resource = StringUtils.parseResource(userWithResource);
        Map<String, Presence> userPresences = presenceMap.get(key);
        if (userPresences == null) {
            Presence presence = new Presence(Presence.Type.unavailable);
            presence.setFrom(userWithResource);
            return presence;
        }
        else {
            Presence presence = userPresences.get(resource);
            if (presence == null) {
                presence = new Presence(Presence.Type.unavailable);
                presence.setFrom(userWithResource);
                return presence;
            }
            else {
                return presence;
            }
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.