Ben Copsey

Added Apple's reachability classes, and functions to turn throttling on and off …

…automatically, depending on the type of connection in use
@@ -462,6 +462,17 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; @@ -462,6 +462,17 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount;
462 + (unsigned long)maxBandwidthPerSecond; 462 + (unsigned long)maxBandwidthPerSecond;
463 + (void)setMaxBandwidthPerSecond:(unsigned long)bytes; 463 + (void)setMaxBandwidthPerSecond:(unsigned long)bytes;
464 464
  465 +#if TARGET_OS_IPHONE
  466 +// Set to YES to automatically turn on throttling when WWAN is connected, and automatically turn it off when it isn't
  467 ++ (void)setShouldThrottleBandwidthForWWAN:(BOOL)throttle;
  468 +
  469 +// Turns on throttling automatically when WWAN is connected using a custom limit, and turns it off automatically when it isn't
  470 ++ (void)throttleBandwidthForWWANUsingLimit:(unsigned long)limit;
  471 +
  472 +// Called when the status of the network changes
  473 ++ (void)reachabilityChanged:(NSNotification *)note;
  474 +#endif
  475 +
465 476
466 477
467 @property (retain) NSString *username; 478 @property (retain) NSString *username;
@@ -12,9 +12,10 @@ @@ -12,9 +12,10 @@
12 12
13 #import "ASIHTTPRequest.h" 13 #import "ASIHTTPRequest.h"
14 #import <zlib.h> 14 #import <zlib.h>
15 -#ifndef TARGET_OS_IPHONE 15 +#if TARGET_OS_IPHONE
  16 +#import "Reachability.h"
  17 +#else
16 #import <SystemConfiguration/SystemConfiguration.h> 18 #import <SystemConfiguration/SystemConfiguration.h>
17 -#import <Security/Security.h>  
18 #endif 19 #endif
19 20
20 // We use our own custom run loop mode as CoreAnimation seems to want to hijack our threads otherwise 21 // We use our own custom run loop mode as CoreAnimation seems to want to hijack our threads otherwise
@@ -2328,7 +2329,37 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800; @@ -2328,7 +2329,37 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
2328 [bandwidthThrottlingLock unlock]; 2329 [bandwidthThrottlingLock unlock];
2329 } 2330 }
2330 2331
  2332 +#if TARGET_OS_IPHONE
  2333 ++ (void)setShouldThrottleBandwidthForWWAN:(BOOL)throttle
  2334 +{
  2335 + if (throttle) {
  2336 + [ASIHTTPRequest throttleBandwidthForWWANUsingLimit:ASIWWANBandwidthThrottleAmount];
  2337 + } else {
  2338 + [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNetworkReachabilityChangedNotification" object:nil];
  2339 + }
  2340 +}
2331 2341
  2342 ++ (void)throttleBandwidthForWWANUsingLimit:(unsigned long)limit
  2343 +{
  2344 + [bandwidthThrottlingLock lock];
  2345 + maxBandwidthPerSecond = limit;
  2346 + [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
  2347 + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil];
  2348 + [bandwidthThrottlingLock unlock];
  2349 + [ASIHTTPRequest reachabilityChanged:nil];
  2350 +}
  2351 +
  2352 ++ (void)reachabilityChanged:(NSNotification *)note
  2353 +{
  2354 + [bandwidthThrottlingLock lock];
  2355 + if ([[Reachability sharedReachability] internetConnectionStatus] == ReachableViaCarrierDataNetwork) {
  2356 + [ASIHTTPRequest setMaxBandwidthPerSecond:maxBandwidthPerSecond];
  2357 + } else {
  2358 + [ASIHTTPRequest setMaxBandwidthPerSecond:0];
  2359 + }
  2360 + [bandwidthThrottlingLock unlock];
  2361 +}
  2362 +#endif
2332 2363
2333 @synthesize username; 2364 @synthesize username;
2334 @synthesize password; 2365 @synthesize password;
  1 +/*
  2 +
  3 +File: Reachability.h
  4 +Abstract: SystemConfiguration framework wrapper.
  5 +
  6 +Version: 1.5
  7 +
  8 +Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc.
  9 +("Apple") in consideration of your agreement to the following terms, and your
  10 +use, installation, modification or redistribution of this Apple software
  11 +constitutes acceptance of these terms. If you do not agree with these terms,
  12 +please do not use, install, modify or redistribute this Apple software.
  13 +
  14 +In consideration of your agreement to abide by the following terms, and subject
  15 +to these terms, Apple grants you a personal, non-exclusive license, under
  16 +Apple's copyrights in this original Apple software (the "Apple Software"), to
  17 +use, reproduce, modify and redistribute the Apple Software, with or without
  18 +modifications, in source and/or binary forms; provided that if you redistribute
  19 +the Apple Software in its entirety and without modifications, you must retain
  20 +this notice and the following text and disclaimers in all such redistributions
  21 +of the Apple Software.
  22 +Neither the name, trademarks, service marks or logos of Apple Inc. may be used
  23 +to endorse or promote products derived from the Apple Software without specific
  24 +prior written permission from Apple. Except as expressly stated in this notice,
  25 +no other rights or licenses, express or implied, are granted by Apple herein,
  26 +including but not limited to any patent rights that may be infringed by your
  27 +derivative works or by other works in which the Apple Software may be
  28 +incorporated.
  29 +
  30 +The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
  31 +WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  32 +WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  33 +PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  34 +COMBINATION WITH YOUR PRODUCTS.
  35 +
  36 +IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  37 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  38 +GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  39 +ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR
  40 +DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF
  41 +CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF
  42 +APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43 +
  44 +Copyright (C) 2008 Apple Inc. All Rights Reserved.
  45 +
  46 +*/
  47 +
  48 +#import <UIKit/UIKit.h>
  49 +#import <SystemConfiguration/SystemConfiguration.h>
  50 +
  51 +@class Reachability;
  52 +
  53 +@interface Reachability : NSObject {
  54 +
  55 +@private
  56 + BOOL _networkStatusNotificationsEnabled;
  57 +
  58 + NSString *_hostName;
  59 + NSString *_address;
  60 +
  61 + NSMutableDictionary *_reachabilityQueries;
  62 +}
  63 +
  64 +/*
  65 + An enumeration that defines the return values of the network state
  66 + of the device.
  67 + */
  68 +typedef enum {
  69 + NotReachable = 0,
  70 + ReachableViaCarrierDataNetwork,
  71 + ReachableViaWiFiNetwork
  72 +} NetworkStatus;
  73 +
  74 +
  75 +// Set to YES to register for changes in network status. Otherwise reachability queries
  76 +// will be handled synchronously.
  77 +@property BOOL networkStatusNotificationsEnabled;
  78 +// The remote host whose reachability will be queried.
  79 +// Either this or 'addressName' must be set.
  80 +@property (nonatomic, retain) NSString *hostName;
  81 +// The IP address of the remote host whose reachability will be queried.
  82 +// Either this or 'hostName' must be set.
  83 +@property (nonatomic, retain) NSString *address;
  84 +// A cache of ReachabilityQuery objects, which encapsulate a SCNetworkReachabilityRef, a host or address, and a run loop. The keys are host names or addresses.
  85 +@property (nonatomic, assign) NSMutableDictionary *reachabilityQueries;
  86 +
  87 +// This class is intended to be used as a singleton.
  88 ++ (Reachability *)sharedReachability;
  89 +
  90 +// Is self.hostName is not nil, determines its reachability.
  91 +// If self.hostName is nil and self.address is not nil, determines the reachability of self.address.
  92 +- (NetworkStatus)remoteHostStatus;
  93 +// Is the device able to communicate with Internet hosts? If so, through which network interface?
  94 +- (NetworkStatus)internetConnectionStatus;
  95 +// Is the device able to communicate with hosts on the local WiFi network? (Typically these are Bonjour hosts).
  96 +- (NetworkStatus)localWiFiConnectionStatus;
  97 +
  98 +/*
  99 + When reachability change notifications are posted, the callback method 'ReachabilityCallback' is called
  100 + and posts a notification that the client application can observe to learn about changes.
  101 + */
  102 +static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void *info);
  103 +
  104 +@end
  105 +
  106 +@interface ReachabilityQuery : NSObject
  107 +{
  108 +@private
  109 + SCNetworkReachabilityRef _reachabilityRef;
  110 + CFMutableArrayRef _runLoops;
  111 + NSString *_hostNameOrAddress;
  112 +}
  113 +// Keep around each network reachability query object so that we can
  114 +// register for updates from those objects.
  115 +@property (nonatomic) SCNetworkReachabilityRef reachabilityRef;
  116 +@property (nonatomic, retain) NSString *hostNameOrAddress;
  117 +@property (nonatomic) CFMutableArrayRef runLoops;
  118 +
  119 +- (void)scheduleOnRunLoop:(NSRunLoop *)inRunLoop;
  120 +
  121 +@end
  122 +
This diff is collapsed. Click to expand it.
@@ -21,4 +21,8 @@ @@ -21,4 +21,8 @@
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 -* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25 +
  26 +A different license may apply to other software included in this package,
  27 +including GHUnit and Apple's Reachability classes. Please consult their
  28 +respective headers for the terms of their individual licenses.
This diff was suppressed by a .gitattributes entry.