use of org.apache.rocketmq.iot.connection.client.Client in project rocketmq-externals by apache.
the class ProduceMessageIntegrationTest method test.
@Test
public void test() {
/* handle the CONNECT message */
MqttConnectMessage connectMessage = getConnectMessage();
producerChannel.writeInbound(connectMessage);
MqttConnAckMessage connAckMessage = producerChannel.readOutbound();
Client client = clientManager.get(producerChannel);
Assert.assertNotNull(client);
Assert.assertEquals(producerId, client.getId());
Assert.assertEquals(producerChannel, client.getCtx().channel());
Assert.assertTrue(client.isConnected());
Assert.assertTrue(producerChannel.isOpen());
Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, connAckMessage.variableHeader().connectReturnCode());
producerChannel.releaseInbound();
/* handle the PUBLISH message when there is no online Consumers */
MqttPublishMessage publishMessage = getMqttPublishMessage();
producerChannel.writeInbound(publishMessage);
MqttMessage pubackMessage = producerChannel.readOutbound();
/* qos 0 should have no PUBACK message */
Assert.assertNull(pubackMessage);
/*
* the message would be discarded simply because there is no Consumers
* and the topic should be created
* */
Assert.assertTrue(subscriptionStore.hasTopic(topicName));
producerChannel.releaseInbound();
/* handle the PUBLISH message when there are online Consumers */
Mockito.when(subscriptionStore.get(topicName)).thenReturn(mockedSubscriptions);
publishMessage = getMqttPublishMessage();
byte[] publishMessagePayload = new byte[publishMessage.payload().readableBytes()];
publishMessage.payload().getBytes(0, publishMessagePayload);
producerChannel.writeInbound(publishMessage);
MqttPublishMessage forwardedMessage = consumerChannel.readOutbound();
byte[] forwardedMessagePayload = new byte[forwardedMessage.payload().readableBytes()];
forwardedMessage.payload().getBytes(0, forwardedMessagePayload);
Assert.assertNotNull(forwardedMessage);
Assert.assertEquals(publishMessage.variableHeader().topicName(), forwardedMessage.variableHeader().topicName());
Assert.assertEquals(publishMessage.variableHeader().packetId(), forwardedMessage.variableHeader().packetId());
Assert.assertArrayEquals(publishMessagePayload, forwardedMessagePayload);
}
use of org.apache.rocketmq.iot.connection.client.Client in project rocketmq-externals by apache.
the class MqttPingreqMessageHandler method handleMessage.
/**
* handle the PINGREQ message from client
* <ol>
* <li>check client exists</li>
* <li>check client is connected</li>
* <li>generate the PINGRESP message</li>
* <li>send the PINGRESP message to the client</li>
* </ol>
*
* @param message
* @return
*/
@Override
public void handleMessage(Message message) {
Client client = message.getClient();
if (client == null || !client.isConnected()) {
return;
}
MqttMessage pingreqMessage = (MqttMessage) message.getPayload();
MqttMessage pingrespMessage = MessageUtil.getMqttPingrespMessage(pingreqMessage);
client.getCtx().writeAndFlush(pingrespMessage);
}
use of org.apache.rocketmq.iot.connection.client.Client in project rocketmq-externals by apache.
the class MqttConnectionHandler method exceptionCaught.
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
Client client = clientManager.get(ctx.channel());
String clientId = client != null ? client.getId() : "null";
doDisconnect(ctx.channel());
log.debug("clientId:{} netty exception caught from {}", clientId, ctx.channel(), cause);
}
Aggregations