Protected the shared variable that maintans binding between active clientID and related IoSessions with lock
This commit is contained in:
@@ -2,6 +2,8 @@ package org.dna.mqtt.moquette.server;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.locks.Lock;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
import org.apache.mina.core.service.IoHandlerAdapter;
|
import org.apache.mina.core.service.IoHandlerAdapter;
|
||||||
import org.apache.mina.core.session.IdleStatus;
|
import org.apache.mina.core.session.IdleStatus;
|
||||||
import org.apache.mina.core.session.IoSession;
|
import org.apache.mina.core.session.IoSession;
|
||||||
@@ -25,11 +27,13 @@ import org.slf4j.LoggerFactory;
|
|||||||
* @author andrea
|
* @author andrea
|
||||||
*/
|
*/
|
||||||
public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
||||||
|
|
||||||
protected static final String ATTR_CLIENTID = "ClientID";
|
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
|
||||||
|
*/
|
||||||
|
Lock m_clientIDsLock = new ReentrantLock();
|
||||||
Map<String, ConnectionDescriptor> m_clientIDs = new HashMap<String, ConnectionDescriptor>();
|
Map<String, ConnectionDescriptor> m_clientIDs = new HashMap<String, ConnectionDescriptor>();
|
||||||
private IMessaging m_messaging;
|
private IMessaging m_messaging;
|
||||||
private IAuthenticator m_authenticator;
|
private IAuthenticator m_authenticator;
|
||||||
@@ -51,6 +55,7 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
break;
|
break;
|
||||||
case PINGREQ:
|
case PINGREQ:
|
||||||
session.write(new PingRespMessage());
|
session.write(new PingRespMessage());
|
||||||
|
break;
|
||||||
case DISCONNECT:
|
case DISCONNECT:
|
||||||
handleDisconnect(session, (DisconnectMessage) msg);
|
handleDisconnect(session, (DisconnectMessage) msg);
|
||||||
|
|
||||||
@@ -77,22 +82,27 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//if an old client with the same ID already exists close its session.
|
m_clientIDsLock.lock();
|
||||||
if (m_clientIDs.containsKey(msg.getClientID())) {
|
try {
|
||||||
//clean the subscriptions if the old used a cleanSession = true
|
//if an old client with the same ID already exists close its session.
|
||||||
IoSession oldSession = m_clientIDs.get(msg.getClientID()).getSession();
|
if (m_clientIDs.containsKey(msg.getClientID())) {
|
||||||
boolean cleanSession = (Boolean) oldSession.getAttribute("cleanSession");
|
//clean the subscriptions if the old used a cleanSession = true
|
||||||
if (cleanSession) {
|
IoSession oldSession = m_clientIDs.get(msg.getClientID()).getSession();
|
||||||
//cleanup topic subscriptions
|
boolean cleanSession = (Boolean) oldSession.getAttribute("cleanSession");
|
||||||
m_messaging.removeSubscriptions(msg.getClientID());
|
if (cleanSession) {
|
||||||
|
//cleanup topic subscriptions
|
||||||
|
m_messaging.removeSubscriptions(msg.getClientID());
|
||||||
|
}
|
||||||
|
|
||||||
|
m_clientIDs.get(msg.getClientID()).getSession().close(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_clientIDs.get(msg.getClientID()).getSession().close(false);
|
ConnectionDescriptor connDescr = new ConnectionDescriptor(msg.getClientID(), session, msg.isCleanSession());
|
||||||
|
m_clientIDs.put(msg.getClientID(), connDescr);
|
||||||
|
} finally {
|
||||||
|
m_clientIDsLock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
ConnectionDescriptor connDescr = new ConnectionDescriptor(msg.getClientID(), session, msg.isCleanSession());
|
|
||||||
m_clientIDs.put(msg.getClientID(), connDescr);
|
|
||||||
|
|
||||||
int keepAlive = msg.getKeepAlive();
|
int keepAlive = msg.getKeepAlive();
|
||||||
session.setAttribute("keepAlive", keepAlive);
|
session.setAttribute("keepAlive", keepAlive);
|
||||||
session.setAttribute("cleanSession", msg.isCleanSession());
|
session.setAttribute("cleanSession", msg.isCleanSession());
|
||||||
@@ -138,7 +148,7 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
|
|
||||||
protected void handleSubscribe(IoSession session, SubscribeMessage msg) {
|
protected void handleSubscribe(IoSession session, SubscribeMessage msg) {
|
||||||
LOG.info("registering the subscriptions");
|
LOG.info("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(ATTR_CLIENTID),
|
||||||
req.getTopic(), AbstractMessage.QOSType.values()[req.getQos()]);
|
req.getTopic(), AbstractMessage.QOSType.values()[req.getQos()]);
|
||||||
}
|
}
|
||||||
@@ -148,7 +158,7 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
ackMessage.setMessageID(msg.getMessageID());
|
ackMessage.setMessageID(msg.getMessageID());
|
||||||
|
|
||||||
//TODO by now it handles only QoS 0 messages
|
//TODO by now it handles only QoS 0 messages
|
||||||
for (int i=0; i < msg.subscriptions().size(); i++) {
|
for (int i = 0; i < msg.subscriptions().size(); i++) {
|
||||||
ackMessage.addType(QOSType.MOST_ONE);
|
ackMessage.addType(QOSType.MOST_ONE);
|
||||||
}
|
}
|
||||||
LOG.info("replying with SubAct to MSG ID {0}", msg.getMessageID());
|
LOG.info("replying with SubAct to MSG ID {0}", msg.getMessageID());
|
||||||
@@ -163,7 +173,12 @@ public class MQTTHandler extends IoHandlerAdapter implements INotifier {
|
|||||||
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(ATTR_CLIENTID);
|
||||||
//remove from clientIDs
|
//remove from clientIDs
|
||||||
m_clientIDs.remove(clientID);
|
m_clientIDsLock.lock();
|
||||||
|
try {
|
||||||
|
m_clientIDs.remove(clientID);
|
||||||
|
} finally {
|
||||||
|
m_clientIDsLock.unlock();
|
||||||
|
}
|
||||||
boolean cleanSession = (Boolean) session.getAttribute("cleanSession");
|
boolean cleanSession = (Boolean) session.getAttribute("cleanSession");
|
||||||
if (cleanSession) {
|
if (cleanSession) {
|
||||||
//cleanup topic subscriptions
|
//cleanup topic subscriptions
|
||||||
|
|||||||
Reference in New Issue
Block a user