Skip to content
Newer
Older
100644 183 lines (154 sloc) 4.58 KB
8d2b918 @AlanQuatermain Implemented new BSD license. To keep the old license, please fork the…
AlanQuatermain authored Mar 29, 2009
1 /*
2 * AQConnectionMultiplexer.m
3 * AQToolkit
4 *
5 * Created by Jim Dovey on 29/01/09.
6 *
7 * Copyright (c) 2009, Jim Dovey
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * Neither the name of this project's author nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
31 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 */
f393b3a @AlanQuatermain Added low-memory download helper.
AlanQuatermain authored Jan 29, 2009
38
39 #import "AQConnectionMultiplexer.h"
40 #import "AQLowMemoryDownloadHelper.h"
41
42 static AQConnectionMultiplexer * __connectionMultiplexer = nil;
43
44 @interface AQConnectionMultiplexer ()
45 - (void) _addHelper: (AQLowMemoryDownloadHelper *) helper;
46 @end
47
48 @implementation AQConnectionMultiplexer
49
50 + (void) attachDownloadHelper: (AQLowMemoryDownloadHelper *) helper
51 {
52 @synchronized(self)
53 {
54 if ( __connectionMultiplexer == nil )
55 {
56 __connectionMultiplexer = [[AQConnectionMultiplexer alloc] init];
57 [__connectionMultiplexer start];
58 }
59 }
85359d8 @AlanQuatermain Added removal routine to multiplexer class (was keeping download help…
AlanQuatermain authored Feb 15, 2009
60
61 [__connectionMultiplexer performSelector: @selector(_addHelper:)
62 onThread: __connectionMultiplexer
63 withObject: helper
64 waitUntilDone: YES];
65 }
66
67 + (void) removeDownloadHelper: (AQLowMemoryDownloadHelper *) helper
68 {
69 if ( __connectionMultiplexer == nil )
70 return;
71
72 [__connectionMultiplexer performSelector: @selector(_removeHelper:)
73 onThread: __connectionMultiplexer
74 withObject: helper
75 waitUntilDone: YES];
76 }
77
78 + (void) cancelPendingTransfers
79 {
80 NSThread * thread = nil;
81 @synchronized(self)
82 {
83 thread = [__connectionMultiplexer retain];
84 }
85
86 if ( thread == nil )
87 return;
88
89 [__connectionMultiplexer performSelector: @selector(_cancelTransfers)
90 onThread: thread
91 withObject: nil
92 waitUntilDone: YES];
93
94 [thread release];
f393b3a @AlanQuatermain Added low-memory download helper.
AlanQuatermain authored Jan 29, 2009
95 }
96
97 - (id) init
98 {
99 if ( [super init] == nil )
100 return ( nil );
101
102 _downloadHelpers = [[NSMutableSet alloc] init];
103 _runThread = YES;
104
105 [[NSNotificationCenter defaultCenter] addObserver: self
106 selector: @selector(terminate)
107 #ifdef IPHONEOS_DEPLOYMENT_TARGET
108 name: UIApplicationWillTerminateNotification
109 #else
110 name: NSApplicationWillTerminateNotification
111 #endif
112 object: nil];
113
114 return ( self );
115 }
116
117 - (void) dealloc
118 {
119 [[NSNotificationCenter defaultCenter] removeObserver: self];
120 [_downloadHelpers release];
121 [super dealloc];
122 }
123
124 - (void) terminate
125 {
126 __connectionMultiplexer = nil;
127 [self performSelector: @selector(_terminate)
128 onThread: self
129 withObject: nil
130 waitUntilDone: YES];
131 [self cancel];
132 [self autorelease];
133 }
134
135 - (void) _addHelper: (AQLowMemoryDownloadHelper *) helper
136 {
85359d8 @AlanQuatermain Added removal routine to multiplexer class (was keeping download help…
AlanQuatermain authored Feb 15, 2009
137 @synchronized(_downloadHelpers)
138 {
139 [_downloadHelpers addObject: helper];
140 }
141
f393b3a @AlanQuatermain Added low-memory download helper.
AlanQuatermain authored Jan 29, 2009
142 [helper start];
143 }
144
85359d8 @AlanQuatermain Added removal routine to multiplexer class (was keeping download help…
AlanQuatermain authored Feb 15, 2009
145 - (void) _removeHelper: (AQLowMemoryDownloadHelper *) helper
146 {
147 @synchronized(_downloadHelpers)
148 {
149 [_downloadHelpers removeObject: helper];
150 }
151 }
152
153 - (void) _cancelTransfers
154 {
155 @synchronized(_downloadHelpers)
156 {
157 [_downloadHelpers makeObjectsPerformSelector: @selector(cancel)];
158 [_downloadHelpers removeAllObjects];
159 }
160 }
161
ad27dbf @AlanQuatermain Fixes for bugs found by the Clang static analyzer.
AlanQuatermain authored Jul 1, 2009
162 - (void) _terminate
163 {
164 [self _cancelTransfers];
165 }
166
f393b3a @AlanQuatermain Added low-memory download helper.
AlanQuatermain authored Jan 29, 2009
167 - (void) main
168 {
169 NSAutoreleasePool * rootPool = [[NSAutoreleasePool alloc] init];
170
171 do
172 {
173 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
174 [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate distantFuture]];
175 [pool drain];
176
177 } while ( [self isCancelled] == NO );
178
179 [rootPool drain];
180 }
181
182 @end
Something went wrong with that request. Please try again.