diff --git a/MASFoundation/Classes/MASConstants.h b/MASFoundation/Classes/MASConstants.h index f6d668c3..003e0284 100644 --- a/MASFoundation/Classes/MASConstants.h +++ b/MASFoundation/Classes/MASConstants.h @@ -556,6 +556,13 @@ static NSString *const _Nonnull MASDeviceDidFailToDeregisterNotification = @"MAS static NSString *const _Nonnull MASDeviceDidDeregisterNotification = @"MASDeviceDidDeregisterNotification"; +/** + * The NSString constant for the device notification indicating that the MASDevice + * has reset locally. + */ +static NSString *const _Nonnull MASDeviceDidResetLocallyNotification = @"MASDeviceDidResetLocallyNotification"; + + ///-------------------------------------- /// @name User Notifications diff --git a/MASFoundation/Classes/MQTT/MASMQTTClient.m b/MASFoundation/Classes/MQTT/MASMQTTClient.m index 732fc50a..deec5139 100644 --- a/MASFoundation/Classes/MQTT/MASMQTTClient.m +++ b/MASFoundation/Classes/MQTT/MASMQTTClient.m @@ -80,31 +80,45 @@ @implementation MASMQTTClient + (instancetype)sharedClient { - static dispatch_once_t onceToken; - if (!_sharedClient) { - dispatch_once(&onceToken, ^{ - + @synchronized(self) + { if ([MASUser currentUser].isAuthenticated && [MASDevice currentDevice].isRegistered) { - + // // Init MQTT client for current gateway // _sharedClient = [[MASMQTTClient alloc] initWithClientId:[MASMQTTHelper mqttClientId] cleanSession:NO]; } else { - + //Return nil in case Authentication or Registration is not done yet. _sharedClient = nil; } - }); + } } return _sharedClient; } +- (void)clearConnection +{ + __block MASMQTTClient *blockSelf = self; + [self disconnectWithCompletionHandler:^(NSUInteger code) { + + if ([blockSelf isEqual:_sharedClient]) + { + @synchronized(blockSelf) + { + _sharedClient = nil; + } + } + }]; +} + + // Initialize is called just before the first object is allocated - (void)initialize { @@ -151,6 +165,19 @@ - (MASMQTTClient *)initWithClientId:(NSString *)clientId cleanSession:(BOOL)clea // } self.queue = dispatch_queue_create(cstrClientId, NULL); + + // + // Subscribe following information to reset the current MQTT session due to the change in SDK's authenticated session + // + // - user logout + // - device de-registration + // - device reset locally + // - gateway switch + // + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearConnection) name:MASUserDidLogoutNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearConnection) name:MASDeviceDidDeregisterNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearConnection) name:MASDeviceDidResetLocallyNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearConnection) name:MASWillSwitchGatewayServerNotification object:nil]; } _sharedClient = self; @@ -158,6 +185,7 @@ - (MASMQTTClient *)initWithClientId:(NSString *)clientId cleanSession:(BOOL)clea return self; } + #pragma mark - Setup methods -(void)setUsername:(NSString *)username Password:(NSString *)password @@ -590,6 +618,7 @@ - (void)disconnectWithCompletionHandler:(MQTTDisconnectionHandler)completionHand mosquitto_disconnect(mosq); } + - (void)reconnect:(NSNotification *)notification { [self reconnect]; diff --git a/MASFoundation/Classes/_private_/services/model/MASModelService.m b/MASFoundation/Classes/_private_/services/model/MASModelService.m index 92284303..c0225d2e 100644 --- a/MASFoundation/Classes/_private_/services/model/MASModelService.m +++ b/MASFoundation/Classes/_private_/services/model/MASModelService.m @@ -1480,7 +1480,7 @@ - (void)logOutDeviceAndClearLocalAccessToken:(BOOL)clearLocal completion:(MASCom // // Post the notification // - [[NSNotificationCenter defaultCenter] postNotificationName:MASDeviceDidFailToRegisterNotification object:blockSelf]; + [[NSNotificationCenter defaultCenter] postNotificationName:MASUserDidFailToLogoutNotification object:blockSelf]; return; } @@ -1496,6 +1496,11 @@ - (void)logOutDeviceAndClearLocalAccessToken:(BOOL)clearLocal completion:(MASCom [blockSelf clearCurrentUserForLogout]; } + // + // Post the notification + // + [[NSNotificationCenter defaultCenter] postNotificationName:MASUserDidLogoutNotification object:blockSelf]; + // // Set id_token and id_token_type to nil // diff --git a/MASFoundation/Classes/models/MASDevice.m b/MASFoundation/Classes/models/MASDevice.m index 645399ce..2f1e952e 100644 --- a/MASFoundation/Classes/models/MASDevice.m +++ b/MASFoundation/Classes/models/MASDevice.m @@ -119,6 +119,11 @@ - (void)resetLocally // re-establish URL session // [[MASNetworkingService sharedService] establishURLSession]; + + // + // Post the did reset locally notification + // + [[NSNotificationCenter defaultCenter] postNotificationName:MASDeviceDidResetLocallyNotification object:self]; }