2016-12-11 10:45:36 +01:00
|
|
|
package io.moquette.spi.impl;
|
|
|
|
|
|
|
|
|
|
import io.moquette.parser.proto.messages.*;
|
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;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import static io.moquette.spi.impl.ProtocolProcessor.asStoredMessage;
|
|
|
|
|
|
2017-01-11 18:42:30 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
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,
|
2016-12-13 23:40:00 +01:00
|
|
|
IMessagesStore messagesStore, BrokerInterceptor interceptor,
|
2017-01-11 11:23:24 +01:00
|
|
|
ConnectionDescriptorStore connectionDescriptors,
|
2016-12-13 23:40:00 +01:00
|
|
|
ISessionsStore sessionsStore, String brokerPort, MessagesPublisher messagesPublisher) {
|
2017-01-11 18:42:30 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void receivedPublishQos2(Channel channel, PublishMessage msg) {
|
|
|
|
|
final String topic = msg.getTopicName();
|
|
|
|
|
//check if the topic can be wrote
|
|
|
|
|
if (checkWriteOnTopic(topic, channel)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final Integer messageID = msg.getMessageID();
|
|
|
|
|
|
|
|
|
|
IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg);
|
|
|
|
|
String clientID = NettyUtils.clientID(channel);
|
|
|
|
|
toStoreMsg.setClientID(clientID);
|
2017-01-11 18:42:30 +01:00
|
|
|
|
|
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
|
LOG.debug("Sending publish message to subscribers. MqttClientId = {}, topic = {}, messageId = {}, payload = {}, subscriptionTree = {}.",
|
|
|
|
|
clientID, topic, msg.getMessageID(), DebugUtils.payload2Str(toStoreMsg.getMessage()),
|
|
|
|
|
subscriptions.dumpTree());
|
|
|
|
|
} else {
|
|
|
|
|
LOG.info("Sending publish message to subscribers. MqttClientId = {}, topic = {}, messageId = {}.", clientID,
|
|
|
|
|
topic, msg.getMessageID());
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 10:45:36 +01:00
|
|
|
//QoS2
|
|
|
|
|
MessageGUID guid = m_messagesStore.storePublishForFuture(toStoreMsg);
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
if (msg.isRetainFlag()) {
|
|
|
|
|
if (!msg.getPayload().hasRemaining()) {
|
|
|
|
|
m_messagesStore.cleanRetained(topic);
|
|
|
|
|
} else {
|
|
|
|
|
m_messagesStore.storeRetained(topic, guid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
String username = NettyUtils.userName(channel);
|
|
|
|
|
m_interceptor.notifyTopicPublished(msg, clientID, username);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Second phase of a publish QoS2 protocol, sent by publisher to the broker. Search the stored message and publish
|
|
|
|
|
* to all interested subscribers.
|
|
|
|
|
* */
|
|
|
|
|
void processPubRel(Channel channel, PubRelMessage msg) {
|
|
|
|
|
String clientID = NettyUtils.clientID(channel);
|
|
|
|
|
int messageID = msg.getMessageID();
|
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
|
|
|
|
|
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
|
LOG.debug("Sending publish message to subscribers. MqttClientId = {}, topic = {}, messageId = {}, payload = {}, subscriptionTree = {}.",
|
|
|
|
|
clientID, topic, msg.getMessageID(), DebugUtils.payload2Str(evt.getMessage()),
|
|
|
|
|
subscriptions.dumpTree());
|
|
|
|
|
} else {
|
|
|
|
|
LOG.info("Sending publish message to subscribers. MqttClientId = {}, topic = {}, messageId = {}.", clientID,
|
|
|
|
|
topic, msg.getMessageID());
|
|
|
|
|
}
|
2016-12-13 23:40:00 +01:00
|
|
|
this.publisher.publish2Subscribers(evt, topicMatchingSubscriptions);
|
2016-12-11 10:45:36 +01:00
|
|
|
|
|
|
|
|
if (evt.isRetained()) {
|
|
|
|
|
if (!evt.getMessage().hasRemaining()) {
|
|
|
|
|
m_messagesStore.cleanRetained(topic);
|
|
|
|
|
} else {
|
|
|
|
|
m_messagesStore.storeRetained(topic, evt.getGuid());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendPubComp(clientID, messageID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void sendPubRec(String clientID, int messageID) {
|
2017-01-11 18:42:30 +01:00
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
|
LOG.debug("Sending PUBREC message. MqttClientId = {}, messageId = {}.", clientID, messageID);
|
|
|
|
|
}
|
2016-12-11 10:45:36 +01:00
|
|
|
PubRecMessage pubRecMessage = new PubRecMessage();
|
|
|
|
|
pubRecMessage.setMessageID(messageID);
|
2017-01-11 11:23:24 +01:00
|
|
|
connectionDescriptors.sendMessage(pubRecMessage,messageID, clientID);
|
2016-12-11 10:45:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendPubComp(String clientID, int messageID) {
|
2017-01-11 18:42:30 +01:00
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
|
LOG.debug("Sending PUBCOMP message. MqttClientId = {}, messageId = {}.", clientID, messageID);
|
|
|
|
|
}
|
2016-12-11 10:45:36 +01:00
|
|
|
PubCompMessage pubCompMessage = new PubCompMessage();
|
|
|
|
|
pubCompMessage.setMessageID(messageID);
|
2017-01-11 11:23:24 +01:00
|
|
|
connectionDescriptors.sendMessage(pubCompMessage, messageID, clientID);
|
2016-12-11 10:45:36 +01:00
|
|
|
}
|
|
|
|
|
}
|