Minor, just code layout

This commit is contained in:
andsel
2017-03-20 21:58:18 +01:00
parent 22b1fa2b0e
commit c659a54158
@@ -19,7 +19,6 @@ package io.moquette.spi.impl;
import io.moquette.interception.InterceptHandler; import io.moquette.interception.InterceptHandler;
import io.moquette.interception.messages.InterceptAcknowledgedMessage; import io.moquette.interception.messages.InterceptAcknowledgedMessage;
import io.moquette.server.ConnectionDescriptor; import io.moquette.server.ConnectionDescriptor;
import io.moquette.server.ConnectionDescriptor.ConnectionState;
import io.moquette.server.ConnectionDescriptorStore; import io.moquette.server.ConnectionDescriptorStore;
import io.moquette.server.netty.NettyUtils; import io.moquette.server.netty.NettyUtils;
import io.moquette.spi.*; import io.moquette.spi.*;
@@ -50,6 +49,7 @@ import static io.moquette.spi.impl.Utils.readBytesAndRewind;
import static io.netty.handler.codec.mqtt.MqttConnectReturnCode.*; import static io.netty.handler.codec.mqtt.MqttConnectReturnCode.*;
import static io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader.from; import static io.netty.handler.codec.mqtt.MqttMessageIdVariableHeader.from;
import static io.netty.handler.codec.mqtt.MqttQoS.*; import static io.netty.handler.codec.mqtt.MqttQoS.*;
import static io.moquette.server.ConnectionDescriptor.ConnectionState.*;
/** /**
* Class responsible to handle the logic of MQTT protocol it's the director of the protocol * Class responsible to handle the logic of MQTT protocol it's the director of the protocol
@@ -160,47 +160,22 @@ public class ProtocolProcessor {
public void init(SubscriptionsStore subscriptions, IMessagesStore storageService, ISessionsStore sessionsStore, public void init(SubscriptionsStore subscriptions, IMessagesStore storageService, ISessionsStore sessionsStore,
IAuthenticator authenticator, boolean allowAnonymous, IAuthorizator authorizator, IAuthenticator authenticator, boolean allowAnonymous, IAuthorizator authorizator,
BrokerInterceptor interceptor) { BrokerInterceptor interceptor) {
init( init(subscriptions, storageService, sessionsStore, authenticator, allowAnonymous, false, authorizator,
subscriptions, interceptor, null);
storageService,
sessionsStore,
authenticator,
allowAnonymous,
false,
authorizator,
interceptor,
null);
} }
public void init(SubscriptionsStore subscriptions, IMessagesStore storageService, ISessionsStore sessionsStore, public void init(SubscriptionsStore subscriptions, IMessagesStore storageService, ISessionsStore sessionsStore,
IAuthenticator authenticator, boolean allowAnonymous, boolean allowZeroByteClientId, IAuthenticator authenticator, boolean allowAnonymous, boolean allowZeroByteClientId,
IAuthorizator authorizator, BrokerInterceptor interceptor) { IAuthorizator authorizator, BrokerInterceptor interceptor) {
init( init(subscriptions, storageService, sessionsStore, authenticator, allowAnonymous, allowZeroByteClientId,
subscriptions, authorizator, interceptor, null);
storageService,
sessionsStore,
authenticator,
allowAnonymous,
allowZeroByteClientId,
authorizator,
interceptor,
null);
} }
public void init(SubscriptionsStore subscriptions, IMessagesStore storageService, ISessionsStore sessionsStore, public void init(SubscriptionsStore subscriptions, IMessagesStore storageService, ISessionsStore sessionsStore,
IAuthenticator authenticator, boolean allowAnonymous, boolean allowZeroByteClientId, IAuthenticator authenticator, boolean allowAnonymous, boolean allowZeroByteClientId,
IAuthorizator authorizator, BrokerInterceptor interceptor, String serverPort) { IAuthorizator authorizator, BrokerInterceptor interceptor, String serverPort) {
init( init(new ConnectionDescriptorStore(sessionsStore), subscriptions, storageService, sessionsStore, authenticator,
new ConnectionDescriptorStore(sessionsStore), allowAnonymous, allowZeroByteClientId, authorizator, interceptor, serverPort);
subscriptions,
storageService,
sessionsStore,
authenticator,
allowAnonymous,
allowZeroByteClientId,
authorizator,
interceptor,
serverPort);
} }
/** /**
@@ -234,7 +209,7 @@ public class ProtocolProcessor {
this.allowZeroByteClientId = allowZeroByteClientId; this.allowZeroByteClientId = allowZeroByteClientId;
m_authorizator = authorizator; m_authorizator = authorizator;
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("Initial subscriptions tree = {}.", subscriptions.dumpTree()); LOG.debug("Initial subscriptions tree={}.", subscriptions.dumpTree());
} }
m_authenticator = authenticator; m_authenticator = authenticator;
m_messagesStore = storageService; m_messagesStore = storageService;
@@ -243,36 +218,16 @@ public class ProtocolProcessor {
LOG.info("Initializing messages publisher..."); LOG.info("Initializing messages publisher...");
final PersistentQueueMessageSender messageSender = new PersistentQueueMessageSender(this.connectionDescriptors); final PersistentQueueMessageSender messageSender = new PersistentQueueMessageSender(this.connectionDescriptors);
this.messagesPublisher = new MessagesPublisher( this.messagesPublisher = new MessagesPublisher(connectionDescriptors, sessionsStore, m_messagesStore,
connectionDescriptors,
sessionsStore,
m_messagesStore,
messageSender); messageSender);
LOG.info("Initializing QoS publish handlers..."); LOG.info("Initializing QoS publish handlers...");
this.qos0PublishHandler = new Qos0PublishHandler( this.qos0PublishHandler = new Qos0PublishHandler(m_authorizator, subscriptions, m_messagesStore, m_interceptor,
m_authorizator,
subscriptions,
m_messagesStore,
m_interceptor,
this.messagesPublisher);
this.qos1PublishHandler = new Qos1PublishHandler(
m_authorizator,
subscriptions,
m_messagesStore,
m_interceptor,
this.connectionDescriptors,
m_server_port,
this.messagesPublisher);
this.qos2PublishHandler = new Qos2PublishHandler(
m_authorizator,
subscriptions,
m_messagesStore,
m_interceptor,
this.connectionDescriptors,
m_sessionsStore,
m_server_port,
this.messagesPublisher); this.messagesPublisher);
this.qos1PublishHandler = new Qos1PublishHandler(m_authorizator, subscriptions, m_messagesStore, m_interceptor,
this.connectionDescriptors, m_server_port, this.messagesPublisher);
this.qos2PublishHandler = new Qos2PublishHandler(m_authorizator, subscriptions, m_messagesStore, m_interceptor,
this.connectionDescriptors, m_sessionsStore, m_server_port, this.messagesPublisher);
LOG.info("Initializing internal republisher..."); LOG.info("Initializing internal republisher...");
this.internalRepublisher = new InternalRepublisher(messageSender); this.internalRepublisher = new InternalRepublisher(messageSender);
@@ -281,13 +236,13 @@ public class ProtocolProcessor {
public void processConnect(Channel channel, MqttConnectMessage msg) { public void processConnect(Channel channel, MqttConnectMessage msg) {
MqttConnectPayload payload = msg.payload(); MqttConnectPayload payload = msg.payload();
String clientId = payload.clientIdentifier(); String clientId = payload.clientIdentifier();
LOG.info("Processing CONNECT message. CId = {}, username = {}", clientId, payload.userName()); LOG.info("Processing CONNECT message. CId={}, username={}", clientId, payload.userName());
if (msg.variableHeader().version() != MqttVersion.MQTT_3_1.protocolLevel() if (msg.variableHeader().version() != MqttVersion.MQTT_3_1.protocolLevel()
&& msg.variableHeader().version() != MqttVersion.MQTT_3_1_1.protocolLevel()) { && msg.variableHeader().version() != MqttVersion.MQTT_3_1_1.protocolLevel()) {
MqttConnAckMessage badProto = connAck(CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION); MqttConnAckMessage badProto = connAck(CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION);
LOG.error("The MQTT protocol version is not valid. CId = {}", clientId); LOG.error("MQTT protocol version is not valid. CId={}", clientId);
channel.writeAndFlush(badProto); channel.writeAndFlush(badProto);
channel.close(); channel.close();
return; return;
@@ -299,16 +254,14 @@ public class ProtocolProcessor {
channel.writeAndFlush(badId); channel.writeAndFlush(badId);
channel.close(); channel.close();
LOG.error("The MQTT client ID cannot be empty. Username = {}", payload.userName()); LOG.error("The MQTT client ID cannot be empty. Username={}", payload.userName());
return; return;
} }
// Generating client id. // Generating client id.
clientId = UUID.randomUUID().toString().replace("-", ""); clientId = UUID.randomUUID().toString().replace("-", "");
LOG.info( LOG.info("Client has connected with a server generated identifier. CId={}, username={}", clientId,
"The client has connected with a server generated identifier. CId = {}, username = {}", payload.userName());
clientId,
payload.userName());
} }
if (!login(channel, msg, clientId)) { if (!login(channel, msg, clientId)) {
@@ -316,21 +269,17 @@ public class ProtocolProcessor {
return; return;
} }
ConnectionDescriptor descriptor = new ConnectionDescriptor( ConnectionDescriptor descriptor = new ConnectionDescriptor(clientId, channel,
clientId, msg.variableHeader().isCleanSession());
channel,
msg.variableHeader().isCleanSession());
ConnectionDescriptor existing = this.connectionDescriptors.addConnection(descriptor); ConnectionDescriptor existing = this.connectionDescriptors.addConnection(descriptor);
if (existing != null) { if (existing != null) {
LOG.info("The client ID is being used in an existing connection. It will be closed. CId = {}", clientId); LOG.info("The client ID is being used in an existing connection. It will be closed. CId={}", clientId);
existing.abort(); existing.abort();
return; return;
} }
initializeKeepAliveTimeout(channel, msg, clientId); initializeKeepAliveTimeout(channel, msg, clientId);
storeWillMessage(msg, clientId); storeWillMessage(msg, clientId);
if (!sendAck(descriptor, msg, clientId)) { if (!sendAck(descriptor, msg, clientId)) {
channel.close(); channel.close();
return; return;
@@ -348,13 +297,12 @@ public class ProtocolProcessor {
channel.close(); channel.close();
return; return;
} }
final boolean success = descriptor final boolean success = descriptor.assignState(MESSAGES_REPUBLISHED, ESTABLISHED);
.assignState(ConnectionState.MESSAGES_REPUBLISHED, ConnectionState.ESTABLISHED);
if (!success) { if (!success) {
channel.close(); channel.close();
} }
LOG.info("The CONNECT message has been processed. CId = {}, username = {}.", clientId, payload.userName()); LOG.info("The CONNECT message has been processed. CId={}, username={}.", clientId, payload.userName());
} }
private MqttConnAckMessage connAck(MqttConnectReturnCode returnCode) { private MqttConnAckMessage connAck(MqttConnectReturnCode returnCode) {
@@ -366,12 +314,8 @@ public class ProtocolProcessor {
} }
private MqttConnAckMessage connAck(MqttConnectReturnCode returnCode, boolean sessionPresent) { private MqttConnAckMessage connAck(MqttConnectReturnCode returnCode, boolean sessionPresent) {
MqttFixedHeader mqttFixedHeader = new MqttFixedHeader( MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.CONNACK, false, MqttQoS.AT_MOST_ONCE,
MqttMessageType.CONNACK, false, 0);
false,
MqttQoS.AT_MOST_ONCE,
false,
0);
MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(returnCode, sessionPresent); MqttConnAckVariableHeader mqttConnAckVariableHeader = new MqttConnAckVariableHeader(returnCode, sessionPresent);
return new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader); return new MqttConnAckMessage(mqttFixedHeader, mqttConnAckVariableHeader);
} }
@@ -383,26 +327,19 @@ public class ProtocolProcessor {
if (msg.variableHeader().hasPassword()) { if (msg.variableHeader().hasPassword()) {
pwd = msg.payload().password().getBytes(); pwd = msg.payload().password().getBytes();
} else if (!this.allowAnonymous) { } else if (!this.allowAnonymous) {
LOG.error( LOG.error("Client didn't supply any password and MQTT anonymous mode is disabled CId={}", clientId);
"The client didn't supply any password and MQTT anonymous mode is disabled. CId = {}.",
clientId);
failedCredentials(channel); failedCredentials(channel);
return false; return false;
} }
if (!m_authenticator.checkValid(clientId, msg.payload().userName(), pwd)) { if (!m_authenticator.checkValid(clientId, msg.payload().userName(), pwd)) {
LOG.error( LOG.error("Authenticator has rejected the MQTT credentials CId={}, username={}, password={}",
"The authenticator has rejected the MQTT credentials. CId = {}, username = {}, password = {}.", clientId, msg.payload().userName(), pwd);
clientId,
msg.payload().userName(),
pwd);
failedCredentials(channel); failedCredentials(channel);
return false; return false;
} }
NettyUtils.userName(channel, msg.payload().userName()); NettyUtils.userName(channel, msg.payload().userName());
} else if (!this.allowAnonymous) { } else if (!this.allowAnonymous) {
LOG.error( LOG.error("Client didn't supply any credentials and MQTT anonymous mode is disabled. CId={}", clientId);
"The client didn't supply any credentials and MQTT anonymous mode is disabled. CId = {}.",
clientId);
failedCredentials(channel); failedCredentials(channel);
return false; return false;
} }
@@ -410,8 +347,8 @@ public class ProtocolProcessor {
} }
private boolean sendAck(ConnectionDescriptor descriptor, MqttConnectMessage msg, final String clientId) { private boolean sendAck(ConnectionDescriptor descriptor, MqttConnectMessage msg, final String clientId) {
LOG.info("Sending connect ACK. CId = {}.", clientId); LOG.info("Sending connect ACK. CId={}.", clientId);
final boolean success = descriptor.assignState(ConnectionState.DISCONNECTED, ConnectionState.SENDACK); final boolean success = descriptor.assignState(DISCONNECTED, SENDACK);
if (!success) { if (!success) {
return false; return false;
} }
@@ -426,17 +363,17 @@ public class ProtocolProcessor {
} }
if (isSessionAlreadyStored) { if (isSessionAlreadyStored) {
LOG.info("Cleaning session. CId = {}.", clientId); LOG.info("Cleaning session. CId={}.", clientId);
clientSession.cleanSession(msg.variableHeader().isCleanSession()); clientSession.cleanSession(msg.variableHeader().isCleanSession());
} }
descriptor.writeAndFlush(okResp); descriptor.writeAndFlush(okResp);
LOG.info("The connect ACK has been sent. CId = {}.", clientId); LOG.info("The connect ACK has been sent. CId={}.", clientId);
return true; return true;
} }
private void initializeKeepAliveTimeout(Channel channel, MqttConnectMessage msg, final String clientId) { private void initializeKeepAliveTimeout(Channel channel, MqttConnectMessage msg, final String clientId) {
int keepAlive = msg.variableHeader().keepAliveTimeSeconds(); int keepAlive = msg.variableHeader().keepAliveTimeSeconds();
LOG.info("Configuring connection. CId = {}.", clientId); LOG.info("Configuring connection. CId={}.", clientId);
NettyUtils.keepAlive(channel, keepAlive); NettyUtils.keepAlive(channel, keepAlive);
// session.attr(NettyUtils.ATTR_KEY_CLEANSESSION).set(msg.variableHeader().isCleanSession()); // session.attr(NettyUtils.ATTR_KEY_CLEANSESSION).set(msg.variableHeader().isCleanSession());
NettyUtils.cleanSession(channel, msg.variableHeader().isCleanSession()); NettyUtils.cleanSession(channel, msg.variableHeader().isCleanSession());
@@ -446,41 +383,29 @@ public class ProtocolProcessor {
int idleTime = Math.round(keepAlive * 1.5f); int idleTime = Math.round(keepAlive * 1.5f);
setIdleTime(channel.pipeline(), idleTime); setIdleTime(channel.pipeline(), idleTime);
LOG.info( LOG.debug("The connection has been configured CId={}, keepAlive={}, cleanSession={}, idleTime={}",
"The connection has been configured. CId = {}, keepAlive = {}, cleanSession = {}, idleTime = {}.", clientId, keepAlive, msg.variableHeader().isCleanSession(), idleTime);
clientId,
keepAlive,
msg.variableHeader().isCleanSession(),
idleTime);
} }
private void storeWillMessage(MqttConnectMessage msg, final String clientId) { private void storeWillMessage(MqttConnectMessage msg, final String clientId) {
// Handle will flag // Handle will flag
if (msg.variableHeader().isWillFlag()) { if (msg.variableHeader().isWillFlag()) {
MqttQoS willQos = MqttQoS.valueOf(msg.variableHeader().willQos()); MqttQoS willQos = MqttQoS.valueOf(msg.variableHeader().willQos());
LOG.info( LOG.info("Configuring MQTT last will and testament CId={}, willQos={}, willTopic={}, willRetain={}",
"Configuring MQTT last will and testament. " clientId, willQos, msg.payload().willTopic(), msg.variableHeader().isWillRetain());
+ "CId = {}, willQos = {}, willTopic = {}, willRetain = {}.",
clientId,
willQos,
msg.payload().willTopic(),
msg.variableHeader().isWillRetain());
byte[] willPayload = msg.payload().willMessage().getBytes(); byte[] willPayload = msg.payload().willMessage().getBytes();
ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(willPayload.length).put(willPayload).flip(); ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(willPayload.length).put(willPayload).flip();
// save the will testament in the clientID store // save the will testament in the clientID store
WillMessage will = new WillMessage( WillMessage will = new WillMessage(msg.payload().willTopic(), bb, msg.variableHeader().isWillRetain(),
msg.payload().willTopic(),
bb,
msg.variableHeader().isWillRetain(),
willQos); willQos);
m_willStore.put(clientId, will); m_willStore.put(clientId, will);
LOG.info("MQTT last will and testament has been configured. CId = {}.", clientId); LOG.info("MQTT last will and testament has been configured. CId={}.", clientId);
} }
} }
private ClientSession createOrLoadClientSession(ConnectionDescriptor descriptor, MqttConnectMessage msg, private ClientSession createOrLoadClientSession(ConnectionDescriptor descriptor, MqttConnectMessage msg,
String clientId) { String clientId) {
final boolean success = descriptor.assignState(ConnectionState.SENDACK, ConnectionState.SESSION_CREATED); final boolean success = descriptor.assignState(SENDACK, SESSION_CREATED);
if (!success) { if (!success) {
return null; return null;
} }
@@ -491,15 +416,14 @@ public class ProtocolProcessor {
clientSession = m_sessionsStore.createNewSession(clientId, msg.variableHeader().isCleanSession()); clientSession = m_sessionsStore.createNewSession(clientId, msg.variableHeader().isCleanSession());
} }
if (msg.variableHeader().isCleanSession()) { if (msg.variableHeader().isCleanSession()) {
LOG.info("Cleaning session. CId = {}.", clientId); LOG.info("Cleaning session. CId={}.", clientId);
clientSession.cleanSession(); clientSession.cleanSession();
} }
return clientSession; return clientSession;
} }
private boolean republish(ConnectionDescriptor descriptor, MqttConnectMessage msg, ClientSession clientSession) { private boolean republish(ConnectionDescriptor descriptor, MqttConnectMessage msg, ClientSession clientSession) {
final boolean success = descriptor final boolean success = descriptor.assignState(SESSION_CREATED, MESSAGES_REPUBLISHED);
.assignState(ConnectionState.SESSION_CREATED, ConnectionState.MESSAGES_REPUBLISHED);
if (!success) { if (!success) {
return false; return false;
} }
@@ -529,10 +453,10 @@ public class ProtocolProcessor {
* Republish QoS1 and QoS2 messages stored into the session for the clientID. * Republish QoS1 and QoS2 messages stored into the session for the clientID.
*/ */
private void republishStoredInSession(ClientSession clientSession) { private void republishStoredInSession(ClientSession clientSession) {
LOG.info("Republishing stored publish events. CId = {}.", clientSession.clientID); LOG.info("Republishing stored publish events. CId={}.", clientSession.clientID);
BlockingQueue<StoredMessage> publishedEvents = clientSession.queue(); BlockingQueue<StoredMessage> publishedEvents = clientSession.queue();
if (publishedEvents.isEmpty()) { if (publishedEvents.isEmpty()) {
LOG.info("There are no stored publish events. ClientId = {}.", clientSession.clientID); LOG.info("There are no stored publish events. ClientId={}.", clientSession.clientID);
return; return;
} }
@@ -558,19 +482,15 @@ public class ProtocolProcessor {
ByteBuf payload = msg.payload(); ByteBuf payload = msg.payload();
byte[] payloadContent = readBytesAndRewind(payload); byte[] payloadContent = readBytesAndRewind(payload);
IMessagesStore.StoredMessage stored = new IMessagesStore.StoredMessage( IMessagesStore.StoredMessage stored = new IMessagesStore.StoredMessage(payloadContent,
payloadContent, msg.fixedHeader().qosLevel(), msg.variableHeader().topicName());
msg.fixedHeader().qosLevel(),
msg.variableHeader().topicName());
stored.setRetained(msg.fixedHeader().isRetain()); stored.setRetained(msg.fixedHeader().isRetain());
stored.setMessageID(msg.variableHeader().messageId()); stored.setMessageID(msg.variableHeader().messageId());
return stored; return stored;
} }
private static IMessagesStore.StoredMessage asStoredMessage(WillMessage will) { private static IMessagesStore.StoredMessage asStoredMessage(WillMessage will) {
IMessagesStore.StoredMessage pub = new IMessagesStore.StoredMessage( IMessagesStore.StoredMessage pub = new IMessagesStore.StoredMessage(will.getPayload().array(), will.getQos(),
will.getPayload().array(),
will.getQos(),
will.getTopic()); will.getTopic());
pub.setRetained(will.isRetained()); pub.setRetained(will.isRetained());
return pub; return pub;
@@ -579,12 +499,8 @@ public class ProtocolProcessor {
public void processPublish(Channel channel, MqttPublishMessage msg) { public void processPublish(Channel channel, MqttPublishMessage msg) {
final MqttQoS qos = msg.fixedHeader().qosLevel(); final MqttQoS qos = msg.fixedHeader().qosLevel();
final String clientId = NettyUtils.clientID(channel); final String clientId = NettyUtils.clientID(channel);
LOG.info( LOG.info("Processing PUBLISH message. CId={}, topic={}, messageId={}, qos={}", clientId,
"Processing PUBLISH message. CId = {}, topic = {}, messageId = {}, qos = {}.", msg.variableHeader().topicName(), msg.variableHeader().messageId(), qos);
clientId,
msg.variableHeader().topicName(),
msg.variableHeader().messageId(),
qos);
switch (qos) { switch (qos) {
case AT_MOST_ONCE: case AT_MOST_ONCE:
this.qos0PublishHandler.receivedPublishQos0(channel, msg); this.qos0PublishHandler.receivedPublishQos0(channel, msg);
@@ -615,7 +531,7 @@ public class ProtocolProcessor {
public void internalPublish(MqttPublishMessage msg, final String clientId) { public void internalPublish(MqttPublishMessage msg, final String clientId) {
final MqttQoS qos = msg.fixedHeader().qosLevel(); final MqttQoS qos = msg.fixedHeader().qosLevel();
final Topic topic = new Topic(msg.variableHeader().topicName()); final Topic topic = new Topic(msg.variableHeader().topicName());
LOG.info("Sending PUBLISH message. Topic = {}, qos = {}.", topic, qos); LOG.info("Sending PUBLISH message. Topic={}, qos={}.", topic, qos);
MessageGUID guid = null; MessageGUID guid = null;
IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg); IMessagesStore.StoredMessage toStoreMsg = asStoredMessage(msg);
@@ -663,7 +579,7 @@ public class ProtocolProcessor {
Topic topic = new Topic(tobeStored.getTopic()); Topic topic = new Topic(tobeStored.getTopic());
List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic); List<Subscription> topicMatchingSubscriptions = subscriptions.matches(topic);
LOG.info("Publishing will message. CId = {}, messageId = {}, topic = {}.", clientID, messageId, topic); LOG.info("Publishing will message. CId={}, messageId={}, topic={}.", clientID, messageId, topic);
this.messagesPublisher.publish2Subscribers(tobeStored, topicMatchingSubscriptions); this.messagesPublisher.publish2Subscribers(tobeStored, topicMatchingSubscriptions);
} }
@@ -694,7 +610,7 @@ public class ProtocolProcessor {
int messageID = messageId(msg); int messageID = messageId(msg);
targetSession.moveInFlightToSecondPhaseAckWaiting(messageID); targetSession.moveInFlightToSecondPhaseAckWaiting(messageID);
// once received a PUBREC reply with a PUBREL(messageID) // once received a PUBREC reply with a PUBREL(messageID)
LOG.debug("Processing PUBREC message. CId = {}, messageId = {}.", clientID, messageID); LOG.debug("Processing PUBREC message. CId={}, messageId={}.", clientID, messageID);
MqttFixedHeader pubRelHeader = new MqttFixedHeader(MqttMessageType.PUBREL, false, AT_LEAST_ONCE, false, 0); MqttFixedHeader pubRelHeader = new MqttFixedHeader(MqttMessageType.PUBREL, false, AT_LEAST_ONCE, false, 0);
MqttMessage pubRelMessage = new MqttMessage(pubRelHeader, from(messageID)); MqttMessage pubRelMessage = new MqttMessage(pubRelHeader, from(messageID));
@@ -704,7 +620,7 @@ public class ProtocolProcessor {
public void processPubComp(Channel channel, MqttMessage msg) { public void processPubComp(Channel channel, MqttMessage msg) {
String clientID = NettyUtils.clientID(channel); String clientID = NettyUtils.clientID(channel);
int messageID = messageId(msg); int messageID = messageId(msg);
LOG.debug("Processing PUBCOMP message. CId = {}, messageId = {}.", clientID, messageID); LOG.debug("Processing PUBCOMP message. CId={}, messageId={}.", clientID, messageID);
// once received the PUBCOMP then remove the message from the temp memory // once received the PUBCOMP then remove the message from the temp memory
ClientSession targetSession = m_sessionsStore.sessionForClient(clientID); ClientSession targetSession = m_sessionsStore.sessionForClient(clientID);
StoredMessage inflightMsg = targetSession.secondPhaseAcknowledged(messageID); StoredMessage inflightMsg = targetSession.secondPhaseAcknowledged(messageID);
@@ -715,7 +631,7 @@ public class ProtocolProcessor {
public void processDisconnect(Channel channel) throws InterruptedException { public void processDisconnect(Channel channel) throws InterruptedException {
final String clientID = NettyUtils.clientID(channel); final String clientID = NettyUtils.clientID(channel);
LOG.info("Processing DISCONNECT message. CId = {}.", clientID); LOG.info("Processing DISCONNECT message. CId={}.", clientID);
channel.flush(); channel.flush();
final ConnectionDescriptor existingDescriptor = this.connectionDescriptors.getConnection(clientID); final ConnectionDescriptor existingDescriptor = this.connectionDescriptors.getConnection(clientID);
if (existingDescriptor == null) { if (existingDescriptor == null) {
@@ -726,82 +642,79 @@ public class ProtocolProcessor {
if (existingDescriptor.doesNotUseChannel(channel)) { if (existingDescriptor.doesNotUseChannel(channel)) {
// another client saved it's descriptor, exit // another client saved it's descriptor, exit
LOG.warn("Another client is using the connection descriptor. Closing connection. CId = {}.", clientID); LOG.warn("Another client is using the connection descriptor. Closing connection. CId={}.", clientID);
existingDescriptor.abort(); existingDescriptor.abort();
return; return;
} }
if (!removeSubscriptions(existingDescriptor, clientID)) { if (!removeSubscriptions(existingDescriptor, clientID)) {
LOG.warn("Unable to remove subscriptions. Closing connection. CId = {}.", clientID); LOG.warn("Unable to remove subscriptions. Closing connection. CId={}.", clientID);
existingDescriptor.abort(); existingDescriptor.abort();
return; return;
} }
if (!dropStoredMessages(existingDescriptor, clientID)) { if (!dropStoredMessages(existingDescriptor, clientID)) {
LOG.warn("Unable to drop stored messages. Closing connection. CId = {}.", clientID); LOG.warn("Unable to drop stored messages. Closing connection. CId={}.", clientID);
existingDescriptor.abort(); existingDescriptor.abort();
return; return;
} }
if (!cleanWillMessageAndNotifyInterceptor(existingDescriptor, clientID)) { if (!cleanWillMessageAndNotifyInterceptor(existingDescriptor, clientID)) {
LOG.warn("Unable to drop will message. Closing connection. CId = {}.", clientID); LOG.warn("Unable to drop will message. Closing connection. CId={}.", clientID);
existingDescriptor.abort(); existingDescriptor.abort();
return; return;
} }
if (!existingDescriptor.close()) { if (!existingDescriptor.close()) {
LOG.info("The connection has been closed. CId = {}.", clientID); LOG.info("The connection has been closed. CId={}.", clientID);
return; return;
} }
boolean stillPresent = this.connectionDescriptors.removeConnection(existingDescriptor); boolean stillPresent = this.connectionDescriptors.removeConnection(existingDescriptor);
if (!stillPresent) { if (!stillPresent) {
// another descriptor was inserted // another descriptor was inserted
LOG.warn("Another descriptor has been inserted. CId = {}.", clientID); LOG.warn("Another descriptor has been inserted. CId={}.", clientID);
return; return;
} }
LOG.info("The DISCONNECT message has been processed. CId = {}.", clientID); LOG.info("The DISCONNECT message has been processed. CId={}.", clientID);
} }
private boolean removeSubscriptions(ConnectionDescriptor descriptor, String clientID) { private boolean removeSubscriptions(ConnectionDescriptor descriptor, String clientID) {
final boolean success = descriptor final boolean success = descriptor.assignState(ESTABLISHED, SUBSCRIPTIONS_REMOVED);
.assignState(ConnectionState.ESTABLISHED, ConnectionState.SUBSCRIPTIONS_REMOVED);
if (!success) { if (!success) {
return false; return false;
} }
if (descriptor.cleanSession) { if (descriptor.cleanSession) {
LOG.info("Removing saved subscriptions. CId = {}.", descriptor.clientID); LOG.info("Removing saved subscriptions. CId={}.", descriptor.clientID);
m_sessionsStore.wipeSubscriptions(clientID); m_sessionsStore.wipeSubscriptions(clientID);
LOG.info("The saved subscriptions have been removed. CId = {}.", descriptor.clientID); LOG.info("The saved subscriptions have been removed. CId={}.", descriptor.clientID);
} }
return true; return true;
} }
private boolean dropStoredMessages(ConnectionDescriptor descriptor, String clientID) { private boolean dropStoredMessages(ConnectionDescriptor descriptor, String clientID) {
final boolean success = descriptor final boolean success = descriptor.assignState(SUBSCRIPTIONS_REMOVED, MESSAGES_DROPPED);
.assignState(ConnectionState.SUBSCRIPTIONS_REMOVED, ConnectionState.MESSAGES_DROPPED);
if (!success) { if (!success) {
return false; return false;
} }
if (descriptor.cleanSession) { if (descriptor.cleanSession) {
LOG.debug("Removing messages of session. CId = {}.", descriptor.clientID); LOG.debug("Removing messages of session. CId={}.", descriptor.clientID);
this.m_sessionsStore.dropQueue(clientID); this.m_sessionsStore.dropQueue(clientID);
LOG.debug("The messages of the session have been removed. CId = {}.", descriptor.clientID); LOG.debug("The messages of the session have been removed. CId={}.", descriptor.clientID);
} }
return true; return true;
} }
private boolean cleanWillMessageAndNotifyInterceptor(ConnectionDescriptor descriptor, String clientID) { private boolean cleanWillMessageAndNotifyInterceptor(ConnectionDescriptor descriptor, String clientID) {
final boolean success = descriptor final boolean success = descriptor.assignState(MESSAGES_DROPPED, INTERCEPTORS_NOTIFIED);
.assignState(ConnectionState.MESSAGES_DROPPED, ConnectionState.INTERCEPTORS_NOTIFIED);
if (!success) { if (!success) {
return false; return false;
} }
LOG.info("Removing will message. ClientId = {}.", descriptor.clientID); LOG.info("Removing will message. ClientId={}.", descriptor.clientID);
// cleanup the will store // cleanup the will store
m_willStore.remove(clientID); m_willStore.remove(clientID);
String username = descriptor.getUsername(); String username = descriptor.getUsername();
@@ -810,7 +723,7 @@ public class ProtocolProcessor {
} }
public void processConnectionLost(String clientID, Channel channel) { public void processConnectionLost(String clientID, Channel channel) {
LOG.info("Processing connection lost event. CId = {}.", clientID); LOG.info("Processing connection lost event. CId={}.", clientID);
ConnectionDescriptor oldConnDescr = new ConnectionDescriptor(clientID, channel, true); ConnectionDescriptor oldConnDescr = new ConnectionDescriptor(clientID, channel, true);
connectionDescriptors.removeConnection(oldConnDescr); connectionDescriptors.removeConnection(oldConnDescr);
// publish the Will message (if any) for the clientID // publish the Will message (if any) for the clientID
@@ -837,7 +750,7 @@ public class ProtocolProcessor {
List<String> topics = msg.payload().topics(); List<String> topics = msg.payload().topics();
String clientID = NettyUtils.clientID(channel); String clientID = NettyUtils.clientID(channel);
LOG.info("Processing UNSUBSCRIBE message. CId = {}, topics = {}.", clientID, topics); LOG.info("Processing UNSUBSCRIBE message. CId={}, topics={}.", clientID, topics);
ClientSession clientSession = m_sessionsStore.sessionForClient(clientID); ClientSession clientSession = m_sessionsStore.sessionForClient(clientID);
for (String t : topics) { for (String t : topics) {
@@ -846,15 +759,12 @@ public class ProtocolProcessor {
if (!validTopic) { if (!validTopic) {
// close the connection, not valid topicFilter is a protocol violation // close the connection, not valid topicFilter is a protocol violation
channel.close(); channel.close();
LOG.error( LOG.error( "Topic filter is not valid. CId={}, topics={}, badTopicFilter={}", clientID, topics,
"The topic filter is not valid. CId = {}, topics = {}, badTopicFilter = {}.", topic);
clientID,
topics,
topic);
return; return;
} }
LOG.debug("Removing subscription. CId = {}, topic = {}.", clientID, topic); LOG.debug("Removing subscription. CId={}, topic={}.", clientID, topic);
subscriptions.removeSubscription(topic, clientID); subscriptions.removeSubscription(topic, clientID);
clientSession.unsubscribeFrom(topic); clientSession.unsubscribeFrom(topic);
String username = NettyUtils.userName(channel); String username = NettyUtils.userName(channel);
@@ -866,35 +776,29 @@ public class ProtocolProcessor {
MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.UNSUBACK, false, AT_LEAST_ONCE, false, 0); MqttFixedHeader fixedHeader = new MqttFixedHeader(MqttMessageType.UNSUBACK, false, AT_LEAST_ONCE, false, 0);
MqttUnsubAckMessage ackMessage = new MqttUnsubAckMessage(fixedHeader, from(messageID)); MqttUnsubAckMessage ackMessage = new MqttUnsubAckMessage(fixedHeader, from(messageID));
LOG.info("Sending UNSUBACK message. CId = {}, topics = {}, messageId = {}.", clientID, topics, messageID); LOG.info("Sending UNSUBACK message. CId={}, topics={}, messageId={}.", clientID, topics, messageID);
channel.writeAndFlush(ackMessage); channel.writeAndFlush(ackMessage);
} }
public void processSubscribe(Channel channel, MqttSubscribeMessage msg) { public void processSubscribe(Channel channel, MqttSubscribeMessage msg) {
String clientID = NettyUtils.clientID(channel); String clientID = NettyUtils.clientID(channel);
int messageID = messageId(msg); int messageID = messageId(msg);
LOG.info("Processing SUBSCRIBE message. CId = {}, messageId = {}.", clientID, messageID); LOG.info("Processing SUBSCRIBE message. CId={}, messageId={}.", clientID, messageID);
RunningSubscription executionKey = new RunningSubscription(clientID, messageID); RunningSubscription executionKey = new RunningSubscription(clientID, messageID);
SubscriptionState currentStatus = this.subscriptionInCourse SubscriptionState currentStatus = this.subscriptionInCourse
.putIfAbsent(executionKey, SubscriptionState.VERIFIED); .putIfAbsent(executionKey, SubscriptionState.VERIFIED);
if (currentStatus != null) { if (currentStatus != null) {
LOG.warn( LOG.warn("Client sent another SUBSCRIBE message while this one was being processed " +
"The client sent another SUBSCRIBE message while this one was being processed. " "CId={}, messageId={}", clientID, messageID);
+ "CId = {}, messageId = {}.",
clientID,
messageID);
return; return;
} }
String username = NettyUtils.userName(channel); String username = NettyUtils.userName(channel);
List<MqttTopicSubscription> ackTopics = doVerify(clientID, username, msg); List<MqttTopicSubscription> ackTopics = doVerify(clientID, username, msg);
MqttSubAckMessage ackMessage = doAckMessageFromValidateFilters(ackTopics, messageID); MqttSubAckMessage ackMessage = doAckMessageFromValidateFilters(ackTopics, messageID);
if (!this.subscriptionInCourse.replace(executionKey, SubscriptionState.VERIFIED, SubscriptionState.STORED)) { if (!this.subscriptionInCourse.replace(executionKey, SubscriptionState.VERIFIED, SubscriptionState.STORED)) {
LOG.warn( LOG.warn("Client sent another SUBSCRIBE message while the topic filters were being verified " +
"The client sent another SUBSCRIBE message while the topic filters were being verified. " "CId={}, messageId={}", clientID, messageID);
+ "CId = {}, messageId = {}.",
clientID,
messageID);
return; return;
} }
@@ -903,7 +807,6 @@ public class ProtocolProcessor {
List<Subscription> newSubscriptions = doStoreSubscription(ackTopics, clientID); List<Subscription> newSubscriptions = doStoreSubscription(ackTopics, clientID);
// save session, persist subscriptions from session // save session, persist subscriptions from session
for (Subscription subscription : newSubscriptions) { for (Subscription subscription : newSubscriptions) {
subscriptions.add(subscription.asClientTopicCouple()); subscriptions.add(subscription.asClientTopicCouple());
} }
@@ -958,32 +861,19 @@ public class ProtocolProcessor {
Topic topic = new Topic(req.topicName()); Topic topic = new Topic(req.topicName());
if (!m_authorizator.canRead(topic, username, clientSession.clientID)) { if (!m_authorizator.canRead(topic, username, clientSession.clientID)) {
// send SUBACK with 0x80, the user hasn't credentials to read the topic // send SUBACK with 0x80, the user hasn't credentials to read the topic
LOG.error( LOG.error("Client does not have read permissions on the topic " +
"The client does not have read permissions on the topic. " "CId={}, username={}, messageId={}, topic={}",
+ "CId = {}, username = {}, messageId = {}, topic = {}.", clientID, username, messageId, topic);
clientID,
username,
messageId,
topic);
ackTopics.add(new MqttTopicSubscription(topic.toString(), FAILURE)); ackTopics.add(new MqttTopicSubscription(topic.toString(), FAILURE));
} else { } else {
MqttQoS qos; MqttQoS qos;
if (topic.isValid()) { if (topic.isValid()) {
LOG.info( LOG.info("Client will be subscribed to the topic " +
"The client will be subscribed to the topic. " "CId={}, username={}, messageId={}, topic={}", clientID, username, messageId, topic);
+ "CId = {}, username = {}, messageId = {}, topic = {}.",
clientID,
username,
messageId,
topic);
qos = req.qualityOfService(); qos = req.qualityOfService();
} else { } else {
LOG.error( LOG.error("Topic filter is not valid CId={}, username={}, messageId={}, topic={}",
"The topic filter is not valid. CId = {}, username = {}, messageId = {}, topic = {}.", clientID, username, messageId, topic);
clientID,
username,
messageId,
topic);
qos = FAILURE; qos = FAILURE;
} }
ackTopics.add(new MqttTopicSubscription(topic.toString(), qos)); ackTopics.add(new MqttTopicSubscription(topic.toString(), qos));
@@ -1007,9 +897,7 @@ public class ProtocolProcessor {
} }
private void publishRetainedMessagesInSession(final Subscription newSubscription, String username) { private void publishRetainedMessagesInSession(final Subscription newSubscription, String username) {
LOG.info( LOG.info("Retrieving retained messages CId={}, topics={}", newSubscription.getClientId(),
"Retrieving retained messages. CId = {}, topics = {}.",
newSubscription.getClientId(),
newSubscription.getTopicFilter()); newSubscription.getTopicFilter());
// scans retained messages to be published to the new subscription // scans retained messages to be published to the new subscription
@@ -1023,11 +911,8 @@ public class ProtocolProcessor {
}); });
if (!messages.isEmpty()) { if (!messages.isEmpty()) {
LOG.info( LOG.info("Publishing retained messages CId={}, topics={}, messagesNo={}",
"Publishing retained messages. CId = {}, topics = {}, messagesNo = {}.", newSubscription.getClientId(), newSubscription.getTopicFilter(), messages.size());
newSubscription.getClientId(),
newSubscription.getTopicFilter(),
messages.size());
} }
ClientSession targetSession = m_sessionsStore.sessionForClient(newSubscription.getClientId()); ClientSession targetSession = m_sessionsStore.sessionForClient(newSubscription.getClientId());
this.internalRepublisher.publishRetained(targetSession, messages); this.internalRepublisher.publishRetained(targetSession, messages);
@@ -1047,12 +932,8 @@ public class ProtocolProcessor {
} else { } else {
// recreate a publish from stored publish in queue // recreate a publish from stored publish in queue
boolean retained = m_messagesStore.getMessageByGuid(msg.getGuid()) != null; boolean retained = m_messagesStore.getMessageByGuid(msg.getGuid()) != null;
MqttPublishMessage pubMsg = createPublishForQos( MqttPublishMessage pubMsg = createPublishForQos( msg.getTopic(), msg.getQos(), msg.getMessage(),
msg.getTopic(), retained, 0);
msg.getQos(),
msg.getMessage(),
retained,
0);
channel.write(pubMsg); channel.write(pubMsg);
} }
} }