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

The following examples show how to use io.vertx.mqtt.MqttEndpoint#accept() . 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: MqttServerSubscribeTest.java    From vertx-mqtt with Apache License 2.0 6 votes vote down vote up
@Override
protected void endpointHandler(MqttEndpoint endpoint, TestContext context) {

  endpoint.subscribeHandler(subscribe -> {

    List<MqttQoS> qos = new ArrayList<>();

    MqttQoS grantedQos =
      subscribe.topicSubscriptions().get(0).topicName().equals(MQTT_TOPIC_FAILURE) ?
        MqttQoS.FAILURE :
        subscribe.topicSubscriptions().get(0).qualityOfService();

    qos.add(grantedQos);
    endpoint.subscribeAcknowledge(subscribe.messageId(), qos);

    this.async.complete();
  });

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

  endpoint.exceptionHandler(t -> {
    log.error("Exception raised", t);

    if (t instanceof DecoderException) {
      this.async.complete();
    }

  });

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

  MqttConnectReturnCode returnCode = this.expectedReturnCode;

  switch (this.expectedReturnCode) {

    case CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD:

      returnCode =
        (endpoint.auth().getUsername().equals(MQTT_USERNAME) &&
          endpoint.auth().getPassword().equals(MQTT_PASSWORD)) ?
          MqttConnectReturnCode.CONNECTION_ACCEPTED :
          MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD;
      break;

    case CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION:

      returnCode = endpoint.protocolVersion() == MqttConnectOptions.MQTT_VERSION_3_1_1 ?
        MqttConnectReturnCode.CONNECTION_ACCEPTED :
        MqttConnectReturnCode.CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION;
      break;
  }

  log.info("return code = " + returnCode);

  if (returnCode == MqttConnectReturnCode.CONNECTION_ACCEPTED) {
    log.info("client id = " + endpoint.clientIdentifier());
    endpoint.accept(false);
  } else {
    endpoint.reject(returnCode);
  }

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

  endpoint.subscribeHandler(subscribe -> {

    endpoint.subscribeAcknowledge(subscribe.messageId(),
      subscribe.topicSubscriptions()
        .stream()
        .map(MqttTopicSubscription::qualityOfService)
        .collect(Collectors.toList()));

    endpoint.publish(this.topic, Buffer.buffer(this.message), subscribe.topicSubscriptions().get(0).qualityOfService(), false, false, publishSent -> {
      context.assertTrue(publishSent.succeeded());
      this.async.complete();
    });
  }).publishAcknowledgeHandler(messageId -> {

    log.info("Message [" + messageId + "] acknowledged");
    this.async.complete();
  }).publishReceivedHandler(messageId -> {

    endpoint.publishRelease(messageId);
  }).publishCompletionHandler(messageId -> {

    log.info("Message [" + messageId + "] acknowledged");
    this.async.complete();
  });

  endpoint.accept(false);
}
 
Example 5
Source File: MqttClientOutOfOrderAcksTest.java    From vertx-mqtt with Apache License 2.0 4 votes vote down vote up
private static void serverLogic(MqttEndpoint mqttEndpoint) {
  log.info("[SERVER] Client connected");

  mqttEndpoint.publishHandler(p -> {
    log.info("[SERVER] Received PUBLISH with message id = " + p.messageId());

    if (p.qosLevel().equals(MqttQoS.EXACTLY_ONCE))
      // when received last PUBLISH message to acknowledge
      if (p.messageId() == 3) {
        // send PUBREC in order 3 --> 2 --> 1 -->
        // the order here is different from QoS 1 because we want to add more shuffling
        mqttEndpoint.publishReceived(3);
        mqttEndpoint.publishReceived(2);
        mqttEndpoint.publishReceived(1);
      }

    if (p.qosLevel().equals(MqttQoS.AT_LEAST_ONCE))
      // when received last PUBLISH message to acknowledge
      if (p.messageId() == 3) {
        //puback in order 2 --> 1 --> 3 -->
        mqttEndpoint.publishAcknowledge(2);
        mqttEndpoint.publishAcknowledge(1);
        mqttEndpoint.publishAcknowledge(3);
      }
  });

  mqttEndpoint.publishReleaseHandler(pr -> {
    log.info("[SERVER] Receive PUBREL with message id = " + pr);
    // we use 1 here because the order of responded by client messages is  3->2->1
    // so we are waiting here for receiving the last message with id=1 PUBREL message from the client
    if (pr == 1) {
      // send PUBREL in order 2 --> 1 --> 3 -->
      mqttEndpoint.publishComplete(2);
      mqttEndpoint.publishComplete(1);
      mqttEndpoint.publishComplete(3);
    }
  });

  mqttEndpoint.disconnectHandler(d -> log.info("[SERVER] Client disconnected"));
  mqttEndpoint.accept(false);
}
 
Example 6
Source File: MqttServerUnsubscribeTest.java    From vertx-mqtt with Apache License 2.0 4 votes vote down vote up
@Override
protected void endpointHandler(MqttEndpoint endpoint, TestContext context) {

  endpoint.subscribeHandler(subscribe -> {

    List<MqttQoS> qos = new ArrayList<>();
    qos.add(subscribe.topicSubscriptions().get(0).qualityOfService());
    endpoint.subscribeAcknowledge(subscribe.messageId(), qos);

    this.subscribeAsync.complete();

  }).unsubscribeHandler(unsubscribe -> {

    endpoint.unsubscribeAcknowledge(unsubscribe.messageId());

    this.unsubscribeAsync.complete();
  });

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

  this.clientConnectedWithSsl = endpoint.isSsl();

  try {
    receivedClientPeerCertificates = endpoint.sslSession().getPeerCertificates();
    receivedClientCertificateValidated = true;
    log.info("Trusted client connected");
  } catch (SSLPeerUnverifiedException e) {
    if (disconnectUntrustedClient) {
      log.info("Disconnecting untrusted client");
      endpoint.close();
      this.async.complete();
      return;
    }
    log.info("Untrusted client connected");
  }

  endpoint.publishHandler(message -> {

    log.info("Just received message on [" + message.topicName() + "] payload [" + message.payload().toString(Charset.defaultCharset()) + "] with QoS [" + message.qosLevel() + "]");

    switch (message.qosLevel()) {

      case AT_LEAST_ONCE:

        endpoint.publishAcknowledge(message.messageId());
        this.async.complete();
        break;

      case EXACTLY_ONCE:

        endpoint.publishReceived(message.messageId());
        break;

      case AT_MOST_ONCE:

        this.async.complete();
        break;
    }

  }).publishReleaseHandler(messageId -> {

    endpoint.publishComplete(messageId);
    this.async.complete();
  });

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

  endpoint.publishHandler(message -> {

    log.info("Just received message on [" + message.topicName() + "] payload [" + message.payload().toString(Charset.defaultCharset()) + "] with QoS [" + message.qosLevel() + "]");

    switch (message.qosLevel()) {

      case AT_LEAST_ONCE:

        endpoint.publishAcknowledge(message.messageId());
        this.async.complete();
        break;

      case EXACTLY_ONCE:

        endpoint.publishReceived(message.messageId());
        break;

      case AT_MOST_ONCE:

        this.async.complete();
        break;
    }

  }).publishReleaseHandler(messageId -> {

    endpoint.publishComplete(messageId);
    this.async.complete();
  });

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

  this.endpoint = endpoint;

  endpoint.disconnectHandler(v -> {

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

  });

  endpoint.accept(false);
}
 
Example 11
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 12
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);
}
 
Example 13
Source File: MqttServerClientPublishTest.java    From vertx-mqtt with Apache License 2.0 3 votes vote down vote up
@Override
protected void endpointHandler(MqttEndpoint endpoint, TestContext context) {

  endpoint.publishHandler(message -> {

    log.info("Just received message on [" + message.topicName() + "] payload [" + message.payload().toString(Charset.defaultCharset()) + "] with QoS [" + message.qosLevel() + "]");

    switch (message.qosLevel()) {

      case AT_LEAST_ONCE:

        endpoint.publishAcknowledge(message.messageId());
        this.async.complete();
        break;

      case EXACTLY_ONCE:

        endpoint.publishReceived(message.messageId());
        break;

      case AT_MOST_ONCE:

        this.async.complete();
        break;
    }

  }).publishReleaseHandler(messageId -> {

    endpoint.publishComplete(messageId);
    this.async.complete();
  });

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

    endpoint.accept(false);
  }