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);
}
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);
}
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;
}
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;
}
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);
}
}
Aggregations