Ben Copsey

Fixed deadlock

Cleanup API
Add throttling control to iPhone upload sample
@@ -65,13 +65,19 @@ static unsigned long maxBandwidthPerSecond = 0; @@ -65,13 +65,19 @@ static unsigned long maxBandwidthPerSecond = 0;
65 // A default figure for throttling bandwidth on mobile devices 65 // A default figure for throttling bandwidth on mobile devices
66 unsigned long const ASIWWANBandwidthThrottleAmount = 14800; 66 unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
67 67
  68 +// YES when bandwidth throttling is active
  69 +// This flag does not denote whether throttling is turned on - rather whether it is currently in use
  70 +// It will be set to NO when throttling is turned on, but a WI-FI connection is active
  71 +BOOL shouldThrottleBandwidth = NO;
  72 +
68 // Private stuff 73 // Private stuff
69 @interface ASIHTTPRequest () 74 @interface ASIHTTPRequest ()
70 75
71 - (BOOL)askDelegateForCredentials; 76 - (BOOL)askDelegateForCredentials;
72 - (BOOL)askDelegateForProxyCredentials; 77 - (BOOL)askDelegateForProxyCredentials;
73 + (void)incrementBandwidthUsedInLastSecond:(unsigned long)bytes; 78 + (void)incrementBandwidthUsedInLastSecond:(unsigned long)bytes;
74 -+ (void)throttleBandwidth; 79 ++ (void)performBandwidthThrottling;
  80 ++ (BOOL)shouldThrottleBandwidth;
75 81
76 @property (assign) BOOL complete; 82 @property (assign) BOOL complete;
77 @property (retain) NSDictionary *responseHeaders; 83 @property (retain) NSDictionary *responseHeaders;
@@ -685,7 +691,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800; @@ -685,7 +691,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
685 if (totalBytesSent > lastBytesSent) { 691 if (totalBytesSent > lastBytesSent) {
686 692
687 // For bandwidth throttling 693 // For bandwidth throttling
688 - if ([ASIHTTPRequest maxBandwidthPerSecond] > 0) { 694 + if ([ASIHTTPRequest shouldThrottleBandwidth] > 0) {
689 [ASIHTTPRequest incrementBandwidthUsedInLastSecond:totalBytesSent-maxBandwidthPerSecond]; 695 [ASIHTTPRequest incrementBandwidthUsedInLastSecond:totalBytesSent-maxBandwidthPerSecond];
690 } 696 }
691 [self setLastActivityTime:[NSDate date]]; 697 [self setLastActivityTime:[NSDate date]];
@@ -700,7 +706,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800; @@ -700,7 +706,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
700 [self updateProgressIndicators]; 706 [self updateProgressIndicators];
701 707
702 // Throttle bandwidth if nescessary 708 // Throttle bandwidth if nescessary
703 - [ASIHTTPRequest throttleBandwidth]; 709 + [ASIHTTPRequest performBandwidthThrottling];
704 710
705 // This thread should wait for 1/4 second for the stream to do something. We'll stop early if it does. 711 // This thread should wait for 1/4 second for the stream to do something. We'll stop early if it does.
706 CFRunLoopRunInMode(ASIHTTPRequestRunMode,0.25,YES); 712 CFRunLoopRunInMode(ASIHTTPRequestRunMode,0.25,YES);
@@ -1655,7 +1661,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800; @@ -1655,7 +1661,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
1655 [self setLastActivityTime:[NSDate date]]; 1661 [self setLastActivityTime:[NSDate date]];
1656 1662
1657 // For bandwidth throttling 1663 // For bandwidth throttling
1658 - if ([ASIHTTPRequest maxBandwidthPerSecond] > 0) { 1664 + if ([ASIHTTPRequest shouldThrottleBandwidth] > 0) {
1659 [ASIHTTPRequest incrementBandwidthUsedInLastSecond:bytesRead]; 1665 [ASIHTTPRequest incrementBandwidthUsedInLastSecond:bytesRead];
1660 } 1666 }
1661 1667
@@ -2282,6 +2288,14 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800; @@ -2282,6 +2288,14 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
2282 2288
2283 #pragma mark bandwidth throttling 2289 #pragma mark bandwidth throttling
2284 2290
  2291 ++ (BOOL)shouldThrottleBandwidth
  2292 +{
  2293 + [bandwidthThrottlingLock lock];
  2294 + BOOL throttle = shouldThrottleBandwidth;
  2295 + [bandwidthThrottlingLock unlock];
  2296 + return throttle;
  2297 +}
  2298 +
2285 + (unsigned long)maxBandwidthPerSecond 2299 + (unsigned long)maxBandwidthPerSecond
2286 { 2300 {
2287 [bandwidthThrottlingLock lock]; 2301 [bandwidthThrottlingLock lock];
@@ -2304,7 +2318,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800; @@ -2304,7 +2318,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
2304 [bandwidthThrottlingLock unlock]; 2318 [bandwidthThrottlingLock unlock];
2305 } 2319 }
2306 2320
2307 -+ (void)throttleBandwidth 2321 ++ (void)performBandwidthThrottling
2308 { 2322 {
2309 // Other requests may have to wait for this lock if we're sleeping, but this is fine, since we already know they shouldn't be sending or receiving data 2323 // Other requests may have to wait for this lock if we're sleeping, but this is fine, since we already know they shouldn't be sending or receiving data
2310 [bandwidthThrottlingLock lock]; 2324 [bandwidthThrottlingLock lock];
@@ -2336,6 +2350,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800; @@ -2336,6 +2350,7 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
2336 [ASIHTTPRequest throttleBandwidthForWWANUsingLimit:ASIWWANBandwidthThrottleAmount]; 2350 [ASIHTTPRequest throttleBandwidthForWWANUsingLimit:ASIWWANBandwidthThrottleAmount];
2337 } else { 2351 } else {
2338 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNetworkReachabilityChangedNotification" object:nil]; 2352 [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNetworkReachabilityChangedNotification" object:nil];
  2353 + [ASIHTTPRequest setMaxBandwidthPerSecond:0];
2339 } 2354 }
2340 } 2355 }
2341 2356
@@ -2353,9 +2368,9 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800; @@ -2353,9 +2368,9 @@ unsigned long const ASIWWANBandwidthThrottleAmount = 14800;
2353 { 2368 {
2354 [bandwidthThrottlingLock lock]; 2369 [bandwidthThrottlingLock lock];
2355 if ([[Reachability sharedReachability] internetConnectionStatus] == ReachableViaCarrierDataNetwork) { 2370 if ([[Reachability sharedReachability] internetConnectionStatus] == ReachableViaCarrierDataNetwork) {
2356 - [ASIHTTPRequest setMaxBandwidthPerSecond:maxBandwidthPerSecond]; 2371 + shouldThrottleBandwidth = YES;
2357 } else { 2372 } else {
2358 - [ASIHTTPRequest setMaxBandwidthPerSecond:0]; 2373 + shouldThrottleBandwidth = NO;
2359 } 2374 }
2360 [bandwidthThrottlingLock unlock]; 2375 [bandwidthThrottlingLock unlock];
2361 } 2376 }
@@ -16,5 +16,5 @@ @@ -16,5 +16,5 @@
16 } 16 }
17 17
18 - (IBAction)performLargeUpload:(id)sender; 18 - (IBAction)performLargeUpload:(id)sender;
19 - 19 +- (IBAction)toggleThrottling:(id)sender;
20 @end 20 @end
@@ -46,6 +46,11 @@ @@ -46,6 +46,11 @@
46 [networkQueue go]; 46 [networkQueue go];
47 } 47 }
48 48
  49 +- (IBAction)toggleThrottling:(id)sender
  50 +{
  51 + [ASIHTTPRequest setShouldThrottleBandwidthForWWAN:[(UISwitch *)sender isOn]];
  52 +}
  53 +
49 - (void)dealloc { 54 - (void)dealloc {
50 [networkQueue release]; 55 [networkQueue release];
51 [super dealloc]; 56 [super dealloc];
@@ -48,12 +48,12 @@ @@ -48,12 +48,12 @@
48 c3MuCgpZb3UnbGwgb25seSBzZWUgYWNjdXJhdGUgcHJvZ3Jlc3MgZm9yIHVwbG9hZHMgd2hlbiB0aGUg 48 c3MuCgpZb3UnbGwgb25seSBzZWUgYWNjdXJhdGUgcHJvZ3Jlc3MgZm9yIHVwbG9hZHMgd2hlbiB0aGUg
49 cmVxdWVzdCBib2R5IGlzIGxhcmdlciB0aGFuIDEyOEtCIChpbiAyLjIuMSBTREspLCBvciB3aGVuIHRo 49 cmVxdWVzdCBib2R5IGlzIGxhcmdlciB0aGFuIDEyOEtCIChpbiAyLjIuMSBTREspLCBvciB3aGVuIHRo
50 ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string> 50 ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string>
51 - <object class="NSFont" key="IBUIFont"> 51 + <object class="NSFont" key="IBUIFont" id="111580411">
52 <string key="NSName">Helvetica</string> 52 <string key="NSName">Helvetica</string>
53 <double key="NSSize">1.400000e+01</double> 53 <double key="NSSize">1.400000e+01</double>
54 <int key="NSfFlags">16</int> 54 <int key="NSfFlags">16</int>
55 </object> 55 </object>
56 - <object class="NSColor" key="IBUITextColor"> 56 + <object class="NSColor" key="IBUITextColor" id="129865504">
57 <int key="NSColorSpace">1</int> 57 <int key="NSColorSpace">1</int>
58 <bytes key="NSRGB">MCAwIDAAA</bytes> 58 <bytes key="NSRGB">MCAwIDAAA</bytes>
59 </object> 59 </object>
@@ -104,6 +104,32 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string> @@ -104,6 +104,32 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string>
104 <bool key="IBUIClipsSubviews">YES</bool> 104 <bool key="IBUIClipsSubviews">YES</bool>
105 <bool key="IBUIMultipleTouchEnabled">YES</bool> 105 <bool key="IBUIMultipleTouchEnabled">YES</bool>
106 </object> 106 </object>
  107 + <object class="IBUISwitch" id="352131301">
  108 + <reference key="NSNextResponder" ref="191373211"/>
  109 + <int key="NSvFlags">292</int>
  110 + <string key="NSFrame">{{206, 266}, {94, 27}}</string>
  111 + <reference key="NSSuperview" ref="191373211"/>
  112 + <bool key="IBUIOpaque">NO</bool>
  113 + <bool key="IBUIClipsSubviews">YES</bool>
  114 + <bool key="IBUIMultipleTouchEnabled">YES</bool>
  115 + <int key="IBUIContentHorizontalAlignment">0</int>
  116 + <int key="IBUIContentVerticalAlignment">0</int>
  117 + </object>
  118 + <object class="IBUILabel" id="636544176">
  119 + <reference key="NSNextResponder" ref="191373211"/>
  120 + <int key="NSvFlags">292</int>
  121 + <string key="NSFrame">{{20, 269}, {207, 21}}</string>
  122 + <reference key="NSSuperview" ref="191373211"/>
  123 + <bool key="IBUIOpaque">NO</bool>
  124 + <bool key="IBUIClipsSubviews">YES</bool>
  125 + <bool key="IBUIUserInteractionEnabled">NO</bool>
  126 + <string key="IBUIText">Throttle WWAN bandwidth:</string>
  127 + <reference key="IBUIFont" ref="111580411"/>
  128 + <reference key="IBUITextColor" ref="129865504"/>
  129 + <nil key="IBUIHighlightedColor"/>
  130 + <int key="IBUIBaselineAdjustment">1</int>
  131 + <float key="IBUIMinimumFontSize">1.000000e+01</float>
  132 + </object>
107 </object> 133 </object>
108 <string key="NSFrameSize">{320, 480}</string> 134 <string key="NSFrameSize">{320, 480}</string>
109 <reference key="NSSuperview"/> 135 <reference key="NSSuperview"/>
@@ -144,6 +170,15 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string> @@ -144,6 +170,15 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string>
144 </object> 170 </object>
145 <int key="connectionID">11</int> 171 <int key="connectionID">11</int>
146 </object> 172 </object>
  173 + <object class="IBConnectionRecord">
  174 + <object class="IBCocoaTouchEventConnection" key="connection">
  175 + <string key="label">toggleThrottling:</string>
  176 + <reference key="source" ref="352131301"/>
  177 + <reference key="destination" ref="372490531"/>
  178 + <int key="IBEventType">13</int>
  179 + </object>
  180 + <int key="connectionID">15</int>
  181 + </object>
147 </object> 182 </object>
148 <object class="IBMutableOrderedSet" key="objectRecords"> 183 <object class="IBMutableOrderedSet" key="objectRecords">
149 <object class="NSArray" key="orderedObjects"> 184 <object class="NSArray" key="orderedObjects">
@@ -164,6 +199,8 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string> @@ -164,6 +199,8 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string>
164 <reference ref="117120328"/> 199 <reference ref="117120328"/>
165 <reference ref="975702463"/> 200 <reference ref="975702463"/>
166 <reference ref="652745716"/> 201 <reference ref="652745716"/>
  202 + <reference ref="636544176"/>
  203 + <reference ref="352131301"/>
167 </object> 204 </object>
168 <reference key="parent" ref="360949347"/> 205 <reference key="parent" ref="360949347"/>
169 </object> 206 </object>
@@ -193,6 +230,16 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string> @@ -193,6 +230,16 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string>
193 <reference key="object" ref="975702463"/> 230 <reference key="object" ref="975702463"/>
194 <reference key="parent" ref="191373211"/> 231 <reference key="parent" ref="191373211"/>
195 </object> 232 </object>
  233 + <object class="IBObjectRecord">
  234 + <int key="objectID">12</int>
  235 + <reference key="object" ref="352131301"/>
  236 + <reference key="parent" ref="191373211"/>
  237 + </object>
  238 + <object class="IBObjectRecord">
  239 + <int key="objectID">13</int>
  240 + <reference key="object" ref="636544176"/>
  241 + <reference key="parent" ref="191373211"/>
  242 + </object>
196 </object> 243 </object>
197 </object> 244 </object>
198 <object class="NSMutableDictionary" key="flattenedProperties"> 245 <object class="NSMutableDictionary" key="flattenedProperties">
@@ -204,6 +251,8 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string> @@ -204,6 +251,8 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string>
204 <string>1.IBEditorWindowLastContentRect</string> 251 <string>1.IBEditorWindowLastContentRect</string>
205 <string>1.IBPluginDependency</string> 252 <string>1.IBPluginDependency</string>
206 <string>10.IBPluginDependency</string> 253 <string>10.IBPluginDependency</string>
  254 + <string>12.IBPluginDependency</string>
  255 + <string>13.IBPluginDependency</string>
207 <string>4.IBPluginDependency</string> 256 <string>4.IBPluginDependency</string>
208 <string>6.IBPluginDependency</string> 257 <string>6.IBPluginDependency</string>
209 </object> 258 </object>
@@ -211,7 +260,9 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string> @@ -211,7 +260,9 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string>
211 <bool key="EncodedWithXMLCoder">YES</bool> 260 <bool key="EncodedWithXMLCoder">YES</bool>
212 <string>UploadViewController</string> 261 <string>UploadViewController</string>
213 <string>UIResponder</string> 262 <string>UIResponder</string>
214 - <string>{{161, 171}, {320, 480}}</string> 263 + <string>{{68, 167}, {320, 480}}</string>
  264 + <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
  265 + <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
215 <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string> 266 <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
216 <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string> 267 <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
217 <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string> 268 <string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
@@ -238,7 +289,7 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string> @@ -238,7 +289,7 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string>
238 </object> 289 </object>
239 </object> 290 </object>
240 <nil key="sourceID"/> 291 <nil key="sourceID"/>
241 - <int key="maxID">11</int> 292 + <int key="maxID">15</int>
242 </object> 293 </object>
243 <object class="IBClassDescriber" key="IBDocument.Classes"> 294 <object class="IBClassDescriber" key="IBDocument.Classes">
244 <object class="NSMutableArray" key="referencedPartialClassDescriptions"> 295 <object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -254,8 +305,17 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string> @@ -254,8 +305,17 @@ ZSByZXF1ZXN0IGJvZHkgaXMgbGFyZ2VyIHRoYW4gMzJLQiAoaW4gMy4wIFNESyk</string>
254 <string key="className">UploadViewController</string> 305 <string key="className">UploadViewController</string>
255 <string key="superclassName">UIViewController</string> 306 <string key="superclassName">UIViewController</string>
256 <object class="NSMutableDictionary" key="actions"> 307 <object class="NSMutableDictionary" key="actions">
257 - <string key="NS.key.0">performLargeUpload:</string> 308 + <bool key="EncodedWithXMLCoder">YES</bool>
258 - <string key="NS.object.0">id</string> 309 + <object class="NSMutableArray" key="dict.sortedKeys">
  310 + <bool key="EncodedWithXMLCoder">YES</bool>
  311 + <string>performLargeUpload:</string>
  312 + <string>toggleThrottling:</string>
  313 + </object>
  314 + <object class="NSMutableArray" key="dict.values">
  315 + <bool key="EncodedWithXMLCoder">YES</bool>
  316 + <string>id</string>
  317 + <string>id</string>
  318 + </object>
259 </object> 319 </object>
260 <object class="NSMutableDictionary" key="outlets"> 320 <object class="NSMutableDictionary" key="outlets">
261 <string key="NS.key.0">progressIndicator</string> 321 <string key="NS.key.0">progressIndicator</string>
This diff was suppressed by a .gitattributes entry.