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: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;
|
2017-02-14 17:23:04 +01:00
|
|
|
import io.moquette.spi.impl.subscriptions.Topic;
|
2016-12-11 10:45:36 +01:00
|
|
|
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;
|
2017-04-04 21:29:38 +02:00
|
|
|
|
|
|
|
|
import static io.moquette.spi.impl.DebugUtils.payload2Str;
|
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
|
|
|
|
2017-04-04 21:29:38 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Qos2PublishHandler.class);
|
2016-12-11 10:45:36 +01:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2017-04-12 17:40:32 +02:00
|
|
|
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) {
|
2017-02-14 17:23:04 +01:00
|
|
|
final Topic topic = new 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-04-04 21:29:38 +02:00
|
|
|
LOG.info("Sending publish message to subscribers CId={}, topic={}, messageId={}", clientID, topic, messageID);
|
2017-02-01 15:57:00 +01:00
|
|
|
if (LOG.isTraceEnabled()) {
|
2017-04-04 21:29:38 +02:00
|
|
|
LOG.trace("payload={}, subs Tree={}", payload2Str(toStoreMsg.getMessage()), subscriptions.dumpTree());
|
2017-02-01 15:57:00 +01:00
|
|
|
}
|
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-04-04 21:29:38 +02:00
|
|
|
ClientSession sourceSession = m_sessionsStore.sessionForClient(clientID);
|
|
|
|
|
sourceSession.markAsInboundInflight(messageID, guid);
|
|
|
|
|
|
2017-02-01 15:57:00 +01:00
|
|
|
sendPubRec(clientID, messageID);
|
2017-03-25 16:05:45 +01:00
|
|
|
|
2017-02-01 15:57:00 +01:00
|
|
|
// 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-04-04 21:29:38 +02:00
|
|
|
LOG.info("Processing PUBREL message. CId={}, messageId={}", clientID, messageID);
|
2016-12-11 10:45:36 +01:00
|
|
|
ClientSession targetSession = m_sessionsStore.sessionForClient(clientID);
|
2017-04-04 21:29:38 +02:00
|
|
|
IMessagesStore.StoredMessage evt = targetSession.inboundInflight(messageID);
|
|
|
|
|
if (evt == null) {
|
|
|
|
|
LOG.warn("Can't find inbound inflight message for CId={}, messageId={}", clientID, messageID);
|
|
|
|
|
throw new IllegalArgumentException("Can't find inbound inflight message");
|
|
|
|
|
}
|
2017-02-14 17:23:04 +01:00
|
|
|
final Topic topic = new Topic(evt.getTopic());
|
2016-12-11 10:45:36 +01:00
|
|
|
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()) {
|
2017-04-04 21:29:38 +02:00
|
|
|
LOG.debug("Sending publish message to subscribers. CId={}, topic={}, messageId={}, payload={}, " +
|
|
|
|
|
"subscriptionTree={}", clientID, topic, messageID, payload2Str(evt.getMessage()),
|
2017-03-25 16:05:45 +01:00
|
|
|
subscriptions.dumpTree());
|
2017-02-01 15:57:00 +01:00
|
|
|
} else {
|
2017-04-04 21:29:38 +02:00
|
|
|
LOG.info("Sending publish message to subscribers. CId={}, topic={}, messageId={}", clientID, topic,
|
2017-03-25 16:05:45 +01:00
|
|
|
messageID);
|
2017-02-01 15:57:00 +01:00
|
|
|
}
|
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-04-04 21:29:38 +02:00
|
|
|
LOG.debug("Sending PUBREC message. CId={}, 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-04-04 21:29:38 +02:00
|
|
|
LOG.debug("Sending PUBCOMP message. CId={}, 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
|
|
|
}
|
|
|
|
|
}
|