use of org.apache.rocketmq.iot.protocol.mqtt.data.Subscription in project rocketmq-externals by apache.
the class ConsumeMessageIntegrationTest method test.
@Test
public void test() {
/* handle the CONNECT message */
MqttConnectMessage connectMessage = getConnectMessage();
embeddedChannel.writeInbound(connectMessage);
MqttConnAckMessage connAckMessage = embeddedChannel.readOutbound();
Client client = clientManager.get(embeddedChannel);
Assert.assertNotNull(client);
Assert.assertTrue(consumerId.equals(client.getId()));
Assert.assertEquals(consumerId, client.getId());
Assert.assertTrue(client.isConnected());
Assert.assertEquals(embeddedChannel, client.getCtx().channel());
Assert.assertTrue(client.isConnected());
Assert.assertTrue(embeddedChannel.isOpen());
Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, connAckMessage.variableHeader().connectReturnCode());
embeddedChannel.releaseInbound();
/* handle the SUBSCRIBE message */
MqttSubscribeMessage subscribeMessage = getMqttSubscribeMessage();
embeddedChannel.writeInbound(subscribeMessage);
List<Subscription> subscriptions = subscriptionStore.get(topicName);
List<String> topics = subscriptionStore.getTopics(topicName);
MqttSubAckMessage ackMessage = embeddedChannel.readOutbound();
Assert.assertNotNull(subscriptionStore.get(topicName));
Assert.assertNotNull(subscriptions);
Assert.assertNotNull(ackMessage);
Assert.assertEquals(subscribePacketId, ackMessage.variableHeader().messageId());
Assert.assertTrue(topics.contains(topicName));
Assert.assertTrue(isClientInSubscriptions(subscriptions, client));
embeddedChannel.releaseInbound();
/* send message to the client */
MqttPublishMessage publishMessage = getMqttPublishMessage();
byte[] expectedPayload = new byte[publishMessage.payload().readableBytes()];
publishMessage.payload().getBytes(0, expectedPayload);
embeddedChannel.writeInbound(publishMessage);
MqttPublishMessage receivedMessage = embeddedChannel.readOutbound();
byte[] actualPayload = new byte[receivedMessage.payload().readableBytes()];
receivedMessage.payload().getBytes(0, actualPayload);
Assert.assertEquals(publishMessage.variableHeader().packetId(), receivedMessage.variableHeader().packetId());
Assert.assertEquals(publishMessage.variableHeader().topicName(), receivedMessage.variableHeader().topicName());
Assert.assertArrayEquals(expectedPayload, actualPayload);
}
use of org.apache.rocketmq.iot.protocol.mqtt.data.Subscription in project rocketmq-externals by apache.
the class MqttMessageForwarder method handleMessage.
/**
* handle PUBLISH message from client
*
* @param message
* @return whether the message is handled successfully
*/
@Override
public void handleMessage(Message message) {
MqttPublishMessage publishMessage = (MqttPublishMessage) message.getPayload();
String topic = publishMessage.variableHeader().topicName();
if (!subscriptionStore.hasTopic(topic)) {
subscriptionStore.addTopic(topic);
}
for (Subscription subscription : subscriptionStore.get(publishMessage.variableHeader().topicName())) {
ByteBuf buf = Unpooled.buffer();
byte[] bytes = new byte[publishMessage.payload().readableBytes()];
publishMessage.payload().getBytes(0, bytes);
buf.writeBytes(bytes);
MqttPublishMessage msg = new MqttPublishMessage(new MqttFixedHeader(MqttMessageType.PUBLISH, publishMessage.fixedHeader().isDup(), MqttQoS.valueOf(MessageUtil.actualQos(publishMessage.fixedHeader().qosLevel().value())), publishMessage.fixedHeader().isRetain(), publishMessage.fixedHeader().remainingLength()), new MqttPublishVariableHeader(publishMessage.variableHeader().topicName(), publishMessage.variableHeader().packetId()), buf);
subscription.getClient().getCtx().writeAndFlush(msg);
}
}
Aggregations