Search in sources :

Example 1 with PUBLISH

use of org.fusesource.mqtt.codec.PUBLISH in project activemq-artemis by apache.

the class MQTTTest method testPacketIdGeneratorCleanSession.

@Ignore
@Test(timeout = 90 * 1000)
public // If there is a good reason for this we should follow ActiveMQ.
void testPacketIdGeneratorCleanSession() throws Exception {
    final String[] cleanClientIds = new String[] { "", "clean-packetid", null };
    final Map<Short, PUBLISH> publishMap = new ConcurrentHashMap<>();
    MQTT[] mqtts = new MQTT[cleanClientIds.length];
    for (int i = 0; i < cleanClientIds.length; i++) {
        mqtts[i] = createMQTTConnection("", true);
        mqtts[i].setKeepAlive((short) 15);
        mqtts[i].setTracer(new Tracer() {

            @Override
            public void onReceive(MQTTFrame frame) {
                LOG.info("Client received:\n" + frame);
                if (frame.messageType() == PUBLISH.TYPE) {
                    PUBLISH publish = new PUBLISH();
                    try {
                        publish.decode(frame);
                        LOG.info("PUBLISH " + publish);
                    } catch (ProtocolException e) {
                        fail("Error decoding publish " + e.getMessage());
                    }
                    if (publishMap.get(publish.messageId()) != null) {
                        assertTrue(publish.dup());
                    }
                    publishMap.put(publish.messageId(), publish);
                }
            }

            @Override
            public void onSend(MQTTFrame frame) {
                LOG.info("Client sent:\n" + frame);
            }
        });
    }
    final Random random = new Random();
    for (short i = 0; i < 10; i++) {
        BlockingConnection connection = mqtts[random.nextInt(cleanClientIds.length)].blockingConnection();
        connection.connect();
        final String TOPIC = "TopicA/";
        connection.subscribe(new Topic[] { new Topic(TOPIC, QoS.EXACTLY_ONCE) });
        // publish non-retained message
        connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
        Message msg = connection.receive(1000, TimeUnit.MILLISECONDS);
        assertNotNull(msg);
        assertEquals(TOPIC, new String(msg.getPayload()));
        msg.ack();
        assertEquals(1, publishMap.size());
        final short id = (short) (i + 1);
        assertNotNull("No message for id " + id, publishMap.get(id));
        publishMap.clear();
        connection.disconnect();
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) ProtocolException(java.net.ProtocolException) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) Tracer(org.fusesource.mqtt.client.Tracer) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) PUBLISH(org.fusesource.mqtt.codec.PUBLISH) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) Random(java.util.Random) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Topic(org.fusesource.mqtt.client.Topic) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with PUBLISH

use of org.fusesource.mqtt.codec.PUBLISH in project activemq-artemis by apache.

the class MQTTTest method testPacketIdGeneratorNonCleanSession.

@Test(timeout = 90 * 1000)
public void testPacketIdGeneratorNonCleanSession() throws Exception {
    final MQTT mqtt = createMQTTConnection("nonclean-packetid", false);
    mqtt.setKeepAlive((short) 15);
    final Map<Short, PUBLISH> publishMap = new ConcurrentHashMap<>();
    mqtt.setTracer(new Tracer() {

        @Override
        public void onReceive(MQTTFrame frame) {
            LOG.info("Client received:\n" + frame);
            if (frame.messageType() == PUBLISH.TYPE) {
                PUBLISH publish = new PUBLISH();
                try {
                    publish.decode(frame);
                    LOG.info("PUBLISH " + publish);
                } catch (ProtocolException e) {
                    fail("Error decoding publish " + e.getMessage());
                }
                if (publishMap.get(publish.messageId()) != null) {
                    assertTrue(publish.dup());
                }
                publishMap.put(publish.messageId(), publish);
            }
        }

        @Override
        public void onSend(MQTTFrame frame) {
            LOG.info("Client sent:\n" + frame);
        }
    });
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    final String TOPIC = "TopicA/";
    connection.subscribe(new Topic[] { new Topic(TOPIC, QoS.EXACTLY_ONCE) });
    // publish non-retained messages
    final int TOTAL_MESSAGES = 10;
    for (int i = 0; i < TOTAL_MESSAGES; i++) {
        connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    }
    // receive half the messages in this session
    for (int i = 0; i < TOTAL_MESSAGES / 2; i++) {
        final Message msg = connection.receive(1000, TimeUnit.MILLISECONDS);
        assertNotNull(msg);
        assertEquals(TOPIC, new String(msg.getPayload()));
        msg.ack();
    }
    connection.disconnect();
    // resume session
    connection = mqtt.blockingConnection();
    connection.connect();
    // receive rest of the messages
    Message msg = null;
    do {
        msg = connection.receive(1000, TimeUnit.MILLISECONDS);
        if (msg != null) {
            assertEquals(TOPIC, new String(msg.getPayload()));
            msg.ack();
        }
    } while (msg != null);
    // make sure we received all message ids
    for (short id = 1; id <= TOTAL_MESSAGES; id++) {
        assertNotNull("No message for id " + id, publishMap.get(id));
    }
    connection.unsubscribe(new String[] { TOPIC });
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) ProtocolException(java.net.ProtocolException) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) Tracer(org.fusesource.mqtt.client.Tracer) PUBLISH(org.fusesource.mqtt.codec.PUBLISH) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Example 3 with PUBLISH

use of org.fusesource.mqtt.codec.PUBLISH in project activemq-artemis by apache.

the class MQTTTest method testResendMessageId.

@Test(timeout = 60 * 1000)
public void testResendMessageId() throws Exception {
    final MQTT mqtt = createMQTTConnection("resend", false);
    mqtt.setKeepAlive((short) 5);
    final List<PUBLISH> publishList = new ArrayList<>();
    mqtt.setTracer(new Tracer() {

        @Override
        public void onReceive(MQTTFrame frame) {
            LOG.info("Client received:\n" + frame);
            if (frame.messageType() == PUBLISH.TYPE) {
                PUBLISH publish = new PUBLISH();
                try {
                    publish.decode(frame);
                } catch (ProtocolException e) {
                    fail("Error decoding publish " + e.getMessage());
                }
                publishList.add(publish);
            }
        }

        @Override
        public void onSend(MQTTFrame frame) {
            LOG.info("Client sent:\n" + frame);
        }
    });
    BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    final String TOPIC = "TopicA/";
    final String[] topics = new String[] { TOPIC, "TopicA/+" };
    connection.subscribe(new Topic[] { new Topic(topics[0], QoS.AT_LEAST_ONCE), new Topic(topics[1], QoS.EXACTLY_ONCE) });
    // publish non-retained message
    connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    assertTrue(Wait.waitFor(() -> publishList.size() == 2, 5000));
    connection.disconnect();
    connection = mqtt.blockingConnection();
    connection.connect();
    assertTrue(Wait.waitFor(() -> publishList.size() == 4, 5000));
    // TODO Investigate if receiving the same ID for overlapping subscriptions is actually spec compliant.
    // In Artemis we send a new ID for every copy of the message.
    // make sure we received duplicate message ids
    // assertTrue(publishList.get(0).messageId() == publishList.get(2).messageId() || publishList.get(0).messageId() == publishList.get(3).messageId());
    // assertTrue(publishList.get(1).messageId() == publishList.get(3).messageId() || publishList.get(1).messageId() == publishList.get(2).messageId());
    // assertTrue(publishList.get(2).dup() && publishList.get(3).dup());
    connection.unsubscribe(topics);
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) ProtocolException(java.net.ProtocolException) Tracer(org.fusesource.mqtt.client.Tracer) ArrayList(java.util.ArrayList) PUBLISH(org.fusesource.mqtt.codec.PUBLISH) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) Test(org.junit.Test)

Example 4 with PUBLISH

use of org.fusesource.mqtt.codec.PUBLISH in project activemq-artemis by apache.

the class MQTTTest method testUniqueMessageIds.

@Test(timeout = 60 * 1000)
public void testUniqueMessageIds() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("foo");
    mqtt.setKeepAlive((short) 2);
    mqtt.setCleanSession(true);
    final List<PUBLISH> publishList = new ArrayList<>();
    mqtt.setTracer(new Tracer() {

        @Override
        public void onReceive(MQTTFrame frame) {
            LOG.info("Client received:\n" + frame);
            if (frame.messageType() == PUBLISH.TYPE) {
                PUBLISH publish = new PUBLISH();
                try {
                    publish.decode(frame);
                } catch (ProtocolException e) {
                    fail("Error decoding publish " + e.getMessage());
                }
                publishList.add(publish);
            }
        }

        @Override
        public void onSend(MQTTFrame frame) {
            LOG.info("Client sent:\n" + frame);
        }
    });
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    // create overlapping subscriptions with different QoSs
    QoS[] qoss = { QoS.AT_MOST_ONCE, QoS.AT_LEAST_ONCE, QoS.EXACTLY_ONCE };
    final String TOPIC = "TopicA/";
    // publish retained message
    connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, true);
    String[] subs = { TOPIC, "TopicA/#", "TopicA/+" };
    for (int i = 0; i < qoss.length; i++) {
        connection.subscribe(new Topic[] { new Topic(subs[i], qoss[i]) });
    }
    // publish non-retained message
    connection.publish(TOPIC, TOPIC.getBytes(), QoS.EXACTLY_ONCE, false);
    int received = 0;
    Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
    do {
        assertNotNull(msg);
        assertEquals(TOPIC, new String(msg.getPayload()));
        msg.ack();
        int waitCount = 0;
        while (publishList.size() <= received && waitCount < 10) {
            Thread.sleep(1000);
            waitCount++;
        }
        msg = connection.receive(5000, TimeUnit.MILLISECONDS);
    } while (msg != null && received++ < subs.length * 2);
    assertEquals("Unexpected number of messages", subs.length * 2, received + 1);
    // AT_MOST_ONCE
    for (int i = 0; i < publishList.size(); i++) {
        for (int j = i + 1; j < publishList.size(); j++) {
            final PUBLISH publish1 = publishList.get(i);
            final PUBLISH publish2 = publishList.get(j);
            boolean qos0 = false;
            if (publish1.qos() == QoS.AT_MOST_ONCE) {
                qos0 = true;
                assertEquals(0, publish1.messageId());
            }
            if (publish2.qos() == QoS.AT_MOST_ONCE) {
                qos0 = true;
                assertEquals(0, publish2.messageId());
            }
            if (!qos0) {
                assertNotEquals(publish1.messageId(), publish2.messageId());
            }
        }
    }
    connection.unsubscribe(subs);
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) ProtocolException(java.net.ProtocolException) AmqpMessage(org.apache.activemq.transport.amqp.client.AmqpMessage) Message(org.fusesource.mqtt.client.Message) BytesMessage(javax.jms.BytesMessage) Tracer(org.fusesource.mqtt.client.Tracer) ArrayList(java.util.ArrayList) PUBLISH(org.fusesource.mqtt.codec.PUBLISH) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) QoS(org.fusesource.mqtt.client.QoS) Topic(org.fusesource.mqtt.client.Topic) Test(org.junit.Test)

Aggregations

ProtocolException (java.net.ProtocolException)4 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)4 BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)4 MQTT (org.fusesource.mqtt.client.MQTT)4 Topic (org.fusesource.mqtt.client.Topic)4 Tracer (org.fusesource.mqtt.client.Tracer)4 MQTTFrame (org.fusesource.mqtt.codec.MQTTFrame)4 PUBLISH (org.fusesource.mqtt.codec.PUBLISH)4 Test (org.junit.Test)4 BytesMessage (javax.jms.BytesMessage)3 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)3 Message (org.fusesource.mqtt.client.Message)3 ArrayList (java.util.ArrayList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Random (java.util.Random)1 QoS (org.fusesource.mqtt.client.QoS)1 Ignore (org.junit.Ignore)1