Search in sources :

Example 11 with MqttClient

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

the class MqttMessageForwarderTest method setup.

@Before
public void setup() {
    client1 = Mockito.spy(new MqttClient());
    channel1 = new EmbeddedChannel();
    client1.setCtx(channel1.pipeline().lastContext());
    client2 = Mockito.mock(MqttClient.class);
    channel2 = new EmbeddedChannel();
    client2.setCtx(channel2.pipeline().lastContext());
    subscription1 = Mockito.spy(Subscription.Builder.newBuilder().client(client1).build());
    subscription2 = Mockito.spy(Subscription.Builder.newBuilder().client(client2).build());
    subscriptions = new ArrayList<>();
    subscriptions.add(subscription1);
    subscriptions.add(subscription2);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Before(org.junit.Before)

Example 12 with MqttClient

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

the class MqttConnectMessageHandler method handleMessage.

@Override
public void handleMessage(Message message) {
    MqttClient client = (MqttClient) message.getClient();
    MqttConnectMessage connectMessage = (MqttConnectMessage) message.getPayload();
    MqttConnectReturnCode returnCode;
    MqttConnAckMessage ackMessage;
    ChannelHandlerContext ctx = client.getCtx();
    if (!isClientIdValid(connectMessage.payload().clientIdentifier())) {
        returnCode = MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
    } else if (!checkUsername(connectMessage.payload().userName()) || !checkPassword(connectMessage.payload().passwordInBytes())) {
        returnCode = MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD;
    } else if (!isAuthorized(connectMessage)) {
        returnCode = MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED;
    } else if (client.isConnected()) {
        // protocol violation and disconnect
        ctx.fireUserEventTriggered(new DisconnectChannelEvent(ctx.channel()));
        return;
    } else if (!isServiceAviable(connectMessage)) {
        returnCode = MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE;
    } else {
        client.setId(connectMessage.payload().clientIdentifier());
        client.setConnected(true);
        client.setCleanSession(connectMessage.variableHeader().isCleanSession());
        ctx.channel().attr(ChannelConfiguration.CHANNEL_IDLE_TIME_ATTRIBUTE_KEY).set(connectMessage.variableHeader().keepAliveTimeSeconds());
        returnCode = MqttConnectReturnCode.CONNECTION_ACCEPTED;
    }
    ackMessage = MessageUtil.getMqttConnackMessage(connectMessage, returnCode);
    ctx.writeAndFlush(ackMessage);
}
Also used : MqttClient(org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient) DisconnectChannelEvent(org.apache.rocketmq.iot.protocol.mqtt.event.DisconnectChannelEvent) MqttConnectMessage(io.netty.handler.codec.mqtt.MqttConnectMessage) MqttConnAckMessage(io.netty.handler.codec.mqtt.MqttConnAckMessage) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode)

Example 13 with MqttClient

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

the class ConsumeMessageIntegrationTest 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_SUBSCRIBE, mqttSubscribeMessageHandler);
    messageDispatcher.registerHandler(Message.Type.MQTT_PUBLISH, mqttMessageForwarder);
    embeddedChannel = new EmbeddedChannel(messageDispatcher);
    subscriptions = new ArrayList<>();
    subscriptions.add(new MqttTopicSubscription(topicName, MqttQoS.AT_MOST_ONCE));
    consuemr = Mockito.spy(new MqttClient());
    consuermCtx = Mockito.mock(ChannelHandlerContext.class);
    Mockito.when(consuemr.getCtx()).thenReturn(consuermCtx);
}
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) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) MqttConnectMessageHandler(org.apache.rocketmq.iot.protocol.mqtt.handler.downstream.impl.MqttConnectMessageHandler) InMemorySubscriptionStore(org.apache.rocketmq.iot.storage.subscription.impl.InMemorySubscriptionStore) 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