Permalink
Browse files

Completed tasks from Asana:

* CL-2091
* fixed one bug with subscription restore
* added delegate methods which will allow to inform about resubscription
  process
* implemented DNS killing logic
  • Loading branch information...
1 parent 73fae69 commit e6e517c4d5e4d4ef4d99c5b7c2375b22904cf869 @parfeon parfeon committed Feb 18, 2013
Showing with 430 additions and 129 deletions.
  1. +2 −0 objective-c/3.4/pubnub.xcodeproj/project.pbxproj
  2. +81 −49 objective-c/3.4/pubnub/Classes/Controller/PNAppDelegate.m
  3. +3 −3 objective-c/3.4/pubnub/Classes/Controller/PNMainViewController.m
  4. +5 −5 objective-c/3.4/pubnub/Classes/Model/PNDataManager.m
  5. +0 −2 objective-c/3.4/pubnub/Classes/View/PNChannelInformationView.m
  6. +3 −3 objective-c/3.4/pubnub/libs/PubNub/Core/PNObservationCenter.h
  7. +34 −13 objective-c/3.4/pubnub/libs/PubNub/Core/PNObservationCenter.m
  8. +93 −27 objective-c/3.4/pubnub/libs/PubNub/Core/PubNub.m
  9. +26 −0 objective-c/3.4/pubnub/libs/PubNub/Data/PNConfiguration+Protected.h
  10. +21 −0 objective-c/3.4/pubnub/libs/PubNub/Data/PNConfiguration.m
  11. +5 −0 objective-c/3.4/pubnub/libs/PubNub/Data/PNError.m
  12. +3 −0 objective-c/3.4/pubnub/libs/PubNub/Misc/PNConstants.h
  13. +7 −0 objective-c/3.4/pubnub/libs/PubNub/Misc/PNMacro.h
  14. +10 −0 objective-c/3.4/pubnub/libs/PubNub/Misc/PNNotifications.h
  15. +28 −5 objective-c/3.4/pubnub/libs/PubNub/Misc/PNStructures.h
  16. +12 −0 objective-c/3.4/pubnub/libs/PubNub/Misc/Protocols/PNDelegate.h
  17. +6 −0 objective-c/3.4/pubnub/libs/PubNub/Misc/Protocols/PNMessageChannelDelegate.h
  18. +38 −18 objective-c/3.4/pubnub/libs/PubNub/Network/PNMessagingChannel.m
  19. +7 −0 objective-c/3.4/pubnub/libs/PubNub/Network/PNReachability.h
  20. +16 −0 objective-c/3.4/pubnub/libs/PubNub/Network/PNReachability.m
  21. +30 −4 objective-c/3.4/pubnub/libs/PubNub/Network/Transport/PNConnection.m
@@ -329,6 +329,7 @@
DD71B9554CFFFD2A43343B36 /* PNChannelEvents.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PNChannelEvents.h; sourceTree = "<group>"; };
DD71B967C56CA2E8F66085ED /* PNTimeTokenResponseParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PNTimeTokenResponseParser.m; sourceTree = "<group>"; };
DD71B96F17DA5856CFBA96A0 /* PNMessageHistoryRequest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PNMessageHistoryRequest.h; sourceTree = "<group>"; };
+ DD71BA0E2595B8F111648D33 /* PNConfiguration+Protected.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "PNConfiguration+Protected.h"; sourceTree = "<group>"; };
DD71BA90D925864CA27764CD /* pubnub-large2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "pubnub-large2.png"; sourceTree = "<group>"; };
DD71BAA5E7F0263BCD399384 /* PNChannelEventsResponseParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PNChannelEventsResponseParser.h; sourceTree = "<group>"; };
DD71BACF5F9CB939AEA62A65 /* PNMessagesHistory+Protected.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "PNMessagesHistory+Protected.h"; sourceTree = "<group>"; };
@@ -785,6 +786,7 @@
DD71B286201A499AF1BF1146 /* PNMessagesHistory.m */,
DD71BFAB0FEB9AF00E5DE50C /* PNMessagesHistory.h */,
DD71BACF5F9CB939AEA62A65 /* PNMessagesHistory+Protected.h */,
+ DD71BA0E2595B8F111648D33 /* PNConfiguration+Protected.h */,
);
path = Data;
sourceTree = "<group>";
@@ -53,39 +53,51 @@ - (void)initializePubNubClient {
if (!connected && error) {
- UIAlertView *infoAlertView = [UIAlertView new];
- infoAlertView.title = [NSString stringWithFormat:@"%@(%@)",
- [error localizedDescription],
- NSStringFromClass([self class])];
- infoAlertView.message = [NSString stringWithFormat:@"Reason:\n%@\nSuggestion:\n%@",
- [error localizedFailureReason],
- [error localizedRecoverySuggestion]];
- [infoAlertView addButtonWithTitle:@"OK"];
- [infoAlertView show];
+ PNLog(PNLogGeneralLevel, self, @"#2 PubNub client was unable to connect because of error: %@",
+ [error localizedDescription],
+ [error localizedFailureReason]);
}
}];
// Subscribe application delegate on subscription updates
// (events when client subscribe on some channel)
- [[PNObservationCenter defaultCenter] addClientChannelSubscriptionObserver:self
- withCallbackBlock:^(NSArray *channels,
- BOOL subscribed,
- PNError *subscriptionError) {
-
- if (subscribed) {
-
- PNLog(PNLogGeneralLevel, self,
- @"{BLOCK-P} PubNub client subscribed on channels: %@",
- channels);
- }
- else {
-
- PNLog(PNLogGeneralLevel, self,
- @"{BLOCK-P} PubNub client subscription failed with error: %@",
- subscriptionError);
- }
- }];
+ [[PNObservationCenter defaultCenter] addClientChannelSubscriptionStateObserver:self
+ withCallbackBlock:^(PNSubscriptionProcessState state,
+ NSArray *channels,
+ PNError *subscriptionError) {
+
+ switch (state) {
+
+ case PNSubscriptionProcessNotSubscribedState:
+
+ PNLog(PNLogGeneralLevel, self,
+ @"{BLOCK-P} PubNub client subscription failed with error: %@",
+ subscriptionError);
+ break;
+
+ case PNSubscriptionProcessSubscribedState:
+
+ PNLog(PNLogGeneralLevel, self,
+ @"{BLOCK-P} PubNub client subscribed on channels: %@",
+ channels);
+ break;
+
+ case PNSubscriptionProcessWillRestoreState:
+
+ PNLog(PNLogGeneralLevel, self,
+ @"{BLOCK-P} PubNub client will restore subscribed on channels: %@",
+ channels);
+ break;
+
+ case PNSubscriptionProcessRestoredState:
+
+ PNLog(PNLogGeneralLevel, self,
+ @"{BLOCK-P} PubNub client restores subscribed on channels: %@",
+ channels);
+ break;
+ }
+ }];
// Subscribe on message arrival events with block
[[PNObservationCenter defaultCenter] addMessageReceiveObserver:self
@@ -130,43 +142,36 @@ - (void)pubnubClient:(PubNub *)client error:(PNError *)error {
}
- (void)pubnubClient:(PubNub *)client willConnectToOrigin:(NSString *)origin {
-
- PNLog(PNLogGeneralLevel, self, @"PubNub client is about to connect to PubNub origin at: %@", origin);
+
if (self.isDisconnectedOnNetworkError) {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Restoring connection"
- message:[NSString stringWithFormat:
- @"PubNub client will restore connection to %@ this will take some time",
- origin]
- delegate:nil
- cancelButtonTitle:nil
- otherButtonTitles:@"OK", nil];
- [alert show];
+ PNLog(PNLogGeneralLevel, self, @"PubNub client trying to restore connection to PubNub origin at: %@", origin);
+ }
+ else {
+
+ PNLog(PNLogGeneralLevel, self, @"PubNub client is about to connect to PubNub origin at: %@", origin);
}
}
- (void)pubnubClient:(PubNub *)client didConnectToOrigin:(NSString *)origin {
-
- PNLog(PNLogGeneralLevel, self, @"PubNub client successfully connected to PubNub origin at: %@", origin);
if (self.isDisconnectedOnNetworkError) {
- self.disconnectedOnNetworkError = NO;
+ PNLog(PNLogGeneralLevel, self, @"PubNub client restored connection to PubNub origin at: %@", origin);
+ }
+ else {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection restored"
- message:[NSString stringWithFormat:
- @"PubNub client restored connection to %@", origin]
- delegate:nil
- cancelButtonTitle:nil
- otherButtonTitles:@"OK", nil];
- [alert show];
+ PNLog(PNLogGeneralLevel, self, @"PubNub client successfully connected to PubNub origin at: %@", origin);
}
+
+
+ self.disconnectedOnNetworkError = NO;
}
- (void)pubnubClient:(PubNub *)client connectionDidFailWithError:(PNError *)error {
- PNLog(PNLogGeneralLevel, self, @"PubNub client was unable to connect because of error: %@", error);
+ PNLog(PNLogGeneralLevel, self, @"#1 PubNub client was unable to connect because of error: %@", error);
self.disconnectedOnNetworkError = error.code == kPNClientConnectionFailedOnInternetFailureError;
}
@@ -193,6 +198,16 @@ - (void)pubnubClient:(PubNub *)client didSubscribeOnChannels:(NSArray *)channels
PNLog(PNLogGeneralLevel, self, @"PubNub client successfully subscribed on channels: %@", channels);
}
+- (void)pubnubClient:(PubNub *)client willRestoreSubscriptionOnChannels:(NSArray *)channels {
+
+ PNLog(PNLogGeneralLevel, self, @"PubNub client resuming subscription on: %@", channels);
+}
+
+- (void)pubnubClient:(PubNub *)client didRestoreSubscriptionOnChannels:(NSArray *)channels {
+
+ PNLog(PNLogGeneralLevel, self, @"PubNub client successfully restored subscription on channels: %@", channels);
+}
+
- (void)pubnubClient:(PubNub *)client subscriptionDidFailWithError:(NSError *)error {
PNLog(PNLogGeneralLevel, self, @"PubNub client failed to subscribe because of error: %@", error);
@@ -277,7 +292,24 @@ - (void) pubnubClient:(PubNub *)client
- (NSNumber *)shouldResubscribeOnConnectionRestore {
- return @(NO);
+ NSNumber *shouldResubscribeOnConnectionRestore = @(NO);
+
+ if ([[PubNub subscribedChannels] count] > 0) {
+
+ NSString *lastTimeToken = [[[PubNub subscribedChannels] lastObject] updateTimeToken];
+
+ if ([shouldResubscribeOnConnectionRestore boolValue]) {
+
+ lastTimeToken = @"0";
+ }
+
+ PNLog(PNLogGeneralLevel, self, @"PubNub client should restore subscription? %@. Resuming at last time token: %@",
+ ![shouldResubscribeOnConnectionRestore boolValue]?@"YES":@"NO",
+ lastTimeToken);
+ }
+
+
+ return shouldResubscribeOnConnectionRestore;
}
#pragma mark -
@@ -543,13 +543,13 @@ - (void)hideNoChannelSelectedMessage {
- (void)creationView:(PNChannelCreationView*)view subscribeOnChannel:(PNChannel *)channel {
- [PubNub subscribeOnChannel:channel withCompletionHandlingBlock:^(NSArray *channels,
- BOOL subscribed,
+ [PubNub subscribeOnChannel:channel withCompletionHandlingBlock:^(PNSubscriptionProcessState state,
+ NSArray *channels,
PNError *subscriptionError) {
NSString *alertMessage = [NSString stringWithFormat:@"Subscribed on channel: %@\nTo be able to send messages, select channel from righthand list",
channel.name];
- if (!subscribed) {
+ if (state == PNSubscriptionProcessNotSubscribedState) {
alertMessage = [NSString stringWithFormat:@"Failed to subscribe on: %@", channel.name];
}
@@ -71,12 +71,12 @@ - (id)init {
self.subscribedChannelsList = [NSMutableArray array];
PNDataManager *weakSelf = self;
- [[PNObservationCenter defaultCenter] addClientChannelSubscriptionObserver:self
- withCallbackBlock:^(NSArray *channels,
- BOOL subscribed,
- PNError *subscriptionError) {
+ [[PNObservationCenter defaultCenter] addClientChannelSubscriptionStateObserver:self
+ withCallbackBlock:^(PNSubscriptionProcessState state,
+ NSArray *channels,
+ PNError *subscriptionError) {
- if (subscribed) {
+ if (state == PNSubscriptionProcessSubscribedState || state == PNSubscriptionProcessRestoredState) {
NSArray *unsortedList = [PubNub subscribedChannels];
NSSortDescriptor *nameSorting = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES];
@@ -8,9 +8,7 @@
#import "PNChannelInformationView.h"
-#import "PNMacro.h"
#import "PNDataManager.h"
-#import "PNChannelInformationDelegate.h"
#pragma mark Private interface methods
@@ -48,9 +48,9 @@
* Add/remove observer which would like to know when PubNub client
* subscribes/unsubscribe on/from channel
*/
-- (void)addClientChannelSubscriptionObserver:(id)observer
- withCallbackBlock:(PNClientChannelSubscriptionHandlerBlock)callbackBlock;
-- (void)removeClientChannelSubscriptionObserver:(id)observer;
+- (void)addClientChannelSubscriptionStateObserver:(id)observer
+ withCallbackBlock:(PNClientChannelSubscriptionHandlerBlock)callbackBlock;
+- (void)removeClientChannelSubscriptionStateObserver:(id)observer;
- (void)addClientChannelUnsubscriptionObserver:(id)observer
withCallbackBlock:(PNClientChannelUnsubscriptionHandlerBlock)callbackBlock;
- (void)removeClientChannelUnsubscriptionObserver:(id)observer;
@@ -173,6 +173,14 @@ - (id)init {
selector:@selector(handleClientSubscriptionProcess:)
name:kPNClientSubscriptionDidCompleteNotification
object:nil];
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(handleClientSubscriptionProcess:)
+ name:kPNClientSubscriptionWillRestoreNotification
+ object:nil];
+ [[NSNotificationCenter defaultCenter] addObserver:self
+ selector:@selector(handleClientSubscriptionProcess:)
+ name:kPNClientSubscriptionDidRestoreNotification
+ object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleClientSubscriptionProcess:)
name:kPNClientSubscriptionDidFailNotification
@@ -338,7 +346,7 @@ - (void)removeClientConnectionStateObserver:(id)observer oneTimeEvent:(BOOL)isOn
#pragma mark - Client channels action/event observation
-- (void)addClientChannelSubscriptionObserver:(id)observer
+- (void)addClientChannelSubscriptionStateObserver:(id)observer
withCallbackBlock:(PNClientChannelSubscriptionHandlerBlock)callbackBlock {
@@ -348,7 +356,7 @@ - (void)addClientChannelSubscriptionObserver:(id)observer
withBlock:callbackBlock];
}
-- (void)removeClientChannelSubscriptionObserver:(id)observer {
+- (void)removeClientChannelSubscriptionStateObserver:(id)observer {
[self removeObserver:observer forEvent:PNObservationEvents.clientSubscriptionOnChannels oneTimeEvent:NO];
}
@@ -618,22 +626,35 @@ - (void)handleClientConnectionStateChange:(NSNotification *)notification {
}
- (void)handleClientSubscriptionProcess:(NSNotification *)notification {
-
+
+
NSArray *channels = nil;
PNError *error = nil;
- BOOL subscribed = NO;
-
- if ([notification.name isEqualToString:kPNClientSubscriptionDidCompleteNotification]) {
+ PNSubscriptionProcessState state = PNSubscriptionProcessNotSubscribedState;
+
+ // Check whether arrived notification that subscription failed or not
+ if ([notification.name isEqualToString:kPNClientSubscriptionDidFailNotification]) {
- subscribed = YES;
-
- channels = (NSArray *)notification.userInfo;
- }
- else {
-
error = (PNError *)notification.userInfo;
channels = error.associatedObject;
}
+ else {
+
+ // Retrieve list of channels on which event is occurred
+ channels = (NSArray *)notification.userInfo;
+ state = PNSubscriptionProcessSubscribedState;
+
+ // Check whether arrived notification that subscription will be restored
+ if ([notification.name isEqualToString:kPNClientSubscriptionWillRestoreNotification]) {
+
+ state = PNSubscriptionProcessWillRestoreState;
+ }
+ // Check whether arrived notification that subscription restored
+ else if ([notification.name isEqualToString:kPNClientSubscriptionDidRestoreNotification]) {
+
+ state = PNSubscriptionProcessRestoredState;
+ }
+ }
// Retrieving list of observers (including one time and persistent observers)
@@ -646,7 +667,7 @@ - (void)handleClientSubscriptionProcess:(NSNotification *)notification {
PNClientChannelSubscriptionHandlerBlock block = [observerData valueForKey:PNObservationObserverData.observerCallbackBlock];
if (block) {
- block(channels, subscribed, error);
+ block(state, channels, error);
}
}];
Oops, something went wrong.

0 comments on commit e6e517c

Please sign in to comment.