未验证 提交 b2169653 编写于 作者: H Heng Du 提交者: GitHub

Merge pull request #1116 from Aaron-He/mqtt

Implementation of handling MQTT client PINGREQ
...@@ -17,14 +17,22 @@ ...@@ -17,14 +17,22 @@
package org.apache.rocketmq.mqtt.mqtthandler.impl; package org.apache.rocketmq.mqtt.mqtthandler.impl;
import io.netty.handler.codec.mqtt.MqttConnectMessage;
import io.netty.handler.codec.mqtt.MqttMessage; import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttPubAckMessage;
import org.apache.rocketmq.common.client.Client;
import org.apache.rocketmq.common.constant.LoggerName; import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.common.protocol.ResponseCode;
import org.apache.rocketmq.logging.InternalLogger; import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory; import org.apache.rocketmq.logging.InternalLoggerFactory;
import org.apache.rocketmq.mqtt.client.IOTClientManagerImpl;
import org.apache.rocketmq.mqtt.exception.WrongMessageTypeException;
import org.apache.rocketmq.mqtt.mqtthandler.MessageHandler; import org.apache.rocketmq.mqtt.mqtthandler.MessageHandler;
import org.apache.rocketmq.mqtt.processor.DefaultMqttMessageProcessor; import org.apache.rocketmq.mqtt.processor.DefaultMqttMessageProcessor;
import org.apache.rocketmq.remoting.RemotingChannel; import org.apache.rocketmq.remoting.RemotingChannel;
import org.apache.rocketmq.remoting.protocol.RemotingCommand; import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.remoting.transport.mqtt.MqttHeader;
public class MqttPingreqMessageHandler implements MessageHandler { public class MqttPingreqMessageHandler implements MessageHandler {
private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.MQTT_LOGGER_NAME); private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.MQTT_LOGGER_NAME);
...@@ -43,6 +51,24 @@ public class MqttPingreqMessageHandler implements MessageHandler { ...@@ -43,6 +51,24 @@ public class MqttPingreqMessageHandler implements MessageHandler {
*/ */
@Override @Override
public RemotingCommand handleMessage(MqttMessage message, RemotingChannel remotingChannel) { public RemotingCommand handleMessage(MqttMessage message, RemotingChannel remotingChannel) {
return null; IOTClientManagerImpl iotClientManager = (IOTClientManagerImpl) defaultMqttMessageProcessor.getIotClientManager();
Client client = iotClientManager.getClient(IOTClientManagerImpl.IOT_GROUP, remotingChannel);
log.debug("Handle MQTT client: {} Pingreq.", client.getClientId());
RemotingCommand response = RemotingCommand.createResponseCommand(MqttHeader.class);
if (client != null && client.isConnected()) {
client.setLastUpdateTimestamp(System.currentTimeMillis());
MqttHeader mqttHeader = (MqttHeader) response.readCustomHeader();
mqttHeader.setMessageType(MqttMessageType.PINGRESP.value());
mqttHeader.setDup(false);
mqttHeader.setQosLevel(0);
mqttHeader.setRetain(false);
mqttHeader.setRemainingLength(0);
response.setCode(ResponseCode.SUCCESS);
response.setRemark(null);
return response;
}
response.setCode(ResponseCode.SYSTEM_ERROR);
response.setRemark("MQTT Client is null or not connected");
return response;
} }
} }
...@@ -132,6 +132,7 @@ public class DefaultMqttMessageProcessor implements RequestProcessor { ...@@ -132,6 +132,7 @@ public class DefaultMqttMessageProcessor implements RequestProcessor {
break; break;
case UNSUBSCRIBE: case UNSUBSCRIBE:
case PINGREQ: case PINGREQ:
break;
case DISCONNECT: case DISCONNECT:
} }
return type2handler.get(MqttMessageType.valueOf(mqttHeader.getMessageType())).handleMessage(mqttMessage, remotingChannel); return type2handler.get(MqttMessageType.valueOf(mqttHeader.getMessageType())).handleMessage(mqttMessage, remotingChannel);
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.rocketmq.mqtt;
import io.netty.handler.codec.mqtt.MqttMessage;
import org.apache.rocketmq.common.client.Client;
import org.apache.rocketmq.common.protocol.ResponseCode;
import org.apache.rocketmq.mqtt.client.IOTClientManagerImpl;
import org.apache.rocketmq.mqtt.mqtthandler.impl.MqttPingreqMessageHandler;
import org.apache.rocketmq.mqtt.processor.DefaultMqttMessageProcessor;
import org.apache.rocketmq.remoting.RemotingChannel;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
@RunWith(MockitoJUnitRunner.class)
public class MqttPingreqMessageHandlerTest {
@Mock
private RemotingChannel remotingChannel;
@Mock
private IOTClientManagerImpl iotClientManager;
@Mock
private MqttMessage mqttMessage;
@Mock
private Client client;
@Mock
private DefaultMqttMessageProcessor processor;
private MqttPingreqMessageHandler mqttPingreqMessageHandler;
@Before
public void init() {
mqttPingreqMessageHandler = new MqttPingreqMessageHandler(processor);
when(processor.getIotClientManager()).thenReturn(iotClientManager);
when(iotClientManager.getClient(IOTClientManagerImpl.IOT_GROUP, remotingChannel)).thenReturn(client);
when(client.getClientId()).thenReturn("Mock Client");
}
@Test
public void testHandlerMessageReturnResp() {
when(client.isConnected()).thenReturn(true);
RemotingCommand response = mqttPingreqMessageHandler.handleMessage(mqttMessage, remotingChannel);
verify(client).setLastUpdateTimestamp(anyLong());
assertEquals(ResponseCode.SUCCESS, response.getCode());
}
@Test
public void testHandlerMessageReturnNull() {
when(client.isConnected()).thenReturn(false);
RemotingCommand response = mqttPingreqMessageHandler.handleMessage(mqttMessage, remotingChannel);
assertEquals(ResponseCode.SYSTEM_ERROR,response.getCode());
}
}
...@@ -38,8 +38,8 @@ public class EncodeDecodeDispatcher { ...@@ -38,8 +38,8 @@ public class EncodeDecodeDispatcher {
encodeDecodeDispatcher.put(MqttMessageType.SUBACK, new MqttSubackEncodeDecode()); encodeDecodeDispatcher.put(MqttMessageType.SUBACK, new MqttSubackEncodeDecode());
encodeDecodeDispatcher.put(MqttMessageType.UNSUBSCRIBE, new MqttUnSubscribeEncodeDecode()); encodeDecodeDispatcher.put(MqttMessageType.UNSUBSCRIBE, new MqttUnSubscribeEncodeDecode());
encodeDecodeDispatcher.put(MqttMessageType.UNSUBACK, new MqttUnSubackEncodeDecode()); encodeDecodeDispatcher.put(MqttMessageType.UNSUBACK, new MqttUnSubackEncodeDecode());
encodeDecodeDispatcher.put(MqttMessageType.PINGREQ, null); encodeDecodeDispatcher.put(MqttMessageType.PINGREQ, new MqttPingReqEncodeDecode() );
encodeDecodeDispatcher.put(MqttMessageType.PINGRESP, null); encodeDecodeDispatcher.put(MqttMessageType.PINGRESP, new MqttPingRespEncodeDecode());
} }
public static Map<MqttMessageType, Message2MessageEncodeDecode> getEncodeDecodeDispatcher() { public static Map<MqttMessageType, Message2MessageEncodeDecode> getEncodeDecodeDispatcher() {
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.rocketmq.remoting.transport.mqtt.dispatcher;
import io.netty.handler.codec.mqtt.MqttFixedHeader;
import io.netty.handler.codec.mqtt.MqttMessage;
import java.io.UnsupportedEncodingException;
import org.apache.rocketmq.remoting.exception.RemotingCommandException;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.remoting.transport.mqtt.MqttHeader;
public class MqttPingReqEncodeDecode implements Message2MessageEncodeDecode {
@Override
public RemotingCommand decode(MqttMessage mqttMessage) {
MqttHeader mqttHeader = new MqttHeader();
MqttFixedHeader mqttFixedHeader = mqttMessage.fixedHeader();
mqttHeader.setMessageType(mqttFixedHeader.messageType().value());
mqttHeader.setDup(mqttFixedHeader.isDup());
mqttHeader.setQosLevel(mqttFixedHeader.qosLevel().value());
mqttHeader.setRetain(mqttFixedHeader.isRetain());
mqttHeader.setRemainingLength(mqttFixedHeader.remainingLength());
return RemotingCommand.createRequestCommand(1000, mqttHeader);
}
@Override
public MqttMessage encode(
RemotingCommand remotingCommand) throws RemotingCommandException, UnsupportedEncodingException {
return null;
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.rocketmq.remoting.transport.mqtt.dispatcher;
import io.netty.handler.codec.mqtt.MqttFixedHeader;
import io.netty.handler.codec.mqtt.MqttMessage;
import io.netty.handler.codec.mqtt.MqttMessageType;
import io.netty.handler.codec.mqtt.MqttQoS;
import java.io.UnsupportedEncodingException;
import org.apache.rocketmq.remoting.exception.RemotingCommandException;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.remoting.transport.mqtt.MqttHeader;
public class MqttPingRespEncodeDecode implements Message2MessageEncodeDecode {
@Override
public RemotingCommand decode(MqttMessage mqttMessage) {
return null;
}
@Override
public MqttMessage encode(
RemotingCommand remotingCommand) throws RemotingCommandException, UnsupportedEncodingException {
MqttHeader mqttHeader = (MqttHeader) remotingCommand.getCustomHeader();
return new MqttMessage(new MqttFixedHeader(MqttMessageType.PINGRESP, mqttHeader.isDup(), MqttQoS.valueOf(mqttHeader.getQosLevel()), mqttHeader.isRetain(), mqttHeader.getRemainingLength()));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册