2017-02-01 15:55:47 +01:00
|
|
|
|
2016-12-11 10:33:49 +01:00
|
|
|
package io.moquette.spi.impl;
|
|
|
|
|
|
2017-01-11 11:23:24 +01:00
|
|
|
import io.moquette.server.ConnectionDescriptorStore;
|
2016-12-11 10:33:49 +01:00
|
|
|
import io.moquette.server.netty.NettyUtils;
|
|
|
|
|
import io.moquette.spi.IMessagesStore;
|
|
|
|
|
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.MqttFixedHeader;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttMessageType;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttPubAckMessage;
|
|
|
|
|
import io.netty.handler.codec.mqtt.MqttPublishMessage;
|
2016-12-11 10:33:49 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import static io.moquette.spi.impl.ProtocolProcessor.asStoredMessage;
|
2017-01-28 10:06:30 +01:00
|
|
|
import static io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader.from;
|
|
|
|
|
import static io.netty.handler.codec.mqtt.MqttQoS.AT_MOST_ONCE;
|
2016-12-11 10:33:49 +01:00
|
|
|
|
2017-01-11 18:42:30 +01:00
|
|
|
class Qos1PublishHandler extends QosPublishHandler {
|
2017-02-01 15:55:47 +01:00
|
|
|
|
2016-12-11 10:33:49 +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-13 23:40:00 +01:00
|
|
|
private final MessagesPublisher publisher;
|
2016-12-11 10:33:49 +01:00
|
|
|
|
|
|
|
|
public Qos1PublishHandler(IAuthorizator authorizator, SubscriptionsStore subscriptions,
|
2017-02-01 15:55:47 +01:00
|
|
|
IMessagesStore messagesStore, BrokerInterceptor interceptor,
|
|
|
|
|
ConnectionDescriptorStore connectionDescriptors, String brokerPort, MessagesPublisher messagesPublisher) {
|
|
|
|
|
super(authorizator);
|
2016-12-11 10:33:49 +01:00
|
|
|
this.subscriptions = subscriptions;
|
|
|
|
|
this.m_messagesStore = messagesStore;
|
|
|
|
|
this.m_interceptor = interceptor;
|
|
|
|
|
this.connectionDescriptors = connectionDescriptors;
|
2016-12-13 23:40:00 +01:00
|
|
|
this.publisher = messagesPublisher;
|
2016-12-11 10:33:49 +01:00
|
|
|
}
|
|
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
void receivedPublishQos1(Channel channel, MqttPublishMessage msg) {
|
2017-02-01 15:55:47 +01:00
|
|
|
// verify if topic can be write
|
2017-01-28 10:06:30 +01:00
|
|
|
final String topic = msg.variableHeader().topicName();
|
2016-12-11 10:33:49 +01:00
|
|
|
if (checkWriteOnTopic(topic, channel)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
final int messageID = msg.variableHeader().messageId();
|
|
|
|
|
|
2017-02-01 15:55:47 +01:00
|
|
|
// route message to subscribers
|
2016-12-11 10:33:49 +01:00
|
|
|
IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg);
|
|
|
|
|
String clientID = NettyUtils.clientID(channel);
|
|
|
|
|
toStoreMsg.setClientID(clientID);
|
2016-12-11 11:33:33 +01:00
|
|
|
|
2017-02-01 15:55:47 +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
|
|
|
|
2016-12-11 10:33:49 +01:00
|
|
|
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
|
2016-12-13 23:40:00 +01:00
|
|
|
this.publisher.publish2Subscribers(toStoreMsg, topicMatchingSubscriptions);
|
2016-12-11 10:33:49 +01:00
|
|
|
|
2017-02-01 15:55:47 +01:00
|
|
|
// send PUBACK
|
|
|
|
|
// TODO Don't send PUBREC for Hz publish notification, if (msg.isLocal()) {
|
|
|
|
|
sendPubAck(clientID, messageID);
|
|
|
|
|
// }
|
2016-12-11 10:33:49 +01:00
|
|
|
|
2017-01-28 10:06:30 +01:00
|
|
|
if (msg.fixedHeader().isRetain()) {
|
|
|
|
|
if (!msg.payload().isReadable()) {
|
2016-12-11 10:33:49 +01:00
|
|
|
m_messagesStore.cleanRetained(topic);
|
|
|
|
|
} else {
|
2017-02-01 15:55:47 +01:00
|
|
|
// before wasn't stored
|
2016-12-11 10:33:49 +01:00
|
|
|
MessageGUID guid = m_messagesStore.storePublishForFuture(toStoreMsg);
|
|
|
|
|
m_messagesStore.storeRetained(topic, guid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String username = NettyUtils.userName(channel);
|
|
|
|
|
m_interceptor.notifyTopicPublished(msg, clientID, username);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendPubAck(String clientId, int messageID) {
|
|
|
|
|
LOG.trace("sendPubAck invoked");
|
2017-01-28 10:06:30 +01:00
|
|
|
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.PUBACK, false, AT_MOST_ONCE, false, 0);
|
|
|
|
|
MqttPubAckMessage pubAckMessage = new MqttPubAckMessage(fixedHeader, from(messageID));
|
2016-12-11 10:33:49 +01:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (connectionDescriptors == null) {
|
2017-02-01 15:55:47 +01:00
|
|
|
throw new RuntimeException(
|
|
|
|
|
"Internal bad error, found connectionDescriptors to null while it should be initialized, "
|
|
|
|
|
+ "somewhere it's overwritten!!");
|
2016-12-11 10:33:49 +01:00
|
|
|
}
|
|
|
|
|
LOG.debug("clientIDs are {}", connectionDescriptors);
|
2017-01-11 11:23:24 +01:00
|
|
|
if (!connectionDescriptors.isConnected(clientId)) {
|
2017-02-01 15:55:47 +01:00
|
|
|
throw new RuntimeException(
|
|
|
|
|
String.format(
|
|
|
|
|
"Can't find a ConnectionDescriptor for client %s in cache %s",
|
|
|
|
|
clientId,
|
|
|
|
|
connectionDescriptors));
|
2016-12-11 10:33:49 +01:00
|
|
|
}
|
2017-01-11 11:23:24 +01:00
|
|
|
connectionDescriptors.sendMessage(pubAckMessage, messageID, clientId);
|
2017-02-01 15:55:47 +01:00
|
|
|
} catch (Throwable t) {
|
2016-12-11 10:33:49 +01:00
|
|
|
LOG.error(null, t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|