Java Code Examples for io.netty.handler.codec.mqtt.MqttConnectReturnCode#CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION

The following examples show how to use io.netty.handler.codec.mqtt.MqttConnectReturnCode#CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION . 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: MqttServerConnectionTest.java    From vertx-mqtt with Apache License 2.0 6 votes vote down vote up
@Test
public void refusedUnacceptableProtocolVersion(TestContext context) {

  this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION;

  try {
    MemoryPersistence persistence = new MemoryPersistence();
    MqttConnectOptions options = new MqttConnectOptions();
    // trying the old 3.1
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1);
    MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
    client.connect(options);
    context.fail();
  } catch (MqttException e) {
    context.assertTrue(e.getReasonCode() == MqttException.REASON_CODE_INVALID_PROTOCOL_VERSION);
  }
}
 
Example 2
Source File: MqttProtocolUtil.java    From ext-opensource-netty with Mozilla Public License 2.0 5 votes vote down vote up
public static MqttConnectReturnCode connectReturnCodeForException(Throwable cause) {
	MqttConnectReturnCode code = MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE;
	if (cause instanceof MqttUnacceptableProtocolVersionException) {
		// 不支持的协议版本
		code = MqttConnectReturnCode.CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION;
	} else if (cause instanceof MqttIdentifierRejectedException) {
		// 不合格的clientId
		code = MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
	} else {
		code = MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE;
	}
	return code;
}
 
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;
}