void handleConnectionLost() { String clientID = NettyUtils.clientID(channel); if (clientID == null || clientID.isEmpty()) { return; } LOG.info("Notifying connection lost event. CId: {}, channel: {}", clientID, channel); Session session = sessionRegistry.retrieve(clientID); if (session.hasWill()) { postOffice.fireWill(session.getWill()); } if (session.isClean()) { sessionRegistry.remove(clientID); } else { sessionRegistry.disconnect(clientID); } connected = false; //dispatch connection lost to intercept. String userName = NettyUtils.userName(channel); postOffice.dispatchConnectionLost(clientID,userName); LOG.trace("dispatch disconnection: clientId={}, userName={}", clientID, userName); }
void bindToSession(MQTTConnection mqttConnection, MqttConnectMessage msg, String clientId) { boolean isSessionAlreadyStored = false; PostConnectAction postConnectAction = PostConnectAction.NONE; if (!pool.containsKey(clientId)) { // case 1 final Session newSession = createNewSession(mqttConnection, msg, clientId); // publish the session final Session previous = pool.putIfAbsent(clientId, newSession); final boolean success = previous == null; if (success) { LOG.trace("case 1, not existing session with CId {}", clientId); } else { postConnectAction = bindToExistingSession(mqttConnection, msg, clientId, newSession); isSessionAlreadyStored = true; } } else { final Session newSession = createNewSession(mqttConnection, msg, clientId); postConnectAction = bindToExistingSession(mqttConnection, msg, clientId, newSession); isSessionAlreadyStored = true; } final boolean msgCleanSessionFlag = msg.variableHeader().isCleanSession(); boolean isSessionAlreadyPresent = !msgCleanSessionFlag && isSessionAlreadyStored; mqttConnection.sendConnAck(isSessionAlreadyPresent); if (postConnectAction == PostConnectAction.SEND_STORED_MESSAGES) { final Session session = pool.get(clientId); session.sendQueuedMessagesWhileOffline(); } }
private void copySessionConfig(MqttConnectMessage msg, Session session) { final boolean clean = msg.variableHeader().isCleanSession(); final Session.Will will; if (msg.variableHeader().isWillFlag()) { will = createWill(msg); } else { will = null; } session.update(clean, will); }
public void disconnect(String clientID) { final Session session = retrieve(clientID); if (session == null) { LOG.debug("Some other thread already removed the session CId={}", clientID); return; } session.disconnect(); }
subscriptions.init(subscriptionsRepository); final Authorizator authorizator = new Authorizator(authorizatorPolicy); SessionRegistry sessions = new SessionRegistry(subscriptions, queueRepository, authorizator); dispatcher = new PostOffice(subscriptions, retainedRepository, sessions, interceptor, authorizator); final BrokerConfiguration brokerConfig = new BrokerConfiguration(config);
if (newIsClean && oldSession.disconnected()) { dropQueuesForClient(clientId); unsubscribe(oldSession); throw new SessionCorruptedException("old session was already changed state"); copySessionConfig(msg, oldSession); oldSession.bind(mqttConnection); reactivateSubscriptions(oldSession, username);
@Test public void testConnAckContainsSessionPresentFlag() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID) .protocolVersion(MqttVersion.MQTT_3_1_1) .build(); NettyUtils.clientID(channel, FAKE_CLIENT_ID); NettyUtils.cleanSession(channel, false); // Connect a first time sut.bindToSession(connection, msg, FAKE_CLIENT_ID); // disconnect sut.disconnect(FAKE_CLIENT_ID); // Exercise, reconnect EmbeddedChannel anotherChannel = new EmbeddedChannel(); MQTTConnection anotherConnection = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZEROBYTE_CLIENT_ID, anotherChannel); sut.bindToSession(anotherConnection, msg, FAKE_CLIENT_ID); // Verify assertEqualsConnAck(CONNECTION_ACCEPTED, anotherChannel.readOutbound()); assertTrue("Connection is accepted and therefore should remain open", anotherChannel.isOpen()); }
void processDisconnect(MqttMessage msg) { final String clientID = NettyUtils.clientID(channel); LOG.trace("Start DISCONNECT CId={}, channel: {}", clientID, channel); if (!connected) { LOG.info("DISCONNECT received on already closed connection, CId={}, channel: {}", clientID, channel); return; } sessionRegistry.disconnect(clientID); connected = false; channel.close().addListener(FIRE_EXCEPTION_ON_FAILURE); LOG.trace("Processed DISCONNECT CId={}, channel: {}", clientID, channel); String userName = NettyUtils.userName(channel); postOffice.dispatchDisconnection(clientID,userName); LOG.trace("dispatch disconnection: clientId={}, userName={}", clientID, userName); }
sessionRegistry.bindToSession(this, msg, clientId);
public void writabilityChanged() { if (channel.isWritable()) { LOG.debug("Channel {} is again writable", channel); final Session session = sessionRegistry.retrieve(getClientId()); session.writabilityChanged(); } }
private void prepareSUT() { mockAuthenticator = new MockAuthenticator(singleton(FAKE_CLIENT_ID), singletonMap(TEST_USER, TEST_PWD)); subscriptions = new CTrieSubscriptionDirectory(); ISubscriptionsRepository subscriptionsRepository = new MemorySubscriptionsRepository(); subscriptions.init(subscriptionsRepository); queueRepository = new MemoryQueueRepository(); final PermitAllAuthorizatorPolicy authorizatorPolicy = new PermitAllAuthorizatorPolicy(); final Authorizator permitAll = new Authorizator(authorizatorPolicy); sessionRegistry = new SessionRegistry(subscriptions, queueRepository, permitAll); sut = new PostOffice(subscriptions, new MemoryRetainedRepository(), sessionRegistry, ConnectionTestUtils.NO_OBSERVERS_INTERCEPTOR, permitAll); }
public void resendNotAckedPublishes() { final Session session = sessionRegistry.retrieve(getClientId()); session.resendInflightNotAcked(); }
private void prepareSUT() { mockAuthenticator = new MockAuthenticator(singleton(FAKE_CLIENT_ID), singletonMap(TEST_USER, TEST_PWD)); subscriptions = new CTrieSubscriptionDirectory(); ISubscriptionsRepository subscriptionsRepository = new MemorySubscriptionsRepository(); subscriptions.init(subscriptionsRepository); queueRepository = new MemoryQueueRepository(); final PermitAllAuthorizatorPolicy authorizatorPolicy = new PermitAllAuthorizatorPolicy(); final Authorizator permitAll = new Authorizator(authorizatorPolicy); sessionRegistry = new SessionRegistry(subscriptions, queueRepository, permitAll); sut = new PostOffice(subscriptions, new MemoryRetainedRepository(), sessionRegistry, ConnectionTestUtils.NO_OBSERVERS_INTERCEPTOR, permitAll); }
private Session createNewSession(MQTTConnection mqttConnection, MqttConnectMessage msg, String clientId) { final boolean clean = msg.variableHeader().isCleanSession(); final Queue<SessionRegistry.EnqueuedMessage> sessionQueue = queues.computeIfAbsent(clientId, (String cli) -> queueRepository.createQueue(cli, clean)); final Session newSession; if (msg.variableHeader().isWillFlag()) { final Session.Will will = createWill(msg); newSession = new Session(clientId, clean, will, sessionQueue); } else { newSession = new Session(clean, clientId, sessionQueue); } newSession.markConnected(); newSession.bind(mqttConnection); return newSession; }
private void processPubRec(MqttMessage msg) { final int messageID = ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId(); final Session session = sessionRegistry.retrieve(getClientId()); session.processPubRec(messageID); }
private MQTTConnection createMQTTConnection(BrokerConfiguration config, Channel channel) { IAuthenticator mockAuthenticator = new MockAuthenticator(singleton(FAKE_CLIENT_ID), singletonMap(TEST_USER, TEST_PWD)); ISubscriptionsDirectory subscriptions = new CTrieSubscriptionDirectory(); ISubscriptionsRepository subscriptionsRepository = new MemorySubscriptionsRepository(); subscriptions.init(subscriptionsRepository); queueRepository = new MemoryQueueRepository(); final PermitAllAuthorizatorPolicy authorizatorPolicy = new PermitAllAuthorizatorPolicy(); final Authorizator permitAll = new Authorizator(authorizatorPolicy); sessionRegistry = new SessionRegistry(subscriptions, queueRepository, permitAll); final PostOffice postOffice = new PostOffice(subscriptions, new MemoryRetainedRepository(), sessionRegistry, ConnectionTestUtils.NO_OBSERVERS_INTERCEPTOR, permitAll); return new MQTTConnection(channel, config, mockAuthenticator, sessionRegistry, postOffice); }
private void processPubAck(MqttMessage msg) { final int messageID = ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId(); Session session = sessionRegistry.retrieve(getClientId()); session.pubAckReceived(messageID); }
private MQTTConnection createMQTTConnection(BrokerConfiguration config, Channel channel) { IAuthenticator mockAuthenticator = new MockAuthenticator(singleton(FAKE_CLIENT_ID), singletonMap(TEST_USER, TEST_PWD)); ISubscriptionsDirectory subscriptions = new CTrieSubscriptionDirectory(); ISubscriptionsRepository subscriptionsRepository = new MemorySubscriptionsRepository(); subscriptions.init(subscriptionsRepository); queueRepository = new MemoryQueueRepository(); final PermitAllAuthorizatorPolicy authorizatorPolicy = new PermitAllAuthorizatorPolicy(); final Authorizator permitAll = new Authorizator(authorizatorPolicy); sut = new SessionRegistry(subscriptions, queueRepository, permitAll); final PostOffice postOffice = new PostOffice(subscriptions, new MemoryRetainedRepository(), sut, ConnectionTestUtils.NO_OBSERVERS_INTERCEPTOR, permitAll); return new MQTTConnection(channel, config, mockAuthenticator, sut, postOffice); }
private void processPubComp(MqttMessage msg) { final int messageID = ((MqttMessageIdVariableHeader) msg.variableHeader()).messageId(); final Session session = sessionRegistry.retrieve(getClientId()); session.processPubComp(messageID); }
private SessionRegistry initPostOfficeAndSubsystems() { subscriptions = new CTrieSubscriptionDirectory(); ISubscriptionsRepository subscriptionsRepository = new MemorySubscriptionsRepository(); subscriptions.init(subscriptionsRepository); retainedRepository = new MemoryRetainedRepository(); queueRepository = new MemoryQueueRepository(); final PermitAllAuthorizatorPolicy authorizatorPolicy = new PermitAllAuthorizatorPolicy(); final Authorizator permitAll = new Authorizator(authorizatorPolicy); SessionRegistry sessionRegistry = new SessionRegistry(subscriptions, queueRepository, permitAll); sut = new PostOffice(subscriptions, retainedRepository, sessionRegistry, ConnectionTestUtils.NO_OBSERVERS_INTERCEPTOR, permitAll); return sessionRegistry; }