Fixed bug #108, remove of clientID for a stolen session the new session get immediately removed

This commit is contained in:
andsel
2015-10-17 18:52:04 +02:00
parent c96ce9d37e
commit a7aba651b8
4 changed files with 20 additions and 11 deletions
@@ -31,11 +31,13 @@ public class NettyChannel implements ServerChannel {
private ChannelHandlerContext m_channel; private ChannelHandlerContext m_channel;
public static final String ATTR_USERNAME = "username"; public static final String ATTR_USERNAME = "username";
public static final String ATTR_SESSION_STOLEN = "sessionStolen";
public static final AttributeKey<Object> ATTR_KEY_KEEPALIVE = AttributeKey.valueOf(Constants.KEEP_ALIVE); public static final AttributeKey<Object> ATTR_KEY_KEEPALIVE = AttributeKey.valueOf(Constants.KEEP_ALIVE);
public static final AttributeKey<Object> ATTR_KEY_CLEANSESSION = AttributeKey.valueOf(Constants.CLEAN_SESSION); public static final AttributeKey<Object> ATTR_KEY_CLEANSESSION = AttributeKey.valueOf(Constants.CLEAN_SESSION);
public static final AttributeKey<Object> ATTR_KEY_CLIENTID = AttributeKey.valueOf(Constants.ATTR_CLIENTID); public static final AttributeKey<Object> ATTR_KEY_CLIENTID = AttributeKey.valueOf(Constants.ATTR_CLIENTID);
public static final AttributeKey<Object> ATTR_KEY_USERNAME = AttributeKey.valueOf(ATTR_USERNAME); public static final AttributeKey<Object> ATTR_KEY_USERNAME = AttributeKey.valueOf(ATTR_USERNAME);
public static final AttributeKey<Object> ATTR_KEY_SESSION_STOLEN = AttributeKey.valueOf(ATTR_SESSION_STOLEN);
NettyChannel(ChannelHandlerContext ctx) { NettyChannel(ChannelHandlerContext ctx) {
m_channel = ctx; m_channel = ctx;
@@ -98,9 +98,14 @@ public class NettyMQTTHandler extends ChannelInboundHandlerAdapter {
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
String clientID = (String) NettyUtils.getAttribute(ctx, NettyChannel.ATTR_KEY_CLIENTID); String clientID = (String) NettyUtils.getAttribute(ctx, NettyChannel.ATTR_KEY_CLIENTID);
if (clientID != null && !clientID.isEmpty()) { if (clientID != null && !clientID.isEmpty()) {
//if the channel was of a correctly connected client, inform messagin //if the channel was of a correctly connected client, inform messaging
//else it was of a not completed CONNECT message //else it was of a not completed CONNECT message or sessionStolen
m_processor.processConnectionLost(new LostConnectionEvent(clientID)); boolean stolen = false;
Boolean stolenAttr = (Boolean) NettyUtils.getAttribute(ctx, NettyChannel.ATTR_KEY_SESSION_STOLEN);
if (stolenAttr != null && stolenAttr == Boolean.TRUE) {
stolen = stolenAttr;
}
m_processor.processConnectionLost(new LostConnectionEvent(clientID, stolen));
} }
ctx.close(/*false*/); ctx.close(/*false*/);
} }
@@ -181,6 +181,7 @@ public class ProtocolProcessor {
cleanSession(msg.getClientID()); cleanSession(msg.getClientID());
} }
oldSession.setAttribute(NettyChannel.ATTR_KEY_SESSION_STOLEN, true);
oldSession.close(false); oldSession.close(false);
LOG.debug("Existing connection with same client ID <{}>, forced to close", msg.getClientID()); LOG.debug("Existing connection with same client ID <{}>, forced to close", msg.getClientID());
} }
@@ -544,14 +545,13 @@ public class ProtocolProcessor {
public void processConnectionLost(LostConnectionEvent evt) { public void processConnectionLost(LostConnectionEvent evt) {
String clientID = evt.clientID; String clientID = evt.clientID;
//If already removed a disconnect message was already processed for this clientID //If already removed a disconnect message was already processed for this clientID
if (m_clientIDs.remove(clientID) != null) { if (evt.sessionStolen && m_clientIDs.remove(clientID) != null) {
//de-activate the subscriptions for this ClientID //de-activate the subscriptions for this ClientID
subscriptions.deactivate(clientID); subscriptions.deactivate(clientID);
LOG.info("Lost connection with client <{}>", clientID); LOG.info("Lost connection with client <{}>", clientID);
} }
//publish the Will message (if any) for the clientID //publish the Will message (if any) for the clientID
if (m_willStore.containsKey(clientID)) { if (!evt.sessionStolen && m_willStore.containsKey(clientID)) {
WillMessage will = m_willStore.get(clientID); WillMessage will = m_willStore.get(clientID);
forwardPublishWill(will, clientID); forwardPublishWill(will, clientID);
m_willStore.remove(clientID); m_willStore.remove(clientID);
@@ -15,19 +15,21 @@
*/ */
package org.eclipse.moquette.spi.impl.events; package org.eclipse.moquette.spi.impl.events;
import org.eclipse.moquette.server.ServerChannel;
/** /**
* Used to model the connection lost event * Used to model the connection lost event
* *
* @author andrea * @author andrea
*/ */
public class LostConnectionEvent extends MessagingEvent { public class LostConnectionEvent extends MessagingEvent {
// public final ServerChannel session;
public final String clientID; public final String clientID;
public final boolean sessionStolen;
public LostConnectionEvent(/*ServerChannel session, */String clientID) { public LostConnectionEvent(String clientID) {
// this.session = session; this(clientID, false);
}
public LostConnectionEvent(String clientID, boolean stolen) {
this.clientID = clientID; this.clientID = clientID;
this.sessionStolen = stolen;
} }
} }