Search in sources :

Example 1 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project activemq-artemis by apache.

the class MQTTProtocolHandler method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    try {
        if (stopped) {
            disconnect(true);
            return;
        }
        MqttMessage message = (MqttMessage) msg;
        // Disconnect if Netty codec failed to decode the stream.
        if (message.decoderResult().isFailure()) {
            log.debug("Bad Message Disconnecting Client.");
            disconnect(true);
            return;
        }
        connection.dataReceived();
        MQTTUtil.logMessage(session.getState(), message, true);
        this.protocolManager.invokeIncoming(message, this.connection);
        switch(message.fixedHeader().messageType()) {
            case CONNECT:
                handleConnect((MqttConnectMessage) message, ctx);
                break;
            case PUBLISH:
                handlePublish((MqttPublishMessage) message);
                break;
            case PUBACK:
                handlePuback((MqttPubAckMessage) message);
                break;
            case PUBREC:
                handlePubrec(message);
                break;
            case PUBREL:
                handlePubrel(message);
                break;
            case PUBCOMP:
                handlePubcomp(message);
                break;
            case SUBSCRIBE:
                handleSubscribe((MqttSubscribeMessage) message);
                break;
            case UNSUBSCRIBE:
                handleUnsubscribe((MqttUnsubscribeMessage) message);
                break;
            case PINGREQ:
                handlePingreq();
                break;
            case DISCONNECT:
                disconnect(false);
                break;
            case UNSUBACK:
            case SUBACK:
            case PINGRESP:
            // The server does not instantiate connections therefore any CONNACK received over a connection is an invalid control message.
            case CONNACK:
            default:
                disconnect(true);
        }
    } catch (Exception e) {
        log.debug("Error processing Control Packet, Disconnecting Client", e);
        disconnect(true);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage)

Example 2 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project activemq-artemis by apache.

the class MQTTProtocolHandler method handlePingreq.

void handlePingreq() {
    MqttMessage pingResp = new MqttMessage(new MqttFixedHeader(MqttMessageType.PINGRESP, false, MqttQoS.AT_MOST_ONCE, false, 0));
    sendToClient(pingResp);
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader)

Example 3 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project activemq-artemis by apache.

the class MQTTInterceptorPropertiesTest method testCheckInterceptedMQTTMessageProperties.

@Test(timeout = 60000)
public void testCheckInterceptedMQTTMessageProperties() throws Exception {
    final String addressQueue = name.getMethodName();
    final String msgText = "Test intercepted message";
    final boolean retained = true;
    Map<String, Object> expectedProperties = new ArrayMap<>();
    expectedProperties.put(ADDRESS, addressQueue);
    expectedProperties.put(MESSAGE_TEXT, msgText);
    expectedProperties.put(RETAINED, retained);
    final MQTTClientProvider subscribeProvider = getMQTTClientProvider();
    initializeConnection(subscribeProvider);
    subscribeProvider.subscribe(addressQueue, AT_MOST_ONCE);
    final CountDownLatch latch = new CountDownLatch(1);
    MQTTInterceptor incomingInterceptor = new MQTTInterceptor() {

        @Override
        public boolean intercept(MqttMessage packet, RemotingConnection connection) throws ActiveMQException {
            System.out.println("incoming");
            if (packet.getClass() == MqttPublishMessage.class) {
                return checkMessageProperties(packet, expectedProperties);
            } else {
                return true;
            }
        }
    };
    MQTTInterceptor outgoingInterceptor = new MQTTInterceptor() {

        @Override
        public boolean intercept(MqttMessage packet, RemotingConnection connection) throws ActiveMQException {
            System.out.println("outgoing");
            if (packet.getClass() == MqttPublishMessage.class) {
                return checkMessageProperties(packet, expectedProperties);
            } else {
                return true;
            }
        }
    };
    server.getRemotingService().addIncomingInterceptor(incomingInterceptor);
    server.getRemotingService().addOutgoingInterceptor(outgoingInterceptor);
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                byte[] payload = subscribeProvider.receive(10000);
                assertNotNull("Should get a message", payload);
                latch.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
    thread.start();
    final MQTTClientProvider publishProvider = getMQTTClientProvider();
    initializeConnection(publishProvider);
    publishProvider.publish(addressQueue, msgText.getBytes(), AT_MOST_ONCE, retained);
    latch.await(10, TimeUnit.SECONDS);
    subscribeProvider.disconnect();
    publishProvider.disconnect();
}
Also used : MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ArrayMap(org.apache.felix.resolver.util.ArrayMap) CountDownLatch(java.util.concurrent.CountDownLatch) MQTTInterceptor(org.apache.activemq.artemis.core.protocol.mqtt.MQTTInterceptor) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Test(org.junit.Test)

Example 4 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage in project netty by netty.

the class MqttHeartBeatClientHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof IdleStateEvent) {
        MqttFixedHeader pingreqFixedHeader = new MqttFixedHeader(MqttMessageType.PINGREQ, false, MqttQoS.AT_MOST_ONCE, false, 0);
        MqttMessage pingreqMessage = new MqttMessage(pingreqFixedHeader);
        ctx.writeAndFlush(pingreqMessage);
        System.out.println("Sent PINGREQ");
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) MqttMessage(io.netty.handler.codec.mqtt.MqttMessage) MqttFixedHeader(io.netty.handler.codec.mqtt.MqttFixedHeader)

Example 5 with MqttMessage

use of io.netty.handler.codec.mqtt.MqttMessage 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

MqttMessage (io.netty.handler.codec.mqtt.MqttMessage)12 MqttPublishMessage (io.netty.handler.codec.mqtt.MqttPublishMessage)5 MqttFixedHeader (io.netty.handler.codec.mqtt.MqttFixedHeader)4 Client (org.apache.rocketmq.iot.connection.client.Client)4 MqttConnAckMessage (io.netty.handler.codec.mqtt.MqttConnAckMessage)3 MqttConnectMessage (io.netty.handler.codec.mqtt.MqttConnectMessage)3 MqttPubAckMessage (io.netty.handler.codec.mqtt.MqttPubAckMessage)2 MqttSubscribeMessage (io.netty.handler.codec.mqtt.MqttSubscribeMessage)2 Message (org.apache.rocketmq.iot.common.data.Message)2 MqttClient (org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)2 Test (org.junit.Test)2 ByteBuf (io.netty.buffer.ByteBuf)1 MqttConnAckVariableHeader (io.netty.handler.codec.mqtt.MqttConnAckVariableHeader)1 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)1 MqttPublishVariableHeader (io.netty.handler.codec.mqtt.MqttPublishVariableHeader)1 MqttSubAckMessage (io.netty.handler.codec.mqtt.MqttSubAckMessage)1 MqttUnsubAckMessage (io.netty.handler.codec.mqtt.MqttUnsubAckMessage)1 MqttUnsubscribeMessage (io.netty.handler.codec.mqtt.MqttUnsubscribeMessage)1 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)1 Date (java.util.Date)1