Removed client active/not active state from persistence (issue #118)

This commit is contained in:
andsel
2015-12-19 18:01:54 +01:00
parent 4f65a59324
commit 8e891da49f
9 changed files with 67 additions and 62 deletions
@@ -401,4 +401,46 @@ public class ServerIntegrationPahoTest {
subscriberB.disconnect();
}
@Test
public void testSubcriptionDoesntStayActiveAfterARestart() throws Exception {
LOG.info("*** testSubcriptionDoesntStayActiveAfterARestart ***");
String tmpDir = System.getProperty("java.io.tmpdir");
//clientForSubscribe1 connect and subscribe to /topic QoS2
MqttClientPersistence dsSubscriberA = new MqttDefaultFilePersistence(tmpDir + File.separator + "clientForSubscribe1");
MqttClient clientForSubscribe1 = new MqttClient("tcp://localhost:1883", "clientForSubscribe1", dsSubscriberA);
TestCallback cbSubscriber1 = new TestCallback();
clientForSubscribe1.setCallback(cbSubscriber1);
clientForSubscribe1.connect();
clientForSubscribe1.subscribe("topic", 0);
//server stop
m_server.stopServer();
System.out.println("\n\n SEVER REBOOTING \n\n");
//server start
startServer();
//clientForSubscribe2 connect and subscribe to /topic QoS2
MqttClientPersistence dsSubscriberB = new MqttDefaultFilePersistence(tmpDir + File.separator + "clientForSubscribe2");
MqttClient clientForSubscribe2 = new MqttClient("tcp://localhost:1883", "clientForSubscribe2", dsSubscriberB);
TestCallback cbSubscriber2 = new TestCallback();
clientForSubscribe2.setCallback(cbSubscriber2);
clientForSubscribe2.connect();
clientForSubscribe2.subscribe("topic", 0);
//clientForPublish publish on /topic with QoS2 a message
MqttClientPersistence dsSubscriberPUB = new MqttDefaultFilePersistence(tmpDir + File.separator + "clientForPublish");
MqttClient clientForPublish = new MqttClient("tcp://localhost:1883", "clientForPublish", dsSubscriberPUB);
clientForPublish.connect();
clientForPublish.publish("topic", "Hello".getBytes(), 2, true);
//verify clientForSubscribe1 doesn't receive a notification but clientForSubscribe2 yes
System.out.println("Before waiting to receive 1 sec from " + clientForSubscribe1.getClientId());
assertFalse(clientForSubscribe1.isConnected());
assertTrue(clientForSubscribe2.isConnected());
System.out.println("Waiting to receive 1 sec from " + clientForSubscribe2.getClientId());
MqttMessage messageOnB = cbSubscriber2.getMessage(true);
assertEquals("Hello", new String(messageOnB.getPayload()));
}
}
@@ -18,6 +18,7 @@ package io.moquette.spi.impl;
import io.moquette.proto.messages.*;
import io.moquette.interception.InterceptHandler;
import io.moquette.proto.messages.AbstractMessage.QOSType;
import io.moquette.server.ConnectionDescriptor;
import io.moquette.server.netty.NettyChannel;
import io.moquette.spi.ClientSession;
import io.moquette.spi.IMatchingCondition;
@@ -504,6 +505,9 @@ public class ProtocolProcessorTest {
}
};
processor.init(subscriptions, memoryMessageStore, sessionsStore, null, true, null, NO_OBSERVERS_INTERCEPTOR);
//just to activate the two sessions
processor.m_clientIDs.put("Sub A", new ConnectionDescriptor("Sub A", null, true));
processor.m_clientIDs.put("Sub B", new ConnectionDescriptor("Sub B", null, true));
//Exercise
processor.route2Subscribers(forwardPublish);
@@ -110,7 +110,7 @@ public class MemorySessionStore implements ISessionsStore {
}
LOG.debug("clientID {} is a newcome, creating it's empty subscriptions set", clientID);
m_persistentSubscriptions.put(clientID, new HashSet<Subscription>());
m_persistentSessions.put(clientID, new MapDBPersistentStore.PersistentSession(cleanSession, false));
m_persistentSessions.put(clientID, new MapDBPersistentStore.PersistentSession(cleanSession));
return new ClientSession(clientID, m_messagesStore, this, cleanSession);
}
@@ -121,11 +121,7 @@ public class MemorySessionStore implements ISessionsStore {
}
MapDBPersistentStore.PersistentSession storedSession = m_persistentSessions.get(clientID);
ClientSession clientSession = new ClientSession(clientID, m_messagesStore, this, storedSession.cleanSession);
if (storedSession.active) {
clientSession.activate();
}
return clientSession;
return new ClientSession(clientID, m_messagesStore, this, storedSession.cleanSession);
}
@Override
@@ -153,25 +149,6 @@ public class MemorySessionStore implements ISessionsStore {
return null;
}
@Override
public void activate(String clientID) {
activationHelper(clientID, true);
}
@Override
public void deactivate(String clientID) {
activationHelper(clientID, false);
}
private void activationHelper(String clientID, boolean activation) {
MapDBPersistentStore.PersistentSession storedSession = m_persistentSessions.get(clientID);
if (storedSession == null) {
throw new IllegalStateException((activation ? "activating" : "deactivating") + " a session never stored/created, clientID <"+ clientID + ">", null);
}
MapDBPersistentStore.PersistentSession newStoredSession = new MapDBPersistentStore.PersistentSession(storedSession.cleanSession, activation);
m_persistentSessions.put(clientID, newStoredSession);
}
@Override
public void inFlightAck(String clientID, int messageID) {
Map<Integer, String> m = this.m_inflightStore.get(clientID);