Fixed serious bug of client descriptor drop on connectionLost event for antoher client with sae ID (bug notified on email by ZhiPeng Yang)

This commit is contained in:
andrea
2015-01-10 17:53:31 +01:00
parent 6bde0e7c9b
commit 1e63652d32
7 changed files with 79 additions and 29 deletions
@@ -79,7 +79,7 @@ public class NettyMQTTHandler extends ChannelInboundHandlerAdapter {
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
NettyChannel channel = m_channelMapper.get(ctx);
String clientID = (String) channel.getAttribute(Constants.ATTR_CLIENTID);
m_messaging.lostConnection(clientID);
m_messaging.lostConnection(channel, clientID);
ctx.close(/*false*/);
synchronized(m_channelMapper) {
m_channelMapper.remove(ctx);
@@ -30,7 +30,7 @@ public interface IMessaging {
void stop();
void lostConnection(String clientID);
void lostConnection(ServerChannel session, String clientID);
void handleProtocolMessage(ServerChannel session, AbstractMessage msg);
}
@@ -28,10 +28,7 @@ import java.util.concurrent.Executors;
import org.eclipse.moquette.spi.IMatchingCondition;
import org.eclipse.moquette.spi.IMessagesStore;
import org.eclipse.moquette.spi.ISessionsStore;
import org.eclipse.moquette.spi.impl.events.MessagingEvent;
import org.eclipse.moquette.spi.impl.events.OutputMessagingEvent;
import org.eclipse.moquette.spi.impl.events.PubAckEvent;
import org.eclipse.moquette.spi.impl.events.PublishEvent;
import org.eclipse.moquette.spi.impl.events.*;
import org.eclipse.moquette.spi.impl.subscriptions.Subscription;
import org.eclipse.moquette.spi.impl.subscriptions.SubscriptionsStore;
import static org.eclipse.moquette.parser.netty.Utils.VERSION_3_1;
@@ -100,13 +97,13 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
private static final Logger LOG = LoggerFactory.getLogger(ProtocolProcessor.class);
private Map<String, ConnectionDescriptor> m_clientIDs = new HashMap<String, ConnectionDescriptor>();
private Map<String, ConnectionDescriptor> m_clientIDs = new HashMap<>();
private SubscriptionsStore subscriptions;
private IMessagesStore m_messagesStore;
private ISessionsStore m_sessionsStore;
private IAuthenticator m_authenticator;
//maps clientID to Will testament, if specified on CONNECT
private Map<String, WillMessage> m_willStore = new HashMap<String, WillMessage>();
private Map<String, WillMessage> m_willStore = new HashMap<>();
private ExecutorService m_executor;
private RingBuffer<ValueEvent> m_ringBuffer;
@@ -163,6 +160,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
//if an old client with the same ID already exists close its session.
if (m_clientIDs.containsKey(msg.getClientID())) {
LOG.info("Found an existing connection with same client ID <{}>, forcing to close", msg.getClientID());
//clean the subscriptions if the old used a cleanSession = true
ServerChannel oldSession = m_clientIDs.get(msg.getClientID()).getSession();
boolean cleanSession = (Boolean) oldSession.getAttribute(Constants.CLEAN_SESSION);
@@ -171,8 +169,8 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
cleanSession(msg.getClientID());
}
m_clientIDs.get(msg.getClientID()).getSession().close(false);
LOG.info("Found an existing connection with same client ID <{}>, forced to close", msg.getClientID());
oldSession.close(false);
LOG.debug("Existing connection with same client ID <{}>, forced to close", msg.getClientID());
}
ConnectionDescriptor connDescr = new ConnectionDescriptor(msg.getClientID(), session, msg.isCleanSession());
@@ -283,7 +281,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
}
private void processPublish(String clientID, String topic, QOSType qos, ByteBuffer message, boolean retain, Integer messageID) {
LOG.info("Publish recieved from clientID <{}> on topic <{}> with QoS {}",
LOG.info("Publish received from clientID <{}> on topic <{}> with QoS {}",
clientID, topic, qos);
String publishKey = null;
@@ -527,7 +525,15 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
LOG.info("Disconnected client <{}> with clean session {}", clientID, cleanSession);
}
void processConnectionLost(String clientID) {
void processConnectionLost(LostConnectionEvent evt) {
String clientID = evt.clientID;
if (m_clientIDs.containsKey(clientID)) {
if (!m_clientIDs.get(clientID).getSession().equals(evt.session)) {
LOG.info("Received a lost connection with client <{}> for a not matching session", clientID);
return;
}
}
//If already removed a disconnect message was already processed for this clientID
if (m_clientIDs.remove(clientID) != null) {
@@ -107,8 +107,8 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
}
@Override
public void lostConnection(String clientID) {
disruptorPublish(new LostConnectionEvent(clientID));
public void lostConnection(ServerChannel session, String clientID) {
disruptorPublish(new LostConnectionEvent(session, clientID));
}
@Override
@@ -145,7 +145,7 @@ public class SimpleMessaging implements IMessaging, EventHandler<ValueEvent> {
}
if (evt instanceof LostConnectionEvent) {
LostConnectionEvent lostEvt = (LostConnectionEvent) evt;
m_processor.processConnectionLost(lostEvt.getClientID());
m_processor.processConnectionLost(lostEvt);
return;
}
@@ -15,20 +15,19 @@
*/
package org.eclipse.moquette.spi.impl.events;
import org.eclipse.moquette.server.ServerChannel;
/**
* Used to model the connection lost event
*
* @author andrea
*/
public class LostConnectionEvent extends MessagingEvent{
private String m_clientID;
public class LostConnectionEvent extends MessagingEvent {
public final ServerChannel session;
public final String clientID;
public LostConnectionEvent(String clienID) {
m_clientID = clienID;
public LostConnectionEvent(ServerChannel session, String clientID) {
this.session = session;
this.clientID = clientID;
}
public String getClientID() {
return m_clientID;
}
}