2017-04-12 17:40:32 +02:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2012-2017 The original author or authors
|
|
|
|
|
* ------------------------------------------------------
|
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
|
* and Apache License v2.0 which accompanies this distribution.
|
|
|
|
|
*
|
|
|
|
|
* The Eclipse Public License is available at
|
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
|
*
|
|
|
|
|
* The Apache License v2.0 is available at
|
|
|
|
|
* http://www.opensource.org/licenses/apache2.0.php
|
|
|
|
|
*
|
|
|
|
|
* You may elect to redistribute this code under either of these licenses.
|
|
|
|
|
*/
|
2017-02-01 15:52:35 +01:00
|
|
|
|
2016-12-11 11:42:10 +01:00
|
|
|
package io.moquette.spi.impl;
|
|
|
|
|
|
2017-01-11 11:23:24 +01:00
|
|
|
import io.moquette.server.ConnectionDescriptorStore;
|
2016-12-11 11:42:10 +01:00
|
|
|
import io.moquette.spi.ClientSession;
|
2017-01-28 10:06:30 +01:00
|
|
|
import io.netty.handler.codec.mqtt.MqttPublishMessage;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttQoS;
|
2016-12-11 11:42:10 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2017-01-09 21:53:26 +01:00
|
|
|
import static io.moquette.spi.impl.ProtocolProcessor.asStoredMessage;
|
2017-01-28 10:06:30 +01:00
|
|
|
import static io.netty.handler.codec.mqtt.MqttQoS.AT_MOST_ONCE;
|
2016-12-13 22:54:59 +01:00
|
|
|
|
2016-12-13 00:46:19 +01:00
|
|
|
class PersistentQueueMessageSender {
|
2016-12-11 11:42:10 +01:00
|
|
|
|
2016-12-13 00:46:19 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(PersistentQueueMessageSender.class);
|
2017-01-11 11:23:24 +01:00
|
|
|
private final ConnectionDescriptorStore connectionDescriptorStore;
|
2016-12-11 11:42:10 +01:00
|
|
|
|
2017-04-12 17:40:32 +02:00
|
|
|
PersistentQueueMessageSender(ConnectionDescriptorStore connectionDescriptorStore) {
|
2017-01-11 11:23:24 +01:00
|
|
|
this.connectionDescriptorStore = connectionDescriptorStore;
|
2016-12-11 11:42:10 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
void sendPublish(ClientSession clientsession, MqttPublishMessage pubMessage) {
|
2016-12-11 11:42:10 +01:00
|
|
|
String clientId = clientsession.clientID;
|
2017-02-01 15:52:35 +01:00
|
|
|
final int messageId = pubMessage.variableHeader().messageId();
|
|
|
|
|
final String topicName = pubMessage.variableHeader().topicName();
|
2017-06-29 09:23:23 +02:00
|
|
|
MqttQoS qos = pubMessage.fixedHeader().qosLevel();
|
2016-12-11 11:42:10 +01:00
|
|
|
if (LOG.isDebugEnabled()) {
|
2017-04-12 17:40:32 +02:00
|
|
|
LOG.debug("Sending PUBLISH message. MessageId={}, CId={}, topic={}, qos={}, payload={}", messageId,
|
2017-06-29 09:23:23 +02:00
|
|
|
clientId, topicName, qos, DebugUtils.payload2Str(pubMessage.payload()));
|
2017-01-11 11:23:24 +01:00
|
|
|
} else {
|
2017-02-01 15:52:35 +01:00
|
|
|
LOG.info("Sending PUBLISH message. MessageId={}, CId={}, topic={}", messageId, clientId, topicName);
|
2016-12-11 11:42:10 +01:00
|
|
|
}
|
|
|
|
|
|
2017-02-01 15:52:35 +01:00
|
|
|
boolean messageDelivered = connectionDescriptorStore.sendMessage(pubMessage, messageId, clientId);
|
2017-01-28 10:06:30 +01:00
|
|
|
|
2017-06-29 09:23:23 +02:00
|
|
|
if (!messageDelivered) {
|
|
|
|
|
if (qos != AT_MOST_ONCE && !clientsession.isCleanSession()) {
|
|
|
|
|
LOG.warn("PUBLISH message could not be delivered. It will be stored. MessageId={}, CId={}, topic={}, "
|
2017-04-12 17:40:32 +02:00
|
|
|
+ "qos={}, cleanSession={}", messageId, clientId, topicName, qos, false);
|
2017-06-29 09:23:23 +02:00
|
|
|
clientsession.enqueue(asStoredMessage(pubMessage));
|
|
|
|
|
} else {
|
|
|
|
|
LOG.warn("PUBLISH message could not be delivered. It will be discarded. MessageId={}, CId={}, topic={}, " +
|
|
|
|
|
"qos={}, cleanSession={}", messageId, clientId, topicName, qos, true);
|
|
|
|
|
}
|
2017-02-01 15:52:35 +01:00
|
|
|
}
|
2016-12-11 11:42:10 +01:00
|
|
|
}
|
|
|
|
|
}
|