2017-02-01 15:55:23 +01:00
|
|
|
|
2016-12-09 15:09:50 +01:00
|
|
|
package io.moquette.spi.impl;
|
|
|
|
|
|
|
|
|
|
import io.moquette.server.netty.NettyUtils;
|
|
|
|
|
import io.moquette.spi.IMessagesStore;
|
|
|
|
|
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-09 15:09:50 +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.MqttPublishMessage;
|
2016-12-09 15:09:50 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import static io.moquette.spi.impl.ProtocolProcessor.asStoredMessage;
|
|
|
|
|
|
2017-01-11 18:42:30 +01:00
|
|
|
class Qos0PublishHandler extends QosPublishHandler {
|
2016-12-09 15:09:50 +01:00
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(Qos0PublishHandler.class);
|
|
|
|
|
|
|
|
|
|
private final SubscriptionsStore subscriptions;
|
|
|
|
|
private final IMessagesStore m_messagesStore;
|
|
|
|
|
private final BrokerInterceptor m_interceptor;
|
2016-12-13 23:40:00 +01:00
|
|
|
private final MessagesPublisher publisher;
|
2016-12-09 15:09:50 +01:00
|
|
|
|
|
|
|
|
public Qos0PublishHandler(IAuthorizator authorizator, SubscriptionsStore subscriptions,
|
2017-02-01 15:55:23 +01:00
|
|
|
IMessagesStore messagesStore, BrokerInterceptor interceptor, MessagesPublisher messagesPublisher) {
|
|
|
|
|
super(authorizator);
|
2016-12-09 15:09:50 +01:00
|
|
|
this.subscriptions = subscriptions;
|
|
|
|
|
this.m_messagesStore = messagesStore;
|
|
|
|
|
this.m_interceptor = interceptor;
|
2016-12-13 23:40:00 +01:00
|
|
|
this.publisher = messagesPublisher;
|
2016-12-09 15:09:50 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
void receivedPublishQos0(Channel channel, MqttPublishMessage msg) {
|
2017-02-01 15:55:23 +01:00
|
|
|
// verify if topic can be write
|
2017-02-14 17:23:04 +01:00
|
|
|
final Topic topic = new Topic(msg.variableHeader().topicName());
|
2016-12-09 15:09:50 +01:00
|
|
|
if (checkWriteOnTopic(topic, channel)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-01 15:55:23 +01:00
|
|
|
// route message to subscribers
|
2016-12-09 15:09:50 +01:00
|
|
|
IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg);
|
|
|
|
|
String clientID = NettyUtils.clientID(channel);
|
|
|
|
|
toStoreMsg.setClientID(clientID);
|
|
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
final int messageID = msg.variableHeader().messageId();
|
|
|
|
|
|
2017-02-01 15:55:23 +01:00
|
|
|
if (LOG.isTraceEnabled()) {
|
2017-03-25 16:05:45 +01:00
|
|
|
LOG.trace("Sending publish message to subscribers. ClientId={}, topic={}, messageId={}, payload={}, " +
|
|
|
|
|
"subscriptionTree={}", clientID, topic, messageID, DebugUtils.payload2Str(toStoreMsg.getMessage()),
|
|
|
|
|
subscriptions.dumpTree());
|
2017-02-01 15:55:23 +01:00
|
|
|
} else {
|
2017-03-25 16:05:45 +01:00
|
|
|
LOG.info("Sending publish message to subscribers. ClientId={}, topic={}, messageId={}", clientID, topic,
|
|
|
|
|
messageID);
|
2017-02-01 15:55:23 +01:00
|
|
|
}
|
|
|
|
|
|
2016-12-09 15:09:50 +01:00
|
|
|
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
2016-12-13 23:40:00 +01:00
|
|
|
this.publisher.publish2Subscribers(toStoreMsg, topicMatchingSubscriptions);
|
2016-12-09 15:09:50 +01:00
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
if (msg.fixedHeader().isRetain()) {
|
2017-02-01 15:55:23 +01:00
|
|
|
// QoS == 0 && retain => clean old retained
|
2016-12-09 15:09:50 +01:00
|
|
|
m_messagesStore.cleanRetained(topic);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String username = NettyUtils.userName(channel);
|
|
|
|
|
m_interceptor.notifyTopicPublished(msg, clientID, username);
|
|
|
|
|
}
|
|
|
|
|
}
|