Example usage for io.netty.handler.codec.mqtt MqttSubscribeMessage MqttSubscribeMessage

List of usage examples for io.netty.handler.codec.mqtt MqttSubscribeMessage MqttSubscribeMessage

Introduction

In this page you can find the example usage for io.netty.handler.codec.mqtt MqttSubscribeMessage MqttSubscribeMessage.

Prototype

public MqttSubscribeMessage(MqttFixedHeader mqttFixedHeader, MqttMessageIdVariableHeader variableHeader,
            MqttSubscribePayload payload) 

Source Link

Usage

From source file:net.anyflow.lannister.message.MessageFactory.java

License:Apache License

public static MqttSubscribeMessage subscribe(int messageId, MqttTopicSubscription... topicSubscriptions) {
    int topicNameSize = 0;
    int topicCount = topicSubscriptions.length;

    for (MqttTopicSubscription item : topicSubscriptions) {
        topicNameSize += item.topicName().getBytes(CharsetUtil.UTF_8).length;
    }/*from w  ww  .j  a  v a  2  s .c o  m*/

    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.SUBSCRIBE, false, MqttQoS.AT_LEAST_ONCE,
            false, 2 + topicNameSize + topicCount);
    MqttMessageIdVariableHeader variableHeader = MqttMessageIdVariableHeader.from(messageId);
    MqttSubscribePayload payload = new MqttSubscribePayload(Lists.newArrayList(topicSubscriptions));

    return new MqttSubscribeMessage(fixedHeader, variableHeader, payload);
}

From source file:org.thingsboard.mqtt.MqttClientImpl.java

License:Apache License

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();
        }//from  w w w .ja v  a  2 s  .  c o m
    }
    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);
    pendingSubscription.addHandler(handler, once);
    this.pendingSubscriptions.put(variableHeader.messageId(), pendingSubscription);
    this.pendingSubscribeTopics.add(topic);
    pendingSubscription.setSent(this.sendAndFlushPacket(message) != null); //If not sent, we will send it when the connection is opened

    pendingSubscription.startRetransmitTimer(this.eventLoop.next(), this::sendAndFlushPacket);

    return future;
}

From source file:org.thingsboard.mqtt.MqttPendingSubscription.java

License:Apache License

void startRetransmitTimer(EventLoop eventLoop, Consumer<Object> sendPacket) {
    if (this.sent) { //If the packet is sent, we can start the retransmit timer
        this.retransmissionHandler.setHandle(
                (fixedHeader, originalMessage) -> sendPacket.accept(new MqttSubscribeMessage(fixedHeader,
                        originalMessage.variableHeader(), originalMessage.payload())));
        this.retransmissionHandler.start(eventLoop);
    }/*from  www  .j a  va 2 s . com*/
}