MqttPingHandler.java 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.Channel;
  18. import io.netty.channel.ChannelFutureListener;
  19. import io.netty.channel.ChannelHandlerContext;
  20. import io.netty.channel.ChannelInboundHandlerAdapter;
  21. import io.netty.handler.codec.mqtt.MqttFixedHeader;
  22. import io.netty.handler.codec.mqtt.MqttMessage;
  23. import io.netty.handler.codec.mqtt.MqttMessageType;
  24. import io.netty.handler.codec.mqtt.MqttQoS;
  25. import io.netty.handler.timeout.IdleStateEvent;
  26. import io.netty.util.ReferenceCountUtil;
  27. import io.netty.util.concurrent.ScheduledFuture;
  28. import java.util.concurrent.TimeUnit;
  29. final class MqttPingHandler extends ChannelInboundHandlerAdapter {
  30. private final int keepaliveSeconds;
  31. private ScheduledFuture<?> pingRespTimeout;
  32. MqttPingHandler(int keepaliveSeconds) {
  33. this.keepaliveSeconds = keepaliveSeconds;
  34. }
  35. @Override
  36. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  37. if (!(msg instanceof MqttMessage)) {
  38. ctx.fireChannelRead(msg);
  39. return;
  40. }
  41. MqttMessage message = (MqttMessage) msg;
  42. if(message.fixedHeader().messageType() == MqttMessageType.PINGREQ){
  43. this.handlePingReq(ctx.channel());
  44. } else if(message.fixedHeader().messageType() == MqttMessageType.PINGRESP){
  45. this.handlePingResp();
  46. }else{
  47. ctx.fireChannelRead(ReferenceCountUtil.retain(msg));
  48. }
  49. }
  50. @Override
  51. public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
  52. super.userEventTriggered(ctx, evt);
  53. if(evt instanceof IdleStateEvent){
  54. IdleStateEvent event = (IdleStateEvent) evt;
  55. switch(event.state()){
  56. case READER_IDLE:
  57. break;
  58. case WRITER_IDLE:
  59. this.sendPingReq(ctx.channel());
  60. break;
  61. }
  62. }
  63. }
  64. private void sendPingReq(Channel channel){
  65. MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PINGREQ, false, MqttQoS.AT_MOST_ONCE, false, 0);
  66. channel.writeAndFlush(new MqttMessage(fixedHeader));
  67. if(this.pingRespTimeout != null){
  68. this.pingRespTimeout = channel.eventLoop().schedule(() -> {
  69. MqttFixedHeader fixedHeader2 = new MqttFixedHeader(MqttMessageType.DISCONNECT, false, MqttQoS.AT_MOST_ONCE, false, 0);
  70. channel.writeAndFlush(new MqttMessage(fixedHeader2)).addListener(ChannelFutureListener.CLOSE);
  71. //TODO: what do when the connection is closed ?
  72. }, this.keepaliveSeconds, TimeUnit.SECONDS);
  73. }
  74. }
  75. private void handlePingReq(Channel channel){
  76. MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0);
  77. channel.writeAndFlush(new MqttMessage(fixedHeader));
  78. }
  79. private void handlePingResp(){
  80. if(this.pingRespTimeout != null && !this.pingRespTimeout.isCancelled() && !this.pingRespTimeout.isDone()){
  81. this.pingRespTimeout.cancel(true);
  82. this.pingRespTimeout = null;
  83. }
  84. }
  85. }