use of io.vertx.mqtt.MqttTopicSubscription in project vertx-examples by vert-x3.
the class Server method start.
@Override
public void start() throws Exception {
MqttServer mqttServer = MqttServer.create(vertx);
mqttServer.endpointHandler(endpoint -> {
// shows main connect info
System.out.println("MQTT client [" + endpoint.clientIdentifier() + "] request to connect, clean session = " + endpoint.isCleanSession());
if (endpoint.auth() != null) {
System.out.println("[username = " + endpoint.auth().userName() + ", password = " + endpoint.auth().password() + "]");
}
if (endpoint.will() != null) {
System.out.println("[will flag = " + endpoint.will().isWillFlag() + " topic = " + endpoint.will().willTopic() + " msg = " + endpoint.will().willMessage() + " QoS = " + endpoint.will().willQos() + " isRetain = " + endpoint.will().isWillRetain() + "]");
}
System.out.println("[keep alive timeout = " + endpoint.keepAliveTimeSeconds() + "]");
// accept connection from the remote client
endpoint.accept(false);
// handling requests for subscriptions
endpoint.subscribeHandler(subscribe -> {
List<MqttQoS> grantedQosLevels = new ArrayList<>();
for (MqttTopicSubscription s : subscribe.topicSubscriptions()) {
System.out.println("Subscription for " + s.topicName() + " with QoS " + s.qualityOfService());
grantedQosLevels.add(s.qualityOfService());
}
// ack the subscriptions request
endpoint.subscribeAcknowledge(subscribe.messageId(), grantedQosLevels);
// just as example, publish a message on the first topic with requested QoS
endpoint.publish(subscribe.topicSubscriptions().get(0).topicName(), Buffer.buffer("Hello from the Vert.x MQTT server"), subscribe.topicSubscriptions().get(0).qualityOfService(), false, false);
// specifing handlers for handling QoS 1 and 2
endpoint.publishAcknowledgeHandler(messageId -> {
System.out.println("Received ack for message = " + messageId);
}).publishReceivedHandler(messageId -> {
endpoint.publishRelease(messageId);
}).publishCompletionHandler(messageId -> {
System.out.println("Received ack for message = " + messageId);
});
});
// handling requests for unsubscriptions
endpoint.unsubscribeHandler(unsubscribe -> {
for (String t : unsubscribe.topics()) {
System.out.println("Unsubscription for " + t);
}
// ack the subscriptions request
endpoint.unsubscribeAcknowledge(unsubscribe.messageId());
});
// handling ping from client
endpoint.pingHandler(v -> {
System.out.println("Ping received from client");
});
// handling disconnect message
endpoint.disconnectHandler(v -> {
System.out.println("Received disconnect from client");
});
// handling closing connection
endpoint.closeHandler(v -> {
System.out.println("Connection closed");
});
// handling incoming published messages
endpoint.publishHandler(message -> {
System.out.println("Just received message on [" + message.topicName() + "] payload [" + message.payload() + "] with QoS [" + message.qosLevel() + "]");
if (message.qosLevel() == MqttQoS.AT_LEAST_ONCE) {
endpoint.publishAcknowledge(message.messageId());
} else if (message.qosLevel() == MqttQoS.EXACTLY_ONCE) {
endpoint.publishReceived(message.messageId());
}
}).publishReleaseHandler(messageId -> {
endpoint.publishComplete(messageId);
});
}).listen(1883, "0.0.0.0", ar -> {
if (ar.succeeded()) {
System.out.println("MQTT server is listening on port " + mqttServer.actualPort());
} else {
System.err.println("Error on starting the server" + ar.cause().getMessage());
}
});
}
use of io.vertx.mqtt.MqttTopicSubscription in project vertx-openshift-it by cescoffier.
the class MqttBroker method configureSubscribeHandler.
private void configureSubscribeHandler(MqttEndpoint endpoint) {
endpoint.subscribeHandler(subscribe -> {
List<MqttQoS> grantedQosLevels = new ArrayList<>();
for (MqttTopicSubscription s : subscribe.topicSubscriptions()) {
System.out.println("Subscription for " + s.topicName() + " with QoS " + s.qualityOfService());
grantedQosLevels.add(s.qualityOfService());
}
endpoint.subscribeAcknowledge(subscribe.messageId(), grantedQosLevels);
endpoint.publish(subscribe.topicSubscriptions().get(0).topicName(), Buffer.buffer("Hello from the Vert.x MQTT server!"), subscribe.topicSubscriptions().get(0).qualityOfService(), false, false);
endpoint.publishAcknowledgeHandler(messageId -> System.out.println("Received ack for message = " + messageId)).publishReceivedHandler(endpoint::publishRelease).publishCompletionHandler(messageId -> System.out.println("Received ack for message = " + messageId));
});
}
use of io.vertx.mqtt.MqttTopicSubscription in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testOnSubscribeIncludesStatusCodeForEachFilter.
/**
* Verifies that the adapter includes a status code for each topic filter in its SUBACK packet.
*/
@SuppressWarnings("unchecked")
@Test
public void testOnSubscribeIncludesStatusCodeForEachFilter() {
// GIVEN a device connected to an adapter
givenAnAdapter(properties);
givenAnEventSenderForAnyTenant();
final MqttEndpoint endpoint = mockEndpoint();
when(endpoint.isConnected()).thenReturn(true);
// WHEN a device sends a SUBSCRIBE packet for several unsupported filters
final List<MqttTopicSubscription> subscriptions = new ArrayList<>();
subscriptions.add(newMockTopicSubscription("unsupported/#", MqttQoS.AT_LEAST_ONCE));
subscriptions.add(newMockTopicSubscription("bumlux/+/+/#", MqttQoS.AT_MOST_ONCE));
subscriptions.add(newMockTopicSubscription("bumlux/+/+/#", MqttQoS.AT_MOST_ONCE));
// and for subscribing to commands
final CommandConsumer commandConsumer = mock(CommandConsumer.class);
when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
when(commandConsumerFactory.createCommandConsumer(eq("tenant-1"), eq("device-A"), VertxMockSupport.anyHandler(), any(), any())).thenReturn(Future.succeededFuture(commandConsumer));
subscriptions.add(newMockTopicSubscription(getCommandSubscriptionTopic("tenant-1", "device-A"), MqttQoS.AT_MOST_ONCE));
subscriptions.add(newMockTopicSubscription(getCommandSubscriptionTopic("tenant-1", "device-B"), MqttQoS.EXACTLY_ONCE));
final MqttSubscribeMessage msg = mock(MqttSubscribeMessage.class);
when(msg.messageId()).thenReturn(15);
when(msg.topicSubscriptions()).thenReturn(subscriptions);
final var mqttDeviceEndpoint = adapter.createMqttDeviceEndpoint(endpoint, null, OptionalInt.empty());
mqttDeviceEndpoint.onSubscribe(msg);
// THEN the adapter sends a SUBACK packet to the device
// which contains a failure status code for each unsupported filter
final ArgumentCaptor<List<MqttQoS>> codeCaptor = ArgumentCaptor.forClass(List.class);
verify(endpoint).subscribeAcknowledge(eq(15), codeCaptor.capture());
assertThat(codeCaptor.getValue()).hasSize(5);
assertThat(codeCaptor.getValue().get(0)).isEqualTo(MqttQoS.FAILURE);
assertThat(codeCaptor.getValue().get(1)).isEqualTo(MqttQoS.FAILURE);
assertThat(codeCaptor.getValue().get(2)).isEqualTo(MqttQoS.FAILURE);
assertThat(codeCaptor.getValue().get(3)).isEqualTo(MqttQoS.AT_MOST_ONCE);
// and sends an empty notification downstream with TTD -1
assertEmptyNotificationHasBeenSentDownstream("tenant-1", "device-A", -1);
}
use of io.vertx.mqtt.MqttTopicSubscription in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method newMockTopicSubscription.
private static MqttTopicSubscription newMockTopicSubscription(final String filter, final MqttQoS qos) {
final MqttTopicSubscription result = mock(MqttTopicSubscription.class);
when(result.qualityOfService()).thenReturn(qos);
when(result.topicName()).thenReturn(filter);
return result;
}
Aggregations