Extracted MQTT session constants into separate class
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
package org.dna.mqtt.moquette.server;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Server constants keeper
|
||||||
|
*/
|
||||||
|
public class Constants {
|
||||||
|
protected static final String ATTR_CLIENTID = "ClientID";
|
||||||
|
public static final String CLEAN_SESSION = "cleanSession";
|
||||||
|
}
|
||||||
@@ -24,7 +24,6 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
||||||
|
|
||||||
protected static final String ATTR_CLIENTID = "ClientID";
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(MQTTHandler.class);
|
private static final Logger LOG = LoggerFactory.getLogger(MQTTHandler.class);
|
||||||
/**
|
/**
|
||||||
* Maps CLIENT_ID to the IoSession that represents the connection
|
* Maps CLIENT_ID to the IoSession that represents the connection
|
||||||
@@ -87,7 +86,7 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
if (m_clientIDs.containsKey(msg.getClientID())) {
|
if (m_clientIDs.containsKey(msg.getClientID())) {
|
||||||
//clean the subscriptions if the old used a cleanSession = true
|
//clean the subscriptions if the old used a cleanSession = true
|
||||||
IoSession oldSession = m_clientIDs.get(msg.getClientID()).getSession();
|
IoSession oldSession = m_clientIDs.get(msg.getClientID()).getSession();
|
||||||
boolean cleanSession = (Boolean) oldSession.getAttribute("cleanSession");
|
boolean cleanSession = (Boolean) oldSession.getAttribute(Constants.CLEAN_SESSION);
|
||||||
if (cleanSession) {
|
if (cleanSession) {
|
||||||
//cleanup topic subscriptions
|
//cleanup topic subscriptions
|
||||||
m_messaging.removeSubscriptions(msg.getClientID());
|
m_messaging.removeSubscriptions(msg.getClientID());
|
||||||
@@ -104,9 +103,9 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
|
|
||||||
int keepAlive = msg.getKeepAlive();
|
int keepAlive = msg.getKeepAlive();
|
||||||
session.setAttribute("keepAlive", keepAlive);
|
session.setAttribute("keepAlive", keepAlive);
|
||||||
session.setAttribute("cleanSession", msg.isCleanSession());
|
session.setAttribute(Constants.CLEAN_SESSION, msg.isCleanSession());
|
||||||
//used to track the client in the subscription and publishing phases.
|
//used to track the client in the subscription and publishing phases.
|
||||||
session.setAttribute(ATTR_CLIENTID, msg.getClientID());
|
session.setAttribute(Constants.ATTR_CLIENTID, msg.getClientID());
|
||||||
|
|
||||||
session.getConfig().setIdleTime(IdleStatus.READER_IDLE, Math.round(keepAlive * 1.5f));
|
session.getConfig().setIdleTime(IdleStatus.READER_IDLE, Math.round(keepAlive * 1.5f));
|
||||||
|
|
||||||
@@ -114,7 +113,7 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
if (msg.isWillFlag()) {
|
if (msg.isWillFlag()) {
|
||||||
QOSType willQos = QOSType.values()[msg.getWillQos()];
|
QOSType willQos = QOSType.values()[msg.getWillQos()];
|
||||||
m_messaging.publish(msg.getWillTopic(), msg.getWillMessage().getBytes(),
|
m_messaging.publish(msg.getWillTopic(), msg.getWillMessage().getBytes(),
|
||||||
willQos, msg.isWillRetain(), msg.getClientID());
|
willQos, msg.isWillRetain(), msg.getClientID(), session);
|
||||||
}
|
}
|
||||||
|
|
||||||
//handle user authentication
|
//handle user authentication
|
||||||
@@ -148,7 +147,7 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
protected void handleSubscribe(IoSession session, SubscribeMessage msg) {
|
protected void handleSubscribe(IoSession session, SubscribeMessage msg) {
|
||||||
LOG.debug("handleSubscribe, registering the subscriptions");
|
LOG.debug("handleSubscribe, registering the subscriptions");
|
||||||
for (SubscribeMessage.Couple req : msg.subscriptions()) {
|
for (SubscribeMessage.Couple req : msg.subscriptions()) {
|
||||||
m_messaging.subscribe((String) session.getAttribute(ATTR_CLIENTID),
|
m_messaging.subscribe((String) session.getAttribute(Constants.ATTR_CLIENTID),
|
||||||
req.getTopic(), AbstractMessage.QOSType.values()[req.getQos()]);
|
req.getTopic(), AbstractMessage.QOSType.values()[req.getQos()]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +166,7 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
private void handleUnsubscribe(IoSession session, UnsubscribeMessage msg) {
|
private void handleUnsubscribe(IoSession session, UnsubscribeMessage msg) {
|
||||||
LOG.info("unregistering the subscriptions");
|
LOG.info("unregistering the subscriptions");
|
||||||
for (String topic : msg.topics()) {
|
for (String topic : msg.topics()) {
|
||||||
m_messaging.unsubscribe(topic, (String) session.getAttribute(ATTR_CLIENTID));
|
m_messaging.unsubscribe(topic, (String) session.getAttribute(Constants.ATTR_CLIENTID));
|
||||||
}
|
}
|
||||||
//ack the client
|
//ack the client
|
||||||
UnsubAckMessage ackMessage = new UnsubAckMessage();
|
UnsubAckMessage ackMessage = new UnsubAckMessage();
|
||||||
@@ -178,19 +177,19 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected void handlePublish(IoSession session, PublishMessage message) {
|
protected void handlePublish(IoSession session, PublishMessage message) {
|
||||||
String clientID = (String) session.getAttribute(ATTR_CLIENTID);
|
String clientID = (String) session.getAttribute(Constants.ATTR_CLIENTID);
|
||||||
|
|
||||||
if (message.getQos() == QOSType.MOST_ONE) {
|
if (message.getQos() == QOSType.MOST_ONE) {
|
||||||
m_messaging.publish(message.getTopicName(), message.getPayload(),
|
m_messaging.publish(message.getTopicName(), message.getPayload(),
|
||||||
message.getQos(), message.isRetainFlag(), clientID);
|
message.getQos(), message.isRetainFlag(), clientID, session);
|
||||||
} else {
|
} else {
|
||||||
m_messaging.publish(message.getTopicName(), message.getPayload(),
|
m_messaging.publish(message.getTopicName(), message.getPayload(),
|
||||||
message.getQos(), message.isRetainFlag(), clientID, message.getMessageID());
|
message.getQos(), message.isRetainFlag(), clientID, message.getMessageID(), session);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void handleDisconnect(IoSession session, DisconnectMessage disconnectMessage) {
|
protected void handleDisconnect(IoSession session, DisconnectMessage disconnectMessage) {
|
||||||
String clientID = (String) session.getAttribute(ATTR_CLIENTID);
|
String clientID = (String) session.getAttribute(Constants.ATTR_CLIENTID);
|
||||||
//remove from clientIDs
|
//remove from clientIDs
|
||||||
// m_clientIDsLock.lock();
|
// m_clientIDsLock.lock();
|
||||||
// try {
|
// try {
|
||||||
@@ -198,7 +197,7 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
// } finally {
|
// } finally {
|
||||||
// m_clientIDsLock.unlock();
|
// m_clientIDsLock.unlock();
|
||||||
// }
|
// }
|
||||||
boolean cleanSession = (Boolean) session.getAttribute("cleanSession");
|
boolean cleanSession = (Boolean) session.getAttribute(Constants.CLEAN_SESSION);
|
||||||
if (cleanSession) {
|
if (cleanSession) {
|
||||||
//cleanup topic subscriptions
|
//cleanup topic subscriptions
|
||||||
m_messaging.removeSubscriptions(clientID);
|
m_messaging.removeSubscriptions(clientID);
|
||||||
@@ -252,7 +251,7 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect(IoSession session) {
|
public void disconnect(IoSession session) {
|
||||||
String clientID = (String) session.getAttribute(ATTR_CLIENTID);
|
String clientID = (String) session.getAttribute(Constants.ATTR_CLIENTID);
|
||||||
m_clientIDsLock.lock();
|
m_clientIDsLock.lock();
|
||||||
try {
|
try {
|
||||||
m_clientIDs.remove(clientID);
|
m_clientIDs.remove(clientID);
|
||||||
|
|||||||
Reference in New Issue
Block a user