2016-12-11 11:42:10 +01:00
|
|
|
package io.moquette.spi.impl;
|
|
|
|
|
|
|
|
|
|
import io.moquette.parser.proto.messages.AbstractMessage;
|
|
|
|
|
import io.moquette.parser.proto.messages.PublishMessage;
|
|
|
|
|
import io.moquette.server.ConnectionDescriptor;
|
|
|
|
|
import io.moquette.spi.ClientSession;
|
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
|
import java.util.concurrent.ConcurrentMap;
|
|
|
|
|
|
2016-12-13 22:16:31 +01:00
|
|
|
import static io.moquette.spi.impl.BestEffortMessageSender.createPublishForQos;
|
|
|
|
|
|
2016-12-13 00:46:19 +01:00
|
|
|
class PersistentQueueMessageSender {
|
2016-12-11 11:42:10 +01:00
|
|
|
|
2016-12-13 00:46:19 +01:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(PersistentQueueMessageSender.class);
|
2016-12-11 11:42:10 +01:00
|
|
|
private final ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors;
|
|
|
|
|
|
2016-12-13 00:46:19 +01:00
|
|
|
public PersistentQueueMessageSender(ConcurrentMap<String, ConnectionDescriptor> connectionDescriptors) {
|
2016-12-11 11:42:10 +01:00
|
|
|
this.connectionDescriptors = connectionDescriptors;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-13 00:41:18 +01:00
|
|
|
void publishQos2(ClientSession clientsession, String topic, AbstractMessage.QOSType qos,
|
2016-12-13 00:48:41 +01:00
|
|
|
ByteBuffer message, int messageID) {
|
2016-12-11 11:42:10 +01:00
|
|
|
String clientId = clientsession.clientID;
|
|
|
|
|
LOG.debug("directSend invoked clientId <{}> on topic <{}> QoS {} retained {} messageID {}",
|
2016-12-13 00:48:41 +01:00
|
|
|
clientId, topic, qos, false, messageID);
|
2016-12-13 22:16:31 +01:00
|
|
|
PublishMessage pubMessage = createPublishForQos(topic, qos, message);
|
|
|
|
|
|
|
|
|
|
//set the PacketIdentifier only for QoS > 0
|
|
|
|
|
pubMessage.setMessageID(messageID);
|
2016-12-11 11:42:10 +01:00
|
|
|
|
|
|
|
|
LOG.info("send publish message to <{}> on topic <{}>", clientId, topic);
|
|
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
|
LOG.debug("content <{}>", DebugUtils.payload2Str(message));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (connectionDescriptors == null) {
|
|
|
|
|
throw new RuntimeException("Internal bad error, found connectionDescriptors to null while it should be " +
|
|
|
|
|
"initialized, somewhere it's overwritten!!");
|
|
|
|
|
}
|
|
|
|
|
if (connectionDescriptors.get(clientId) == null) {
|
|
|
|
|
//TODO while we were publishing to the target client, that client disconnected,
|
|
|
|
|
// could happen is not an error HANDLE IT
|
|
|
|
|
throw new RuntimeException(String.format("Can't find a ConnectionDescriptor for client <%s> in cache <%s>",
|
|
|
|
|
clientId, connectionDescriptors));
|
|
|
|
|
}
|
|
|
|
|
Channel channel = connectionDescriptors.get(clientId).channel;
|
|
|
|
|
LOG.trace("Session for clientId {}", clientId);
|
|
|
|
|
if (channel.isWritable()) {
|
|
|
|
|
LOG.debug("channel is writable");
|
|
|
|
|
//if channel is writable don't enqueue
|
|
|
|
|
channel.writeAndFlush(pubMessage);
|
|
|
|
|
} else {
|
|
|
|
|
//enqueue to the client session
|
|
|
|
|
LOG.debug("enqueue to client session");
|
|
|
|
|
clientsession.enqueue(pubMessage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|