Search in sources :

Example 66 with Message

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

the class MessageTest method testHighestPriority.

/**
     * User0 is connected from 2 resources. User0 is available in both resources
     * but with different priority presence values. User1 sends a message to the
     * bare JID of User0. Check that the resource with highest priority will get
     * the messages.
     *
     * @throws Exception if an error occurs.
     */
public void testHighestPriority() throws Exception {
    // Create another connection for the same user of connection 1
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
    conn3.connect();
    conn3.login(getUsername(0), getPassword(0), "Home");
    // Set this connection as highest priority
    Presence presence = new Presence(Presence.Type.available);
    presence.setPriority(10);
    conn3.sendStanza(presence);
    // Set this connection as highest priority
    presence = new Presence(Presence.Type.available);
    presence.setPriority(5);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    // User0 listen in both connected clients
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    StanzaCollector coll3 = conn3.createStanzaCollector(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("Resource with lowest priority got the message", message);
    message = (Message) coll3.nextResult(2000);
    assertNotNull(message);
    assertEquals("Test 1", message.getBody());
    message = (Message) coll3.nextResult(1000);
    assertNotNull(message);
    assertEquals("Test 2", message.getBody());
    conn3.disconnect();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 67 with Message

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

the class MessageTest method testDirectPresence.

/**
     * Will a user recieve a message from another after only sending the user a directed presence,
     * or will Wildfire intercept for offline storage?
     *
     * 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).sendStanza(new Presence(Presence.Type.available));
    Presence presence = new Presence(Presence.Type.available);
    presence.setTo(getBareJID(1));
    getConnection(0).sendStanza(presence);
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    try {
        getConnection(1).getChatManager().createChat(getBareJID(0), null).sendMessage("Test 1");
    } catch (XMPPException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    Message message = (Message) collector.nextResult(2500);
    assertNotNull("Message not recieved from remote user", message);
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 68 with Message

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

the class MessageTest method testMostRecentActive.

/**
     * User0 is connected from 2 resources. User0 is available in both resources
     * with same priority presence values and same show values. User1 sends a message to the
     * bare JID of User0. Check that the resource with most recent activity will get
     * the messages.
     *
     * @throws Exception if an error occurs.
     */
public void testMostRecentActive() throws Exception {
    // Create another connection for the same user of connection 1
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
    conn3.connect();
    conn3.login(getUsername(0), getPassword(0), "Home");
    // Set this connection as highest priority
    Presence presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(10);
    conn3.sendStanza(presence);
    // Set this connection as highest priority
    presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(10);
    getConnection(0).sendStanza(presence);
    connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
    conn4.connect();
    conn4.login(getUsername(0), getPassword(0), "Home2");
    presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(4);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    // User0 listen in both connected clients
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    StanzaCollector coll3 = conn3.createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    StanzaCollector coll4 = conn4.createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    // Send a message from this resource to indicate most recent activity 
    conn3.sendStanza(new Message("admin@" + getXMPPServiceDomain()));
    // 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("Resource with oldest activity got the message", message);
    message = (Message) coll4.nextResult(2000);
    assertNull(message);
    message = (Message) coll3.nextResult(2000);
    assertNotNull(message);
    assertEquals("Test 1", message.getBody());
    message = (Message) coll3.nextResult(1000);
    assertNotNull(message);
    assertEquals("Test 2", message.getBody());
    conn3.disconnect();
    conn4.disconnect();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 69 with Message

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

the class ThrottleTestWriter method main.

/**
     * Starts the throttle test write client.
     *
     * @param args application arguments.
     */
public static void main(String[] args) {
    if (args.length != 3) {
        System.out.println("Usage: java ThrottleTestWriter [server] [username] [password]");
        System.exit(0);
    }
    String server = args[0];
    String username = args[1];
    String password = args[2];
    try {
        // Connect to the server, without TLS encryption.
        ConnectionConfiguration config = new ConnectionConfiguration(server);
        config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
        final XMPPConnection con = new XMPPConnection(config);
        System.out.print("Connecting to " + server + "... ");
        con.connect();
        con.login(username, password, "writer");
        System.out.print("success.");
        System.out.println("");
        // Get the "real" server address.
        server = con.getServiceName();
        String writerAddress = username + "@" + server + "/writer";
        final String readerAddress = username + "@" + server + "/reader";
        System.out.println("Registered as " + writerAddress);
        // Look for the reader process.
        System.out.print("Looking for " + readerAddress + "...");
        while (true) {
            IQ testIQ = new Time();
            testIQ.setType(IQ.Type.GET);
            testIQ.setTo(readerAddress);
            PacketCollector collector = con.createPacketCollector(new PacketIDFilter(testIQ.getPacketID()));
            con.sendPacket(testIQ);
            // Wait 5 seconds.
            long start = System.currentTimeMillis();
            Packet result = collector.nextResult(5000);
            collector.cancel();
            // If we got a result, continue.
            if (result != null && result.getError() == null) {
                System.out.println(" found reader. Starting packet flood.");
                break;
            }
            System.out.print(".");
            long end = System.currentTimeMillis();
            if (end - start < 5000) {
                try {
                    Thread.sleep(5000 - (end - start));
                } catch (Exception e) {
                // ignore.
                }
            }
        }
        // Create a process to log how many packets we're writing out.
        Runnable statsRunnable = new Runnable() {

            public void run() {
                while (!done) {
                    try {
                        Thread.sleep(5000);
                    } catch (Exception e) {
                    /* ignore */
                    }
                    int count = packetCount.getAndSet(0);
                    System.out.println("Packets per second: " + (count / 5));
                }
            }
        };
        Thread statsThread = new Thread(statsRunnable);
        statsThread.setDaemon(true);
        statsThread.start();
        // Now start flooding packets.
        Message testMessage = new Message(readerAddress);
        testMessage.setBody("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        while (!done) {
            con.sendPacket(testMessage);
            packetCount.getAndIncrement();
        }
    } catch (Exception e) {
        System.out.println("\nError: " + e.getMessage());
        e.printStackTrace();
    }
}
Also used : Packet(org.jivesoftware.smack.packet.Packet) Message(org.jivesoftware.smack.packet.Message) IQ(org.jivesoftware.smack.packet.IQ) Time(org.jivesoftware.smackx.packet.Time) XMPPConnection(org.jivesoftware.smack.XMPPConnection) ConnectionConfiguration(org.jivesoftware.smack.ConnectionConfiguration) PacketCollector(org.jivesoftware.smack.PacketCollector) PacketIDFilter(org.jivesoftware.smack.filter.PacketIDFilter)

Example 70 with Message

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

the class OfflineMessageManager method getMessages.

/**
     * Returns a List of the offline <tt>Messages</tt> whose stamp matches the specified
     * request. The request will include the list of stamps that uniquely identifies
     * the offline messages to retrieve. The returned offline messages will not be deleted
     * from the server. Use {@link #deleteMessages(java.util.List)} to delete the messages.
     *
     * @param nodes the list of stamps that uniquely identifies offline message.
     * @return a List with the offline <tt>Messages</tt> that were received as part of
     *         this request.
     * @throws XMPPErrorException If the user is not allowed to make this request or the server does
     *                       not support offline message retrieval.
     * @throws NoResponseException if there was no response from the server.
     * @throws NotConnectedException 
     * @throws InterruptedException 
     */
public List<Message> getMessages(final List<String> nodes) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
    List<Message> messages = new ArrayList<Message>();
    OfflineMessageRequest request = new OfflineMessageRequest();
    for (String node : nodes) {
        OfflineMessageRequest.Item item = new OfflineMessageRequest.Item(node);
        item.setAction("view");
        request.addItem(item);
    }
    // Filter offline messages that were requested by this request
    StanzaFilter messageFilter = new AndFilter(PACKET_FILTER, new StanzaFilter() {

        @Override
        public boolean accept(Stanza packet) {
            OfflineMessageInfo info = (OfflineMessageInfo) packet.getExtension("offline", namespace);
            return nodes.contains(info.getNode());
        }
    });
    int pendingNodes = nodes.size();
    StanzaCollector messageCollector = connection.createStanzaCollector(messageFilter);
    try {
        connection.createStanzaCollectorAndSend(request).nextResultOrThrow();
        // Collect the received offline messages
        Message message = messageCollector.nextResult();
        while (message != null && pendingNodes > 0) {
            pendingNodes--;
            messages.add(message);
            message = messageCollector.nextResult();
        }
    } finally {
        // Stop queuing offline messages
        messageCollector.cancel();
    }
    return messages;
}
Also used : StanzaFilter(org.jivesoftware.smack.filter.StanzaFilter) Message(org.jivesoftware.smack.packet.Message) OfflineMessageInfo(org.jivesoftware.smackx.offline.packet.OfflineMessageInfo) Stanza(org.jivesoftware.smack.packet.Stanza) ArrayList(java.util.ArrayList) AndFilter(org.jivesoftware.smack.filter.AndFilter) OfflineMessageRequest(org.jivesoftware.smackx.offline.packet.OfflineMessageRequest) StanzaCollector(org.jivesoftware.smack.StanzaCollector)

Aggregations

Message (org.jivesoftware.smack.packet.Message)166 Test (org.junit.Test)57 Presence (org.jivesoftware.smack.packet.Presence)21 XMPPException (org.jivesoftware.smack.XMPPException)15 StanzaCollector (org.jivesoftware.smack.StanzaCollector)14 NetworkException (com.xabber.android.data.NetworkException)13 Stanza (org.jivesoftware.smack.packet.Stanza)13 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)13 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)12 ExtensionElement (org.jivesoftware.smack.packet.ExtensionElement)12 AccountItem (com.xabber.android.data.account.AccountItem)11 XMPPConnection (org.jivesoftware.smack.XMPPConnection)11 Date (java.util.Date)10 Jid (org.jxmpp.jid.Jid)10 Chat (org.jivesoftware.smack.Chat)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 ArrayList (java.util.ArrayList)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 InputStream (java.io.InputStream)6 Forwarded (org.jivesoftware.smackx.forward.packet.Forwarded)6