当前位置: 首页>>代码示例>>Java>>正文


Java MqttConnectReturnCode类代码示例

本文整理汇总了Java中io.netty.handler.codec.mqtt.MqttConnectReturnCode的典型用法代码示例。如果您正苦于以下问题:Java MqttConnectReturnCode类的具体用法?Java MqttConnectReturnCode怎么用?Java MqttConnectReturnCode使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MqttConnectReturnCode类属于io.netty.handler.codec.mqtt包,在下文中一共展示了MqttConnectReturnCode类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: processAuthTokenConnect

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
private void processAuthTokenConnect(ChannelHandlerContext ctx, MqttConnectMessage msg) {
  String userName = msg.payload().userName();
  String clientIdentifier = msg.payload().clientIdentifier();
  if (StringUtils.isEmpty(userName)) {
    // ctx.writeAndFlush(createMqttConnAckMsg(CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD));
    // ctx.close();
    ctx.writeAndFlush(createMqttConnAckMsg(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD));
    connected = false;
  } else {
    boolean login = deviceSessionCtx.login(new DeviceTokenCredentials(userName));
    if (!login) {
      ctx.writeAndFlush(createMqttConnAckMsg(CONNECTION_REFUSED_NOT_AUTHORIZED));
      connected = false;
    } else {
      MemoryMetaPool.registerClienId(clientIdentifier, ctx.channel());

      ctx.writeAndFlush(createMqttConnAckMsg(CONNECTION_ACCEPTED));
      connected = true;
      checkGatewaySession();
    }
    // }
  }

}
 
开发者ID:osswangxining,项目名称:iothub,代码行数:25,代码来源:MqttTransportHandler.java

示例2: handleEndpointConnection

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
/**
 * Invoked when a client sends its <em>CONNECT</em> packet.
 * <p>
 * Authenticates the client (if required) and registers handlers for processing
 * messages published by the client.
 * 
 * @param endpoint The MQTT endpoint representing the client.
 */
final void handleEndpointConnection(final MqttEndpoint endpoint) {

    LOG.debug("connection request from client [clientId: {}]", endpoint.clientIdentifier());

    if (!isConnected()) {
        endpoint.reject(MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE);
        LOG.debug("connection request from client [clientId: {}] rejected: {}",
                endpoint.clientIdentifier(), MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE);

    } else {
        if (getConfig().isAuthenticationRequired()) {
            handleEndpointConnectionWithAuthentication(endpoint);
        } else {
            handleEndpointConnectionWithoutAuthentication(endpoint);
        }
    }
}
 
开发者ID:eclipse,项目名称:hono,代码行数:26,代码来源:AbstractVertxBasedMqttProtocolAdapter.java

示例3: testEndpointHandlerRejectsUnauthenticatedDevices

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
/**
 * Verifies that an adapter that is configured to require devices to authenticate,
 * rejects connections from devices not providing any credentials.
 */
@Test
public void testEndpointHandlerRejectsUnauthenticatedDevices() {

    // GIVEN an adapter that does require devices to authenticate
    MqttServer server = getMqttServer(false);
    AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);

    forceClientMocksToConnected();

    // WHEN a device connects without providing any credentials
    MqttEndpoint endpoint = mock(MqttEndpoint.class);
    adapter.handleEndpointConnection(endpoint);

    // THEN the connection is refused
    verify(endpoint).reject(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD);
}
 
开发者ID:eclipse,项目名称:hono,代码行数:21,代码来源:AbstractVertxBasedMqttProtocolAdapterTest.java

示例4: generateClientId

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
private String generateClientId(ChannelHandlerContext ctx, boolean cleanSession) {
	if (cleanSession) {
		if (Settings.INSTANCE.getBoolean("mqttserver.acceptEmptyClientId", true)) {
			return "Lannister_"
					+ Long.toString(ClusterDataFactory.INSTANCE.createIdGenerator("clientIdGenerator").newId()); // [MQTT-3.1.3-6],[MQTT-3.1.3-7]
		}
		else {
			sendNoneAcceptMessage(ctx, MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED);
			return null;
		}
	}
	else {
		sendNoneAcceptMessage(ctx, MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED); // [MQTT-3.1.3-8]
		return null;
	}
}
 
开发者ID:anyflow,项目名称:lannister,代码行数:17,代码来源:ConnectReceiver.java

示例5: testCONNECTION_REFUSED_SERVER_UNAVAILABLE

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
@Test
public void testCONNECTION_REFUSED_SERVER_UNAVAILABLE() throws Exception {
	ServiceChecker prev = Plugins.INSTANCE.put(ServiceChecker.class, new ServiceChecker() {
		@Override
		public Plugin clone() {
			return this;
		}

		@Override
		public boolean isServiceAvailable() {
			return false;
		}
	});

	MqttConnAckMessage ret = executeNormalChannelRead0(TestUtil.newClientId(), true, null);

	Assert.assertEquals(ret.variableHeader().connectReturnCode(),
			MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE);

	Plugins.INSTANCE.put(ServiceChecker.class, prev);
}
 
开发者ID:anyflow,项目名称:lannister,代码行数:22,代码来源:ConnectReceiverTest.java

示例6: testCONNECTION_REFUSED_NOT_AUTHORIZED

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
@Test
public void testCONNECTION_REFUSED_NOT_AUTHORIZED() throws Exception {
	Authorizer prev = Plugins.INSTANCE.put(Authorizer.class, new Authorizer() {
		@Override
		public Plugin clone() {
			return this;
		}

		@Override
		public boolean isAuthorized(String clientId, String username) {
			return false;
		}
	});

	MqttConnAckMessage ret = executeNormalChannelRead0(TestUtil.newClientId(), true, null);

	Assert.assertEquals(ret.variableHeader().connectReturnCode(),
			MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED);

	Plugins.INSTANCE.put(Authorizer.class, prev);
}
 
开发者ID:anyflow,项目名称:lannister,代码行数:22,代码来源:ConnectReceiverTest.java

示例7: testLive

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
@Test
public void testLive() throws Exception {
	ConnectOptions options = new ConnectOptions();
	options.clientId(TestUtil.newClientId());

	MqttClient client = new MqttClient("mqtt://localhost:" + Settings.INSTANCE.mqttPort());
	MqttConnectReturnCode ret = client.connectOptions(options).connect();

	Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, ret);

	Assert.assertTrue(client.isConnected());

	HttpClient httpClient = new HttpClient(
			"http://localhost:" + Settings.INSTANCE.httpPort() + "/api/sessions?filter=live");
	HttpResponse res = httpClient.get();

	Assert.assertEquals(HttpResponseStatus.OK, res.status());
	Assert.assertEquals(new Integer(1), JsonPath.read(res.content().toString(CharsetUtil.UTF_8), "$.length()"));

	client.disconnect(true);

	Assert.assertFalse(client.isConnected());
}
 
开发者ID:anyflow,项目名称:lannister,代码行数:24,代码来源:SessionsTest.java

示例8: testWillToNullOnNormalDisconnect

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
@Test
public void testWillToNullOnNormalDisconnect() throws Exception {
	String willTopic = "will";
	String message = "ASTALAVISTA";

	String clientId = TestUtil.newClientId();
	ConnectOptions options = new ConnectOptions();
	options.clientId(clientId);
	options.will(
			new Message(-1, willTopic, null, message.getBytes(CharsetUtil.UTF_8), MqttQoS.AT_LEAST_ONCE, false));
	options.cleanSession(false);

	MqttClient client0 = new MqttClient("mqtt://localhost:" + Settings.INSTANCE.mqttPort());
	MqttConnectReturnCode ret = client0.connectOptions(options).connect();

	Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, ret);
	Assert.assertTrue(client0.isConnected());

	Assert.assertTrue(Session.NEXUS.get(clientId).will() != null
			&& Session.NEXUS.get(clientId).will().topicName().equals(willTopic));

	client0.disconnect(true);

	Thread.sleep(100);
	Assert.assertNull(Session.NEXUS.get(clientId).will());
}
 
开发者ID:anyflow,项目名称:lannister,代码行数:27,代码来源:WillTest.java

示例9: handleConnack

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
/**
 * Used for calling the connect handler when the server replies to the request
 *
 * @param msg  connection response message
 */
void handleConnack(MqttConnAckMessage msg) {

  synchronized (this.connection) {

    this.isConnected = msg.code() == MqttConnectReturnCode.CONNECTION_ACCEPTED;

    if (this.connectHandler != null) {

      if (msg.code() == MqttConnectReturnCode.CONNECTION_ACCEPTED) {
        this.connectHandler.handle(Future.succeededFuture(msg));
      } else {
        MqttConnectionException exception = new MqttConnectionException(msg.code());
        log.error(String.format("Connection refused by the server - code: %s", msg.code()));
        this.connectHandler.handle(Future.failedFuture(exception));
      }
    }
  }
}
 
开发者ID:vert-x3,项目名称:vertx-mqtt,代码行数:24,代码来源:MqttClientImpl.java

示例10: refusedBadUsernamePassword

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
@Test
public void refusedBadUsernamePassword(TestContext context) {

  this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD;

  try {
    MemoryPersistence persistence = new MemoryPersistence();
    MqttConnectOptions options = new MqttConnectOptions();
    options.setUserName("wrong_username");
    options.setPassword("wrong_password".toCharArray());
    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_FAILED_AUTHENTICATION);
  }
}
 
开发者ID:vert-x3,项目名称:vertx-mqtt,代码行数:18,代码来源:MqttServerConnectionTest.java

示例11: refusedUnacceptableProtocolVersion

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
@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);
  }
}
 
开发者ID:vert-x3,项目名称:vertx-mqtt,代码行数:18,代码来源:MqttServerConnectionTest.java

示例12: connectionAlreadyAccepted

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
@Test
public void connectionAlreadyAccepted(TestContext context) throws Exception {

  this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_ACCEPTED;

  MemoryPersistence persistence = new MemoryPersistence();
  MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
  client.connect();

  try {
    // try to accept a connection already accepted
    this.endpoint.accept(false);
    context.fail();
  } catch (IllegalStateException e) {
    // Ok
  }
}
 
开发者ID:vert-x3,项目名称:vertx-mqtt,代码行数:18,代码来源:MqttServerConnectionTest.java

示例13: refusedClientIdZeroBytes

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
@Test
public void refusedClientIdZeroBytes(TestContext context) {

  this.expectedReturnCode = MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;

  try {
    MemoryPersistence persistence = new MemoryPersistence();
    MqttConnectOptions options = new MqttConnectOptions();
    options.setCleanSession(false);
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
    MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "", persistence);
    client.connect(options);
    context.fail();
  } catch (MqttException e) {
    context.assertTrue(e.getReasonCode() == MqttException.REASON_CODE_INVALID_CLIENT_ID);
    context.assertNotNull(rejection);
  }
}
 
开发者ID:vert-x3,项目名称:vertx-mqtt,代码行数:19,代码来源:MqttServerConnectionTest.java

示例14: testEndpointHandlerFailsWithoutConnect

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
/**
 * Verifies that a connection attempt from a device is refused if the adapter is not
 * connected to all of the services it depends on.
 */
@Test
public void testEndpointHandlerFailsWithoutConnect() {

    // GIVEN an endpoint
    MqttEndpoint endpoint = mock(MqttEndpoint.class);

    MqttServer server = getMqttServer(false);
    AbstractVertxBasedMqttProtocolAdapter<ProtocolAdapterProperties> adapter = getAdapter(server);

    adapter.handleEndpointConnection(endpoint);
    verify(endpoint).reject(MqttConnectReturnCode.CONNECTION_REFUSED_SERVER_UNAVAILABLE);
}
 
开发者ID:eclipse,项目名称:hono,代码行数:17,代码来源:AbstractVertxBasedMqttProtocolAdapterTest.java

示例15: connack

import io.netty.handler.codec.mqtt.MqttConnectReturnCode; //导入依赖的package包/类
public static MqttConnAckMessage connack(MqttConnectReturnCode returnCode, boolean sessionPresent) {
	MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false,
			2);
	MqttConnAckVariableHeader variableHeader = new MqttConnAckVariableHeader(returnCode, sessionPresent);

	return new MqttConnAckMessage(fixedHeader, variableHeader);
}
 
开发者ID:anyflow,项目名称:lannister,代码行数:8,代码来源:MqttMessageFactory.java


注:本文中的io.netty.handler.codec.mqtt.MqttConnectReturnCode类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。