Search in sources :

Example 1 with DisconnectChannelEvent

use of org.apache.rocketmq.iot.protocol.mqtt.event.DisconnectChannelEvent in project rocketmq-externals by apache.

the class MqttConnectionHandlerTest method testHandleDisconnectChannelEvent.

@Test
public void testHandleDisconnectChannelEvent() throws Exception {
    DisconnectChannelEvent event = new DisconnectChannelEvent(channel);
    handler.userEventTriggered(ctx, event);
    Mockito.verify(clientManager).remove(channel);
    Mockito.verify(channel).close();
}
Also used : DisconnectChannelEvent(org.apache.rocketmq.iot.protocol.mqtt.event.DisconnectChannelEvent) Test(org.junit.Test)

Example 2 with DisconnectChannelEvent

use of org.apache.rocketmq.iot.protocol.mqtt.event.DisconnectChannelEvent 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)

Aggregations

DisconnectChannelEvent (org.apache.rocketmq.iot.protocol.mqtt.event.DisconnectChannelEvent)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 MqttConnAckMessage (io.netty.handler.codec.mqtt.MqttConnAckMessage)1 MqttConnectMessage (io.netty.handler.codec.mqtt.MqttConnectMessage)1 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)1 MqttClient (org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient)1 Test (org.junit.Test)1