Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Yang Gao | 5fc9029 | 2015-02-20 09:46:22 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Jorge Canizales | 9409ad8 | 2015-02-18 16:19:56 -0800 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 34 | #import "GRPCCall.h" |
| 35 | |
Jorge Canizales | c2d7ecb | 2015-02-27 01:22:41 -0800 | [diff] [blame] | 36 | #include <grpc/grpc.h> |
Jorge Canizales | 59bb9d7 | 2015-06-22 19:04:15 -0700 | [diff] [blame] | 37 | #include <grpc/support/time.h> |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 38 | #import <RxLibrary/GRXConcurrentWriteable.h> |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 39 | |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 40 | #import "private/GRPCWrappedCall.h" |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 41 | #import "private/NSData+GRPC.h" |
| 42 | #import "private/NSDictionary+GRPC.h" |
| 43 | #import "private/NSError+GRPC.h" |
| 44 | |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 45 | NSString * const kGRPCStatusMetadataKey = @"io.grpc.StatusMetadataKey"; |
| 46 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 47 | @interface GRPCCall () <GRXWriteable> |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 48 | @end |
| 49 | |
| 50 | // The following methods of a C gRPC call object aren't reentrant, and thus |
| 51 | // calls to them must be serialized: |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 52 | // - start_batch |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 53 | // - destroy |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 54 | // |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 55 | // start_batch with a SEND_MESSAGE argument can only be called after the |
| 56 | // OP_COMPLETE event for any previous write is received. This is achieved by |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 57 | // pausing the requests writer immediately every time it writes a value, and |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 58 | // resuming it again when OP_COMPLETE is received. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 59 | // |
murgatroid99 | b5c076f | 2015-04-27 17:25:36 -0700 | [diff] [blame] | 60 | // Similarly, start_batch with a RECV_MESSAGE argument can only be called after |
| 61 | // the OP_COMPLETE event for any previous read is received.This is easier to |
| 62 | // enforce, as we're writing the received messages into the writeable: |
| 63 | // start_batch is enqueued once upon receiving the OP_COMPLETE event for the |
| 64 | // RECV_METADATA batch, and then once after receiving each OP_COMPLETE event for |
| 65 | // each RECV_MESSAGE batch. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 66 | @implementation GRPCCall { |
| 67 | dispatch_queue_t _callQueue; |
| 68 | |
murgatroid99 | 30b7d4e | 2015-04-24 10:36:43 -0700 | [diff] [blame] | 69 | GRPCWrappedCall *_wrappedCall; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 70 | dispatch_once_t _callAlreadyInvoked; |
| 71 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 72 | // The C gRPC library has less guarantees on the ordering of events than we |
| 73 | // do. Particularly, in the face of errors, there's no ordering guarantee at |
| 74 | // all. This wrapper over our actual writeable ensures thread-safety and |
| 75 | // correct ordering. |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 76 | GRXConcurrentWriteable *_responseWriteable; |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 77 | GRXWriter *_requestWriter; |
Jorge Canizales | 544963e | 2015-06-12 19:46:27 -0700 | [diff] [blame] | 78 | |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 79 | // To create a retain cycle when a call is started, up until it finishes. See |
| 80 | // |startWithWriteable:| and |finishWithError:|. |
| 81 | GRPCCall *_self; |
| 82 | |
Jorge Canizales | 544963e | 2015-06-12 19:46:27 -0700 | [diff] [blame] | 83 | NSMutableDictionary *_requestMetadata; |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 84 | NSMutableDictionary *_responseMetadata; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @synthesize state = _state; |
| 88 | |
| 89 | - (instancetype)init { |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 90 | return [self initWithHost:nil path:nil requestsWriter:nil]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // Designated initializer |
| 94 | - (instancetype)initWithHost:(NSString *)host |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 95 | path:(NSString *)path |
Jorge Canizales | 5604712 | 2015-07-17 12:18:08 -0700 | [diff] [blame] | 96 | requestsWriter:(GRXWriter *)requestWriter { |
Jorge Canizales | be808e3 | 2015-07-04 14:37:58 -0700 | [diff] [blame] | 97 | if (!host || !path) { |
Jorge Canizales | 597ef98 | 2015-07-31 23:31:56 -0700 | [diff] [blame] | 98 | [NSException raise:NSInvalidArgumentException format:@"Neither host nor path can be nil."]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 99 | } |
Jorge Canizales | 6bbfcc3 | 2015-06-17 14:10:52 -0700 | [diff] [blame] | 100 | if (requestWriter.state != GRXWriterStateNotStarted) { |
Jorge Canizales | 597ef98 | 2015-07-31 23:31:56 -0700 | [diff] [blame] | 101 | [NSException raise:NSInvalidArgumentException |
| 102 | format:@"The requests writer can't be already started."]; |
Jorge Canizales | 6bbfcc3 | 2015-06-17 14:10:52 -0700 | [diff] [blame] | 103 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 104 | if ((self = [super init])) { |
Jorge Canizales | 3a5253e | 2015-07-31 23:48:56 -0700 | [diff] [blame] | 105 | _wrappedCall = [[GRPCWrappedCall alloc] initWithHost:host path:path]; |
Jorge Canizales | 8c5318a | 2015-08-05 18:50:08 -0700 | [diff] [blame] | 106 | if (!_wrappedCall) { |
| 107 | return nil; |
| 108 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 109 | |
| 110 | // Serial queue to invoke the non-reentrant methods of the grpc_call object. |
| 111 | _callQueue = dispatch_queue_create("org.grpc.call", NULL); |
| 112 | |
| 113 | _requestWriter = requestWriter; |
Jorge Canizales | 544963e | 2015-06-12 19:46:27 -0700 | [diff] [blame] | 114 | |
| 115 | _requestMetadata = [NSMutableDictionary dictionary]; |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 116 | _responseMetadata = [NSMutableDictionary dictionary]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 117 | } |
| 118 | return self; |
| 119 | } |
| 120 | |
Jorge Canizales | 544963e | 2015-06-12 19:46:27 -0700 | [diff] [blame] | 121 | #pragma mark Metadata |
| 122 | |
| 123 | - (NSMutableDictionary *)requestMetadata { |
| 124 | return _requestMetadata; |
| 125 | } |
| 126 | |
| 127 | - (void)setRequestMetadata:(NSDictionary *)requestMetadata { |
| 128 | _requestMetadata = [NSMutableDictionary dictionaryWithDictionary:requestMetadata]; |
| 129 | } |
| 130 | |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 131 | - (NSDictionary *)responseMetadata { |
| 132 | return _responseMetadata; |
| 133 | } |
| 134 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 135 | #pragma mark Finish |
| 136 | |
| 137 | - (void)finishWithError:(NSError *)errorOrNil { |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 138 | // If the call isn't retained anywhere else, it can be deallocated now. |
| 139 | _self = nil; |
| 140 | |
| 141 | // If there were still request messages coming, stop them. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 142 | _requestWriter.state = GRXWriterStateFinished; |
| 143 | _requestWriter = nil; |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 144 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 145 | if (errorOrNil) { |
| 146 | [_responseWriteable cancelWithError:errorOrNil]; |
| 147 | } else { |
| 148 | [_responseWriteable enqueueSuccessfulCompletion]; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | - (void)cancelCall { |
| 153 | // Can be called from any thread, any number of times. |
murgatroid99 | b56609c | 2015-04-28 16:41:11 -0700 | [diff] [blame] | 154 | [_wrappedCall cancel]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | - (void)cancel { |
| 158 | [self finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 159 | code:GRPCErrorCodeCancelled |
| 160 | userInfo:nil]]; |
| 161 | [self cancelCall]; |
| 162 | } |
| 163 | |
| 164 | - (void)dealloc { |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 165 | __block GRPCWrappedCall *wrappedCall = _wrappedCall; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 166 | dispatch_async(_callQueue, ^{ |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 167 | wrappedCall = nil; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 168 | }); |
| 169 | } |
| 170 | |
| 171 | #pragma mark Read messages |
| 172 | |
| 173 | // Only called from the call queue. |
| 174 | // The handler will be called from the network queue. |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 175 | - (void)startReadWithHandler:(void(^)(grpc_byte_buffer *))handler { |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 176 | // TODO(jcanizales): Add error handlers for async failures |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 177 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMessage alloc] initWithHandler:handler]]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Called initially from the network queue once response headers are received, |
| 181 | // then "recursively" from the responseWriteable queue after each response from the |
| 182 | // server has been written. |
| 183 | // If the call is currently paused, this is a noop. Restarting the call will invoke this |
| 184 | // method. |
| 185 | // TODO(jcanizales): Rename to readResponseIfNotPaused. |
| 186 | - (void)startNextRead { |
| 187 | if (self.state == GRXWriterStatePaused) { |
| 188 | return; |
| 189 | } |
| 190 | __weak GRPCCall *weakSelf = self; |
Jorge Canizales | 35f003b | 2015-07-17 21:14:36 -0700 | [diff] [blame] | 191 | __weak GRXConcurrentWriteable *weakWriteable = _responseWriteable; |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 192 | |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 193 | dispatch_async(_callQueue, ^{ |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 194 | [weakSelf startReadWithHandler:^(grpc_byte_buffer *message) { |
| 195 | if (message == NULL) { |
murgatroid99 | b56609c | 2015-04-28 16:41:11 -0700 | [diff] [blame] | 196 | // No more messages from the server |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 197 | return; |
| 198 | } |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 199 | NSData *data = [NSData grpc_dataWithByteBuffer:message]; |
| 200 | grpc_byte_buffer_destroy(message); |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 201 | if (!data) { |
| 202 | // The app doesn't have enough memory to hold the server response. We |
| 203 | // don't want to throw, because the app shouldn't crash for a behavior |
| 204 | // that's on the hands of any server to have. Instead we finish and ask |
| 205 | // the server to cancel. |
| 206 | // |
| 207 | // TODO(jcanizales): No canonical code is appropriate for this situation |
| 208 | // (because it's just a client problem). Use another domain and an |
| 209 | // appropriately-documented code. |
| 210 | [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 211 | code:GRPCErrorCodeInternal |
| 212 | userInfo:nil]]; |
| 213 | [weakSelf cancelCall]; |
| 214 | return; |
| 215 | } |
Jorge Canizales | 4c6f778 | 2015-07-17 23:13:36 -0700 | [diff] [blame] | 216 | [weakWriteable enqueueValue:data completionHandler:^{ |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 217 | [weakSelf startNextRead]; |
| 218 | }]; |
| 219 | }]; |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | #pragma mark Send headers |
| 224 | |
murgatroid99 | 69927d6 | 2015-04-24 13:32:48 -0700 | [diff] [blame] | 225 | // TODO(jcanizales): Rename to commitHeaders. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 226 | - (void)sendHeaders:(NSDictionary *)metadata { |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 227 | // TODO(jcanizales): Add error handlers for async failures |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 228 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMetadata alloc] |
| 229 | initWithMetadata:metadata ?: @{} handler:nil]]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | #pragma mark GRXWriteable implementation |
| 233 | |
| 234 | // Only called from the call queue. The error handler will be called from the |
| 235 | // network queue if the write didn't succeed. |
| 236 | - (void)writeMessage:(NSData *)message withErrorHandler:(void (^)())errorHandler { |
| 237 | |
| 238 | __weak GRPCCall *weakSelf = self; |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 239 | void(^resumingHandler)(void) = ^{ |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 240 | // Resume the request writer. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 241 | GRPCCall *strongSelf = weakSelf; |
| 242 | if (strongSelf) { |
| 243 | strongSelf->_requestWriter.state = GRXWriterStateStarted; |
| 244 | } |
| 245 | }; |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 246 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendMessage alloc] |
| 247 | initWithMessage:message |
| 248 | handler:resumingHandler]] errorHandler:errorHandler]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Jorge Canizales | a90a9c3 | 2015-05-18 17:12:41 -0700 | [diff] [blame] | 251 | - (void)writeValue:(id)value { |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 252 | // TODO(jcanizales): Throw/assert if value isn't NSData. |
| 253 | |
| 254 | // Pause the input and only resume it when the C layer notifies us that writes |
| 255 | // can proceed. |
| 256 | _requestWriter.state = GRXWriterStatePaused; |
| 257 | |
| 258 | __weak GRPCCall *weakSelf = self; |
| 259 | dispatch_async(_callQueue, ^{ |
| 260 | [weakSelf writeMessage:value withErrorHandler:^{ |
| 261 | [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 262 | code:GRPCErrorCodeInternal |
| 263 | userInfo:nil]]; |
| 264 | }]; |
| 265 | }); |
| 266 | } |
| 267 | |
| 268 | // Only called from the call queue. The error handler will be called from the |
| 269 | // network queue if the requests stream couldn't be closed successfully. |
| 270 | - (void)finishRequestWithErrorHandler:(void (^)())errorHandler { |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 271 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpSendClose alloc] init]] |
| 272 | errorHandler:errorHandler]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Jorge Canizales | b2c300c | 2015-05-18 17:19:16 -0700 | [diff] [blame] | 275 | - (void)writesFinishedWithError:(NSError *)errorOrNil { |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 276 | _requestWriter = nil; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 277 | if (errorOrNil) { |
| 278 | [self cancel]; |
| 279 | } else { |
| 280 | __weak GRPCCall *weakSelf = self; |
| 281 | dispatch_async(_callQueue, ^{ |
| 282 | [weakSelf finishRequestWithErrorHandler:^{ |
| 283 | [weakSelf finishWithError:[NSError errorWithDomain:kGRPCErrorDomain |
| 284 | code:GRPCErrorCodeInternal |
| 285 | userInfo:nil]]; |
| 286 | }]; |
| 287 | }); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | #pragma mark Invoke |
| 292 | |
| 293 | // Both handlers will eventually be called, from the network queue. Writes can start immediately |
| 294 | // after this. |
| 295 | // The first one (metadataHandler), when the response headers are received. |
| 296 | // The second one (completionHandler), whenever the RPC finishes for any reason. |
murgatroid99 | 54e93d4 | 2015-04-27 09:29:49 -0700 | [diff] [blame] | 297 | - (void)invokeCallWithMetadataHandler:(void(^)(NSDictionary *))metadataHandler |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 298 | completionHandler:(void(^)(NSError *, NSDictionary *))completionHandler { |
murgatroid99 | ca38ddb | 2015-04-29 13:16:42 -0700 | [diff] [blame] | 299 | // TODO(jcanizales): Add error handlers for async failures |
murgatroid99 | 6cc4680 | 2015-04-28 09:35:48 -0700 | [diff] [blame] | 300 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvMetadata alloc] |
| 301 | initWithHandler:metadataHandler]]]; |
| 302 | [_wrappedCall startBatchWithOperations:@[[[GRPCOpRecvStatus alloc] |
| 303 | initWithHandler:completionHandler]]]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | - (void)invokeCall { |
| 307 | __weak GRPCCall *weakSelf = self; |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 308 | [self invokeCallWithMetadataHandler:^(NSDictionary *headers) { |
| 309 | // Response headers received. |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 310 | GRPCCall *strongSelf = weakSelf; |
| 311 | if (strongSelf) { |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 312 | [strongSelf->_responseMetadata addEntriesFromDictionary:headers]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 313 | [strongSelf startNextRead]; |
| 314 | } |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 315 | } completionHandler:^(NSError *error, NSDictionary *trailers) { |
| 316 | GRPCCall *strongSelf = weakSelf; |
| 317 | if (strongSelf) { |
| 318 | [strongSelf->_responseMetadata addEntriesFromDictionary:trailers]; |
| 319 | |
Jorge Canizales | c58a110 | 2015-06-15 19:03:41 -0700 | [diff] [blame] | 320 | if (error) { |
| 321 | NSMutableDictionary *userInfo = |
| 322 | [NSMutableDictionary dictionaryWithDictionary:error.userInfo]; |
| 323 | userInfo[kGRPCStatusMetadataKey] = strongSelf->_responseMetadata; |
| 324 | error = [NSError errorWithDomain:error.domain code:error.code userInfo:userInfo]; |
| 325 | } |
Jorge Canizales | f3a4f2c | 2015-06-12 22:12:50 -0700 | [diff] [blame] | 326 | [strongSelf finishWithError:error]; |
| 327 | } |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 328 | }]; |
| 329 | // Now that the RPC has been initiated, request writes can start. |
| 330 | [_requestWriter startWithWriteable:self]; |
| 331 | } |
| 332 | |
| 333 | #pragma mark GRXWriter implementation |
| 334 | |
| 335 | - (void)startWithWriteable:(id<GRXWriteable>)writeable { |
Jorge Canizales | 6531b2b | 2015-07-18 00:19:14 -0700 | [diff] [blame] | 336 | // Create a retain cycle so that this instance lives until the RPC finishes (or is cancelled). |
| 337 | // This makes RPCs in which the call isn't externally retained possible (as long as it is started |
| 338 | // before being autoreleased). |
| 339 | // Care is taken not to retain self strongly in any of the blocks used in this implementation, so |
| 340 | // that the life of the instance is determined by this retain cycle. |
| 341 | _self = self; |
| 342 | |
| 343 | _responseWriteable = [[GRXConcurrentWriteable alloc] initWithWriteable:writeable]; |
Jorge Canizales | 5e0efd9 | 2015-02-17 18:23:58 -0800 | [diff] [blame] | 344 | [self sendHeaders:_requestMetadata]; |
| 345 | [self invokeCall]; |
| 346 | } |
| 347 | |
| 348 | - (void)setState:(GRXWriterState)newState { |
| 349 | // Manual transitions are only allowed from the started or paused states. |
| 350 | if (_state == GRXWriterStateNotStarted || _state == GRXWriterStateFinished) { |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | switch (newState) { |
| 355 | case GRXWriterStateFinished: |
| 356 | _state = newState; |
| 357 | // Per GRXWriter's contract, setting the state to Finished manually |
| 358 | // means one doesn't wish the writeable to be messaged anymore. |
| 359 | [_responseWriteable cancelSilently]; |
| 360 | _responseWriteable = nil; |
| 361 | return; |
| 362 | case GRXWriterStatePaused: |
| 363 | _state = newState; |
| 364 | return; |
| 365 | case GRXWriterStateStarted: |
| 366 | if (_state == GRXWriterStatePaused) { |
| 367 | _state = newState; |
| 368 | [self startNextRead]; |
| 369 | } |
| 370 | return; |
| 371 | case GRXWriterStateNotStarted: |
| 372 | return; |
| 373 | } |
| 374 | } |
| 375 | @end |