Search in sources :

Example 1 with MqttClient

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

the class ProduceMessageIntegrationTest method setup.

@Before
public void setup() {
    /* start the mocked MQTTBridge */
    clientManager = new ClientManagerImpl();
    subscriptionStore = Mockito.spy(new InMemorySubscriptionStore());
    mqttConnectMessageHandler = new MqttConnectMessageHandler(clientManager);
    mqttMessageForwarder = new MqttMessageForwarder(subscriptionStore);
    dispatcher = new MessageDispatcher(clientManager);
    dispatcher.registerHandler(Message.Type.MQTT_CONNECT, mqttConnectMessageHandler);
    dispatcher.registerHandler(Message.Type.MQTT_PUBLISH, mqttMessageForwarder);
    producerChannel = new EmbeddedChannel(dispatcher);
    mockedConsuemr = Mockito.spy(new MqttClient());
    mockedConsumerCtx = Mockito.mock(ChannelHandlerContext.class);
    mockedSubscriptions = new ArrayList<>();
    mockedSubscriptions.add(Subscription.Builder.newBuilder().client(mockedConsuemr).qos(0).build());
    Mockito.when(mockedConsuemr.getCtx()).thenReturn(mockedConsumerCtx);
    Mockito.when(mockedConsumerCtx.writeAndFlush(Mockito.any(MqttPublishMessage.class))).then(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock mock) throws Throwable {
            MqttPublishMessage publishMessage = (MqttPublishMessage) mock.getArguments()[0];
            consumerChannel.writeOutbound(publishMessage);
            return consumerChannel.newSucceededFuture();
        }
    });
}
Also used : MqttMessageForwarder(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttMessageForwarder) ClientManagerImpl(org.apache.rocketmq.iot.connection.client.impl.ClientManagerImpl) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) MqttConnectMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttConnectMessageHandler) MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MessageDispatcher(org.apache.rocketmq.iot.protocol.mqtt.handler.MessageDispatcher) MqttPublishMessage(io.netty.handler.codec.mqtt.MqttPublishMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) InMemorySubscriptionStore(org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore) Before(org.junit.Before)

Example 2 with MqttClient

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

the class InMemorySubscriptionTest method setup.

@Before
public void setup() throws IllegalAccessException {
    subscriptionStore = new InMemorySubscriptionStore();
    topic1 = "test/in/memory/topic-a";
    topic2 = "test/in/memory/topic-b";
    topic3 = "test/in/disk/topic-c";
    client1 = Mockito.spy(new MqttClient());
    client2 = Mockito.spy(new MqttClient());
    client1.setId("test-client-1");
    client2.setId("test-client-2");
    subscription1 = Subscription.Builder.newBuilder().client(client1).build();
    subscription2 = Subscription.Builder.newBuilder().client(client2).build();
    subscriptionStore.addTopic(topic1);
    subscriptionStore.addTopic(topic2);
    subscriptionStore.addTopic(topic3);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) InMemorySubscriptionStore(org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore) Before(org.junit.Before)

Example 3 with MqttClient

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

the class MqttPingreqMessageHandlerTest method setup.

@Before
public void setup() {
    clientManager = Mockito.mock(ClientManager.class);
    ctx = Mockito.mock(ChannelHandlerContext.class);
    client = Mockito.spy(new MqttClient());
    client.setConnected(true);
    handler = new MqttPingreqMessageHandler();
    Mockito.when(client.getCtx()).thenReturn(ctx);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) ClientManager(org.apache.rocketmq.iot.connection.client.ClientManager) MqttPingreqMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttPingreqMessageHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Before(org.junit.Before)

Example 4 with MqttClient

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

the class MqttSubscribeMessageHandler method handleMessage.

/**
 * handle the SUBSCRIBE message from the client
 * <ol>
 * <li>validate the topic filters in each subscription</li>
 * <li>set actual qos of each filter</li>
 * <li>get the topics matching given filters</li>
 * <li>check the client authorization of each topic</li>
 * <li>generate SUBACK message which includes the subscription result for each TopicFilter</li>
 * <li>send SUBACK message to the client</li>
 * </ol>
 *
 * @param message the message wrapping MqttSubscriptionMessage
 * @return
 */
@Override
public void handleMessage(Message message) {
    Client client = message.getClient();
    MqttSubscribeMessage subscribeMessage = (MqttSubscribeMessage) message.getPayload();
    List<MqttTopicSubscription> topicSubscriptions = subscribeMessage.payload().topicSubscriptions();
    List<Integer> grantQoss = new ArrayList<>();
    topicSubscriptions.forEach(s -> {
        String topic = s.topicName();
        int actualQos = MessageUtil.actualQos(s.qualityOfService().value());
        grantQoss.add(actualQos);
        subscriptionStore.append(topic, Subscription.Builder.newBuilder().client((MqttClient) client).qos(actualQos).build());
    });
    MqttSubAckMessage subackMessage = MessageUtil.getMqttSubackMessage(subscribeMessage, new MqttSubAckPayload(grantQoss));
    client.getCtx().writeAndFlush(subackMessage);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MqttSubscribeMessage(io.netty.handler.codec.mqtt.MqttSubscribeMessage) MqttSubAckMessage(io.netty.handler.codec.mqtt.MqttSubAckMessage) MqttTopicSubscription(io.netty.handler.codec.mqtt.MqttTopicSubscription) ArrayList(java.util.ArrayList) Client(org.apache.rocketmq.iot.connection.client.Client) MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MqttSubAckPayload(io.netty.handler.codec.mqtt.MqttSubAckPayload)

Example 5 with MqttClient

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

the class MessageDispatcher method channelRead0.

@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (!(msg instanceof MqttMessage)) {
        return;
    }
    Client client = clientManager.get(ctx.channel());
    if (client == null) {
        client = new MqttClient();
        client.setCtx(ctx);
        clientManager.put(ctx.channel(), client);
    }
    MqttMessage mqttMessage = (MqttMessage) msg;
    Message message = MessageUtil.getMessage(mqttMessage);
    message.setClient(client);
    dispatch(message);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) Message(org.apache.rocketmq.iot.common.data.Message) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) Client(org.apache.rocketmq.iot.connection.client.Client) MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)

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