SGDownloadQueue.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. //
  2. // SGDownloadQueue.m
  3. // OfflineBreakPointDownload
  4. //
  5. // Created by Shangen Zhang on 16/11/26.
  6. // Copyright © 2016年 Shangen Zhang. All rights reserved.
  7. //
  8. #import "SGDownloadQueue.h"
  9. #import "SGDownloadOperation.h"
  10. #import "SGDownloadManager.h"
  11. #import "NSURLSession+SGDownloadTask.h"
  12. #import <MJExtension.h>
  13. @interface SGDownloadQueue ()
  14. // 列队管理集合
  15. @property (nonatomic,strong) NSMutableArray <SGDownloadOperation *> *operations;
  16. @end
  17. @implementation SGDownloadQueue
  18. - (instancetype)init {
  19. if (self = [super init]) {
  20. //_maxCount = 3;
  21. _maxCount = 1;
  22. [self registeNotification];
  23. }
  24. return self;
  25. }
  26. - (void)dealloc {
  27. [[NSNotificationCenter defaultCenter] removeObserver:self];
  28. }
  29. - (void)didResiveDownloadFileCompete:(NSNotification *)noti {
  30. SGDownloadOperation *operation = noti.object;
  31. if (operation) {
  32. [self.operations removeObject:operation];
  33. }
  34. }
  35. - (NSMutableArray *)getOperationDoing {
  36. NSMutableArray *doingArray = [NSMutableArray array];
  37. for (SGDownloadOperation *operation in self.operations) {
  38. if (operation.downloadState == DownloadStateDoing) {
  39. [doingArray addObject:operation];
  40. }
  41. }
  42. return doingArray;
  43. }
  44. - (NSMutableArray *)getOperationWaiting {
  45. NSMutableArray *waitingArray = [NSMutableArray array];
  46. [self.operations enumerateObjectsUsingBlock:^(SGDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  47. SGDownloadOperation *operation = (SGDownloadOperation *)obj;
  48. if (operation.downloadState == DownloadStateWaiting) {
  49. [waitingArray addObject:operation];
  50. }
  51. }];
  52. return waitingArray;
  53. }
  54. #pragma mark - handle Out operations
  55. - (void)addDownloadWithSession:(NSURLSession *)session URL:(NSURL *)url begin:(void(^)(NSString *))begin progress:(void(^)(NSInteger,NSInteger))progress complete:(void(^)(NSDictionary *,NSError *))complet {
  56. // 获取operation对象
  57. SGDownloadOperation *operation = [self operationWithUrl:url.absoluteString];
  58. if (operation == nil) { // 之前不存在此任务
  59. operation = [[SGDownloadOperation alloc] initWith:url.absoluteString session:session];
  60. if (operation == nil) {
  61. // 没有下载任务代表已下载完成
  62. NSDictionary *fileInfo = [SGCacheManager queryFileInfoWithUrl:url.absoluteString];
  63. if (fileInfo && complet) {
  64. complet(fileInfo,nil);
  65. }else {
  66. complet(nil,[NSError errorWithDomain:@"构建下载任务失败" code:-1 userInfo:nil]);
  67. }
  68. return;
  69. }
  70. [self.operations addObject:operation];
  71. }
  72. // 回调赋值operation
  73. // [operation configCallBacksWithDidReceiveResponse:begin didReceivData:progress didComplete:complet];
  74. // [operation.dataTask resume];
  75. }
  76. - (void)addDownloadWithSession:(NSURLSession *)session URL:(NSURL *)url fileType:(NSInteger)fileType fileSize:(NSInteger)fileSize begin:(void(^)(NSString * filePath))begin progress:(void(^)(NSInteger completeSize,NSInteger expectSize))progress complete:(void(^)(NSDictionary *respose,NSError *error))complet {
  77. // 获取operation对象
  78. SGDownloadOperation *operation = [self operationWithUrl:url.absoluteString];
  79. if (operation == nil) { // 队列里不存在此任务
  80. operation = [[SGDownloadOperation alloc] initWith:url.absoluteString session:session fileType:fileType fileSize:fileSize];
  81. if (operation == nil) {
  82. // 没有下载任务代表已下载完成
  83. NSDictionary *fileInfo = [SGCacheManager queryFileInfoWithUrl:url.absoluteString];
  84. if (fileInfo && complet) {
  85. complet(fileInfo,nil);
  86. }else {
  87. complet(nil,[NSError errorWithDomain:@"构建下载任务失败" code:-1 userInfo:nil]);
  88. }
  89. return;
  90. }
  91. [self.operations addObject:operation];
  92. }
  93. // 回调赋值operation
  94. // [operation configCallBacksWithDidReceiveResponse:begin didReceivData:progress didComplete:complet];
  95. if ([self getOperationDoing].count < self.maxCount) { // 下载中任务数少于最大任务限制
  96. [operation.dataTask resume];
  97. [self operationStartWithOperation:operation];
  98. }else { // 下载中任务数大于最大任务限制
  99. // [operation.dataTask suspend];
  100. [self operationWaitingWithOperation:operation];
  101. // HLog(@"下载中任务数等于最大任务限制:%zd",[self getOperationDoing].count);
  102. }
  103. }
  104. - (void)operateDownloadWithUrl:(NSString *)url session:(NSURLSession *)session handle:(DownloadHandleType)handle {
  105. // 1、任务列表里取任务
  106. SGDownloadOperation *operation = [self operationWithUrl:url];
  107. // 2、本地plist文件里提取的任务
  108. if (!operation) {
  109. NSDictionary *dict = [SGCacheManager queryFileInfoWithUrl:url];
  110. operation = [SGDownloadOperation mj_objectWithKeyValues:dict];
  111. }
  112. // 3、本地plist文件里提取的任务不存在dataTask
  113. if (!operation.dataTask && operation.currentSize != operation.totalSize) {
  114. operation.dataTask = [self.session sg_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
  115. [self.operations addObject:operation];
  116. }
  117. if (operation) {
  118. switch (handle) {
  119. case DownloadHandleTypeStart:
  120. switch (operation.dataTask.state) {
  121. case NSURLSessionTaskStateRunning:
  122. HLog(@"NSURLSessionTaskStateRunning");
  123. break;
  124. case NSURLSessionTaskStateSuspended:
  125. HLog(@"NSURLSessionTaskStateSuspended");
  126. operation.dataTask = [self.session sg_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
  127. break;
  128. case NSURLSessionTaskStateCanceling:
  129. HLog(@"NSURLSessionTaskStateCanceling");
  130. operation.dataTask = [self.session sg_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
  131. break;
  132. case NSURLSessionTaskStateCompleted:
  133. HLog(@"NSURLSessionTaskStateCompleted");
  134. break;
  135. default:
  136. break;
  137. }
  138. if ([self getOperationDoing].count >= self.maxCount) { // 下载中任务数超过最大任务限制
  139. HLog(@"下载中的任务数超过最大限制")
  140. // [operation.dataTask suspend]; // 暂停
  141. [self operationWaitingWithOperation:operation];
  142. return;
  143. }
  144. [operation.dataTask resume]; // 开始
  145. [self operationStartWithOperation:operation];
  146. break;
  147. case DownloadHandleTypeSuspend:
  148. [operation.dataTask suspend]; // 暂停
  149. [self operationSuspendWithOperation:operation];
  150. break;
  151. case DownloadHandleTypeCancel:
  152. if (operation.dataTask) { // 任务列表删除任务
  153. [operation.dataTask cancel]; // 取消
  154. [self.operations removeObject:operation];
  155. }
  156. [SGCacheManager deleteFileWithUrl:url]; // plist删除任务
  157. [self operationDeleteWithOperation:operation];
  158. break;
  159. }
  160. }
  161. }
  162. - (void)cancelAllTasks {
  163. // 取消所有的任务
  164. [self.operations enumerateObjectsUsingBlock:^(SGDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  165. [obj.dataTask cancel];
  166. }];
  167. // 清理内存
  168. self.operations = nil;
  169. }
  170. - (void)suspendAllTasksWithSession:(NSURLSession *)session {
  171. // 暂停所有的任务
  172. [self.operations enumerateObjectsUsingBlock:^(SGDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  173. SGDownloadOperation *operation = (SGDownloadOperation *)obj;
  174. if (operation.currentSize != operation.totalSize) { // 暂停未完成的下载任务
  175. if (!operation.dataTask) { // 给plist里的任务添加dataTask 添加到operations
  176. operation.dataTask = [self.session sg_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
  177. }
  178. [operation.dataTask suspend];
  179. [self operationSuspendWithOperation:operation];
  180. }else {
  181. HLog(@"已完成的任务");
  182. }
  183. }];
  184. }
  185. - (void)startAllTasksWithSession:(NSURLSession *)session {
  186. // 开始所有的任务
  187. [self.operations enumerateObjectsUsingBlock:^(SGDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  188. SGDownloadOperation *operation = (SGDownloadOperation *)obj;
  189. if (!operation.dataTask) { // 给plist里的任务添加dataTask 添加到operations
  190. operation.dataTask = [self.session sg_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
  191. }
  192. if ([self getOperationDoing].count < self.maxCount) { // 下载中任务数少于最大任务限制
  193. switch (operation.dataTask.state) {
  194. case NSURLSessionTaskStateRunning:
  195. HLog(@"NSURLSessionTaskStateRunning");
  196. break;
  197. case NSURLSessionTaskStateSuspended:
  198. HLog(@"NSURLSessionTaskStateSuspended");
  199. break;
  200. case NSURLSessionTaskStateCanceling:
  201. HLog(@"NSURLSessionTaskStateCanceling");
  202. operation.dataTask = [self.session sg_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
  203. break;
  204. case NSURLSessionTaskStateCompleted:
  205. HLog(@"NSURLSessionTaskStateCompleted");
  206. break;
  207. default:
  208. break;
  209. }
  210. [operation.dataTask resume]; // 开始
  211. [self operationStartWithOperation:operation];
  212. }else { // 下载中任务数大于最大任务限制
  213. // [operation.dataTask suspend];
  214. [self operationWaitingWithOperation:operation];
  215. // HLog(@"下载中任务数等于最大任务限制:%zd",[self getOperationDoing].count);
  216. }
  217. }];
  218. }
  219. #pragma mark-监听通知
  220. - (void)registeNotification {
  221. // 监听完成通知
  222. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didResiveDownloadFileCompete:) name:SGDownloadCompleteNoti object:nil];
  223. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:SGDownloadTaskExeEnd object:nil];
  224. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:SGDownloadTaskExeError object:nil];
  225. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:SGDownloadTaskExeSuspend object:nil];
  226. // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:SGDownloadTaskExeDelete object:nil];
  227. }
  228. - (void)taskExeEnd:(NSNotification *)notification
  229. {
  230. NSMutableArray *operationWaiting = [self getOperationWaiting];
  231. [operationWaiting enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  232. SGDownloadOperation *operation = obj;
  233. if ([self getOperationDoing].count < self.maxCount) {
  234. [self operateDownloadWithUrl:operation.url session:self.session handle:DownloadHandleTypeStart];
  235. }else {
  236. *stop = YES;
  237. }
  238. }];
  239. }
  240. #pragma mark 发送通知
  241. // 暂停某一个operation 保存本地 通知外界
  242. - (void)operationSuspendWithOperation:(SGDownloadOperation *)operation {
  243. operation.downloadState = DownloadStateSuspended;
  244. [SGCacheManager saveFileInfoWithDict:[operation downLoadInfoWithFinished:NO]];
  245. [[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeSuspend object:nil userInfo:@{@"operation" : operation}];
  246. }
  247. // 重启某一个operation 保存本地 通知外界
  248. - (void)operationStartWithOperation:(SGDownloadOperation *)operation {
  249. operation.downloadState = DownloadStateDoing;
  250. [SGCacheManager saveFileInfoWithDict:[operation downLoadInfoWithFinished:NO]];
  251. [[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeing object:nil userInfo:@{@"operation" : operation}];
  252. }
  253. // 等待某一个operation 保存本地 通知外界
  254. - (void)operationWaitingWithOperation:(SGDownloadOperation *)operation {
  255. operation.downloadState = DownloadStateWaiting;
  256. [SGCacheManager saveFileInfoWithDict:[operation downLoadInfoWithFinished:NO]];
  257. [[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeSuspend object:nil userInfo:@{@"operation" : operation}];
  258. }
  259. // 删除某一个operation 保存本地 通知外界
  260. - (void)operationDeleteWithOperation:(SGDownloadOperation *)operation {
  261. [[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeDelete object:nil userInfo:nil];
  262. }
  263. #pragma mark - handle download
  264. - (void)dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response {
  265. [[self oprationWithDataTask:dataTask] operateWithResponse:response];
  266. }
  267. - (void)dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
  268. [[self oprationWithDataTask:dataTask] operateWithReceivingData:data];
  269. }
  270. - (void)task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
  271. [[self oprationWithDataTask:task] operateWithComplete:error];
  272. }
  273. #pragma mark - query operation
  274. - (SGDownloadOperation *)operationWithUrl:(NSString *)url{
  275. __block SGDownloadOperation *operation = nil;
  276. [self.operations enumerateObjectsUsingBlock:^(SGDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  277. if ([obj.url isEqualToString:url]) {
  278. operation = obj;
  279. *stop = YES;
  280. }
  281. }];
  282. return operation;
  283. }
  284. // 寻找operation
  285. - (SGDownloadOperation *)oprationWithDataTask:(NSURLSessionTask *)dataTask {
  286. __block SGDownloadOperation *operation = nil;
  287. [self.operations enumerateObjectsUsingBlock:^(SGDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  288. if (obj.dataTask == dataTask) {
  289. operation = obj;
  290. *stop = YES;
  291. }
  292. }];
  293. return operation;
  294. }
  295. #pragma mark - lazy load
  296. - (NSMutableArray<SGDownloadOperation *> *)operations {
  297. if (!_operations) {
  298. _operations = [NSMutableArray array];
  299. [self addOperationsFromPlist];
  300. }
  301. return _operations;
  302. }
  303. - (void)addOperationsFromPlist {
  304. [self.operations removeAllObjects];
  305. NSMutableArray *operations = [[SGDownloadManager shareManager] getAllOperation];
  306. for (SGDownloadOperation *operationPlist in operations) {
  307. // 1、任务列表里取任务
  308. SGDownloadOperation *operation = [self operationWithUrl:operationPlist.url];
  309. // 2、本地plist文件里提取的任务
  310. if (!operation) {
  311. NSDictionary *dict = [SGCacheManager queryFileInfoWithUrl:operationPlist.url];
  312. operation = [SGDownloadOperation mj_objectWithKeyValues:dict];
  313. }
  314. // 3、本地plist文件里提取的任务不存在dataTask
  315. if (!operation.dataTask) {
  316. operation.dataTask = [self.session sg_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
  317. operation.handle = [NSFileHandle fileHandleForWritingAtPath:operation.fullPath];
  318. // [operation configCallBacksWithDidReceiveResponse:begin didReceivData:progress didComplete:complet];
  319. [self.operations addObject:operation];
  320. }
  321. }
  322. }
  323. @end