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