Example usage for io.netty.handler.codec.mqtt MqttConnectVariableHeader MqttConnectVariableHeader

List of usage examples for io.netty.handler.codec.mqtt MqttConnectVariableHeader MqttConnectVariableHeader

Introduction

In this page you can find the example usage for io.netty.handler.codec.mqtt MqttConnectVariableHeader MqttConnectVariableHeader.

Prototype

public MqttConnectVariableHeader(String name, int version, boolean hasUserName, boolean hasPassword,
            boolean isWillRetain, int willQos, boolean isWillFlag, boolean isCleanSession,
            int keepAliveTimeSeconds) 

Source Link

Usage

From source file:io.crate.mqtt.protocol.MqttProcessorTest.java

@Test
public void testConnectWithWrongMqttVersion() throws Exception {
    EmbeddedChannel ch = new EmbeddedChannel();

    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_LEAST_ONCE,
            false, 0);//from   w w  w.  jav a  2 s. c o m
    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader("connect", (byte) 1, false, false,
            false, (byte) 1, false, false, 60);
    MqttConnectPayload payload = new MqttConnectPayload("mqttClient", "someTopic", new byte[0], null, null);
    processor.handleConnect(ch, (MqttConnectMessage) io.netty.handler.codec.mqtt.MqttMessageFactory
            .newMessage(fixedHeader, variableHeader, payload));

    MqttConnAckMessage response = ch.readOutbound();
    assertThat(response.variableHeader().connectReturnCode(),
            is(MqttConnectReturnCode.CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION));
    assertFalse(response.variableHeader().isSessionPresent());
}

From source file:io.moquette.server.ServerLowlevelMessagesIntegrationTests.java

License:Open Source License

private static MqttConnectMessage createConnectMessage(String clientID, int keepAlive) {
    MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE,
            false, 0);/*from  w  ww.  j av  a 2 s . c o  m*/
    MqttConnectVariableHeader mqttConnectVariableHeader = new MqttConnectVariableHeader(
            MqttVersion.MQTT_3_1.protocolName(), MqttVersion.MQTT_3_1.protocolLevel(), false, false, false, 1,
            false, true, keepAlive);
    MqttConnectPayload mqttConnectPayload = new MqttConnectPayload(clientID, null, null, null, null);
    return new MqttConnectMessage(mqttFixedHeader, mqttConnectVariableHeader, mqttConnectPayload);
}

From source file:io.netty.example.mqtt.heartBeat.MqttHeartBeatClientHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    MqttFixedHeader connectFixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false,
            MqttQoS.AT_MOST_ONCE, false, 0);
    MqttConnectVariableHeader connectVariableHeader = new MqttConnectVariableHeader(PROTOCOL_NAME_MQTT_3_1_1,
            PROTOCOL_VERSION_MQTT_3_1_1, true, true, false, 0, false, false, 20);
    MqttConnectPayload connectPayload = new MqttConnectPayload(clientId, null, null, userName, password);
    MqttConnectMessage connectMessage = new MqttConnectMessage(connectFixedHeader, connectVariableHeader,
            connectPayload);//from w ww  .j a  va 2 s. c  om
    ctx.writeAndFlush(connectMessage);
    System.out.println("Sent CONNECT");
}

From source file:io.vertx.mqtt.impl.MqttClientImpl.java

License:Apache License

private void doConnect(int port, String host, String serverName,
        Handler<AsyncResult<MqttConnAckMessage>> connectHandler) {

    log.debug(String.format("Trying to connect with %s:%d", host, port));
    this.client.connect(port, host, serverName, done -> {

        // the TCP connection fails
        if (done.failed()) {
            log.error(String.format("Can't connect to %s:%d", host, port), done.cause());
            if (connectHandler != null) {
                connectHandler.handle(Future.failedFuture(done.cause()));
            }//from ww  w .  j  a v  a  2  s  .com
        } else {
            log.info(String.format("Connection with %s:%d established successfully", host, port));

            NetSocketInternal soi = (NetSocketInternal) done.result();
            ChannelPipeline pipeline = soi.channelHandlerContext().pipeline();
            this.connectHandler = connectHandler;

            if (options.isAutoGeneratedClientId()
                    && (options.getClientId() == null || options.getClientId().isEmpty())) {
                options.setClientId(generateRandomClientId());
            }

            initChannel(pipeline);
            this.connection = new MqttClientConnection(this, soi, options);

            soi.messageHandler(msg -> connection.handleMessage(msg));
            soi.closeHandler(v -> handleClosed());

            // an exception at connection level
            soi.exceptionHandler(this::handleException);

            MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, AT_MOST_ONCE,
                    false, 0);

            MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader(PROTOCOL_NAME,
                    PROTOCOL_VERSION, options.hasUsername(), options.hasPassword(), options.isWillRetain(),
                    options.getWillQoS(), options.isWillFlag(), options.isCleanSession(),
                    options.getKeepAliveTimeSeconds());

            MqttConnectPayload payload = new MqttConnectPayload(
                    options.getClientId() == null ? "" : options.getClientId(), options.getWillTopic(),
                    options.getWillMessage() != null ? options.getWillMessage().getBytes(StandardCharsets.UTF_8)
                            : null,
                    options.hasUsername() ? options.getUsername() : null,
                    options.hasPassword() ? options.getPassword().getBytes() : null);

            io.netty.handler.codec.mqtt.MqttMessage connect = MqttMessageFactory.newMessage(fixedHeader,
                    variableHeader, payload);

            this.write(connect);
        }

    });
}

From source file:io.vertx.mqtt.test.server.MqttServerBadClientTest.java

License:Apache License

private MqttMessage createConnectPacket(MqttClientOptions options) {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE,
            false, 0);//from  w w  w  .jav  a2 s  .c o  m

    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader(PROTOCOL_NAME, PROTOCOL_VERSION,
            options.hasUsername(), options.hasPassword(), options.isWillRetain(), options.getWillQoS(),
            options.isWillFlag(), options.isCleanSession(), options.getKeepAliveTimeSeconds());

    MqttConnectPayload payload = new MqttConnectPayload(
            options.getClientId() == null ? "" : options.getClientId(), options.getWillTopic(),
            options.getWillMessage() != null ? options.getWillMessage().getBytes(StandardCharsets.UTF_8) : null,
            options.hasUsername() ? options.getUsername() : null,
            options.hasPassword() ? options.getPassword().getBytes(StandardCharsets.UTF_8) : null);

    return MqttMessageFactory.newMessage(fixedHeader, variableHeader, payload);
}

From source file:net.anyflow.lannister.message.MessageFactory.java

License:Apache License

public static MqttConnectMessage connect(ConnectOptions options) {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE,
            false, 10);// w w  w . j  ava  2s. c  om
    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader(options.version().protocolName(),
            options.version().protocolLevel(), options.userName() != null, options.password() != null,
            options.will() == null ? false : options.will().isRetain(),
            options.will() == null ? 0 : options.will().qos().value(), options.will() != null,
            options.cleanSession(), options.keepAliveTimeSeconds());

    MqttConnectPayload payload = new MqttConnectPayload(Strings.nullToEmpty(options.clientId()),
            options.will() == null ? "" : options.will().topicName(),
            options.will() == null ? "" : new String(options.will().message()),
            Strings.nullToEmpty(options.userName()), Strings.nullToEmpty(options.password()));

    return new MqttConnectMessage(fixedHeader, variableHeader, payload);
}

From source file:net.anyflow.lannister.packetreceiver.ConnectReceiverTest.java

License:Apache License

private MqttConnAckMessage executeNormalChannelRead0(String clientId, boolean cleanSession, ChannelId channelId)
        throws Exception {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE,
            false, 10);// www  . java2  s.c  om
    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();
}

From source file:org.thingsboard.mqtt.MqttChannelHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
    super.channelActive(ctx);

    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE,
            false, 0);//  w  w  w . j  av a  2 s.  c o m
    MqttConnectVariableHeader variableHeader = new MqttConnectVariableHeader(
            this.client.getClientConfig().getProtocolVersion().protocolName(), // Protocol Name
            this.client.getClientConfig().getProtocolVersion().protocolLevel(), // Protocol Level
            this.client.getClientConfig().getUsername() != null, // Has Username
            this.client.getClientConfig().getPassword() != null, // Has Password
            this.client.getClientConfig().getLastWill() != null // Will Retain
                    && this.client.getClientConfig().getLastWill().isRetain(),
            this.client.getClientConfig().getLastWill() != null // Will QOS
                    ? this.client.getClientConfig().getLastWill().getQos().value()
                    : 0,
            this.client.getClientConfig().getLastWill() != null, // Has Will
            this.client.getClientConfig().isCleanSession(), // Clean Session
            this.client.getClientConfig().getTimeoutSeconds() // Timeout
    );
    MqttConnectPayload payload = new MqttConnectPayload(this.client.getClientConfig().getClientId(),
            this.client.getClientConfig().getLastWill() != null
                    ? this.client.getClientConfig().getLastWill().getTopic()
                    : null,
            this.client.getClientConfig().getLastWill() != null
                    ? this.client.getClientConfig().getLastWill().getMessage().getBytes(CharsetUtil.UTF_8)
                    : null,
            this.client.getClientConfig().getUsername(),
            this.client.getClientConfig().getPassword() != null
                    ? this.client.getClientConfig().getPassword().getBytes(CharsetUtil.UTF_8)
                    : null);
    ctx.channel().writeAndFlush(new MqttConnectMessage(fixedHeader, variableHeader, payload));
}