Java Code Examples for io.vertx.mqtt.MqttEndpoint#closeHandler()

The following examples show how to use io.vertx.mqtt.MqttEndpoint#closeHandler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractVertxBasedMqttProtocolAdapter.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
private Future<Device> registerHandlers(final MqttEndpoint endpoint, final Device authenticatedDevice,
        final OptionalInt traceSamplingPriority) {
    final CommandSubscriptionsManager<T> cmdSubscriptionsManager = new CommandSubscriptionsManager<>(vertx, getConfig());
    endpoint.closeHandler(v -> close(endpoint, authenticatedDevice, cmdSubscriptionsManager, traceSamplingPriority));
    endpoint.publishHandler(
            message -> handlePublishedMessage(MqttContext.fromPublishPacket(message, endpoint, authenticatedDevice)));
    endpoint.publishAcknowledgeHandler(cmdSubscriptionsManager::handlePubAck);
    endpoint.subscribeHandler(subscribeMsg -> onSubscribe(endpoint, authenticatedDevice, subscribeMsg, cmdSubscriptionsManager,
            traceSamplingPriority));
    endpoint.unsubscribeHandler(unsubscribeMsg -> onUnsubscribe(endpoint, authenticatedDevice, unsubscribeMsg,
            cmdSubscriptionsManager, traceSamplingPriority));
    if (authenticatedDevice == null) {
        metrics.incrementUnauthenticatedConnections();
    } else {
        metrics.incrementConnections(authenticatedDevice.getTenantId());
    }
    return Future.succeededFuture(authenticatedDevice);
}
 
Example 2
Source File: AbstractVertxBasedMqttProtocolAdapterTest.java    From hono with Eclipse Public License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void testOnSubscribeRegistersAndClosesConnection(final MqttQoS qos) {

    // GIVEN a device connected to an adapter
    final Promise<ProtonDelivery> outcome = Promise.promise();
    outcome.complete(mock(ProtonDelivery.class));
    final DownstreamSender sender = givenAnEventSenderForOutcome(outcome);
    final MqttServer server = getMqttServer(false);
    final AbstractVertxBasedMqttProtocolAdapter<MqttProtocolAdapterProperties> adapter = getAdapter(server);
    final MqttEndpoint endpoint = mockEndpoint();
    when(endpoint.keepAliveTimeSeconds()).thenReturn(10); // 10 seconds

    // WHEN a device subscribes to commands
    final ProtocolAdapterCommandConsumer commandConsumer = mock(ProtocolAdapterCommandConsumer.class);
    when(commandConsumer.close(any())).thenReturn(Future.succeededFuture());
    when(commandConsumerFactory.createCommandConsumer(eq("tenant"), eq("deviceId"), any(Handler.class), any(), any()))
                    .thenReturn(Future.succeededFuture(commandConsumer));
    final List<MqttTopicSubscription> subscriptions = Collections.singletonList(
            newMockTopicSubscription(getCommandSubscriptionTopic("tenant", "deviceId"), qos));
    final MqttSubscribeMessage msg = mock(MqttSubscribeMessage.class);
    when(msg.messageId()).thenReturn(15);
    when(msg.topicSubscriptions()).thenReturn(subscriptions);

    final CommandSubscriptionsManager<MqttProtocolAdapterProperties> cmdSubscriptionsManager = new CommandSubscriptionsManager<>(vertx, config);
    endpoint.closeHandler(
            handler -> adapter.close(endpoint, new Device("tenant", "deviceId"), cmdSubscriptionsManager, OptionalInt.empty()));
    adapter.onSubscribe(endpoint, null, msg, cmdSubscriptionsManager, OptionalInt.empty());

    // THEN the adapter creates a command consumer that is checked periodically
    verify(commandConsumerFactory).createCommandConsumer(eq("tenant"), eq("deviceId"), any(Handler.class), any(), any());
    // and the adapter registers a hook on the connection to the device
    final ArgumentCaptor<Handler<Void>> closeHookCaptor = ArgumentCaptor.forClass(Handler.class);
    verify(endpoint).closeHandler(closeHookCaptor.capture());
    // which closes the command consumer when the device disconnects
    closeHookCaptor.getValue().handle(null);
    verify(commandConsumer).close(any());
    // and sends an empty notification downstream with TTD 0
    final ArgumentCaptor<Message> msgCaptor = ArgumentCaptor.forClass(Message.class);
    verify(sender, times(2)).sendAndWaitForOutcome(msgCaptor.capture(), any());
    assertThat(msgCaptor.getValue().getContentType()).isEqualTo(EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION);
    assertThat(MessageHelper.getTimeUntilDisconnect(msgCaptor.getValue())).isEqualTo(0);
}
 
Example 3
Source File: MqttServerNetworkIssueTest.java    From vertx-mqtt with Apache License 2.0 3 votes vote down vote up
@Override
protected void endpointHandler(MqttEndpoint endpoint, TestContext context) {

  endpoint.closeHandler(v -> {

    log.info("MQTT remote client connection closed");
  });

  endpoint.accept(false);
}
 
Example 4
Source File: MqttServerDisconnectTest.java    From vertx-mqtt with Apache License 2.0 3 votes vote down vote up
@Override
protected void endpointHandler(MqttEndpoint endpoint, TestContext context) {

  endpoint.disconnectHandler(v -> {

    log.info("MQTT remote client disconnected");
  });

  endpoint.closeHandler(v -> {

    log.info("MQTT remote client connection closed");
  });

  endpoint.accept(false);
}