Search in sources :

Example 6 with MqttClient

use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.

the class MqttConnectionHandler method doDisconnect.

/**
 * disconnect the channel, save the Subscription of the client which the channel belongs to if CleanSession
 * if set to <b>false</b>, otherwise discard them
 *
 * @param channel
 */
private void doDisconnect(Channel channel) {
    if (channel == null) {
        return;
    }
    MqttClient client = (MqttClient) clientManager.get(channel);
    if (client != null) {
        if (client.isCleanSession()) {
            subscriptionStore.getTopicFilters(client.getId()).forEach(filter -> {
                subscriptionStore.getTopics(filter).forEach(topic -> {
                    subscriptionStore.remove(topic, client);
                });
            });
            clientManager.remove(channel);
        } else {
        // TODO support Sticky Session
        }
    }
    channel.close();
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)

Example 7 with MqttClient

use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.

the class MqttPubackMessageHandler method handleMessage.

/**
 * handle the PUBACK message from the client
 * <ol>
 *     <li>remove the message from the published in-flight messages</li>
 *     <li>ack the message in the MessageStore</li>
 * </ol>
 * @param
 * @return
 */
@Override
public void handleMessage(Message message) {
    MqttPubAckMessage pubAckMessage = (MqttPubAckMessage) message.getPayload();
    // TODO: remove the message from the published in-flight message
    messageStore.ack(message, (List<MqttClient>) message.getClient());
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MqttPubAckMessage(io.netty.handler.codec.mqtt.MqttPubAckMessage)

Example 8 with MqttClient

use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.

the class PubSubIntegrationTest method test.

@Test
public void test() {
    /* the consumer connect and subscribe */
    /* handle CONNECT message from consumer */
    MqttConnectMessage consuemrConnectMessage = getConnectMessage(consumerId);
    consumerChannel.writeInbound(consuemrConnectMessage);
    MqttClient managedConsuemr = (MqttClient) clientManager.get(consumerChannel);
    MqttConnAckMessage consumerConnAckMessage = consumerChannel.readOutbound();
    Assert.assertNotNull(managedConsuemr);
    Assert.assertEquals(consumerId, managedConsuemr.getId());
    Assert.assertTrue(managedConsuemr.isConnected());
    Assert.assertTrue(consumerChannel.isOpen());
    Assert.assertEquals(consumerId, consuemrConnectMessage.payload().clientIdentifier());
    Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, consumerConnAckMessage.variableHeader().connectReturnCode());
    Assert.assertEquals(consuemrConnectMessage.variableHeader().isCleanSession(), consumerConnAckMessage.variableHeader().isSessionPresent());
    consumerChannel.releaseInbound();
    /* handle SUBSCRIBE message from consumer*/
    MqttSubscribeMessage consumerSubscribeMessage = getMqttSubscribeMessage();
    consumerChannel.writeInbound(consumerSubscribeMessage);
    MqttSubAckMessage consuemrSubAckMessage = consumerChannel.readOutbound();
    Assert.assertNotNull(consuemrSubAckMessage);
    Assert.assertEquals(consumerSubscribeMessage.variableHeader().messageId(), consuemrSubAckMessage.variableHeader().messageId());
    Assert.assertEquals(topicSubscriptions.size(), consuemrSubAckMessage.payload().grantedQoSLevels().size());
    consumerChannel.releaseInbound();
    /* the producer publish message to the topic */
    /* handle CONNECT message from producer */
    MqttConnectMessage producerConnectMessage = getConnectMessage(producerId);
    producerChannel.writeInbound(producerConnectMessage);
    MqttConnAckMessage producerConnAckMessage = producerChannel.readOutbound();
    MqttClient managedProducer = (MqttClient) clientManager.get(producerChannel);
    Assert.assertNotNull(managedProducer);
    Assert.assertNotNull(producerConnAckMessage);
    Assert.assertTrue(managedProducer.isConnected());
    Assert.assertTrue(producerChannel.isOpen());
    Assert.assertEquals(producerId, managedProducer.getId());
    Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, producerConnAckMessage.variableHeader().connectReturnCode());
    Assert.assertEquals(producerConnectMessage.variableHeader().isCleanSession(), producerConnAckMessage.variableHeader().isSessionPresent());
    producerChannel.releaseInbound();
    /* handle PUBLISH message from producer */
    MqttPublishMessage producerPublishMessage = getMqttPublishMessage();
    byte[] expectedPayload = new byte[producerPublishMessage.payload().readableBytes()];
    producerPublishMessage.payload().getBytes(0, expectedPayload);
    producerChannel.writeInbound(producerPublishMessage);
    MqttPublishMessage consumerReceivedMessage = consumerChannel.readOutbound();
    byte[] actualPayload = new byte[consumerReceivedMessage.payload().readableBytes()];
    consumerReceivedMessage.payload().getBytes(0, actualPayload);
    Assert.assertNotNull(consumerReceivedMessage);
    Assert.assertEquals(producerPublishMessage.variableHeader().packetId(), consumerReceivedMessage.variableHeader().packetId());
    Assert.assertEquals(producerPublishMessage.variableHeader().topicName(), consumerReceivedMessage.variableHeader().topicName());
    Assert.assertArrayEquals(expectedPayload, actualPayload);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) 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) Test(org.junit.Test)

Example 9 with MqttClient

use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.

the class PubSubIntegrationTest method setup.

@Before
public void setup() {
    clientManager = new ClientManagerImpl();
    subscriptionStore = new InMemorySubscriptionStore();
    messageDispatcher = new MessageDispatcher(clientManager);
    mqttConnectMessageHandler = new MqttConnectMessageHandler(clientManager);
    mqttSubscribeMessageHandler = new MqttSubscribeMessageHandler(subscriptionStore);
    mqttMessageForwarder = new MqttMessageForwarder(subscriptionStore);
    messageDispatcher.registerHandler(Message.Type.MQTT_CONNECT, mqttConnectMessageHandler);
    messageDispatcher.registerHandler(Message.Type.MQTT_PUBLISH, mqttMessageForwarder);
    messageDispatcher.registerHandler(Message.Type.MQTT_SUBSCRIBE, mqttSubscribeMessageHandler);
    producer = Mockito.spy(new MqttClient());
    producer.setId(producerId);
    consumer = Mockito.spy(new MqttClient());
    consumer.setId(consumerId);
    producerChannel = new EmbeddedChannel(messageDispatcher);
    producer.setCtx(producerChannel.pipeline().context("producer-ctx"));
    consumerChannel = new EmbeddedChannel(messageDispatcher);
    consumer.setCtx(consumerChannel.pipeline().context("consumer-ctx"));
    topicSubscriptions.add(new MqttTopicSubscription(topicName, MqttQoS.AT_MOST_ONCE));
// Mockito.when(consumerCtx.writeAndFlush(Mockito.any(MqttMessage.class))).then(
// new Answer<Object>() {
// @Override public Object answer(InvocationOnMock mock) throws Throwable {
// MqttMessage message = (MqttMessage) mock.getArguments()[0];
// consumerChannel.writeOutbound(message);
// return consumerChannel.newSucceededFuture();
// }
// }
// );
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MessageDispatcher(org.apache.rocketmq.iot.protocol.mqtt.handler.MessageDispatcher) MqttMessageForwarder(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttMessageForwarder) ClientManagerImpl(org.apache.rocketmq.iot.connection.client.impl.ClientManagerImpl) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) MqttSubscribeMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttSubscribeMessageHandler) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) MqttConnectMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttConnectMessageHandler) InMemorySubscriptionStore(org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore) Before(org.junit.Before)

Example 10 with MqttClient

use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.

the class AbstractMqttMessageHandlerTest method setup.

@Before
public void setup() {
    subscriptionStore = Mockito.mock(SubscriptionStore.class);
    clientManager = Mockito.mock(ClientManager.class);
    client = Mockito.spy(new MqttClient());
    initMessageHandler();
    mockHandler = new MockHandler(messageHandler);
    embeddedChannel = new EmbeddedChannel(mockHandler);
    initMessage();
    mock();
    Mockito.when(client.getCtx()).thenReturn(embeddedChannel.pipeline().context(mockHandler));
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) SubscriptionStore(org.apache.rocketmq.iot.storage.subscription.SubscriptionStore) ClientManager(org.apache.rocketmq.iot.connection.client.ClientManager) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Before(org.junit.Before)

Aggregations

MqttClient (org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)13 Before (org.junit.Before)7 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)5 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)4 InMemorySubscriptionStore (org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore)4 MqttTopicSubscription (io.netty.handler.codec.mqtt.MqttTopicSubscription)3 ClientManagerImpl (org.apache.rocketmq.iot.connection.client.impl.ClientManagerImpl)3 MessageDispatcher (org.apache.rocketmq.iot.protocol.mqtt.handler.MessageDispatcher)3 MqttConnectMessageHandler (org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttConnectMessageHandler)3 MqttMessageForwarder (org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttMessageForwarder)3 MqttConnAckMessage (io.netty.handler.codec.mqtt.MqttConnAckMessage)2 MqttConnectMessage (io.netty.handler.codec.mqtt.MqttConnectMessage)2 MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)2 MqttSubAckMessage (io.netty.handler.codec.mqtt.MqttSubAckMessage)2 MqttSubscribeMessage (io.netty.handler.codec.mqtt.MqttSubscribeMessage)2 Client (org.apache.rocketmq.iot.connection.client.Client)2 ClientManager (org.apache.rocketmq.iot.connection.client.ClientManager)2 MqttSubscribeMessageHandler (org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttSubscribeMessageHandler)2 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)1 MqttMessage (io.netty.handler.codec.mqtt.MqttMessage)1