Skip to content
This repository was archived by the owner on Jan 21, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions MASFoundation/Classes/MASConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 36 additions & 7 deletions MASFoundation/Classes/MQTT/MASMQTTClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -151,13 +165,27 @@ - (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;

return self;
}


#pragma mark - Setup methods

-(void)setUsername:(NSString *)username Password:(NSString *)password
Expand Down Expand Up @@ -590,6 +618,7 @@ - (void)disconnectWithCompletionHandler:(MQTTDisconnectionHandler)completionHand
mosquitto_disconnect(mosq);
}


- (void)reconnect:(NSNotification *)notification
{
[self reconnect];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
//
Expand Down
5 changes: 5 additions & 0 deletions MASFoundation/Classes/models/MASDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}


Expand Down