Search in sources :

Example 16 with MqttFixedHeader

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

the class PubSubIntegrationTest method getMqttSubscribeMessage.

private MqttSubscribeMessage getMqttSubscribeMessage() {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(consumerSubscribeId);
    MqttSubscribePayload payload = new MqttSubscribePayload(topicSubscriptions);
    return new MqttSubscribeMessage(fixedHeader, variableHeader, payload);
}
Also used : MqttMessageIdVariableHeader(io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) MqttSubscribePayload(io.netty.handler.codec.mqtt.MqttSubscribePayload)

Example 17 with MqttFixedHeader

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

the class PubSubIntegrationTest method getConnectMessage.

private MqttConnectMessage getConnectMessage(String clientId) {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader("MQTT", 4, false, false, false, MqttQoS.AT_MOST_ONCE.value(), true, true, 60);
    MqttConnectPayload payload = new MqttConnectPayload(clientId, "test-will-topic", "the test client is down".getBytes(), null, null);
    return new MqttConnectMessage(fixedHeader, 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)

Example 18 with MqttFixedHeader

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

the class MqttClientImpl method publish.

/**
 * Publish a message to the given payload, using the given qos and optional retain
 *
 * @param topic   The topic to publish to
 * @param payload The payload to send
 * @param qos     The qos to use while publishing
 * @param retain  true if you want to retain the message on the server, false otherwise
 * @return A future which will be completed when the message is delivered to the server
 */
@Override
public Future<Void> publish(String topic, ByteBuf payload, MqttQoS qos, boolean retain) {
    Promise<Void> future = new DefaultPromise<>(this.eventLoop.next());
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, qos, retain, 0);
    MqttPublishVariableHeader variableHeader = new MqttPublishVariableHeader(topic, getNewMessageId().messageId());
    MqttPublishMessage message = new MqttPublishMessage(fixedHeader, variableHeader, payload);
    MqttPendingPublish pendingPublish = new MqttPendingPublish(variableHeader.packetId(), future, payload.retain(), message, qos, () -> !pendingPublishes.containsKey(variableHeader.packetId()));
    this.pendingPublishes.put(pendingPublish.getMessageId(), pendingPublish);
    ChannelFuture channelFuture = this.sendAndFlushPacket(message);
    if (channelFuture != null) {
        channelFuture.addListener(result -> {
            pendingPublish.setSent(true);
            if (result.cause() != null) {
                pendingPublishes.remove(pendingPublish.getMessageId());
                future.setFailure(result.cause());
            } else {
                if (pendingPublish.isSent() && pendingPublish.getQos() == MqttQoS.AT_MOST_ONCE) {
                    pendingPublishes.remove(pendingPublish.getMessageId());
                    // We don't get an ACK for QOS 0
                    pendingPublish.getFuture().setSuccess(null);
                } else if (pendingPublish.isSent()) {
                    pendingPublish.startPublishRetransmissionTimer(eventLoop.next(), MqttClientImpl.this::sendAndFlushPacket);
                } else {
                    pendingPublishes.remove(pendingPublish.getMessageId());
                }
            }
        });
    } else {
        pendingPublishes.remove(pendingPublish.getMessageId());
    }
    return future;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) DefaultPromise(io.netty.util.concurrent.DefaultPromise) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader)

Example 19 with MqttFixedHeader

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

the class MqttClientImpl method createSubscription.

private Future<Void> createSubscription(String topic, MqttHandler handler, boolean once, MqttQoS qos) {
    if (this.pendingSubscribeTopics.contains(topic)) {
        Optional<Map.Entry<Integer, MqttPendingSubscription>> subscriptionEntry = this.pendingSubscriptions.entrySet().stream().filter((e) -> e.getValue().getTopic().equals(topic)).findAny();
        if (subscriptionEntry.isPresent()) {
            subscriptionEntry.get().getValue().addHandler(handler, once);
            return subscriptionEntry.get().getValue().getFuture();
        }
    }
    if (this.serverSubscriptions.contains(topic)) {
        MqttSubscription subscription = new MqttSubscription(topic, handler, once);
        this.subscriptions.put(topic, subscription);
        this.handlerToSubscribtion.put(handler, subscription);
        return this.channel.newSucceededFuture();
    }
    Promise<Void> future = new DefaultPromise<>(this.eventLoop.next());
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE, false, 0);
    MqttTopicSubscription subscription = new MqttTopicSubscription(topic, qos);
    MqttMessageIdVariableHeader variableHeader = getNewMessageId();
    MqttSubscribePayload payload = new MqttSubscribePayload(Collections.singletonList(subscription));
    MqttSubscribeMessage message = new MqttSubscribeMessage(fixedHeader, variableHeader, payload);
    final MqttPendingSubscription pendingSubscription = new MqttPendingSubscription(future, topic, message, () -> !pendingSubscriptions.containsKey(variableHeader.messageId()));
    pendingSubscription.addHandler(handler, once);
    this.pendingSubscriptions.put(variableHeader.messageId(), pendingSubscription);
    this.pendingSubscribeTopics.add(topic);
    // If not sent, we will send it when the connection is opened
    pendingSubscription.setSent(this.sendAndFlushPacket(message) != null);
    pendingSubscription.startRetransmitTimer(this.eventLoop.next(), this::sendAndFlushPacket);
    return future;
}
Also used : MqttDecoder(io.netty.handler.codec.mqtt.MqttDecoder) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader) MqttMessageType(io.netty.handler.codec.mqtt.MqttMessageType) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttUnsubscribePayload(io.netty.handler.codec.mqtt.MqttUnsubscribePayload) MqttSubscribePayload(io.netty.handler.codec.mqtt.MqttSubscribePayload) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) HashMultimap(com.google.common.collect.HashMultimap) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MqttEncoder(io.netty.handler.codec.mqtt.MqttEncoder) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) SocketChannel(io.netty.channel.socket.SocketChannel) ImmutableSet(com.google.common.collect.ImmutableSet) EventLoopGroup(io.netty.channel.EventLoopGroup) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) ChannelInitializer(io.netty.channel.ChannelInitializer) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) SslContext(io.netty.handler.ssl.SslContext) Promise(io.netty.util.concurrent.Promise) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) Bootstrap(io.netty.bootstrap.Bootstrap) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) DefaultPromise(io.netty.util.concurrent.DefaultPromise) MqttUnsubscribeMessage(io.netty.handler.codec.mqtt.MqttUnsubscribeMessage) Optional(java.util.Optional) MqttMessageIdVariableHeader(io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) Future(io.netty.util.concurrent.Future) Collections(java.util.Collections) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) MqttMessageIdVariableHeader(io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader) DefaultPromise(io.netty.util.concurrent.DefaultPromise) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) MqttSubscribePayload(io.netty.handler.codec.mqtt.MqttSubscribePayload)

Example 20 with MqttFixedHeader

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

the class MqttPingHandler method sendPingReq.

private void sendPingReq(Channel channel) {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PINGREQ, false, MqttQoS.AT_MOST_ONCE, false, 0);
    channel.writeAndFlush(new MqttMessage(fixedHeader));
    if (this.pingRespTimeout != null) {
        this.pingRespTimeout = channel.eventLoop().schedule(() -> {
            MqttFixedHeader fixedHeader2 = new MqttFixedHeader(MqttMessageType.DISCONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
            channel.writeAndFlush(new MqttMessage(fixedHeader2)).addListener(ChannelFutureListener.CLOSE);
        // TODO: what do when the connection is closed ?
        }, this.keepaliveSeconds, TimeUnit.SECONDS);
    }
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader)

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