use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.
the class MqttConnectionHandler method doDisconnect.
/**
* disconnect the channel, save the Subscription of the client which the channel belongs to if CleanSession
* if set to <b>false</b>, otherwise discard them
*
* @param channel
*/
private void doDisconnect(Channel channel) {
if (channel == null) {
return;
}
MqttClient client = (MqttClient) clientManager.get(channel);
if (client != null) {
if (client.isCleanSession()) {
subscriptionStore.getTopicFilters(client.getId()).forEach(filter -> {
subscriptionStore.getTopics(filter).forEach(topic -> {
subscriptionStore.remove(topic, client);
});
});
clientManager.remove(channel);
} else {
// TODO support Sticky Session
}
}
channel.close();
}
use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.
the class MqttPubackMessageHandler method handleMessage.
/**
* handle the PUBACK message from the client
* <ol>
* <li>remove the message from the published in-flight messages</li>
* <li>ack the message in the MessageStore</li>
* </ol>
* @param
* @return
*/
@Override
public void handleMessage(Message message) {
MqttPubAckMessage pubAckMessage = (MqttPubAckMessage) message.getPayload();
// TODO: remove the message from the published in-flight message
messageStore.ack(message, (List<MqttClient>) message.getClient());
}
use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.
the class PubSubIntegrationTest method test.
@Test
public void test() {
/* the consumer connect and subscribe */
/* handle CONNECT message from consumer */
MqttConnectMessage consuemrConnectMessage = getConnectMessage(consumerId);
consumerChannel.writeInbound(consuemrConnectMessage);
MqttClient managedConsuemr = (MqttClient) clientManager.get(consumerChannel);
MqttConnAckMessage consumerConnAckMessage = consumerChannel.readOutbound();
Assert.assertNotNull(managedConsuemr);
Assert.assertEquals(consumerId, managedConsuemr.getId());
Assert.assertTrue(managedConsuemr.isConnected());
Assert.assertTrue(consumerChannel.isOpen());
Assert.assertEquals(consumerId, consuemrConnectMessage.payload().clientIdentifier());
Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, consumerConnAckMessage.variableHeader().connectReturnCode());
Assert.assertEquals(consuemrConnectMessage.variableHeader().isCleanSession(), consumerConnAckMessage.variableHeader().isSessionPresent());
consumerChannel.releaseInbound();
/* handle SUBSCRIBE message from consumer*/
MqttSubscribeMessage consumerSubscribeMessage = getMqttSubscribeMessage();
consumerChannel.writeInbound(consumerSubscribeMessage);
MqttSubAckMessage consuemrSubAckMessage = consumerChannel.readOutbound();
Assert.assertNotNull(consuemrSubAckMessage);
Assert.assertEquals(consumerSubscribeMessage.variableHeader().messageId(), consuemrSubAckMessage.variableHeader().messageId());
Assert.assertEquals(topicSubscriptions.size(), consuemrSubAckMessage.payload().grantedQoSLevels().size());
consumerChannel.releaseInbound();
/* the producer publish message to the topic */
/* handle CONNECT message from producer */
MqttConnectMessage producerConnectMessage = getConnectMessage(producerId);
producerChannel.writeInbound(producerConnectMessage);
MqttConnAckMessage producerConnAckMessage = producerChannel.readOutbound();
MqttClient managedProducer = (MqttClient) clientManager.get(producerChannel);
Assert.assertNotNull(managedProducer);
Assert.assertNotNull(producerConnAckMessage);
Assert.assertTrue(managedProducer.isConnected());
Assert.assertTrue(producerChannel.isOpen());
Assert.assertEquals(producerId, managedProducer.getId());
Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, producerConnAckMessage.variableHeader().connectReturnCode());
Assert.assertEquals(producerConnectMessage.variableHeader().isCleanSession(), producerConnAckMessage.variableHeader().isSessionPresent());
producerChannel.releaseInbound();
/* handle PUBLISH message from producer */
MqttPublishMessage producerPublishMessage = getMqttPublishMessage();
byte[] expectedPayload = new byte[producerPublishMessage.payload().readableBytes()];
producerPublishMessage.payload().getBytes(0, expectedPayload);
producerChannel.writeInbound(producerPublishMessage);
MqttPublishMessage consumerReceivedMessage = consumerChannel.readOutbound();
byte[] actualPayload = new byte[consumerReceivedMessage.payload().readableBytes()];
consumerReceivedMessage.payload().getBytes(0, actualPayload);
Assert.assertNotNull(consumerReceivedMessage);
Assert.assertEquals(producerPublishMessage.variableHeader().packetId(), consumerReceivedMessage.variableHeader().packetId());
Assert.assertEquals(producerPublishMessage.variableHeader().topicName(), consumerReceivedMessage.variableHeader().topicName());
Assert.assertArrayEquals(expectedPayload, actualPayload);
}
use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.
the class PubSubIntegrationTest 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_PUBLISH, mqttMessageForwarder);
messageDispatcher.registerHandler(Message.Type.MQTT_SUBSCRIBE, mqttSubscribeMessageHandler);
producer = Mockito.spy(new MqttClient());
producer.setId(producerId);
consumer = Mockito.spy(new MqttClient());
consumer.setId(consumerId);
producerChannel = new EmbeddedChannel(messageDispatcher);
producer.setCtx(producerChannel.pipeline().context("producer-ctx"));
consumerChannel = new EmbeddedChannel(messageDispatcher);
consumer.setCtx(consumerChannel.pipeline().context("consumer-ctx"));
topicSubscriptions.add(new MqttTopicSubscription(topicName, MqttQoS.AT_MOST_ONCE));
// Mockito.when(consumerCtx.writeAndFlush(Mockito.any(MqttMessage.class))).then(
// new Answer<Object>() {
// @Override public Object answer(InvocationOnMock mock) throws Throwable {
// MqttMessage message = (MqttMessage) mock.getArguments()[0];
// consumerChannel.writeOutbound(message);
// return consumerChannel.newSucceededFuture();
// }
// }
// );
}
use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.
the class AbstractMqttMessageHandlerTest method setup.
@Before
public void setup() {
subscriptionStore = Mockito.mock(SubscriptionStore.class);
clientManager = Mockito.mock(ClientManager.class);
client = Mockito.spy(new MqttClient());
initMessageHandler();
mockHandler = new MockHandler(messageHandler);
embeddedChannel = new EmbeddedChannel(mockHandler);
initMessage();
mock();
Mockito.when(client.getCtx()).thenReturn(embeddedChannel.pipeline().context(mockHandler));
}
Aggregations