Search in sources :

Example 16 with MqttMessage

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

the class MqttTransportHandler method processProvisionSessionMsg.

private void processProvisionSessionMsg(ChannelHandlerContext ctx, MqttMessage msg) {
    switch(msg.fixedHeader().messageType()) {
        case PUBLISH:
            MqttPublishMessage mqttMsg = (MqttPublishMessage) msg;
            String topicName = mqttMsg.variableHeader().topicName();
            int msgId = mqttMsg.variableHeader().packetId();
            try {
                if (topicName.equals(MqttTopics.DEVICE_PROVISION_REQUEST_TOPIC)) {
                    try {
                        TransportProtos.ProvisionDeviceRequestMsg provisionRequestMsg = deviceSessionCtx.getContext().getJsonMqttAdaptor().convertToProvisionRequestMsg(deviceSessionCtx, mqttMsg);
                        transportService.process(provisionRequestMsg, new DeviceProvisionCallback(ctx, msgId, provisionRequestMsg));
                        log.trace("[{}][{}] Processing provision publish msg [{}][{}]!", sessionId, deviceSessionCtx.getDeviceId(), topicName, msgId);
                    } catch (Exception e) {
                        if (e instanceof JsonParseException || (e.getCause() != null && e.getCause() instanceof JsonParseException)) {
                            TransportProtos.ProvisionDeviceRequestMsg provisionRequestMsg = deviceSessionCtx.getContext().getProtoMqttAdaptor().convertToProvisionRequestMsg(deviceSessionCtx, mqttMsg);
                            transportService.process(provisionRequestMsg, new DeviceProvisionCallback(ctx, msgId, provisionRequestMsg));
                            deviceSessionCtx.setProvisionPayloadType(TransportPayloadType.PROTOBUF);
                            log.trace("[{}][{}] Processing provision publish msg [{}][{}]!", sessionId, deviceSessionCtx.getDeviceId(), topicName, msgId);
                        } else {
                            throw e;
                        }
                    }
                } else {
                    log.debug("[{}] Unsupported topic for provisioning requests: {}!", sessionId, topicName);
                    ctx.close();
                }
            } catch (RuntimeException e) {
                log.warn("[{}] Failed to process publish msg [{}][{}]", sessionId, topicName, msgId, e);
                ctx.close();
            } catch (AdaptorException e) {
                log.debug("[{}] Failed to process publish msg [{}][{}]", sessionId, topicName, msgId, e);
                ctx.close();
            }
            break;
        case PINGREQ:
            ctx.writeAndFlush(new MqttMessage(new MqttFixedHeader(PINGRESP, false, AT_MOST_ONCE, false, 0)));
            break;
        case DISCONNECT:
            ctx.close();
            break;
    }
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos) JsonParseException(com.google.gson.JsonParseException) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) JsonParseException(com.google.gson.JsonParseException) TbRateLimitsException(org.thingsboard.server.common.msg.tools.TbRateLimitsException) IOException(java.io.IOException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage)

Example 17 with MqttMessage

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

the class MqttTransportHandler method processRegularSessionMsg.

void processRegularSessionMsg(ChannelHandlerContext ctx, MqttMessage msg) {
    switch(msg.fixedHeader().messageType()) {
        case PUBLISH:
            processPublish(ctx, (MqttPublishMessage) msg);
            break;
        case SUBSCRIBE:
            processSubscribe(ctx, (MqttSubscribeMessage) msg);
            break;
        case UNSUBSCRIBE:
            processUnsubscribe(ctx, (MqttUnsubscribeMessage) msg);
            break;
        case PINGREQ:
            if (checkConnected(ctx, msg)) {
                ctx.writeAndFlush(new MqttMessage(new MqttFixedHeader(PINGRESP, false, AT_MOST_ONCE, false, 0)));
                transportService.reportActivity(deviceSessionCtx.getSessionInfo());
            }
            break;
        case DISCONNECT:
            ctx.close();
            break;
        case PUBACK:
            int msgId = ((MqttPubAckMessage) msg).variableHeader().messageId();
            TransportProtos.ToDeviceRpcRequestMsg rpcRequest = rpcAwaitingAck.remove(msgId);
            if (rpcRequest != null) {
                transportService.process(deviceSessionCtx.getSessionInfo(), rpcRequest, RpcStatus.DELIVERED, TransportServiceCallback.EMPTY);
            }
            break;
        default:
            break;
    }
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) TransportProtos(org.thingsboard.server.gen.transport.TransportProtos)

Example 18 with MqttMessage

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

the class DeviceSessionCtx method tryProcessQueuedMsgs.

public void tryProcessQueuedMsgs(Consumer<MqttMessage> msgProcessor) {
    while (!msgQueue.isEmpty()) {
        if (msgQueueProcessorLock.tryLock()) {
            try {
                MqttMessage msg;
                while ((msg = msgQueue.poll()) != null) {
                    try {
                        msgQueueSize.decrementAndGet();
                        msgProcessor.accept(msg);
                    } finally {
                        ReferenceCountUtil.safeRelease(msg);
                    }
                }
            } finally {
                msgQueueProcessorLock.unlock();
            }
        } else {
            return;
        }
    }
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage)

Example 19 with MqttMessage

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

the class MqttPingHandler method handlePingReq.

private void handlePingReq(Channel channel) {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0);
    channel.writeAndFlush(new MqttMessage(fixedHeader));
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader)

Example 20 with MqttMessage

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

the class MqttClientImpl method disconnect.

@Override
public void disconnect() {
    disconnected = true;
    if (this.channel != null) {
        MqttMessage message = new MqttMessage(new MqttFixedHeader(MqttMessageType.DISCONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0));
        this.sendAndFlushPacket(message).addListener(future1 -> channel.close());
    }
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader)

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