This repository has been archived by the owner on Apr 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathREDownloadTasksQueue.m
528 lines (452 loc) · 17.4 KB
/
REDownloadTasksQueue.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
* Copyright (c) 2014 - 2017 Kulykov Oleh <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import "REDownloadTasksQueue.h"
#import "REDownloadTasksQueuePrivate.h"
#import "REDownloadTasksQueueSerializer.h"
#import "REDownloadTasksQueueTaskInfo.h"
#import <pthread.h>
#import <NSMutableNumber/NSMutableNumber.h>
#import <Inlineobjc/NSString+Inlineobjc.h>
#import <Inlineobjc/NSArray+Inlineobjc.h>
const NSTimeInterval kREDownloadTasksQueueDefaultRequestTimeout = 40;
const NSURLRequestCachePolicy kREDownloadTasksQueueDefaultRequestCachePolicy = NSURLRequestReloadIgnoringCacheData;
NSString * const _Nonnull kREDownloadTasksQueueProgressChangedNotification = @"kREDownloadTasksQueueProgressChangedNotification";
NSString * const _Nonnull kREDownloadTasksQueueDidDownloadURLProgressChangedNotification = @"kREDownloadTasksQueueDidDownloadURLProgressChangedNotification";
NSString * const _Nonnull kREDownloadTasksQueueErrorNotification = @"kREDownloadTasksQueueErrorNotification";
NSString * const _Nonnull kREDownloadTasksQueueDidFinishedNotification = @"kREDownloadTasksQueueDidFinishedNotification";
NSString * const _Nonnull kREDownloadTasksQueueProgressKey = @"progress";
NSString * const _Nonnull kREDownloadTasksQueueUserObjectKey = @"userObject";
NSString * const _Nonnull kREDownloadTasksQueueQueueKey = @"queue";
NSString * const _Nonnull kREDownloadTasksQueueErrorKey = @"error";
NSString * const _Nonnull kREDownloadTasksQueueStoreURLKey = @"storeURL";
NSString * const _Nonnull kREDownloadTasksQueueDownloadURLKey = @"downloadURL";
@implementation REDownloadTasksQueue
static bool ___initRecursiveMutex(pthread_mutex_t * mutex) {
pthread_mutexattr_t attr;
if (pthread_mutexattr_init(&attr) == 0) {
bool isInit = false;
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) == 0) {
isInit = (pthread_mutex_init(mutex, &attr) == 0);
}
pthread_mutexattr_destroy(&attr);
return isInit;
}
return false;
}
- (void) lock {
pthread_mutex_lock(&_mutex);
}
- (void) unlock {
pthread_mutex_unlock(&_mutex);
}
- (BOOL) isCanceled {
BOOL value = NO;
pthread_mutex_lock(&_mutex);
value = _isCanceled;
pthread_mutex_unlock(&_mutex);
return value;
}
- (NSUInteger) tasksCount {
NSUInteger value = 0;
pthread_mutex_lock(&_mutex);
value = _infos ? [_infos count] : 0;
pthread_mutex_unlock(&_mutex);
return value;
}
- (BOOL) continueOnTaskError {
BOOL value = NO;
pthread_mutex_lock(&_mutex);
value = _isContinueOnTaskError;
pthread_mutex_unlock(&_mutex);
return value;
}
- (void) setContinueOnTaskError:(BOOL) value {
pthread_mutex_lock(&_mutex);
_isContinueOnTaskError = value;
pthread_mutex_unlock(&_mutex);
}
- (BOOL) startNextTask {
if (_isCanceled) return NO;
BOOL isStarted = NO;
pthread_mutex_lock(&_mutex);
NSArray * infosArray = self.infos;
if (NSArrayIsNotEmpty(infosArray) && (_active < _numberResumed))
{
const NSUInteger needStart = _numberResumed - _active;
NSUInteger started = 0;
for (REDownloadTasksQueueTaskInfo * info in infosArray)
{
if (!info.isStarted)
{
NSURLSessionDownloadTask * t = [info task];
info.isStarted = YES;
[t resume];
if (++started >= needStart) break;
}
}
isStarted = (started > 0);
}
pthread_mutex_unlock(&_mutex);
return isStarted;
}
- (void) reportDidDownloadURLProgressWithInfo:(REDownloadTasksQueueTaskInfo *) info {
if (!info || _reportType == REDownloadTasksQueueReportNone) return;
const float progress = [self downloadProgress];
NSURL * toURL = [info storeURL];
NSURL * fromURL = [info originalURL];
NSMutableDictionary * userInfo = nil;
if (_reportType & REDownloadTasksQueueReportViaNotifications) {
userInfo = [NSMutableDictionary dictionaryWithCapacity:5];
[userInfo setObject:self forKey:kREDownloadTasksQueueQueueKey];
[userInfo setObject:_userObject ? _userObject : [NSNull null]
forKey:kREDownloadTasksQueueUserObjectKey];
if (fromURL) [userInfo setObject:fromURL forKey:kREDownloadTasksQueueDownloadURLKey];
if (toURL) [userInfo setObject:toURL forKey:kREDownloadTasksQueueStoreURLKey];
[userInfo setObject:[NSNumber numberWithFloat:progress] forKey:kREDownloadTasksQueueProgressKey];
}
id<REDownloadTasksQueueDelegate> d = self.delegate;
if (d && ![d respondsToSelector:@selector(onREDownloadTasksQueue:didDownloadURL:andStoredToURL:withProgress:)]) d = nil;
dispatch_async(dispatch_get_main_queue(), ^{
if (_onDidDownloadURLProgressHandler && (_reportType & REDownloadTasksQueueReportViaBlocks)) _onDidDownloadURLProgressHandler(self, fromURL, toURL, progress);
if (userInfo) [[NSNotificationCenter defaultCenter] postNotificationName:kREDownloadTasksQueueDidDownloadURLProgressChangedNotification
object:self
userInfo:userInfo];
if (d) [d onREDownloadTasksQueue:self didDownloadURL:fromURL andStoredToURL:toURL withProgress:progress];
});
}
- (void) reportProgress {
if (_reportType == REDownloadTasksQueueReportNone) return;
const float progress = [self downloadProgress];
NSMutableDictionary * info = nil;
if (_reportType & REDownloadTasksQueueReportViaNotifications) {
info = [NSMutableDictionary dictionaryWithCapacity:3];
[info setObject:self forKey:kREDownloadTasksQueueQueueKey];
[info setObject:_userObject ? _userObject : [NSNull null]
forKey:kREDownloadTasksQueueUserObjectKey];
[info setObject:[NSNumber numberWithFloat:progress] forKey:kREDownloadTasksQueueProgressKey];
}
id<REDownloadTasksQueueDelegate> d = self.delegate;
if (d && ![d respondsToSelector:@selector(onREDownloadTasksQueue:progress:)]) d = nil;
dispatch_async(dispatch_get_main_queue(), ^{
if (_onProgressHandler && (_reportType & REDownloadTasksQueueReportViaBlocks)) _onProgressHandler(self, progress);
if (info) [[NSNotificationCenter defaultCenter] postNotificationName:kREDownloadTasksQueueProgressChangedNotification
object:self
userInfo:info];
if (d) [d onREDownloadTasksQueue:self progress:progress];
});
}
- (void) reportFinished {
if (_isFinished || _reportType == REDownloadTasksQueueReportNone) return;
_isFinished = YES;
NSMutableDictionary * info = nil;
if (_reportType & REDownloadTasksQueueReportViaNotifications) {
info = [NSMutableDictionary dictionaryWithCapacity:2];
[info setObject:self forKey:kREDownloadTasksQueueQueueKey];
[info setObject:_userObject ? _userObject : [NSNull null]
forKey:kREDownloadTasksQueueUserObjectKey];
}
id<REDownloadTasksQueueDelegate> d = self.delegate;
if (d && ![d respondsToSelector:@selector(onREDownloadTasksQueueFinished:)]) d = nil;
dispatch_async(dispatch_get_main_queue(), ^{
if (_onFinishedHandler && (_reportType & REDownloadTasksQueueReportViaBlocks)) _onFinishedHandler(self);
if (info) [[NSNotificationCenter defaultCenter] postNotificationName:kREDownloadTasksQueueDidFinishedNotification
object:self
userInfo:info];
if (d) [d onREDownloadTasksQueueFinished:self];
});
}
- (void) reportError:(NSError *) error
withInfo:(REDownloadTasksQueueTaskInfo *) info {
if (_lastError || !error || !info || _reportType == REDownloadTasksQueueReportNone) return;
self.lastError = error;
NSURL * toURL = [info storeURL];
NSURL * fromURL = [info originalURL];
NSMutableDictionary * userInfo = nil;
if (_reportType & REDownloadTasksQueueReportViaNotifications) {
userInfo = [NSMutableDictionary dictionaryWithCapacity:5];
[userInfo setObject:self forKey:kREDownloadTasksQueueQueueKey];
[userInfo setObject:_userObject ? _userObject : [NSNull null]
forKey:kREDownloadTasksQueueUserObjectKey];
if (error) [userInfo setObject:error forKey:kREDownloadTasksQueueErrorKey];
if (fromURL) [userInfo setObject:fromURL forKey:kREDownloadTasksQueueDownloadURLKey];
if (toURL) [userInfo setObject:toURL forKey:kREDownloadTasksQueueStoreURLKey];
}
id<REDownloadTasksQueueDelegate> d = self.delegate;
if (d && ![d respondsToSelector:@selector(onREDownloadTasksQueue:error:downloadURL:storeURL:)]) d = nil;
dispatch_async(dispatch_get_main_queue(), ^{
if (_onErrorOccurredHandler && (_reportType & REDownloadTasksQueueReportViaBlocks)) _onErrorOccurredHandler(self, error, fromURL, toURL);
if (userInfo) [[NSNotificationCenter defaultCenter] postNotificationName:kREDownloadTasksQueueErrorNotification
object:self
userInfo:userInfo];
if (d) [d onREDownloadTasksQueue:self error:error downloadURL:fromURL storeURL:toURL];
});
}
- (BOOL) addURL:(NSURL *) url withStorePath:(NSString *) storePath {
if (!url || [url isFileURL] || [url isFileReferenceURL]) return NO;
if (!storePath || [storePath length] == 0) return NO;
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:url
cachePolicy:_cachePolicy
timeoutInterval:_timeoutInterval];
return [self addURLRequest:request withStorePath:storePath];
}
- (BOOL) addURLString:(NSString *) urlString
withStorePath:(NSString *) storePath {
return (NSStringIsNotEmpty(urlString)) ? [self addURL:[NSURL URLWithString:urlString] withStorePath:storePath] : NO;
}
- (BOOL) addURLRequest:(NSURLRequest *) urlRequest withStorePath:(NSString *) storePath {
NSURL * url = urlRequest ? [urlRequest URL] : nil;
if (!url || [url isFileURL] || [url isFileReferenceURL]) return NO;
if (NSStringIsEmpty(storePath)) return NO;
REDownloadTasksQueueTaskInfo * info = [REDownloadTasksQueueTaskInfo infoWithTask:[_session downloadTaskWithRequest:urlRequest completionHandler:NULL]];
if (info) {
info.storePath = storePath;
pthread_mutex_lock(&_mutex);
if (_infos) [_infos addObject:info];
else self.infos = [NSMutableArray arrayWithObject:info];
_total += 1;
pthread_mutex_unlock(&_mutex);
return YES;
}
return NO;
}
- (REDownloadTasksQueueTaskInfo *) infoForTask:(NSURLSessionDownloadTask *) task {
if (task && NSArrayIsNotEmpty(_infos)) {
const NSUInteger tID = [task taskIdentifier];
for (REDownloadTasksQueueTaskInfo * info in _infos) {
if ([info taskIdentifier] == tID) {
return info;
}
}
}
return nil;
}
- (void) start {
_isCanceled = NO;
[self startNextTask];
}
- (void) cancelWithCompletionHandler:(void(^)(void)) handler {
_isCanceled = YES;
pthread_mutex_lock(&_mutex);
NSMutableNumber * runningCount = [NSMutableNumber numberWithInt:0];
NSArray * infosArray = self.infos;
if (NSArrayIsNotEmpty(infosArray)) {
int count = 0;
for (REDownloadTasksQueueTaskInfo * info in infosArray) {
const BOOL isCanceledRunning = [info cancelRunningWithCompletionHandler:^{
[self lock];
const int count = [runningCount intValue] - 1;
[runningCount setIntValue:count];
[self unlock];
if (count <= 0) {
[runningCount setIntValue:INT_MAX];
if (handler) dispatch_async(dispatch_get_main_queue(), ^{ handler(); });
}
}];
if (isCanceledRunning) count++;
}
[runningCount setIntValue:count];
if (count == 0 && handler) {
dispatch_async(dispatch_get_main_queue(), ^{ handler(); });
}
} else if (handler) {
dispatch_async(dispatch_get_main_queue(), ^{ handler(); });
}
if (_session) {
[_session resetWithCompletionHandler:^{
NSURLSession * session = self.session;
self.session = nil;
if (session) [session invalidateAndCancel];
}];
}
pthread_mutex_unlock(&_mutex);
}
- (float) downloadProgress {
double value = 0;
pthread_mutex_lock(&_mutex);
value = _done / _total;
if (value < 0) value = 0;
else if (value > 1) value = 1;
pthread_mutex_unlock(&_mutex);
return (float)value;
}
- (NSUInteger) numberOfResumedTasks {
NSUInteger value = 0;
pthread_mutex_lock(&_mutex);
value = _numberResumed;
pthread_mutex_unlock(&_mutex);
return value;
}
- (void) setNumberOfResumedTasks:(NSUInteger) value {
if (value > 0 && value <= 64) {
pthread_mutex_lock(&_mutex);
_numberResumed = value;
pthread_mutex_unlock(&_mutex);
}
}
- (NSUInteger) numberOfMaximumConcurrentTasks {
NSUInteger value = 0;
pthread_mutex_lock(&_mutex);
value = self.operationQueue.maxConcurrentOperationCount;
pthread_mutex_unlock(&_mutex);
return value;
}
- (void) setNumberOfMaximumConcurrentTasks:(NSUInteger) value {
if (value > 0 && value <= 32) {
pthread_mutex_lock(&_mutex);
self.operationQueue.maxConcurrentOperationCount = value;
pthread_mutex_unlock(&_mutex);
}
}
- (BOOL) downloadTasksQueueAddInit {
_reportType = REDownloadTasksQueueReportViaNotifications | REDownloadTasksQueueReportViaBlocks;
_total = 0;
_done = 0;
_active = 0;
_numberResumed = 4;
_cachePolicy = kREDownloadTasksQueueDefaultRequestCachePolicy;
_timeoutInterval = kREDownloadTasksQueueDefaultRequestTimeout;
_isCanceled = NO;
_isFinished = NO;
_isContinueOnTaskError = NO;
self.userObject = [NSNull null];
self.sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
if (!_sessionConfiguration) return NO;
self.operationQueue = [[NSOperationQueue alloc] init];
if (!_operationQueue) return NO;
self.operationQueue.maxConcurrentOperationCount = 2;
if (!___initRecursiveMutex(&_mutex)) return NO;
self.session = [NSURLSession sessionWithConfiguration:_sessionConfiguration
delegate:self
delegateQueue:_operationQueue];
if (!_session) return NO;
return YES;
}
- (id) init {
self = [super init];
if (self) {
if (![self downloadTasksQueueAddInit]) return nil;
}
return self;
}
- (void) dealloc {
[self cancelWithCompletionHandler:nil];
pthread_mutex_destroy(&_mutex);
}
#pragma mark - session delegate
- (void) URLSession:(NSURLSession *) session
downloadTask:(NSURLSessionDownloadTask *) downloadTask
didFinishDownloadingToURL:(NSURL *) location {
pthread_mutex_lock(&_mutex);
REDownloadTasksQueueTaskInfo * info = [self infoForTask:downloadTask];
if (info) {
if (_active > 0) {
_active--;
}
NSError * error = nil;
if ([info moveToDestivationFromURL:location withError:&error]) {
[_infos removeObject:info];
BOOL isFinished = NO;
if (NSArrayIsEmpty(_infos)) {
_done = _total;
isFinished = YES;
} else {
const int64_t bytesExpectedToWrite = info.bytesExpectedToWrite;
const int64_t bytesWritted = info.bytesWritted;
if (bytesExpectedToWrite > 0 && bytesWritted > 0) {
const int64_t writed = bytesExpectedToWrite - bytesWritted;
const double progress = ((double)writed) / bytesExpectedToWrite;
_done += progress;
} else {
_done += 1;
}
[self startNextTask];
}
[self reportProgress];
[self reportDidDownloadURLProgressWithInfo:info];
if (isFinished) {
[self cancelWithCompletionHandler:nil];
[self reportFinished];
}
} else {
[self cancelWithCompletionHandler:nil];
[self reportError:error withInfo:info];
}
}
pthread_mutex_unlock(&_mutex);
}
- (void) URLSession:(NSURLSession *) session
downloadTask:(NSURLSessionDownloadTask *) downloadTask
didWriteData:(int64_t) bytesWritten
totalBytesWritten:(int64_t) totalBytesWritten
totalBytesExpectedToWrite:(int64_t) totalBytesExpectedToWrite {
if (bytesWritten <= 0 || totalBytesWritten <= 0 || totalBytesExpectedToWrite <= 0) return;
pthread_mutex_lock(&_mutex);
REDownloadTasksQueueTaskInfo * info = [self infoForTask:downloadTask];
if (info) {
if (info.bytesWritted <= 0 || info.bytesExpectedToWrite <= 0) {
_active++;
}
info.bytesWritted = totalBytesWritten;
info.bytesExpectedToWrite = totalBytesExpectedToWrite;
const double progress = ((double)bytesWritten) / totalBytesExpectedToWrite;
_done += progress;
[self reportProgress];
}
pthread_mutex_unlock(&_mutex);
}
- (void) URLSession:(NSURLSession *) session
task:(NSURLSessionTask *) task
didCompleteWithError:(NSError *) error {
pthread_mutex_lock(&_mutex);
if ([task isKindOfClass:[NSURLSessionDownloadTask class]]) {
REDownloadTasksQueueTaskInfo * info = [self infoForTask:(NSURLSessionDownloadTask *)task];
if (info) {
if (!info.isCanceled && info.isStarted) {
if (_isContinueOnTaskError) {
if (_active > 0) {
_active--;
}
[_infos removeObject:info];
BOOL isFinished = NO;
if (NSArrayIsEmpty(_infos)) {
_done = _total;
isFinished = YES;
} else {
_done += 1;
[self startNextTask];
}
[self reportProgress];
[self reportError:error withInfo:info];
if (isFinished) {
[self cancelWithCompletionHandler:nil];
[self reportFinished];
}
} else {
[self cancelWithCompletionHandler:nil];
[self reportError:error withInfo:info];
}
}
}
}
pthread_mutex_unlock(&_mutex);
}
@end