NO-JIRA Clean up around MQTTConnectionManager

This commit is contained in:
Martyn Taylor 2017-02-14 10:10:25 +00:00 committed by Justin Bertram
parent 3bd5c9df6a
commit bd1f244dfc
1 changed files with 28 additions and 34 deletions

View File

@ -119,28 +119,28 @@ public class MQTTConnectionManager {
return (ServerSessionImpl) serverSession;
}
void disconnect() {
synchronized void disconnect() {
if (session == null) {
return;
}
try {
if (session.getSessionState() != null) {
String clientId = session.getSessionState().getClientId();
if (clientId != null)
CONNECTED_CLIENTS.remove(clientId);
if (session.getState().isWill()) {
session.getConnectionManager().sendWill();
try {
session.stop();
session.getConnection().destroy();
if (session.getState().isWill()) {
session.getConnectionManager().sendWill();
}
} catch (Exception e) {
log.error("Error disconnecting client: " + e.getMessage());
} finally {
if (session.getSessionState() != null) {
session.getSessionState().setAttached(false);
String clientId = session.getSessionState().getClientId();
if (clientId != null) {
CONNECTED_CLIENTS.remove(clientId);
}
}
session.stop();
session.getConnection().disconnect(false);
session.getConnection().destroy();
} catch (Exception e) {
/* FIXME Failure during disconnect would leave the session state in an unrecoverable state. We should handle
errors more gracefully.
*/
log.error("Error disconnecting client: " + e.getMessage());
}
}
@ -150,25 +150,19 @@ public class MQTTConnectionManager {
}
private MQTTSessionState getSessionState(String clientId) throws InterruptedException {
synchronized (MQTTSession.SESSIONS) {
/* [MQTT-3.1.2-6] If CleanSession is set to 1, the Client and Server MUST discard any previous Session and
* start a new one This Session lasts as long as the Network Connection. State data associated with this Session
* MUST NOT be reused in any subsequent Session */
/* [MQTT-3.1.2-6] If CleanSession is set to 1, the Client and Server MUST discard any previous Session and
* start a new one This Session lasts as long as the Network Connection. State data associated with this Session
* MUST NOT be reused in any subsequent Session */
/* [MQTT-3.1.2-4] Attach an existing session if one exists (if cleanSession flag is false) otherwise create
a new one. */
MQTTSessionState state = MQTTSession.SESSIONS.get(clientId);
if (state != null) {
// TODO Add a count down latch for handling wait during attached session state.
while (state.getAttached()) {
Thread.sleep(1000);
}
return state;
} else {
state = new MQTTSessionState(clientId);
MQTTSession.SESSIONS.put(clientId, state);
return state;
}
/* [MQTT-3.1.2-4] Attach an existing session if one exists (if cleanSession flag is false) otherwise create
a new one. */
MQTTSessionState state = MQTTSession.SESSIONS.get(clientId);
if (state != null) {
return state;
} else {
state = new MQTTSessionState(clientId);
MQTTSession.SESSIONS.put(clientId, state);
return state;
}
}