Search in sources :

Example 1 with MQTTFrame

use of org.fusesource.mqtt.codec.MQTTFrame 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 MQTTFrame

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

the class MQTTTest method testDuplicateSubscriptions.

@Test(timeout = 60 * 1000)
public void testDuplicateSubscriptions() throws Exception {
    MQTT mqtt = createMQTTConnection();
    mqtt.setClientId("foo");
    mqtt.setKeepAlive((short) 20);
    final int[] actualQoS = { -1 };
    mqtt.setTracer(new Tracer() {

        @Override
        public void onReceive(MQTTFrame frame) {
            // validate the QoS
            if (frame.messageType() == PUBLISH.TYPE) {
                actualQoS[0] = frame.qos().ordinal();
            }
        }
    });
    final BlockingConnection connection = mqtt.blockingConnection();
    connection.connect();
    final String RETAIN = "RETAIN";
    connection.publish("TopicA", RETAIN.getBytes(), QoS.EXACTLY_ONCE, true);
    QoS[] qoss = { QoS.AT_MOST_ONCE, QoS.AT_MOST_ONCE, QoS.AT_LEAST_ONCE, QoS.EXACTLY_ONCE };
    for (QoS qos : qoss) {
        connection.subscribe(new Topic[] { new Topic("TopicA", qos) });
        final Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
        assertNotNull("No message for " + qos, msg);
        assertEquals(RETAIN, new String(msg.getPayload()));
        msg.ack();
        int waitCount = 0;
        while (actualQoS[0] == -1 && waitCount < 10) {
            Thread.sleep(1000);
            waitCount++;
        }
        assertEquals(qos.ordinal(), actualQoS[0]);
        actualQoS[0] = -1;
    }
    connection.unsubscribe(new String[] { "TopicA" });
    connection.disconnect();
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) QoS(org.fusesource.mqtt.client.QoS) 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) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) Test(org.junit.Test)

Example 3 with MQTTFrame

use of org.fusesource.mqtt.codec.MQTTFrame 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 4 with MQTTFrame

use of org.fusesource.mqtt.codec.MQTTFrame 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 5 with MQTTFrame

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

the class MQTTTest method testMQTTRetainQoS.

@Test(timeout = 60 * 1000)
public void testMQTTRetainQoS() throws Exception {
    String[] topics = { "AT_MOST_ONCE", "AT_LEAST_ONCE", "EXACTLY_ONCE" };
    for (int i = 0; i < topics.length; i++) {
        final String topic = topics[i];
        MQTT mqtt = createMQTTConnection();
        mqtt.setClientId("foo");
        mqtt.setKeepAlive((short) 2);
        final int[] actualQoS = { -1 };
        mqtt.setTracer(new Tracer() {

            @Override
            public void onReceive(MQTTFrame frame) {
                // validate the QoS
                if (frame.messageType() == PUBLISH.TYPE) {
                    actualQoS[0] = frame.qos().ordinal();
                }
            }
        });
        final BlockingConnection connection = mqtt.blockingConnection();
        connection.connect();
        connection.publish(topic, topic.getBytes(), QoS.EXACTLY_ONCE, true);
        connection.subscribe(new Topic[] { new Topic(topic, QoS.valueOf(topic)) });
        final Message msg = connection.receive(5000, TimeUnit.MILLISECONDS);
        assertNotNull(msg);
        assertEquals(topic, new String(msg.getPayload()));
        int waitCount = 0;
        while (actualQoS[0] == -1 && waitCount < 10) {
            Thread.sleep(1000);
            waitCount++;
        }
        assertEquals(i, actualQoS[0]);
        msg.ack();
        connection.unsubscribe(new String[] { topic });
        connection.disconnect();
    }
}
Also used : MQTT(org.fusesource.mqtt.client.MQTT) 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) BlockingConnection(org.fusesource.mqtt.client.BlockingConnection) Topic(org.fusesource.mqtt.client.Topic) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame) Test(org.junit.Test)

Aggregations

MQTTFrame (org.fusesource.mqtt.codec.MQTTFrame)8 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)6 BlockingConnection (org.fusesource.mqtt.client.BlockingConnection)6 MQTT (org.fusesource.mqtt.client.MQTT)6 Topic (org.fusesource.mqtt.client.Topic)6 Tracer (org.fusesource.mqtt.client.Tracer)6 Test (org.junit.Test)6 BytesMessage (javax.jms.BytesMessage)5 AmqpMessage (org.apache.activemq.transport.amqp.client.AmqpMessage)5 Message (org.fusesource.mqtt.client.Message)5 ProtocolException (java.net.ProtocolException)4 PUBLISH (org.fusesource.mqtt.codec.PUBLISH)4 ArrayList (java.util.ArrayList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 QoS (org.fusesource.mqtt.client.QoS)2 ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)1 Random (java.util.Random)1 UTF8Buffer (org.fusesource.hawtbuf.UTF8Buffer)1 CONNECT (org.fusesource.mqtt.codec.CONNECT)1 Ignore (org.junit.Ignore)1