2017-02-01 15:57:00 +01:00
|
|
|
|
2016-12-11 10:45:36 +01:00
|
|
|
package io.moquette.spi.impl;
|
|
|
|
|
|
2017-01-11 11:23:24 +01:00
|
|
|
import io.moquette.server.ConnectionDescriptorStore;
|
2016-12-11 10:45:36 +01:00
|
|
|
import io.moquette.server.netty.NettyUtils;
|
|
|
|
|
import io.moquette.spi.ClientSession;
|
|
|
|
|
import io.moquette.spi.IMessagesStore;
|
|
|
|
|
import io.moquette.spi.ISessionsStore;
|
|
|
|
|
import io.moquette.spi.MessageGUID;
|
|
|
|
|
import io.moquette.spi.impl.subscriptions.Subscription;
|
|
|
|
|
import io.moquette.spi.impl.subscriptions.SubscriptionsStore;
|
|
|
|
|
import io.moquette.spi.security.IAuthorizator;
|
|
|
|
|
import io.netty.channel.Channel;
|
2017-01-28 10:06:30 +01:00
|
|
|
import io.netty.handler.codec.mqtt.*;
|
2016-12-11 10:45:36 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
2017-01-28 10:06:30 +01:00
|
|
|
import java.util.List;
|
2016-12-11 10:45:36 +01:00
|
|
|
import static io.moquette.spi.impl.ProtocolProcessor.asStoredMessage;
|
2017-01-28 10:06:30 +01:00
|
|
|
import static io.moquette.spi.impl.Utils.messageId;
|
|
|
|
|
import static io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader.from;
|
|
|
|
|
import static io.netty.handler.codec.mqtt.MqttQoS.AT_MOST_ONCE;
|
2017-01-11 18:42:30 +01:00
|
|
|
|
2017-02-01 15:57:00 +01:00
|
|
|
class Qos2PublishHandler extends QosPublishHandler {
|
2016-12-11 10:45:36 +01:00
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Qos1PublishHandler.class);
|
|
|
|
|
|
|
|
|
|
private final SubscriptionsStore subscriptions;
|
|
|
|
|
private final IMessagesStore m_messagesStore;
|
|
|
|
|
private final BrokerInterceptor m_interceptor;
|
2017-01-11 11:23:24 +01:00
|
|
|
private final ConnectionDescriptorStore connectionDescriptors;
|
2016-12-11 10:45:36 +01:00
|
|
|
private final ISessionsStore m_sessionsStore;
|
2016-12-13 23:40:00 +01:00
|
|
|
private final MessagesPublisher publisher;
|
2016-12-11 10:45:36 +01:00
|
|
|
|
|
|
|
|
public Qos2PublishHandler(IAuthorizator authorizator, SubscriptionsStore subscriptions,
|
2017-02-01 15:57:00 +01:00
|
|
|
IMessagesStore messagesStore, BrokerInterceptor interceptor,
|
|
|
|
|
ConnectionDescriptorStore connectionDescriptors, ISessionsStore sessionsStore, String brokerPort,
|
|
|
|
|
MessagesPublisher messagesPublisher) {
|
|
|
|
|
super(authorizator);
|
2016-12-11 10:45:36 +01:00
|
|
|
this.subscriptions = subscriptions;
|
|
|
|
|
this.m_messagesStore = messagesStore;
|
|
|
|
|
this.m_interceptor = interceptor;
|
|
|
|
|
this.connectionDescriptors = connectionDescriptors;
|
|
|
|
|
this.m_sessionsStore = sessionsStore;
|
2016-12-13 23:40:00 +01:00
|
|
|
this.publisher = messagesPublisher;
|
2016-12-11 10:45:36 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
void receivedPublishQos2(Channel channel, MqttPublishMessage msg) {
|
|
|
|
|
final String topic = msg.variableHeader().topicName();
|
2017-02-01 15:57:00 +01:00
|
|
|
// check if the topic can be wrote
|
2016-12-11 10:45:36 +01:00
|
|
|
if (checkWriteOnTopic(topic, channel)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-01-28 10:06:30 +01:00
|
|
|
final int messageID = msg.variableHeader().messageId();
|
2016-12-11 10:45:36 +01:00
|
|
|
|
|
|
|
|
IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg);
|
|
|
|
|
String clientID = NettyUtils.clientID(channel);
|
|
|
|
|
toStoreMsg.setClientID(clientID);
|
2017-01-11 18:42:30 +01:00
|
|
|
|
2017-02-01 15:57:00 +01:00
|
|
|
if (LOG.isTraceEnabled()) {
|
|
|
|
|
LOG.trace(
|
|
|
|
|
"Sending publish message to subscribers. MqttClientId = {}, topic = {}, messageId = {}, "
|
|
|
|
|
+ "payload = {}, subscriptionTree = {}.",
|
|
|
|
|
clientID,
|
|
|
|
|
topic,
|
|
|
|
|
messageID,
|
|
|
|
|
DebugUtils.payload2Str(toStoreMsg.getMessage()),
|
|
|
|
|
subscriptions.dumpTree());
|
|
|
|
|
} else {
|
|
|
|
|
LOG.info(
|
|
|
|
|
"Sending publish message to subscribers. MqttClientId = {}, topic = {}, messageId = {}.",
|
|
|
|
|
clientID,
|
|
|
|
|
topic,
|
|
|
|
|
messageID);
|
|
|
|
|
}
|
2017-01-11 18:42:30 +01:00
|
|
|
|
2017-02-01 15:57:00 +01:00
|
|
|
// QoS2
|
2016-12-11 10:45:36 +01:00
|
|
|
MessageGUID guid = m_messagesStore.storePublishForFuture(toStoreMsg);
|
2017-02-01 15:57:00 +01:00
|
|
|
// TODO Don't send PUBREC for Hz publish notification, if (msg.isLocal()) {
|
|
|
|
|
sendPubRec(clientID, messageID);
|
|
|
|
|
// }
|
|
|
|
|
// Next the client will send us a pub rel
|
|
|
|
|
// NB publish to subscribers for QoS 2 happen upon PUBREL from publisher
|
2016-12-11 10:45:36 +01:00
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
if (msg.fixedHeader().isRetain()) {
|
|
|
|
|
if (msg.payload().readableBytes() == 0) {
|
2016-12-11 10:45:36 +01:00
|
|
|
m_messagesStore.cleanRetained(topic);
|
|
|
|
|
} else {
|
|
|
|
|
m_messagesStore.storeRetained(topic, guid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
String username = NettyUtils.userName(channel);
|
|
|
|
|
m_interceptor.notifyTopicPublished(msg, clientID, username);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-02-01 15:57:00 +01:00
|
|
|
* Second phase of a publish QoS2 protocol, sent by publisher to the broker. Search the stored
|
|
|
|
|
* message and publish to all interested subscribers.
|
|
|
|
|
*/
|
2017-01-28 10:06:30 +01:00
|
|
|
void processPubRel(Channel channel, MqttMessage msg) {
|
2016-12-11 10:45:36 +01:00
|
|
|
String clientID = NettyUtils.clientID(channel);
|
2017-01-28 10:06:30 +01:00
|
|
|
int messageID = messageId(msg);
|
2017-01-11 18:42:30 +01:00
|
|
|
LOG.info("Processing PUBREL message. MqttClientId = {}, messageId = {}.", clientID, messageID);
|
2016-12-11 10:45:36 +01:00
|
|
|
ClientSession targetSession = m_sessionsStore.sessionForClient(clientID);
|
|
|
|
|
IMessagesStore.StoredMessage evt = targetSession.storedMessage(messageID);
|
|
|
|
|
final String topic = evt.getTopic();
|
|
|
|
|
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
2017-01-11 18:42:30 +01:00
|
|
|
|
2017-02-01 15:57:00 +01:00
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
|
LOG.debug(
|
|
|
|
|
"Sending publish message to subscribers. "
|
|
|
|
|
+ "MqttClientId = {}, topic = {}, messageId = {}, payload = {}, subscriptionTree = {}.",
|
|
|
|
|
clientID,
|
|
|
|
|
topic,
|
|
|
|
|
messageID,
|
|
|
|
|
DebugUtils.payload2Str(evt.getMessage()),
|
|
|
|
|
subscriptions.dumpTree());
|
|
|
|
|
} else {
|
|
|
|
|
LOG.info(
|
|
|
|
|
"Sending publish message to subscribers. MqttClientId = {}, topic = {}, messageId = {}.",
|
|
|
|
|
clientID,
|
|
|
|
|
topic,
|
|
|
|
|
messageID);
|
|
|
|
|
}
|
2016-12-13 23:40:00 +01:00
|
|
|
this.publisher.publish2Subscribers(evt, topicMatchingSubscriptions);
|
2016-12-11 10:45:36 +01:00
|
|
|
|
|
|
|
|
if (evt.isRetained()) {
|
2017-01-28 10:06:30 +01:00
|
|
|
if (evt.getMessage().readableBytes() == 0) {
|
2016-12-11 10:45:36 +01:00
|
|
|
m_messagesStore.cleanRetained(topic);
|
|
|
|
|
} else {
|
|
|
|
|
m_messagesStore.storeRetained(topic, evt.getGuid());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendPubComp(clientID, messageID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendPubRec(String clientID, int messageID) {
|
2017-01-18 23:27:02 +01:00
|
|
|
LOG.debug("Sending PUBREC message. MqttClientId = {}, messageId = {}.", clientID, messageID);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBREC, false, AT_MOST_ONCE, false, 0);
|
|
|
|
|
MqttMessage pubRecMessage = new MqttMessage(fixedHeader, from(messageID));
|
2017-02-01 15:57:00 +01:00
|
|
|
connectionDescriptors.sendMessage(pubRecMessage, messageID, clientID);
|
2016-12-11 10:45:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendPubComp(String clientID, int messageID) {
|
2017-01-18 23:27:02 +01:00
|
|
|
LOG.debug("Sending PUBCOMP message. MqttClientId = {}, messageId = {}.", clientID, messageID);
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBCOMP, false, AT_MOST_ONCE, false, 0);
|
|
|
|
|
MqttMessage pubCompMessage = new MqttMessage(fixedHeader, from(messageID));
|
2017-01-11 11:23:24 +01:00
|
|
|
connectionDescriptors.sendMessage(pubCompMessage, messageID, clientID);
|
2016-12-11 10:45:36 +01:00
|
|
|
}
|
|
|
|
|
}
|