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

项目:lannister    文件:MqttMessageFactory.java   
public static MqttConnectMessage connect(ConnectOptions options) {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT, false, MqttQoS.AT_MOST_ONCE, false,
            10);
    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(), CharsetUtil.UTF_8),
            Strings.nullToEmpty(options.userName()), Strings.nullToEmpty(options.password()));

    return new MqttConnectMessage(fixedHeader, variableHeader, payload);
}
项目: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();
}
项目:j1st-mqtt    文件:MessageMetricsHandler.java   
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof MqttMessage) {
        MqttMessage mqtt = (MqttMessage) msg;
        if (StringUtils.isBlank(this.clientId) && mqtt.fixedHeader().messageType() == MqttMessageType.CONNECT) {
            this.clientId = ((MqttConnectPayload) mqtt.payload()).clientId();
        }
        if (StringUtils.isNotBlank(this.clientId)) {
            this.metrics.measurement(this.clientId, this.brokerId, MessageDirection.IN, mqtt.fixedHeader().messageType());
        }
        this.metrics.measurement(this.brokerId, MessageDirection.IN, mqtt.fixedHeader().messageType());
    }
    ctx.fireChannelRead(msg);
}
项目:vertx-mqtt    文件:MqttServerBadClientTest.java   
private MqttMessage createConnectPacket(MqttClientOptions options) {
  MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNECT,
    false,
    MqttQoS.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(StandardCharsets.UTF_8) : null
  );

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