Search in sources :

Example 11 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project activemq-artemis by apache.

the class MQTTProtocolHandler method send.

protected void send(int messageId, String topicName, int qosLevel, boolean isRetain, ByteBuf payload, int deliveryCount) {
    boolean redelivery = qosLevel == 0 ? false : (deliveryCount > 0);
    MqttFixedHeader header = new MqttFixedHeader(MqttMessageType.PUBLISH, redelivery, MqttQoS.valueOf(qosLevel), isRetain, 0);
    MqttPublishVariableHeader varHeader = new MqttPublishVariableHeader(topicName, messageId);
    MqttMessage publish = new MqttPublishMessage(header, varHeader, payload);
    sendToClient(publish);
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader)

Example 12 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project rocketmq-externals by apache.

the class ProduceMessageIntegrationTest method test.

@Test
public void test() {
    /* handle the CONNECT message */
    MqttConnectMessage connectMessage = getConnectMessage();
    producerChannel.writeInbound(connectMessage);
    MqttConnAckMessage connAckMessage = producerChannel.readOutbound();
    Client client = clientManager.get(producerChannel);
    Assert.assertNotNull(client);
    Assert.assertEquals(producerId, client.getId());
    Assert.assertEquals(producerChannel, client.getCtx().channel());
    Assert.assertTrue(client.isConnected());
    Assert.assertTrue(producerChannel.isOpen());
    Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, connAckMessage.variableHeader().connectReturnCode());
    producerChannel.releaseInbound();
    /* handle the PUBLISH message when there is no online Consumers */
    MqttPublishMessage publishMessage = getMqttPublishMessage();
    producerChannel.writeInbound(publishMessage);
    MqttMessage pubackMessage = producerChannel.readOutbound();
    /* qos 0 should have no PUBACK message */
    Assert.assertNull(pubackMessage);
    /*
         * the message would be discarded simply because there is no Consumers
         * and the topic should be created
         * */
    Assert.assertTrue(subscriptionStore.hasTopic(topicName));
    producerChannel.releaseInbound();
    /* handle the PUBLISH message when there are online Consumers */
    Mockito.when(subscriptionStore.get(topicName)).thenReturn(mockedSubscriptions);
    publishMessage = getMqttPublishMessage();
    byte[] publishMessagePayload = new byte[publishMessage.payload().readableBytes()];
    publishMessage.payload().getBytes(0, publishMessagePayload);
    producerChannel.writeInbound(publishMessage);
    MqttPublishMessage forwardedMessage = consumerChannel.readOutbound();
    byte[] forwardedMessagePayload = new byte[forwardedMessage.payload().readableBytes()];
    forwardedMessage.payload().getBytes(0, forwardedMessagePayload);
    Assert.assertNotNull(forwardedMessage);
    Assert.assertEquals(publishMessage.variableHeader().topicName(), forwardedMessage.variableHeader().topicName());
    Assert.assertEquals(publishMessage.variableHeader().packetId(), forwardedMessage.variableHeader().packetId());
    Assert.assertArrayEquals(publishMessagePayload, forwardedMessagePayload);
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) MqttConnAckMessage(io.netty.handler.codec.mqtt.MqttConnAckMessage) Client(org.apache.rocketmq.iot.connection.client.Client) MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) Test(org.junit.Test)

Example 13 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project rocketmq-externals by apache.

the class MqttPingreqMessageHandler method handleMessage.

/**
 * handle the PINGREQ message from client
 * <ol>
 * <li>check client exists</li>
 * <li>check client is connected</li>
 * <li>generate the PINGRESP message</li>
 * <li>send the PINGRESP message to the client</li>
 * </ol>
 *
 * @param message
 * @return
 */
@Override
public void handleMessage(Message message) {
    Client client = message.getClient();
    if (client == null || !client.isConnected()) {
        return;
    }
    MqttMessage pingreqMessage = (MqttMessage) message.getPayload();
    MqttMessage pingrespMessage = MessageUtil.getMqttPingrespMessage(pingreqMessage);
    client.getCtx().writeAndFlush(pingrespMessage);
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) Client(org.apache.rocketmq.iot.connection.client.Client)

Example 14 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project rocketmq-externals by apache.

the class MessageUtil method getMessage.

public static Message getMessage(MqttMessage mqttMessage) {
    Message message = new Message();
    message.setPayload(mqttMessage);
    switch(mqttMessage.fixedHeader().messageType()) {
        case CONNECT:
            message.setType(Message.Type.MQTT_CONNECT);
            break;
        case CONNACK:
            message.setType(Message.Type.MQTT_CONNACK);
            break;
        case PUBLISH:
            message.setType(Message.Type.MQTT_PUBLISH);
            break;
        case PUBACK:
            message.setType(Message.Type.MQTT_PUBACK);
            break;
        case PUBREC:
            message.setType(Message.Type.MQTT_PUBREC);
            break;
        case PUBREL:
            message.setType(Message.Type.MQTT_PUBREL);
            break;
        case PUBCOMP:
            message.setType(Message.Type.MQTT_PUBCOMP);
        case SUBSCRIBE:
            message.setType(Message.Type.MQTT_SUBSCRIBE);
            break;
        case SUBACK:
            message.setType(Message.Type.MQTT_SUBACK);
            break;
        case UNSUBSCRIBE:
            message.setType(Message.Type.MQTT_UNSUBSCRIBE);
            break;
        case UNSUBACK:
            message.setType(Message.Type.MQTT_UNSUBACK);
            break;
        case PINGREQ:
            message.setType(Message.Type.MQTT_PINGREQ);
            break;
        case PINGRESP:
            message.setType(Message.Type.MQTT_PINGRESP);
            break;
        case DISCONNECT:
            message.setType(Message.Type.MQTT_DISCONNECT);
            break;
        default:
    }
    return message;
}
Also used : MqttPubAckMessage(io.netty.handler.codec.mqtt.MqttPubAckMessage) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttSubAckMessage(io.netty.handler.codec.mqtt.MqttSubAckMessage) MqttConnAckMessage(io.netty.handler.codec.mqtt.MqttConnAckMessage) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttUnsubAckMessage(io.netty.handler.codec.mqtt.MqttUnsubAckMessage) Message(org.apache.rocketmq.iot.common.data.Message) MqttUnsubscribeMessage(io.netty.handler.codec.mqtt.MqttUnsubscribeMessage)

Example 15 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project netty by netty.

the class MqttHeartBeatBrokerHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    MqttMessage mqttMessage = (MqttMessage) msg;
    System.out.println("Received MQTT message: " + mqttMessage);
    switch(mqttMessage.fixedHeader().messageType()) {
        case CONNECT:
            MqttFixedHeader connackFixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
            MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_ACCEPTED, false);
            MqttConnAckMessage connack = new MqttConnAckMessage(connackFixedHeader, mqttConnAckVariableHeader);
            ctx.writeAndFlush(connack);
            break;
        case PINGREQ:
            MqttFixedHeader pingreqFixedHeader = new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0);
            MqttMessage pingResp = new MqttMessage(pingreqFixedHeader);
            ctx.writeAndFlush(pingResp);
            break;
        case DISCONNECT:
            ctx.close();
            break;
        default:
            System.out.println("Unexpected message type: " + mqttMessage.fixedHeader().messageType());
            ReferenceCountUtil.release(msg);
            ctx.close();
    }
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttConnAckVariableHeader(io.netty.handler.codec.mqtt.MqttConnAckVariableHeader) MqttConnAckMessage(io.netty.handler.codec.mqtt.MqttConnAckMessage)

Aggregations

MqttMessage (io.netty.handler.codec.mqtt.MqttMessage)20 MqttFixedHeader (io.netty.handler.codec.mqtt.MqttFixedHeader)10 MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)6 Client (org.apache.rocketmq.iot.connection.client.Client)4 MqttConnAckMessage (io.netty.handler.codec.mqtt.MqttConnAckMessage)3 MqttConnectMessage (io.netty.handler.codec.mqtt.MqttConnectMessage)3 MqttPubAckMessage (io.netty.handler.codec.mqtt.MqttPubAckMessage)2 MqttSubscribeMessage (io.netty.handler.codec.mqtt.MqttSubscribeMessage)2 Message (org.apache.rocketmq.iot.common.data.Message)2 MqttClient (org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)2 Test (org.junit.Test)2 TransportProtos (org.thingsboard.server.gen.transport.TransportProtos)2 JsonParseException (com.google.gson.JsonParseException)1 ByteBuf (io.netty.buffer.ByteBuf)1 MqttConnAckVariableHeader (io.netty.handler.codec.mqtt.MqttConnAckVariableHeader)1 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)1 MqttMessageIdVariableHeader (io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader)1 MqttPublishVariableHeader (io.netty.handler.codec.mqtt.MqttPublishVariableHeader)1 MqttSubAckMessage (io.netty.handler.codec.mqtt.MqttSubAckMessage)1 MqttUnsubAckMessage (io.netty.handler.codec.mqtt.MqttUnsubAckMessage)1