Java Code Examples for io.netty.handler.codec.mqtt.MqttMessageType#CONNACK

The following examples show how to use io.netty.handler.codec.mqtt.MqttMessageType#CONNACK . 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: Connect.java    From WeEvent with Apache License 2.0 5 votes vote down vote up
public MqttMessage processConnect(MqttConnectMessage msg, SessionContext sessionData) {
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_LEAST_ONCE, false, 0);

    String clientId = sessionData.getClientId();
    if (StringUtils.isBlank(clientId)) {
        log.error("clientId is empty, reject");
        return MqttMessageFactory.newMessage(fixedHeader,
                new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED, false), null);
    }

    // verify userName and password
    String username = msg.payload().userName();
    String password = msg.payload().passwordInBytes() == null ? null : new String(msg.payload().passwordInBytes(), StandardCharsets.UTF_8);
    if (!this.authService.verifyUserName(username, password)) {
        log.error("verify account failed, reject");
        return MqttMessageFactory.newMessage(fixedHeader,
                new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD, false), null);
    }

    if (this.sessionStore.existSession(clientId)) {
        log.info("exist client id, force to delete the older");
        this.sessionStore.removeSession(clientId);
    }

    // store new session
    this.sessionStore.addSession(clientId, sessionData);

    log.info("MQTT connected, clientId: {}", clientId);
    return MqttMessageFactory.newMessage(fixedHeader,
            new MqttConnAckVariableHeader(MqttConnectReturnCode.CONNECTION_ACCEPTED, false), null);
}
 
Example 2
Source File: MqttProtocolHandler.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private MqttConnAckMessage sendAckToClient(Channel client, MqttConnectMessage connectMessage, MqttConnectReturnCode ackCode, boolean sessionPresent) {

        MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, connectMessage.fixedHeader().qosLevel(), false, 0);
        MqttConnAckMessage connAckMessage = (MqttConnAckMessage) MqttMessageFactory.newMessage(
                mqttFixedHeader,
                new MqttConnAckVariableHeader(ackCode, sessionPresent),
                null);
        client.writeAndFlush(connAckMessage);
        return connAckMessage;
    }
 
Example 3
Source File: MqttMessageFactory.java    From lannister with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 4
Source File: MqttEndpointImpl.java    From vertx-mqtt with Apache License 2.0 5 votes vote down vote up
private Future<Void> write(io.netty.handler.codec.mqtt.MqttMessage mqttMessage) {
  synchronized (this.conn) {
    if (mqttMessage.fixedHeader().messageType() != MqttMessageType.CONNACK) {
      this.checkConnected();
    }
    return this.conn.writeMessage(mqttMessage);
  }
}
 
Example 5
Source File: MessageUtil.java    From iot-mqtt with Apache License 2.0 4 votes vote down vote up
public static MqttConnAckMessage getConnectAckMessage(MqttConnectReturnCode returnCode,boolean sessionPresent){
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE, false, 0);
    MqttConnAckVariableHeader variableHeade = new MqttConnAckVariableHeader(returnCode,sessionPresent);
    return new MqttConnAckMessage(fixedHeader,variableHeade);
}
 
Example 6
Source File: ConnectAckHandler.java    From joyqueue with Apache License 2.0 4 votes vote down vote up
@Override
public MqttMessageType type() {
    return MqttMessageType.CONNACK;
}
 
Example 7
Source File: MqttHardwareLoginHandler.java    From blynk-server with GNU General Public License v3.0 4 votes vote down vote up
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);
}
 
Example 8
Source File: MQTTProtocolHandler.java    From activemq-artemis with Apache License 2.0 4 votes vote down vote up
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);
}