Search in sources :

Example 1 with MQTTSession

use of com.swiftmq.impl.mqtt.session.MQTTSession in project swiftmq-ce by iitsoftware.

the class MQTTConnection method visit.

@Override
public void visit(POConnect po) {
    if (closed || protocolInvalid)
        return;
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.mqttSwiftlet.getName(), toString() + ", visit, po=" + po + " ...");
    if (nConnectPackets > 0) {
        protocolInvalid = true;
        initiateClose("protocol error, multiple connect packets");
        return;
    }
    nConnectPackets++;
    connectMessage = po.getMessage();
    MqttConnectVariableHeader variableConnectHeader = connectMessage.variableHeader();
    MqttConnectPayload payload = connectMessage.payload();
    clientId = payload.clientIdentifier();
    if (clientId == null || clientId.length() == 0) {
        if (!variableConnectHeader.isCleanSession())
            rc = MqttConnectReturnCode.CONNECTION_REFUSED_IDENTIFIER_REJECTED;
        else
            clientId = UUID.randomUUID().toString();
    }
    if (rc == null) {
        String password = null;
        if (variableConnectHeader.hasUserName())
            username = payload.userName();
        if (variableConnectHeader.hasPassword())
            password = new String(payload.passwordInBytes());
        try {
            ctx.authSwiftlet.verifyHostLogin(username, remoteHostname);
            String pwd = ctx.authSwiftlet.getPassword(username);
            if (password == pwd || password != null && password.equals(pwd)) {
                rc = MqttConnectReturnCode.CONNECTION_ACCEPTED;
                activeLogin = ctx.authSwiftlet.createActiveLogin(username, "MQTT");
                activeLogin.setClientId(clientId);
                authenticated = true;
            } else
                throw new AuthenticationException("invalid password");
            keepaliveInterval = (long) ((double) (variableConnectHeader.keepAliveTimeSeconds() * 1000.0) * 1.5);
            if (keepaliveInterval > 0) {
                ctx.timerSwiftlet.addTimerListener(keepaliveInterval, this);
            }
            if (variableConnectHeader.isWillFlag())
                will = new Will(payload.willTopic(), variableConnectHeader.willQos(), variableConnectHeader.isWillRetain(), payload.willMessageInBytes());
            cleanSession = variableConnectHeader.isCleanSession();
            if (!cleanSession) {
                ctx.sessionRegistry.associateSession(clientId, this);
            } else {
                ctx.sessionRegistry.removeSession(clientId);
                MQTTSession session = new MQTTSession(ctx, clientId, false);
                session.associate(this);
                associated(session);
            }
            hasLastWill = variableConnectHeader.isWillFlag();
            lastWillRetain = variableConnectHeader.isWillRetain();
            lastWillQoS = MqttQoS.valueOf(variableConnectHeader.willQos());
            lastWillTopic = payload.willTopic();
            lastWillPayload = new ByteBuf(payload.willMessageInBytes());
            try {
                usage.getProperty("client-id").setValue(clientId);
                usage.getProperty("username").setValue(username);
                usage.getProperty("mqtt-protlevel").setValue(variableConnectHeader.version());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (AuthenticationException e) {
            rc = MqttConnectReturnCode.CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD;
        } catch (ResourceLimitException e) {
            rc = MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED;
        }
    }
    MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, connectMessage.fixedHeader().isDup(), connectMessage.fixedHeader().qosLevel(), connectMessage.fixedHeader().isRetain(), 2);
    MqttConnAckVariableHeader variableHeader = new MqttConnAckVariableHeader(rc, false);
    connAckMessage = new MqttConnAckMessage(fixedHeader, variableHeader);
    if (rc != MqttConnectReturnCode.CONNECTION_ACCEPTED) {
        protocolInvalid = true;
        initiateClose("not authenticated");
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.mqttSwiftlet.getName(), toString() + ", visit, po=" + po + " done");
}
Also used : MQTTSession(com.swiftmq.impl.mqtt.session.MQTTSession) AuthenticationException(com.swiftmq.swiftlet.auth.AuthenticationException) ByteBuf(com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf) AuthenticationException(com.swiftmq.swiftlet.auth.AuthenticationException) ResourceLimitException(com.swiftmq.swiftlet.auth.ResourceLimitException) ResourceLimitException(com.swiftmq.swiftlet.auth.ResourceLimitException)

Aggregations

MQTTSession (com.swiftmq.impl.mqtt.session.MQTTSession)1 ByteBuf (com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf)1 AuthenticationException (com.swiftmq.swiftlet.auth.AuthenticationException)1 ResourceLimitException (com.swiftmq.swiftlet.auth.ResourceLimitException)1