Permalink
Browse files

implemented reachability observer

  • Loading branch information...
1 parent 9b6da5d commit a327fc39ea67d2b1542bacad76f50749ee62301e @gharlan gharlan committed Jun 21, 2012
Showing with 403 additions and 1 deletion.
  1. +10 −0 Travis Toolbar.xcodeproj/project.pbxproj
  2. +42 −1 src/PusherConnection.m
  3. +86 −0 src/Reachability.h
  4. +265 −0 src/Reachability.m
@@ -22,6 +22,8 @@
8D11072B0486CEB800E47090 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 089C165CFE840E0CC02AAC07 /* InfoPlist.strings */; };
8D11072D0486CEB800E47090 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 29B97316FDCFA39411CA2CEA /* main.m */; settings = {ATTRIBUTES = (); }; };
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */; };
+ 9D48CED31593733B00955842 /* SystemConfiguration.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9DAF569015932D77002018B5 /* SystemConfiguration.framework */; };
+ 9DAF568F15932852002018B5 /* Reachability.m in Sources */ = {isa = PBXBuildFile; fileRef = 9DAF568E15932852002018B5 /* Reachability.m */; };
DD06BD7413170793007673AA /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD06BD7313170793007673AA /* CoreLocation.framework */; };
DD06BDC713171314007673AA /* Growl Registration Ticket.growlRegDict in Resources */ = {isa = PBXBuildFile; fileRef = DD06BDAA13170FF6007673AA /* Growl Registration Ticket.growlRegDict */; };
DD06BDE2131714CA007673AA /* travis_logo.png in Resources */ = {isa = PBXBuildFile; fileRef = DD06BDE1131714CA007673AA /* travis_logo.png */; };
@@ -90,6 +92,9 @@
2EE31A7E15644ABD004F33FE /* Travis Toolbar.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = "Travis Toolbar.entitlements"; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = resources/Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* Travis Toolbar.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Travis Toolbar.app"; sourceTree = BUILT_PRODUCTS_DIR; };
+ 9DAF568D15932852002018B5 /* Reachability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = Reachability.h; path = src/Reachability.h; sourceTree = "<group>"; };
+ 9DAF568E15932852002018B5 /* Reachability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = Reachability.m; path = src/Reachability.m; sourceTree = "<group>"; };
+ 9DAF569015932D77002018B5 /* SystemConfiguration.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SystemConfiguration.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/SystemConfiguration.framework; sourceTree = DEVELOPER_DIR; };
DD06BD7313170793007673AA /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = /System/Library/Frameworks/CoreLocation.framework; sourceTree = "<absolute>"; };
DD06BDAA13170FF6007673AA /* Growl Registration Ticket.growlRegDict */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; name = "Growl Registration Ticket.growlRegDict"; path = "resources/Growl Registration Ticket.growlRegDict"; sourceTree = "<group>"; };
DD06BDE1131714CA007673AA /* travis_logo.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = travis_logo.png; path = resources/travis_logo.png; sourceTree = "<group>"; };
@@ -101,6 +106,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
+ 9D48CED31593733B00955842 /* SystemConfiguration.framework in Frameworks */,
8D11072F0486CEB800E47090 /* Cocoa.framework in Frameworks */,
DD06BD7413170793007673AA /* CoreLocation.framework in Frameworks */,
2EE31A7115642547004F33FE /* Pusher.framework in Frameworks */,
@@ -113,6 +119,7 @@
1058C7A0FEA54F0111CA2CBB /* Linked Frameworks */ = {
isa = PBXGroup;
children = (
+ 9DAF569015932D77002018B5 /* SystemConfiguration.framework */,
2EE31A7015642547004F33FE /* Pusher.framework */,
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */,
DD06BD7313170793007673AA /* CoreLocation.framework */,
@@ -215,6 +222,8 @@
2EDA04741565C0A70043A3A6 /* PreferencesController.m */,
2EDA04761565C67C0043A3A6 /* Preferences.h */,
2EDA04771565C67C0043A3A6 /* Preferences.m */,
+ 9DAF568D15932852002018B5 /* Reachability.h */,
+ 9DAF568E15932852002018B5 /* Reachability.m */,
);
name = "Travis Toolbar";
sourceTree = "<group>";
@@ -307,6 +316,7 @@
2EDA045D156450750043A3A6 /* TravisEventData.m in Sources */,
2EDA04751565C0A70043A3A6 /* PreferencesController.m in Sources */,
2EDA04781565C67C0043A3A6 /* Preferences.m in Sources */,
+ 9DAF568F15932852002018B5 /* Reachability.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -12,13 +12,15 @@
#import "Constants.h"
#import "TravisEventData.h"
#import "Preferences.h"
+#import "Reachability.h"
#import "PusherConnection.h"
@interface PusherConnection ()
@property (strong) PTPusher *pusher;
@property (strong) PTPusherChannel *channel;
+@property (strong) Reachability *reachability;
- (void)handleStarted:(PTPusherEvent *)event;
- (void)handleFinished:(PTPusherEvent *)event;
@@ -29,12 +31,12 @@ @implementation PusherConnection
@synthesize pusher = _pusher;
@synthesize channel = _channel;
+@synthesize reachability = _reachability;
- (id)init {
self = [super init];
if (self) {
self.pusher = [PTPusher pusherWithKey:kPusherApiKey delegate:self encrypted:YES];
- self.pusher.reconnectAutomatically = YES;
self.channel = [self.pusher subscribeToChannelNamed:kPusherChannelName];
@@ -45,6 +47,45 @@ - (id)init {
return self;
}
+- (void)pusher:(PTPusher *)client connectionDidConnect:(PTPusherConnection *)connection {
+ self.pusher.reconnectAutomatically = YES;
+}
+
+- (void)pusher:(PTPusher *)pusher connection:(PTPusherConnection *)connection didDisconnectWithError:(NSError *)error {
+ [self pusher:pusher connection:connection failedWithError:error];
+}
+
+- (void)pusher:(PTPusher *)pusher connection:(PTPusherConnection *)connection failedWithError:(NSError *)error {
+ if (self.reachability == nil) {
+ self.reachability = [Reachability reachabilityForInternetConnection];
+ }
+
+ if ([self.reachability currentReachabilityStatus] == NotReachable) {
+ // there is no point in trying to reconnect at this point
+ self.pusher.reconnectAutomatically = NO;
+
+ // start observing the reachability status to see when we come back online
+ [[NSNotificationCenter defaultCenter]
+ addObserver:self
+ selector:@selector(reachabilityChanged:)
+ name:kReachabilityChangedNotification
+ object:self.reachability];
+
+ [self.reachability startNotifier];
+ }
+}
+
+- (void)reachabilityChanged:(NSNotification *)note {
+ if ([self.reachability currentReachabilityStatus] != NotReachable) {
+ // we seem to have some kind of network reachability, so try again
+ [self.pusher connect];
+
+ // we can stop observing reachability changes now
+ [[NSNotificationCenter defaultCenter] removeObserver:self];
+ [self.reachability stopNotifier];
+ }
+}
+
- (void)handleStarted:(PTPusherEvent *)event {
TravisEventData *eventData = [[TravisEventData alloc] initWithEventData:event.data];
NSArray *repos = [[Preferences alloc] objectForKey:@"repositories"];
View
@@ -0,0 +1,86 @@
+/*
+
+ File: Reachability.h
+ Abstract: Basic demonstration of how to use the SystemConfiguration Reachablity APIs.
+
+ Version: 2.2 (modified for OS X)
+
+ Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
+ ("Apple") in consideration of your agreement to the following terms, and your
+ use, installation, modification or redistribution of this Apple software
+ constitutes acceptance of these terms. If you do not agree with these terms,
+ please do not use, install, modify or redistribute this Apple software.
+
+ In consideration of your agreement to abide by the following terms, and subject
+ to these terms, Apple grants you a personal, non-exclusive license, under
+ Apple's copyrights in this original Apple software (the "Apple Software"), to
+ use, reproduce, modify and redistribute the Apple Software, with or without
+ modifications, in source and/or binary forms; provided that if you redistribute
+ the Apple Software in its entirety and without modifications, you must retain
+ this notice and the following text and disclaimers in all such redistributions
+ of the Apple Software.
+ Neither the name, trademarks, service marks or logos of Apple Inc. may be used
+ to endorse or promote products derived from the Apple Software without specific
+ prior written permission from Apple. Except as expressly stated in this notice,
+ no other rights or licenses, express or implied, are granted by Apple herein,
+ including but not limited to any patent rights that may be infringed by your
+ derivative works or by other works in which the Apple Software may be
+ incorporated.
+
+ The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
+ WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
+ WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
+ COMBINATION WITH YOUR PRODUCTS.
+
+ IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
+ DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
+ CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
+ APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ Copyright (C) 2010 Apple Inc. All Rights Reserved.
+
+ */
+
+
+#import <Foundation/Foundation.h>
+#import <SystemConfiguration/SystemConfiguration.h>
+#import <netinet/in.h>
+
+typedef enum {
+ NotReachable = 0,
+ Reachable
+} NetworkStatus;
+#define kReachabilityChangedNotification @"kNetworkReachabilityChangedNotification"
+
+@interface Reachability: NSObject
+{
+ BOOL localWiFiRef;
+ SCNetworkReachabilityRef reachabilityRef;
+}
+
+//reachabilityWithHostName- Use to check the reachability of a particular host name.
++ (Reachability*) reachabilityWithHostName: (NSString*) hostName;
+
+//reachabilityWithAddress- Use to check the reachability of a particular IP address.
++ (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;
+
+//reachabilityForInternetConnection- checks whether the default route is available.
+// Should be used by applications that do not connect to a particular host
++ (Reachability*) reachabilityForInternetConnection;
+
+//reachabilityForLocalWiFi- checks whether a local wifi connection is available.
++ (Reachability*) reachabilityForLocalWiFi;
+
+//Start listening for reachability notifications on the current run loop
+- (BOOL) startNotifier;
+- (void) stopNotifier;
+
+- (NetworkStatus) currentReachabilityStatus;
+//WWAN may be available, but not active until a connection has been established.
+//WiFi may require a connection for VPN on Demand.
+- (BOOL) connectionRequired;
+@end
Oops, something went wrong.

0 comments on commit a327fc3

Please sign in to comment.