summaryrefslogtreecommitdiff
path: root/SmartDeviceLink/SDLAsynchronousRPCRequestOperation.m
blob: a891f7268394e95f99e0b2562c90216d07fe04e2 (plain)
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
//
//  SDLRPCRequestOperation.m
//  SmartDeviceLink
//
//  Created by Joel Fischer on 1/30/18.
//  Copyright © 2018 smartdevicelink. All rights reserved.
//

#import "SDLAsynchronousRPCRequestOperation.h"

#import "SDLConnectionManagerType.h"
#import "SDLError.h"
#import "SDLGlobals.h"

NS_ASSUME_NONNULL_BEGIN

@interface SDLAsynchronousRPCRequestOperation ()

@property (weak, nonatomic) id<SDLConnectionManagerType> connectionManager;
@property (copy, nonatomic, nullable) SDLMultipleAsyncRequestProgressHandler progressHandler;
@property (copy, nonatomic, nullable) SDLMultipleRequestCompletionHandler completionHandler;
@property (copy, nonatomic, nullable) SDLResponseHandler responseHandler;

@property (strong, nonatomic) NSUUID *operationId;
@property (assign, nonatomic) NSUInteger requestsComplete;
@property (assign, nonatomic) NSUInteger requestsStarted;
@property (assign, nonatomic, readonly) float percentComplete;
@property (assign, nonatomic) BOOL requestFailed;

@end

@implementation SDLAsynchronousRPCRequestOperation {
    BOOL executing;
    BOOL finished;
}

- (instancetype)init {
    self = [super init];
    if (!self) { return nil; }

    executing = NO;
    finished = NO;

    _operationId = [NSUUID UUID];
    _requestsComplete = 0;
    _requestsStarted = 0;
    _requestFailed = NO;

    return self;
}

- (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connectionManager requests:(NSArray<SDLRPCRequest *> *)requests progressHandler:(nullable SDLMultipleAsyncRequestProgressHandler)progressHandler completionHandler:(nullable SDLMultipleRequestCompletionHandler)completionHandler {
    self = [self init];

    _connectionManager = connectionManager;
    _requests = requests;
    _progressHandler = progressHandler;
    _completionHandler = completionHandler;

    return self;
}

- (instancetype)initWithConnectionManager:(id<SDLConnectionManagerType>)connectionManager request:(SDLRPCRequest *)request responseHandler:(nullable SDLResponseHandler)responseHandler {
    self = [self init];

    _connectionManager = connectionManager;
    _requests = @[request];
    _responseHandler = responseHandler;

    return self;
}

- (void)start {
    [super start];

    [self sdl_sendRequests];
}

- (void)sdl_sendRequests {
    for (SDLRPCRequest *request in self.requests) {
        if (self.isCancelled) {
            [self sdl_abortOperationWithRequest:request];
            break;
        }

        [self sdl_sendRequest:request];
        self.requestsStarted++;
    }
}

- (void)sdl_sendRequest:(SDLRPCRequest *)request {
    __weak typeof(self) weakSelf = self;
    [self.connectionManager sendConnectionRequest:request withResponseHandler:^(__kindof SDLRPCRequest * _Nullable request, __kindof SDLRPCResponse * _Nullable response, NSError * _Nullable error) {
        __strong typeof(self) strongSelf = weakSelf;

        if (strongSelf.isCancelled) {
            [self sdl_abortOperationWithRequest:request];
            BLOCK_RETURN;
        }

        strongSelf.requestsComplete++;

        // If this request failed set our internal request failed to YES
        if (error != nil) {
            strongSelf.requestFailed = YES;
        }

        if (strongSelf.progressHandler != NULL) {
            strongSelf.progressHandler(request, response, error, strongSelf.percentComplete);
        } else if (strongSelf.responseHandler != NULL) {
            strongSelf.responseHandler(request, response, error);
        }

        // If we've received responses for all requests, call the completion handler.
        if (strongSelf.requestsComplete >= strongSelf.requests.count) {
            [strongSelf finishOperation];
        }
    }];
}

- (void)sdl_abortOperationWithRequest:(SDLRPCRequest *)request {
    if (self.isFinished) { return; }
    self.requestFailed = YES;

    for (NSUInteger i = self.requestsComplete; i <= self.requests.count; i++) {
        if (self.progressHandler != NULL) {
            self.progressHandler(self.requests[i], nil, [NSError sdl_lifecycle_multipleRequestsCancelled], self.percentComplete);
        }

        if (self.responseHandler != NULL) {
            self.responseHandler(request, nil, [NSError sdl_lifecycle_multipleRequestsCancelled]);
        }
    }

    [self finishOperation];
}

#pragma mark - Getters

- (float)percentComplete {
    return (float)self.requestsComplete / (float)self.requests.count;
}

#pragma mark - Property Overrides

- (void)finishOperation {
    if (self.completionHandler != NULL) {
        self.completionHandler(!self.requestFailed);
    }

    [super finishOperation];
}

- (nullable NSString *)name {
    return [NSString stringWithFormat:@"%@ - %@", self.class, self.operationId];
}

- (NSOperationQueuePriority)queuePriority {
    return NSOperationQueuePriorityNormal;
}

- (NSString *)description {
    return [NSString stringWithFormat:@"%@, request count=%lu, requests started=%lu, finished=%lu, failed=%@", self.name, (unsigned long)self.requests.count, (unsigned long)self.requestsStarted, (unsigned long)self.requestsComplete, (self.requestFailed ? @"YES": @"NO")];
}

- (NSString *)debugDescription {
    return [NSString stringWithFormat:@"%@, request count=%lu, requests started=%lu, finished=%lu, failed=%@, requests=%@", self.name, (unsigned long)self.requests.count, (unsigned long)self.requestsStarted, (unsigned long)self.requestsComplete, (self.requestFailed ? @"YES": @"NO"), self.requests];
}

@end

NS_ASSUME_NONNULL_END