ASINetworkQueue.m
4.3 KB
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
//
// ASINetworkQueue.m
// asi-http-request
//
// Created by Ben Copsey on 07/11/2008.
// Copyright 2008 All-Seeing Interactive. All rights reserved.
//
#import "ASINetworkQueue.h"
#import "ASIHTTPRequest.h"
@implementation ASINetworkQueue
- (id)init
{
self = [super init];
delegate = NULL;
requestDidFinishSelector = NULL;
requestDidFailSelector = NULL;
queueDidFinishSelector = NULL;
shouldCancelAllRequestsOnFailure = YES;
uploadProgressDelegate = nil;
uploadProgressBytes = 0;
uploadProgressTotalBytes = 0;
downloadProgressDelegate = nil;
downloadProgressBytes = 0;
downloadProgressTotalBytes = 0;
requestsCount = 0;
requestsCompleteCount = 0;
return self;
}
- (void)cancelAllOperations
{
uploadProgressBytes = 0;
uploadProgressTotalBytes = 0;
downloadProgressBytes = 0;
downloadProgressTotalBytes = 0;
[super cancelAllOperations];
}
- (void)setUploadProgressDelegate:(id)newDelegate
{
uploadProgressDelegate = newDelegate;
SEL selector = @selector(setMaxValue:);
if ([uploadProgressDelegate respondsToSelector:selector]) {
double max = 1.0;
NSMethodSignature *signature = [[uploadProgressDelegate class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];
[invocation setArgument:&max atIndex:2];
[invocation invokeWithTarget:uploadProgressDelegate];
}
}
- (void)setDownloadProgressDelegate:(id)newDelegate
{
downloadProgressDelegate = newDelegate;
SEL selector = @selector(setMaxValue:);
if ([downloadProgressDelegate respondsToSelector:selector]) {
double max = 1.0;
NSMethodSignature *signature = [[downloadProgressDelegate class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(setMaxValue:)];
[invocation setArgument:&max atIndex:2];
[invocation invokeWithTarget:downloadProgressDelegate];
}
}
//Only add ASIHTTPRequests to this queue
- (void)addOperation:(NSOperation *)operation
{
if ([operation isKindOfClass:[ASIHTTPRequest class]]) {
requestsCount++;
[(ASIHTTPRequest *)operation setUploadProgressDelegate:self];
[(ASIHTTPRequest *)operation setDownloadProgressDelegate:self];
[(ASIHTTPRequest *)operation setDelegate:self];
[(ASIHTTPRequest *)operation setDidFailSelector:@selector(requestDidFail:)];
[(ASIHTTPRequest *)operation setDidFinishSelector:@selector(requestDidFinish:)];
[super addOperation:operation];
}
}
- (void)requestDidFail:(ASIHTTPRequest *)request
{
if (requestDidFailSelector) {
[delegate performSelector:requestDidFailSelector withObject:request];
}
if (shouldCancelAllRequestsOnFailure) {
[self cancelAllOperations];
}
requestsCompleteCount++;
}
- (void)requestDidFinish:(ASIHTTPRequest *)request
{
requestsCompleteCount++;
if (requestDidFinishSelector) {
[delegate performSelector:requestDidFinishSelector withObject:request];
}
if (queueDidFinishSelector && requestsCompleteCount == requestsCount) {
[delegate performSelector:queueDidFinishSelector withObject:self];
}
}
- (void)incrementUploadSizeBy:(int)bytes
{
if (!uploadProgressDelegate) {
return;
}
uploadProgressTotalBytes += bytes;
[self incrementUploadProgressBy:0];
}
- (void)incrementUploadProgressBy:(int)bytes
{
if (!uploadProgressDelegate || uploadProgressTotalBytes == 0) {
return;
}
uploadProgressBytes += bytes;
double progress = (uploadProgressBytes*1.0)/(uploadProgressTotalBytes*1.0);
[ASIHTTPRequest setProgress:progress forProgressIndicator:uploadProgressDelegate];
}
- (void)incrementDownloadSizeBy:(int)bytes
{
if (!downloadProgressDelegate) {
return;
}
downloadProgressTotalBytes += bytes;
[self incrementDownloadProgressBy:0];
}
- (void)incrementDownloadProgressBy:(int)bytes
{
if (!downloadProgressDelegate || downloadProgressTotalBytes == 0) {
return;
}
downloadProgressBytes += bytes;
double progress = (downloadProgressBytes*1.0)/(downloadProgressTotalBytes*1.0);
[ASIHTTPRequest setProgress:progress forProgressIndicator:downloadProgressDelegate];
}
@synthesize uploadProgressDelegate;
@synthesize downloadProgressDelegate;
@synthesize requestDidFinishSelector;
@synthesize requestDidFailSelector;
@synthesize queueDidFinishSelector;
@synthesize shouldCancelAllRequestsOnFailure;
@synthesize delegate;
@end