Search in sources :

Example 36 with MqttFixedHeader

use of io.netty.handler.codec.mqtt.MqttFixedHeader 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 37 with MqttFixedHeader

use of io.netty.handler.codec.mqtt.MqttFixedHeader 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 38 with MqttFixedHeader

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

the class JsonMqttAdaptor method createMqttPublishMsg.

protected MqttPublishMessage createMqttPublishMsg(MqttDeviceAwareSessionContext ctx, String topic, JsonElement json) {
    MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, ctx.getQoSForTopic(topic), false, 0);
    MqttPublishVariableHeader header = new MqttPublishVariableHeader(topic, ctx.nextMsgId());
    ByteBuf payload = ALLOCATOR.buffer();
    payload.writeBytes(json.toString().getBytes(UTF8));
    return new MqttPublishMessage(mqttFixedHeader, header, payload);
}
Also used : MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader) ByteBuf(io.netty.buffer.ByteBuf)

Example 39 with MqttFixedHeader

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

the class MqttTransportHandlerTest method getMqttPublishMessage.

MqttPublishMessage getMqttPublishMessage() {
    MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, true, MqttQoS.AT_LEAST_ONCE, false, 123);
    MqttPublishVariableHeader variableHeader = new MqttPublishVariableHeader("v1/gateway/telemetry", packedId.incrementAndGet());
    ByteBuf payload = new EmptyByteBuf(new PooledByteBufAllocator());
    return new MqttPublishMessage(mqttFixedHeader, variableHeader, payload);
}
Also used : MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader) ByteBuf(io.netty.buffer.ByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) PooledByteBufAllocator(io.netty.buffer.PooledByteBufAllocator)

Example 40 with MqttFixedHeader

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

the class MqttTransportHandlerTest method getMqttConnectMessage.

MqttConnectMessage getMqttConnectMessage() {
    MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, true, MqttQoS.AT_LEAST_ONCE, false, 123);
    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader("device", packedId.incrementAndGet(), true, true, true, 1, true, false, 60);
    MqttConnectPayload payload = new MqttConnectPayload("clientId", "topic", "message".getBytes(StandardCharsets.UTF_8), "username", "password".getBytes(StandardCharsets.UTF_8));
    return new MqttConnectMessage(mqttFixedHeader, variableHeader, payload);
}
Also used : MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttConnectPayload(io.netty.handler.codec.mqtt.MqttConnectPayload) MqttConnectVariableHeader(io.netty.handler.codec.mqtt.MqttConnectVariableHeader)

Aggregations

MqttFixedHeader (io.netty.handler.codec.mqtt.MqttFixedHeader)43 MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)13 MqttPublishVariableHeader (io.netty.handler.codec.mqtt.MqttPublishVariableHeader)12 MqttMessage (io.netty.handler.codec.mqtt.MqttMessage)11 MqttMessageIdVariableHeader (io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader)11 ByteBuf (io.netty.buffer.ByteBuf)6 MqttConnectMessage (io.netty.handler.codec.mqtt.MqttConnectMessage)6 MqttConnectPayload (io.netty.handler.codec.mqtt.MqttConnectPayload)6 MqttConnectVariableHeader (io.netty.handler.codec.mqtt.MqttConnectVariableHeader)6 MqttConnAckMessage (io.netty.handler.codec.mqtt.MqttConnAckMessage)4 MqttConnAckVariableHeader (io.netty.handler.codec.mqtt.MqttConnAckVariableHeader)4 MqttSubscribeMessage (io.netty.handler.codec.mqtt.MqttSubscribeMessage)4 MqttSubscribePayload (io.netty.handler.codec.mqtt.MqttSubscribePayload)4 MqttSubAckMessage (io.netty.handler.codec.mqtt.MqttSubAckMessage)3 MqttUnsubscribeMessage (io.netty.handler.codec.mqtt.MqttUnsubscribeMessage)3 MqttUnsubscribePayload (io.netty.handler.codec.mqtt.MqttUnsubscribePayload)3 ChannelFuture (io.netty.channel.ChannelFuture)2 MqttPubAckMessage (io.netty.handler.codec.mqtt.MqttPubAckMessage)2 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)2 MqttSubAckPayload (io.netty.handler.codec.mqtt.MqttSubAckPayload)2