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();
}
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);
}
Aggregations