2016-12-13 00:41:18 +01:00
|
|
|
package io.moquette.spi.impl;
|
|
|
|
|
|
|
|
|
|
import io.moquette.parser.proto.messages.AbstractMessage;
|
2016-12-13 22:44:58 +01:00
|
|
|
import io.moquette.parser.proto.messages.PublishMessage;
|
2016-12-13 00:41:18 +01:00
|
|
|
import io.moquette.server.ConnectionDescriptor;
|
|
|
|
|
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 org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.concurrent.ConcurrentMap;
|
|
|
|
|
|
|
|
|
|
import static io.moquette.spi.impl.ProtocolProcessor.lowerQosToTheSubscriptionDesired;
|
|
|
|
|
|
2016-12-13 00:50:25 +01:00
|
|
|
class MessagesPublisher {
|
2016-12-13 00:41:18 +01:00
|
|
|
|
2016-12-13 00:50:25 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(MessagesPublisher.class);
|
2016-12-13 00:41:18 +01:00
|
|
|
private final ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors;
|
|
|
|
|
private final ISessionsStore m_sessionsStore;
|
|
|
|
|
private final IMessagesStore m_messagesStore;
|
2016-12-13 23:40:00 +01:00
|
|
|
private final PersistentQueueMessageSender messageSender;
|
2016-12-13 00:41:18 +01:00
|
|
|
|
2016-12-13 00:50:25 +01:00
|
|
|
public MessagesPublisher(ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors, ISessionsStore sessionsStore,
|
2016-12-13 23:40:00 +01:00
|
|
|
IMessagesStore messagesStore, PersistentQueueMessageSender messageSender) {
|
2016-12-13 00:41:18 +01:00
|
|
|
this.connectionDescriptors = connectionDescriptors;
|
|
|
|
|
this.m_sessionsStore = sessionsStore;
|
|
|
|
|
this.m_messagesStore = messagesStore;
|
2016-12-13 23:40:00 +01:00
|
|
|
this.messageSender = messageSender;
|
2016-12-13 00:41:18 +01:00
|
|
|
}
|
|
|
|
|
|
2016-12-13 23:40:00 +01:00
|
|
|
static PublishMessage notRetainedPublish(String topic, AbstractMessage.QOSType qos, ByteBuffer message) {
|
2016-12-13 23:26:31 +01:00
|
|
|
PublishMessage pubMessage = new PublishMessage();
|
|
|
|
|
pubMessage.setRetainFlag(false);
|
|
|
|
|
pubMessage.setTopicName(topic);
|
|
|
|
|
pubMessage.setQos(qos);
|
|
|
|
|
pubMessage.setPayload(message);
|
|
|
|
|
return pubMessage;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-13 00:41:18 +01:00
|
|
|
void publish2Subscribers(IMessagesStore.StoredMessage pubMsg, List<Subscription> topicMatchingSubscriptions) {
|
|
|
|
|
final String topic = pubMsg.getTopic();
|
|
|
|
|
final AbstractMessage.QOSType publishingQos = pubMsg.getQos();
|
|
|
|
|
final ByteBuffer origMessage = pubMsg.getMessage();
|
|
|
|
|
|
|
|
|
|
//if QoS 1 or 2 store the message
|
|
|
|
|
MessageGUID guid = null;
|
|
|
|
|
if (publishingQos != AbstractMessage.QOSType.MOST_ONE) {
|
|
|
|
|
guid = m_messagesStore.storePublishForFuture(pubMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LOG.trace("Found {} matching subscriptions to <{}>", topicMatchingSubscriptions.size(), topic);
|
|
|
|
|
for (final Subscription sub : topicMatchingSubscriptions) {
|
|
|
|
|
AbstractMessage.QOSType qos = lowerQosToTheSubscriptionDesired(sub, publishingQos);
|
|
|
|
|
ClientSession targetSession = m_sessionsStore.sessionForClient(sub.getClientId());
|
|
|
|
|
|
|
|
|
|
boolean targetIsActive = this.connectionDescriptors.containsKey(sub.getClientId());
|
|
|
|
|
|
2016-12-13 23:18:43 +01:00
|
|
|
LOG.debug("Broker republishing to client <{}> topicFilter <{}> qos <{}>, active {}",
|
2016-12-13 00:41:18 +01:00
|
|
|
sub.getClientId(), sub.getTopicFilter(), qos, targetIsActive);
|
|
|
|
|
ByteBuffer message = origMessage.duplicate();
|
|
|
|
|
if (targetIsActive) {
|
2016-12-13 23:40:00 +01:00
|
|
|
PublishMessage publishMsg = notRetainedPublish(topic, qos, message);
|
2016-12-13 23:02:54 +01:00
|
|
|
if (qos != AbstractMessage.QOSType.MOST_ONE) {
|
2016-12-13 00:41:18 +01:00
|
|
|
//QoS 1 or 2
|
|
|
|
|
int messageId = targetSession.nextPacketId();
|
|
|
|
|
targetSession.inFlightAckWaiting(guid, messageId);
|
2016-12-13 22:44:58 +01:00
|
|
|
//set the PacketIdentifier only for QoS > 0
|
|
|
|
|
publishMsg.setMessageID(messageId);
|
2016-12-13 00:41:18 +01:00
|
|
|
}
|
2016-12-13 23:40:00 +01:00
|
|
|
this.messageSender.sendPublish(targetSession, publishMsg);
|
2016-12-13 00:41:18 +01:00
|
|
|
} else {
|
|
|
|
|
if (!targetSession.isCleanSession()) {
|
|
|
|
|
//store the message in targetSession queue to deliver
|
|
|
|
|
targetSession.enqueueToDeliver(guid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|