Search in sources :

Example 1 with Subscription

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);
}
Also used : MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) MqttSubAckMessage(io.netty.handler.codec.mqtt.MqttSubAckMessage) MqttConnAckMessage(io.netty.handler.codec.mqtt.MqttConnAckMessage) Client(org.apache.rocketmq.iot.connection.client.Client) MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) Subscription(org.apache.rocketmq.iot.protocol.mqtt.data.Subscription) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) Test(org.junit.Test)

Example 2 with Subscription

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);
    }
}
Also used : MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) MqttPublishVariableHeader(io.netty.handler.codec.mqtt.MqttPublishVariableHeader) Subscription(org.apache.rocketmq.iot.protocol.mqtt.data.Subscription) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)2 Subscription (org.apache.rocketmq.iot.protocol.mqtt.data.Subscription)2 ByteBuf (io.netty.buffer.ByteBuf)1 MqttConnAckMessage (io.netty.handler.codec.mqtt.MqttConnAckMessage)1 MqttConnectMessage (io.netty.handler.codec.mqtt.MqttConnectMessage)1 MqttFixedHeader (io.netty.handler.codec.mqtt.MqttFixedHeader)1 MqttPublishVariableHeader (io.netty.handler.codec.mqtt.MqttPublishVariableHeader)1 MqttSubAckMessage (io.netty.handler.codec.mqtt.MqttSubAckMessage)1 MqttSubscribeMessage (io.netty.handler.codec.mqtt.MqttSubscribeMessage)1 MqttTopicSubscription (io.netty.handler.codec.mqtt.MqttTopicSubscription)1 Client (org.apache.rocketmq.iot.connection.client.Client)1 MqttClient (org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)1 Test (org.junit.Test)1