use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.
the class ProduceMessageIntegrationTest method setup.
@Before
public void setup() {
/* start the mocked MQTTBridge */
clientManager = new ClientManagerImpl();
subscriptionStore = Mockito.spy(new InMemorySubscriptionStore());
mqttConnectMessageHandler = new MqttConnectMessageHandler(clientManager);
mqttMessageForwarder = new MqttMessageForwarder(subscriptionStore);
dispatcher = new MessageDispatcher(clientManager);
dispatcher.registerHandler(Message.Type.MQTT_CONNECT, mqttConnectMessageHandler);
dispatcher.registerHandler(Message.Type.MQTT_PUBLISH, mqttMessageForwarder);
producerChannel = new EmbeddedChannel(dispatcher);
mockedConsuemr = Mockito.spy(new MqttClient());
mockedConsumerCtx = Mockito.mock(ChannelHandlerContext.class);
mockedSubscriptions = new ArrayList<>();
mockedSubscriptions.add(Subscription.Builder.newBuilder().client(mockedConsuemr).qos(0).build());
Mockito.when(mockedConsuemr.getCtx()).thenReturn(mockedConsumerCtx);
Mockito.when(mockedConsumerCtx.writeAndFlush(Mockito.any(MqttPublishMessage.class))).then(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock mock) throws Throwable {
MqttPublishMessage publishMessage = (MqttPublishMessage) mock.getArguments()[0];
consumerChannel.writeOutbound(publishMessage);
return consumerChannel.newSucceededFuture();
}
});
}
use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.
the class InMemorySubscriptionTest method setup.
@Before
public void setup() throws IllegalAccessException {
subscriptionStore = new InMemorySubscriptionStore();
topic1 = "test/in/memory/topic-a";
topic2 = "test/in/memory/topic-b";
topic3 = "test/in/disk/topic-c";
client1 = Mockito.spy(new MqttClient());
client2 = Mockito.spy(new MqttClient());
client1.setId("test-client-1");
client2.setId("test-client-2");
subscription1 = Subscription.Builder.newBuilder().client(client1).build();
subscription2 = Subscription.Builder.newBuilder().client(client2).build();
subscriptionStore.addTopic(topic1);
subscriptionStore.addTopic(topic2);
subscriptionStore.addTopic(topic3);
}
use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.
the class MqttPingreqMessageHandlerTest method setup.
@Before
public void setup() {
clientManager = Mockito.mock(ClientManager.class);
ctx = Mockito.mock(ChannelHandlerContext.class);
client = Mockito.spy(new MqttClient());
client.setConnected(true);
handler = new MqttPingreqMessageHandler();
Mockito.when(client.getCtx()).thenReturn(ctx);
}
use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient in project rocketmq-externals by apache.
the class MqttSubscribeMessageHandler method handleMessage.
/**
* handle the SUBSCRIBE message from the client
* <ol>
* <li>validate the topic filters in each subscription</li>
* <li>set actual qos of each filter</li>
* <li>get the topics matching given filters</li>
* <li>check the client authorization of each topic</li>
* <li>generate SUBACK message which includes the subscription result for each TopicFilter</li>
* <li>send SUBACK message to the client</li>
* </ol>
*
* @param message the message wrapping MqttSubscriptionMessage
* @return
*/
@Override
public void handleMessage(Message message) {
Client client = message.getClient();
MqttSubscribeMessage subscribeMessage = (MqttSubscribeMessage) message.getPayload();
List<MqttTopicSubscription> topicSubscriptions = subscribeMessage.payload().topicSubscriptions();
List<Integer> grantQoss = new ArrayList<>();
topicSubscriptions.forEach(s -> {
String topic = s.topicName();
int actualQos = MessageUtil.actualQos(s.qualityOfService().value());
grantQoss.add(actualQos);
subscriptionStore.append(topic, Subscription.Builder.newBuilder().client((MqttClient) client).qos(actualQos).build());
});
MqttSubAckMessage subackMessage = MessageUtil.getMqttSubackMessage(subscribeMessage, new MqttSubAckPayload(grantQoss));
client.getCtx().writeAndFlush(subackMessage);
}
use of org.apache.rocketmq.iot.protocol.mqtt.data.MqttClient 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);
}
Aggregations