Search in sources :

Example 6 with MQTTFrame

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

Example 7 with MQTTFrame

use of org.fusesource.mqtt.codec.MQTTFrame in project fabric8 by jboss-fuse.

the class MqttProtocol method snoopConnectionParameters.

@Override
public void snoopConnectionParameters(final SocketWrapper socket, final Buffer received, final Handler<ConnectionParameters> handler) {
    final MqttProtocolDecoder h = new MqttProtocolDecoder(this);
    h.errorHandler(new Handler<String>() {

        @Override
        public void handle(String error) {
            LOG.info("STOMP protocol decoding error: " + error);
            socket.close();
        }
    });
    h.codecHandler(new Handler<MQTTFrame>() {

        @Override
        public void handle(MQTTFrame event) {
            try {
                if (event.messageType() == org.fusesource.mqtt.codec.CONNECT.TYPE) {
                    CONNECT connect = new CONNECT().decode(event);
                    ConnectionParameters parameters = new ConnectionParameters();
                    if (connect.clientId() != null) {
                        parameters.protocolClientId = connect.clientId().toString();
                    }
                    if (connect.userName() != null) {
                        parameters.protocolUser = connect.userName().toString();
                        // containing the virtual host info.
                        if (parameters.protocolUser.contains("/")) {
                            // Strip off the virtual host part of the username..
                            String[] parts = parameters.protocolUser.split("/", 2);
                            parameters.protocolVirtualHost = parts[0];
                            parameters.protocolUser = parts[1];
                            // Update the connect frame to strip out the virtual host from the username field...
                            connect.userName(new UTF8Buffer(parameters.protocolUser));
                            // re-write the received buffer /w  the updated connect frame
                            Buffer tail = received.getBuffer((int) h.getBytesDecoded(), received.length());
                            BufferSupport.setLength(received, 0);
                            append(received, connect.encode());
                            received.appendBuffer(tail);
                        }
                    }
                    handler.handle(parameters);
                } else {
                    LOG.info("Expected a CONNECT frame");
                    socket.close();
                }
            } catch (java.net.ProtocolException e) {
                LOG.info("Invalid MQTT frame: " + e, e);
                socket.close();
            }
        }
    });
    socket.readStream().dataHandler(h);
    h.handle(received);
}
Also used : Buffer(org.vertx.java.core.buffer.Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) UTF8Buffer(org.fusesource.hawtbuf.UTF8Buffer) ConnectionParameters(io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters) CONNECT(org.fusesource.mqtt.codec.CONNECT) MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame)

Example 8 with MQTTFrame

use of org.fusesource.mqtt.codec.MQTTFrame in project fabric8 by jboss-fuse.

the class MqttProtocol method append.

static void append(Buffer self, MQTTFrame value) {
    MQTTFrame frame = (MQTTFrame) value;
    self.appendByte(frame.header());
    int remaining = 0;
    for (org.fusesource.hawtbuf.Buffer buffer : frame.buffers) {
        remaining += buffer.length;
    }
    do {
        byte digit = (byte) (remaining & 0x7F);
        remaining >>>= 7;
        if (remaining > 0) {
            digit |= 0x80;
        }
        self.appendByte(digit);
    } while (remaining > 0);
    for (org.fusesource.hawtbuf.Buffer buffer : frame.buffers) {
        // TODO: see if we avoid the byte[] conversion.
        self.appendBytes(buffer.toByteArray());
    }
}
Also used : MQTTFrame(org.fusesource.mqtt.codec.MQTTFrame)

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