Fixed bug in deletion of valid conn descriptor once different clients with same ID connects (issues #116 and #124)

This commit is contained in:
andsel
2015-12-27 13:19:16 +01:00
parent 571eaa1378
commit 163858e47b
7 changed files with 94 additions and 5 deletions
@@ -38,4 +38,23 @@ public class ConnectionDescriptor {
public String toString() {
return "ConnectionDescriptor{" + "clientID=" + clientID + ", cleanSession=" + cleanSession + '}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ConnectionDescriptor that = (ConnectionDescriptor) o;
if (clientID != null ? !clientID.equals(that.clientID) : that.clientID != null) return false;
return !(session != null ? !session.equals(that.session) : that.session != null);
}
@Override
public int hashCode() {
int result = clientID != null ? clientID.hashCode() : 0;
result = 31 * result + (session != null ? session.hashCode() : 0);
return result;
}
}
@@ -73,4 +73,20 @@ public class NettyChannel implements ServerChannel {
String clientID = (String) getAttribute(ATTR_KEY_CLIENTID);
return "session [clientID: "+ clientID +"]";
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NettyChannel that = (NettyChannel) o;
return !(m_channel != null ? !m_channel.channel().equals(that.m_channel.channel()) : that.m_channel != null);
}
@Override
public int hashCode() {
return m_channel != null ? m_channel.channel().hashCode() : 0;
}
}
@@ -96,7 +96,7 @@ public class NettyMQTTHandler extends ChannelInboundHandlerAdapter {
if (stolenAttr != null && stolenAttr == Boolean.TRUE) {
stolen = stolenAttr;
}
m_processor.processConnectionLost(clientID, stolen);
m_processor.processConnectionLost(clientID, stolen, new NettyChannel(ctx));
}
ctx.close();
}
@@ -18,6 +18,7 @@ package io.moquette.spi.impl;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import io.moquette.server.ConnectionDescriptor;
import io.moquette.server.ServerChannel;
@@ -92,7 +93,7 @@ public class ProtocolProcessor {
private static final Logger LOG = LoggerFactory.getLogger(ProtocolProcessor.class);
protected Map<String, ConnectionDescriptor> m_clientIDs;
protected ConcurrentMap<String, ConnectionDescriptor> m_clientIDs;
private SubscriptionsStore subscriptions;
private boolean allowAnonymous;
private IAuthorizator m_authorizator;
@@ -102,7 +103,7 @@ public class ProtocolProcessor {
private BrokerInterceptor m_interceptor;
//maps clientID to Will testament, if specified on CONNECT
private Map<String, WillMessage> m_willStore = new ConcurrentHashMap<>();
private ConcurrentMap<String, WillMessage> m_willStore = new ConcurrentHashMap<>();
ProtocolProcessor() {}
@@ -230,6 +231,7 @@ public class ProtocolProcessor {
republishStoredInSession(clientSession);
}
LOG.info("CONNECT processed");
// LOG.info("CONNECT clients descriptors {}", m_clientIDs);
}
private void failedCredentials(ServerChannel session) {
@@ -630,9 +632,11 @@ public class ProtocolProcessor {
LOG.info("DISCONNECT client <{}> finished", clientID, cleanSession);
}
public void processConnectionLost(String clientID, boolean sessionStolen) {
public void processConnectionLost(String clientID, boolean sessionStolen, NettyChannel channel) {
ConnectionDescriptor oldConnDescr = new ConnectionDescriptor(clientID, channel, true);
m_clientIDs.remove(clientID, oldConnDescr);
//If already removed a disconnect message was already processed for this clientID
if (sessionStolen && m_clientIDs.remove(clientID) != null) {
if (sessionStolen) {
//de-activate the subscriptions for this ClientID
ClientSession clientSession = m_sessionsStore.sessionForClient(clientID);
clientSession.deactivate();