Java 类io.netty.handler.codec.mqtt.MqttConnAckMessage 实例源码

项目:lannister    文件:ConnectReceiverTest.java   
@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);
}
项目:lannister    文件:ConnectReceiverTest.java   
@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);
}
项目:lannister    文件:ConnectReceiverTest.java   
private MqttConnAckMessage executeNormalChannelRead0(String clientId, boolean cleanSession, ChannelId channelId)
        throws Exception {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false,
            10);
    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader("MQTT", 4, true, true, true, 0, true,
            cleanSession, 60);
    MqttConnectPayload payload = new MqttConnectPayload(clientId, "willtopic", "willmessage", "username",
            "password");

    MqttConnectMessage msg = new MqttConnectMessage(fixedHeader, variableHeader, payload);

    ChannelId cid = channelId == null ? TestUtil.newChannelId(clientId, false) : channelId;

    EmbeddedChannel channel = new EmbeddedChannel(cid, new ConnectReceiver());

    channel.writeInbound(msg);

    return channel.readOutbound();
}
项目:lannister    文件:MqttMessageFactory.java   
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);
}
项目:lannister    文件:ConnectReceiverTest.java   
@Test
public void testCleanSessionWithoutClientIdReturnFalse() throws Exception {
    Settings.INSTANCE.setProperty("mqttserver.acceptEmptyClientId", "false");
    MqttConnAckMessage ret = executeNormalChannelRead0("", true, null);

    Assert.assertEquals(MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED,
            ret.variableHeader().connectReturnCode());
    Settings.INSTANCE.setProperty("mqttserver.acceptEmptyClientId", "true");
}
项目:lannister    文件:ConnectReceiverTest.java   
@Test
public void nonCleanSession() throws Exception {
    String clientId = TestUtil.newClientId();

    MqttConnAckMessage ret = executeNormalChannelRead0(clientId, false, null);

    Assert.assertEquals(ret.variableHeader().connectReturnCode(), MqttConnectReturnCode.CONNECTION_ACCEPTED);
}
项目:moquette    文件:NettyChannelAssertions.java   
static void assertEqualsConnAck(String msg, MqttConnectReturnCode expectedCode, Object connAck) {
    assertTrue("connAck is not an instance of ConnAckMessage", connAck instanceof MqttConnAckMessage);
    MqttConnAckMessage connAckMsg = (MqttConnAckMessage) connAck;

    if (msg == null)
        assertEquals(expectedCode, connAckMsg.variableHeader().connectReturnCode());
    else
        assertEquals(msg, expectedCode, connAckMsg.variableHeader().connectReturnCode());
}
项目:iothub    文件:MqttTransportHandler.java   
private MqttConnAckMessage createMqttConnAckMsg(MqttConnectReturnCode returnCode) {
  MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(CONNACK, false, AT_MOST_ONCE, false, 0);
  MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(returnCode, true);
  return new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader);
}
项目:iotplatform    文件:MqttTransportHandler.java   
private MqttConnAckMessage createMqttConnAckMsg(MqttConnectReturnCode returnCode) {
  MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(CONNACK, false, AT_MOST_ONCE, false, 0);
  MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(returnCode, true);
  return new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader);
}
项目:lannister    文件:MqttClient.java   
public MqttConnectReturnCode connect() throws InterruptedException {

        Class<? extends SocketChannel> socketChannelClass;

        if (Literals.NETTY_EPOLL.equals(Settings.INSTANCE.nettyTransportMode())) {
            group = new EpollEventLoopGroup(1, new DefaultThreadFactory("client"));
            socketChannelClass = EpollSocketChannel.class;
        }
        else {
            group = new NioEventLoopGroup(1, new DefaultThreadFactory("client"));
            socketChannelClass = NioSocketChannel.class;
        }

        bootstrap.group(group).channel(socketChannelClass).handler(new ChannelInitializer<SocketChannel>() {
            @Override
            protected void initChannel(SocketChannel ch) throws Exception {
                if ("mqtts".equalsIgnoreCase(uri.getScheme())) {
                    SslContext sslCtx = SslContextBuilder.forClient().trustManager(trustManagerFactory).build();

                    ch.pipeline().addLast(sslCtx.newHandler(ch.alloc(), uri.getHost(), uri.getPort()));
                }

                ch.pipeline().addLast(MqttDecoder.class.getName(), new MqttDecoder());
                ch.pipeline().addLast(MqttEncoder.class.getName(), MqttEncoder.INSTANCE);
                ch.pipeline().addLast(MqttPacketReceiver.class.getName(),
                        new MqttPacketReceiver(MqttClient.this, receiver, sharedObject));
            }
        });

        channel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();

        normalizeMessage(options.will());
        send(MqttMessageFactory.connect(options));

        synchronized (sharedObject.locker()) {
            int timeout = Settings.INSTANCE.getInt("mqttclient.responseTimeoutSeconds", 15);

            sharedObject.locker().wait(timeout * 1000);
        }
        if (sharedObject.receivedMessage() == null) { return null; }

        return ((MqttConnAckMessage) sharedObject.receivedMessage()).variableHeader().connectReturnCode();
    }
项目:lannister    文件:ConnectReceiverTest.java   
@Test
public void testDefaultChannelRead0() throws Exception {
    MqttConnAckMessage ret = executeNormalChannelRead0(TestUtil.newClientId(), true, null);

    Assert.assertEquals(ret.variableHeader().connectReturnCode(), MqttConnectReturnCode.CONNECTION_ACCEPTED);
}
项目:lannister    文件:ConnectReceiverTest.java   
@Test
public void testCleanSessionWithoutClientIdReturnTrue() throws Exception {
    MqttConnAckMessage ret = executeNormalChannelRead0("", true, null);

    Assert.assertEquals(MqttConnectReturnCode.CONNECTION_ACCEPTED, ret.variableHeader().connectReturnCode());
}
项目:blynk-server    文件:MqttHardwareLoginHandler.java   
private static MqttConnAckMessage createConnAckMessage(MqttConnectReturnCode code) {
    MqttFixedHeader mqttFixedHeader =
            new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 2);
    MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(code, true);
    return new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader);
}
项目:activemq-artemis    文件:MQTTProtocolHandler.java   
void sendConnack(MqttConnectReturnCode returnCode) {
   MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
   MqttConnAckVariableHeader varHeader = new MqttConnAckVariableHeader(returnCode, true);
   MqttConnAckMessage message = new MqttConnAckMessage(fixedHeader, varHeader);
   sendToClient(message);
}
项目:lannister    文件:ConnectReceiverTest.java   
@Test
public void cleanSessionOnSameClientIdSession() throws Exception {
    String clientId = TestUtil.newClientId();

    executeNormalChannelRead0(clientId, true, null);

    MqttConnAckMessage ret = executeNormalChannelRead0(clientId, true, null);

    Assert.assertEquals(ret.variableHeader().connectReturnCode(), MqttConnectReturnCode.CONNECTION_ACCEPTED);
}