Search in sources :

Example 1 with MqttQoS

use of io.netty.handler.codec.mqtt.MqttQoS in project vertx-examples by vert-x3.

the class Server method start.

@Override
public void start() throws Exception {
    MqttServer mqttServer = MqttServer.create(vertx);
    mqttServer.endpointHandler(endpoint -> {
        // shows main connect info
        System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, clean session = " + endpoint.isCleanSession());
        if (endpoint.auth() != null) {
            System.out.println("[username = " + endpoint.auth().userName() + ", password = " + endpoint.auth().password() + "]");
        }
        if (endpoint.will() != null) {
            System.out.println("[will flag = " + endpoint.will().isWillFlag() + " topic = " + endpoint.will().willTopic() + " msg = " + endpoint.will().willMessage() + " QoS = " + endpoint.will().willQos() + " isRetain = " + endpoint.will().isWillRetain() + "]");
        }
        System.out.println("[keep alive timeout = " + endpoint.keepAliveTimeSeconds() + "]");
        // accept connection from the remote client
        endpoint.accept(false);
        // handling requests for subscriptions
        endpoint.subscribeHandler(subscribe -> {
            List<MqttQoS> grantedQosLevels = new ArrayList<>();
            for (MqttTopicSubscription s : subscribe.topicSubscriptions()) {
                System.out.println("Subscription for " + s.topicName() + " with QoS " + s.qualityOfService());
                grantedQosLevels.add(s.qualityOfService());
            }
            // ack the subscriptions request
            endpoint.subscribeAcknowledge(subscribe.messageId(), grantedQosLevels);
            // just as example, publish a message on the first topic with requested QoS
            endpoint.publish(subscribe.topicSubscriptions().get(0).topicName(), Buffer.buffer("Hello from the Vert.x MQTT server"), subscribe.topicSubscriptions().get(0).qualityOfService(), false, false);
            // specifing handlers for handling QoS 1 and 2
            endpoint.publishAcknowledgeHandler(messageId -> {
                System.out.println("Received ack for message = " + messageId);
            }).publishReceivedHandler(messageId -> {
                endpoint.publishRelease(messageId);
            }).publishCompletionHandler(messageId -> {
                System.out.println("Received ack for message = " + messageId);
            });
        });
        // handling requests for unsubscriptions
        endpoint.unsubscribeHandler(unsubscribe -> {
            for (String t : unsubscribe.topics()) {
                System.out.println("Unsubscription for " + t);
            }
            // ack the subscriptions request
            endpoint.unsubscribeAcknowledge(unsubscribe.messageId());
        });
        // handling ping from client
        endpoint.pingHandler(v -> {
            System.out.println("Ping received from client");
        });
        // handling disconnect message
        endpoint.disconnectHandler(v -> {
            System.out.println("Received disconnect from client");
        });
        // handling closing connection
        endpoint.closeHandler(v -> {
            System.out.println("Connection closed");
        });
        // handling incoming published messages
        endpoint.publishHandler(message -> {
            System.out.println("Just received message on [" + message.topicName() + "] payload [" + message.payload() + "] with QoS [" + message.qosLevel() + "]");
            if (message.qosLevel() == MqttQoS.AT_LEAST_ONCE) {
                endpoint.publishAcknowledge(message.messageId());
            } else if (message.qosLevel() == MqttQoS.EXACTLY_ONCE) {
                endpoint.publishReceived(message.messageId());
            }
        }).publishReleaseHandler(messageId -> {
            endpoint.publishComplete(messageId);
        });
    }).listen(1883, "0.0.0.0", ar -> {
        if (ar.succeeded()) {
            System.out.println("MQTT server is listening on port " + mqttServer.actualPort());
        } else {
            System.err.println("Error on starting the server" + ar.cause().getMessage());
        }
    });
}
Also used : MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) MqttTopicSubscription(io.vertx.mqtt.MqttTopicSubscription) AbstractVerticle(io.vertx.core.AbstractVerticle) Runner(io.vertx.example.mqtt.util.Runner) MqttServer(io.vertx.mqtt.MqttServer) ArrayList(java.util.ArrayList) MqttTopicSubscription(io.vertx.mqtt.MqttTopicSubscription) MqttServer(io.vertx.mqtt.MqttServer) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with MqttQoS

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

the class MQTTProtocolHandler method sendPublishProtocolControlMessage.

void sendPublishProtocolControlMessage(int messageId, MqttMessageType messageType) {
    MqttQoS qos = (messageType == MqttMessageType.PUBREL) ? MqttQoS.AT_LEAST_ONCE : MqttQoS.AT_MOST_ONCE;
    MqttFixedHeader fixedHeader = new // Spec requires 01 in header for rel
    MqttFixedHeader(// Spec requires 01 in header for rel
    messageType, // Spec requires 01 in header for rel
    false, // Spec requires 01 in header for rel
    qos, false, 0);
    MqttPubAckMessage rel = new MqttPubAckMessage(fixedHeader, MqttMessageIdVariableHeader.from(messageId));
    sendToClient(rel);
}
Also used : MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttPubAckMessage(io.netty.handler.codec.mqtt.MqttPubAckMessage) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS)

Example 3 with MqttQoS

use of io.netty.handler.codec.mqtt.MqttQoS in project vertx-openshift-it by cescoffier.

the class MqttBroker method configureSubscribeHandler.

private void configureSubscribeHandler(MqttEndpoint endpoint) {
    endpoint.subscribeHandler(subscribe -> {
        List<MqttQoS> grantedQosLevels = new ArrayList<>();
        for (MqttTopicSubscription s : subscribe.topicSubscriptions()) {
            System.out.println("Subscription for " + s.topicName() + " with QoS " + s.qualityOfService());
            grantedQosLevels.add(s.qualityOfService());
        }
        endpoint.subscribeAcknowledge(subscribe.messageId(), grantedQosLevels);
        endpoint.publish(subscribe.topicSubscriptions().get(0).topicName(), Buffer.buffer("Hello from the Vert.x MQTT server!"), subscribe.topicSubscriptions().get(0).qualityOfService(), false, false);
        endpoint.publishAcknowledgeHandler(messageId -> System.out.println("Received ack for message = " + messageId)).publishReceivedHandler(endpoint::publishRelease).publishCompletionHandler(messageId -> System.out.println("Received ack for message = " + messageId));
    });
}
Also used : MqttTopicSubscription(io.vertx.mqtt.MqttTopicSubscription) ArrayList(java.util.ArrayList) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS)

Example 4 with MqttQoS

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

the class MqttTransportHandler method processSubscribe.

private void processSubscribe(ChannelHandlerContext ctx, MqttSubscribeMessage mqttMsg) {
    if (!checkConnected(ctx)) {
        return;
    }
    log.trace("[{}] Processing subscription [{}]!", sessionId, mqttMsg.variableHeader().messageId());
    List<Integer> grantedQoSList = new ArrayList<>();
    for (MqttTopicSubscription subscription : mqttMsg.payload().topicSubscriptions()) {
        String topicName = subscription.topicName();
        // TODO: handle this qos level.
        MqttQoS reqQoS = subscription.qualityOfService();
        try {
            if (topicName.equals(DEVICE_ATTRIBUTES_TOPIC)) {
                AdaptorToSessionActorMsg msg = adaptor.convertToActorMsg(deviceSessionCtx, SUBSCRIBE_ATTRIBUTES_REQUEST, mqttMsg);
                processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), msg));
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else if (topicName.equals(DEVICE_RPC_REQUESTS_SUB_TOPIC)) {
                AdaptorToSessionActorMsg msg = adaptor.convertToActorMsg(deviceSessionCtx, SUBSCRIBE_RPC_COMMANDS_REQUEST, mqttMsg);
                processor.process(new BasicToDeviceActorSessionMsg(deviceSessionCtx.getDevice(), msg));
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else if (topicName.equals(DEVICE_RPC_RESPONSE_SUB_TOPIC)) {
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else if (topicName.equals(DEVICE_ATTRIBUTES_RESPONSES_TOPIC)) {
                deviceSessionCtx.setAllowAttributeResponses();
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else if (topicName.equals(GATEWAY_ATTRIBUTES_TOPIC)) {
                grantedQoSList.add(getMinSupportedQos(reqQoS));
            } else {
                log.warn("[{}] Failed to subscribe to [{}][{}]", sessionId, topicName, reqQoS);
                grantedQoSList.add(FAILURE.value());
            }
        } catch (AdaptorException e) {
            log.warn("[{}] Failed to subscribe to [{}][{}]", sessionId, topicName, reqQoS);
            grantedQoSList.add(FAILURE.value());
        }
    }
    ctx.writeAndFlush(createSubAckMessage(mqttMsg.variableHeader().messageId(), grantedQoSList));
}
Also used : AdaptorException(org.thingsboard.server.common.transport.adaptor.AdaptorException) AdaptorToSessionActorMsg(org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg) BasicToDeviceActorSessionMsg(org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg) ArrayList(java.util.ArrayList) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS)

Aggregations

MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)4 ArrayList (java.util.ArrayList)3 MqttTopicSubscription (io.vertx.mqtt.MqttTopicSubscription)2 MqttFixedHeader (io.netty.handler.codec.mqtt.MqttFixedHeader)1 MqttPubAckMessage (io.netty.handler.codec.mqtt.MqttPubAckMessage)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Buffer (io.vertx.core.buffer.Buffer)1 Runner (io.vertx.example.mqtt.util.Runner)1 MqttServer (io.vertx.mqtt.MqttServer)1 List (java.util.List)1 AdaptorToSessionActorMsg (org.thingsboard.server.common.msg.session.AdaptorToSessionActorMsg)1 BasicToDeviceActorSessionMsg (org.thingsboard.server.common.msg.session.BasicToDeviceActorSessionMsg)1 AdaptorException (org.thingsboard.server.common.transport.adaptor.AdaptorException)1