Skip to content

Commit

Permalink
add custom hub push and fetch, reconnect by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Leo Vandriel committed Oct 22, 2014
1 parent dc6c225 commit 76704ba
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 55 deletions.
3 changes: 3 additions & 0 deletions CHANGLOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ Change Log

### master (unreleased)

* Set NWHub defaults to autoConnect:YES
* Add a bunch of helpers to NWHub and NWPusher

### 0.5.2 (2014-10-19)

* Add convenient connect methods
Expand Down
11 changes: 6 additions & 5 deletions Classes/NWHub.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,19 @@
- (NSUInteger)pushPayload:(NSString *)payload token:(NSString *)token;
- (NSUInteger)pushPayload:(NSString *)payload tokens:(NSArray *)tokens;
- (NSUInteger)pushPayloads:(NSArray *)payloads token:(NSString *)token;
- (NSUInteger)pushNotifications:(NSArray *)notifications autoReconnect:(BOOL)reconnect;
- (NSUInteger)pushNotifications:(NSArray *)notifications;
- (NSUInteger)fetchFailed;

- (BOOL)pushPayload:(NSString *)payload token:(NSString *)token error:(NSError **)error;
- (BOOL)pushNotification:(NWNotification *)notification autoReconnect:(BOOL)reconnect error:(NSError **)error;
- (BOOL)pushNotifications:(NSArray *)notifications autoReconnect:(BOOL)reconnect error:(NSError **)error;

- (NSUInteger)flushFailed;
- (BOOL)fetchFailed:(BOOL *)failed autoReconnect:(BOOL)reconnect error:(NSError **)error;
- (BOOL)trimIdentifiers;

// deprecated

- (NWError)connectWithIdentity:(NWIdentityRef)identity __deprecated;
- (NWError)connectWithPKCS12Data:(NSData *)data password:(NSString *)password __deprecated;
- (NWError)reconnect __deprecated;
- (NSUInteger)pushNotifications:(NSArray *)notifications autoReconnect:(BOOL)reconnect __deprecated;
- (NSUInteger)flushFailed __deprecated;

@end
91 changes: 56 additions & 35 deletions Classes/NWHub.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ - (NSUInteger)pushPayload:(NSString *)payload token:(NSString *)token
{
NSUInteger identifier = _index++;
NWNotification *notification = [[NWNotification alloc] initWithPayload:payload token:token identifier:identifier expiration:nil priority:0];
return [self pushNotifications:@[notification] autoReconnect:NO];
return [self pushNotifications:@[notification]];
}

- (NSUInteger)pushPayload:(NSString *)payload tokens:(NSArray *)tokens
Expand All @@ -92,7 +92,7 @@ - (NSUInteger)pushPayload:(NSString *)payload tokens:(NSArray *)tokens
NWNotification *notification = [[NWNotification alloc] initWithPayload:payload token:token identifier:identifier expiration:nil priority:0];
[notifications addObject:notification];
}
return [self pushNotifications:notifications autoReconnect:NO];
return [self pushNotifications:notifications];
}

- (NSUInteger)pushPayloads:(NSArray *)payloads token:(NSString *)token
Expand All @@ -103,15 +103,15 @@ - (NSUInteger)pushPayloads:(NSArray *)payloads token:(NSString *)token
NWNotification *notification = [[NWNotification alloc] initWithPayload:payload token:token identifier:identifier expiration:nil priority:0];
[notifications addObject:notification];
}
return [self pushNotifications:notifications autoReconnect:NO];
return [self pushNotifications:notifications];
}

- (NSUInteger)pushNotifications:(NSArray *)notifications autoReconnect:(BOOL)reconnect
- (NSUInteger)pushNotifications:(NSArray *)notifications
{
NSUInteger fails = 0;
for (NWNotification *notification in notifications) {
if (!notification.identifier) notification.identifier = _index++;
BOOL success = [self pushNotification:notification autoReconnect:reconnect error:nil];
BOOL success = [self pushNotification:notification autoReconnect:YES error:nil];
if (!success) {
fails++;
}
Expand All @@ -121,13 +121,6 @@ - (NSUInteger)pushNotifications:(NSArray *)notifications autoReconnect:(BOOL)rec

#pragma mark - Pushing with NSError

- (BOOL)pushPayload:(NSString *)payload token:(NSString *)token error:(NSError *__autoreleasing *)error
{
NSUInteger identifier = _index++;
NWNotification *notification = [[NWNotification alloc] initWithPayload:payload token:token identifier:identifier expiration:nil priority:0];
return [self pushNotifications:@[notification] autoReconnect:NO error:error];
}

- (BOOL)pushNotification:(NWNotification *)notification autoReconnect:(BOOL)reconnect error:(NSError *__autoreleasing *)error
{
NSError *e = nil;
Expand Down Expand Up @@ -163,45 +156,62 @@ - (BOOL)pushNotifications:(NSArray *)notifications autoReconnect:(BOOL)reconnect

#pragma mark - Fetching failed

- (BOOL)fetchFailed
- (NSUInteger)fetchFailed
{
NSUInteger count = 0;
[self fetchFailed:&count max:1000 autoReconnect:YES error:nil];
return count;
}

- (BOOL)fetchFailed:(NSUInteger *)failed max:(NSUInteger)max autoReconnect:(BOOL)reconnect error:(NSError *__autoreleasing *)error
{
for (NSUInteger i = 0; i < max; i++) {
BOOL f = NO;
BOOL fetched = [self fetchFailed:&f autoReconnect:reconnect error:error];
if (!fetched) {
return fetched;
}
if (!f) {
if (failed) *failed = i;
break;
}
}
[self trimIdentifiers];
return YES;
}

- (BOOL)fetchFailed:(BOOL *)failed autoReconnect:(BOOL)reconnect error:(NSError *__autoreleasing *)error
{
NSUInteger identifier = 0;
NSError *apnError = nil;
BOOL fetch = [_pusher fetchFailedIdentifier:&identifier apnError:&apnError error:nil];
BOOL fetch = [_pusher fetchFailedIdentifier:&identifier apnError:&apnError error:error];
if (!fetch) {
return fetch;
}
if (!identifier && !apnError) {
return NO;
}
NWNotification *notification = _notificationForIdentifier[@(identifier)][0];
if ([_delegate respondsToSelector:@selector(notification:didFailWithResult:)]) {
[_delegate notification:notification didFailWithResult:apnError.code];
}
if ([_delegate respondsToSelector:@selector(notification:didFailWithError:)]) {
[_delegate notification:notification didFailWithError:apnError];
if (failed) *failed = !!apnError;
if (apnError) {
NWNotification *notification = _notificationForIdentifier[@(identifier)][0];
if ([_delegate respondsToSelector:@selector(notification:didFailWithResult:)]) {
[_delegate notification:notification didFailWithResult:apnError.code];
}
if ([_delegate respondsToSelector:@selector(notification:didFailWithError:)]) {
[_delegate notification:notification didFailWithError:apnError];
}
if (reconnect) {
[self reconnectWithError:error];
}
}
return YES;
}

- (NSUInteger)collectGarbage
- (BOOL)trimIdentifiers
{
NSDate *oldBefore = [NSDate dateWithTimeIntervalSinceNow:-_feedbackSpan];
NSArray *old = [[_notificationForIdentifier keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
return [oldBefore compare:obj[1]] == NSOrderedDescending;
}] allObjects];
[_notificationForIdentifier removeObjectsForKeys:old];
return old.count;
}

- (NSUInteger)flushFailed
{
NSUInteger count = 0;
for (BOOL failed = YES; failed; count++) {
failed = [self fetchFailed];
}
[self collectGarbage];
return count - 1;
return !!old.count;
}

#pragma mark - Deprecated
Expand All @@ -224,4 +234,15 @@ - (NWError)reconnect
return [self reconnectWithError:&error] ? kNWSuccess : error.code;
}

- (NSUInteger)flushFailed
{
return [self fetchFailed];
}

- (NSUInteger)pushNotifications:(NSArray *)notifications autoReconnect:(BOOL)reconnect
{
NSAssert(reconnect, @"autoReconnect == false is ignored");
return [self pushNotifications:notifications];
}

@end
6 changes: 3 additions & 3 deletions Classes/NWPushFeedback.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
- (BOOL)connectWithPKCS12Data:(NSData *)data password:(NSString *)password error:(NSError **)error;
- (void)disconnect;

+ (instancetype)connectWithIdentity:(NWIdentityRef)identity error:(NSError **)error;
+ (instancetype)connectWithPKCS12Data:(NSData *)data password:(NSString *)password error:(NSError **)error;

- (BOOL)readTokenData:(NSData **)token date:(NSDate **)date error:(NSError **)error;
- (BOOL)readToken:(NSString **)token date:(NSDate **)date error:(NSError **)error;
- (NSArray *)readTokenDatePairsWithMax:(NSUInteger)max error:(NSError **)error;

+ (instancetype)connectWithIdentity:(NWIdentityRef)identity error:(NSError **)error;
+ (instancetype)connectWithPKCS12Data:(NSData *)data password:(NSString *)password error:(NSError **)error;

// deprecated

- (NWError)connectWithIdentity:(NWIdentityRef)identity __deprecated;
Expand Down
6 changes: 3 additions & 3 deletions Classes/NWPusher.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
- (BOOL)reconnectWithError:(NSError **)error;
- (void)disconnect;

+ (instancetype)connectWithIdentity:(NWIdentityRef)identity error:(NSError **)error;
+ (instancetype)connectWithPKCS12Data:(NSData *)data password:(NSString *)password error:(NSError **)error;

- (BOOL)pushPayload:(NSString *)payload token:(NSString *)token identifier:(NSUInteger)identifier error:(NSError **)error;
- (BOOL)pushNotification:(NWNotification *)notification type:(NWNotificationType)type error:(NSError **)error;

- (BOOL)fetchFailedIdentifier:(NSUInteger *)identifier apnError:(NSError **)apnError error:(NSError **)error;
- (NSArray *)fetchFailedIdentifierErrorPairsWithMax:(NSUInteger)max error:(NSError **)error;

+ (instancetype)connectWithIdentity:(NWIdentityRef)identity error:(NSError **)error;
+ (instancetype)connectWithPKCS12Data:(NSData *)data password:(NSString *)password error:(NSError **)error;

// deprecated

- (NWError)connectWithIdentity:(NWIdentityRef)identity __deprecated;
Expand Down
26 changes: 18 additions & 8 deletions Mac/NWAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ - (IBAction)push:(NSButton *)sender
{
[self addTokenAndUpdateCombo];
[self push];
[self upPayloadTextIndex];
}

- (IBAction)reconnect:(NSButton *)sender
Expand Down Expand Up @@ -360,15 +361,24 @@ - (void)push
NWLogInfo(@"Pushing..");
dispatch_async(_serial, ^{
NWNotification *notification = [[NWNotification alloc] initWithPayload:payload token:token identifier:0 expiration:expiry priority:priority];
NSUInteger failed = [_hub pushNotifications:@[notification] autoReconnect:NO];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
dispatch_after(popTime, _serial, ^(void){
NSUInteger failed2 = failed + [_hub flushFailed];
if (!failed2) NWLogInfo(@"Payload has been pushed");
dispatch_async(dispatch_get_main_queue(), ^{
[self upPayloadTextIndex];
NSError *error = nil;
BOOL pushed = [_hub pushNotification:notification autoReconnect:YES error:&error];
if (pushed) {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
dispatch_after(popTime, _serial, ^(void){
NSError *error = nil;
BOOL failed = NO;
BOOL fetched = [_hub fetchFailed:&failed autoReconnect:YES error:&error];
if (fetched) {
if (!failed) NWLogInfo(@"Payload has been pushed");
} else {
NWLogWarn(@"Unable to fetch failed: %@", error.localizedDescription);
}
[_hub trimIdentifiers];
});
});
} else {
NWLogWarn(@"Unable to push: %@", error.localizedDescription);
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion Touch/NWAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ - (void)push
NSUInteger failed = [_hub pushPayload:payload token:token];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC));
dispatch_after(popTime, _serial, ^(void){
NSUInteger failed2 = failed + [_hub flushFailed];
NSUInteger failed2 = failed + [_hub fetchFailed];
if (!failed2) NWLogInfo(@"Payload has been pushed");
});
});
Expand Down

0 comments on commit 76704ba

Please sign in to comment.