Search in sources :

Example 1 with ByteBuf

use of com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf 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)

Example 2 with ByteBuf

use of com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf in project swiftmq-ce by iitsoftware.

the class PacketDecoder method finishPacket.

private void finishPacket(Connection connection) throws IOException {
    if (traceSpace.enabled)
        trace("PacketDecoder/finishPacket");
    if (available() >= packetSize) {
        ByteBuf byteBuf = new ByteBuf(packetSize);
        byteBuf.writeBytes(buffer, readPos, packetSize);
        byteBuf.reset();
        try {
            mqttDecoder.decode(byteBuf, decoded);
            mqttListener.onMessage(decoded);
            decoded.clear();
        } catch (Exception e) {
            if (traceSpace.enabled)
                trace("PacketDecoder/finishPacket: exception=" + e);
            mqttListener.onException(e);
        }
        readPos += packetSize;
        packetCompleted(connection);
    }
}
Also used : ByteBuf(com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf) IOException(java.io.IOException)

Example 3 with ByteBuf

use of com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf in project swiftmq-ce by iitsoftware.

the class MQTTSession method visit.

@Override
public void visit(POSendMessage po) {
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.mqttSwiftlet.getName(), toString() + ", visit, po=" + po + " ...");
    try {
        BytesMessageImpl jmsMessage = (BytesMessageImpl) po.getJmsMessage();
        jmsMessage.reset();
        MqttQoS qos = po.getQos();
        String topicName = topicNameTranslateReverse(po.getTopicName());
        byte[] b = new byte[(int) jmsMessage.getBodyLength()];
        jmsMessage.readBytes(b);
        ByteBuf byteBuf = new ByteBuf(b);
        byteBuf.reset();
        int packetId = -1;
        if (qos != MqttQoS.AT_MOST_ONCE) {
            if (pid == 65535)
                pid = 1;
            packetId = pid++;
            outboundPackets.put(packetId, po);
        }
        mqttConnection.getOutboundQueue().enqueue(new MqttPublishMessage(new MqttFixedHeader(MqttMessageType.PUBLISH, false, qos, false, 0), new MqttPublishVariableHeader(topicName, packetId), byteBuf));
        if (qos == MqttQoS.AT_MOST_ONCE) {
            po.getTransaction().commit();
            po.getSubscription().restart();
        } else
            addReplay(packetId, new MqttPublishMessage(new MqttFixedHeader(MqttMessageType.PUBLISH, true, qos, false, 0), new MqttPublishVariableHeader(topicName, packetId), byteBuf));
        incMsgsReceived(1);
    } catch (QueueTransactionClosedException qtc) {
    } catch (Exception e) {
        mqttConnection.initiateClose("send message: exception=" + e);
    }
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.mqttSwiftlet.getName(), toString() + ", visit, po=" + po + " done");
}
Also used : QueueTransactionClosedException(com.swiftmq.swiftlet.queue.QueueTransactionClosedException) BytesMessageImpl(com.swiftmq.jms.BytesMessageImpl) ByteBuf(com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf) EntityAddException(com.swiftmq.mgmt.EntityAddException) QueueTransactionClosedException(com.swiftmq.swiftlet.queue.QueueTransactionClosedException)

Example 4 with ByteBuf

use of com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf in project swiftmq-ce by iitsoftware.

the class Producer method send.

public void send(MqttPublishMessage mqttPublishMessage) throws Exception {
    MqttFixedHeader fixedHeader = mqttPublishMessage.fixedHeader();
    ByteBuf payload = mqttPublishMessage.payload();
    BytesMessageImpl bytesMessage = new BytesMessageImpl();
    bytesMessage.setJMSDestination(topic);
    bytesMessage.setJMSTimestamp(System.currentTimeMillis());
    bytesMessage.setJMSMessageID(session.getMqttConnection().nextId());
    bytesMessage.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
    bytesMessage.setStringProperty(MessageImpl.PROP_USER_ID, session.getMqttConnection().getActiveLogin().getUserName());
    bytesMessage.setStringProperty(MessageImpl.PROP_CLIENT_ID, session.getClientId());
    if (fixedHeader.isDup())
        bytesMessage.setBooleanProperty(MessageImpl.PROP_DOUBT_DUPLICATE, true);
    bytesMessage.setIntProperty(PROP_QOS, fixedHeader.qosLevel().value());
    payload.reset();
    byte[] b = new byte[payload.size()];
    payload.readBytes(b);
    bytesMessage.writeBytes(b);
    transaction = sender.createTransaction();
    transaction.putMessage(bytesMessage);
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace(ctx.mqttSwiftlet.getName(), toString() + ", sent: " + bytesMessage);
}
Also used : MqttFixedHeader(com.swiftmq.impl.mqtt.v311.netty.handler.codec.mqtt.MqttFixedHeader) BytesMessageImpl(com.swiftmq.jms.BytesMessageImpl) ByteBuf(com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf)

Aggregations

ByteBuf (com.swiftmq.impl.mqtt.v311.netty.buffer.ByteBuf)4 BytesMessageImpl (com.swiftmq.jms.BytesMessageImpl)2 MQTTSession (com.swiftmq.impl.mqtt.session.MQTTSession)1 MqttFixedHeader (com.swiftmq.impl.mqtt.v311.netty.handler.codec.mqtt.MqttFixedHeader)1 EntityAddException (com.swiftmq.mgmt.EntityAddException)1 AuthenticationException (com.swiftmq.swiftlet.auth.AuthenticationException)1 ResourceLimitException (com.swiftmq.swiftlet.auth.ResourceLimitException)1 QueueTransactionClosedException (com.swiftmq.swiftlet.queue.QueueTransactionClosedException)1 IOException (java.io.IOException)1