MqttPendingUnsubscription.java 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /**
  2. * Copyright © 2016-2020 The Thingsboard Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.shuhe.mqtt;
  17. import io.netty.channel.EventLoop;
  18. import io.netty.handler.codec.mqtt.MqttUnsubscribeMessage;
  19. import io.netty.util.concurrent.Promise;
  20. import java.util.function.Consumer;
  21. final class MqttPendingUnsubscription {
  22. private final Promise<Void> future;
  23. private final String topic;
  24. private final com.shuhe.mqtt.RetransmissionHandler<MqttUnsubscribeMessage> retransmissionHandler = new com.shuhe.mqtt.RetransmissionHandler<>();
  25. MqttPendingUnsubscription(Promise<Void> future, String topic, MqttUnsubscribeMessage unsubscribeMessage) {
  26. this.future = future;
  27. this.topic = topic;
  28. this.retransmissionHandler.setOriginalMessage(unsubscribeMessage);
  29. }
  30. Promise<Void> getFuture() {
  31. return future;
  32. }
  33. String getTopic() {
  34. return topic;
  35. }
  36. void startRetransmissionTimer(EventLoop eventLoop, Consumer<Object> sendPacket) {
  37. this.retransmissionHandler.setHandle((fixedHeader, originalMessage) ->
  38. sendPacket.accept(new MqttUnsubscribeMessage(fixedHeader, originalMessage.variableHeader(), originalMessage.payload())));
  39. this.retransmissionHandler.start(eventLoop);
  40. }
  41. void onUnsubackReceived(){
  42. this.retransmissionHandler.stop();
  43. }
  44. }