blob: fe3d51da53a58a409621433744fdd028872e64a3 [file] [log] [blame]
murgatroid9930b7d4e2015-04-24 10:36:43 -07001/*
2 *
3 * Copyright 2015, Google Inc.
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
murgatroid9930b7d4e2015-04-24 10:36:43 -070034#import "GRPCWrappedCall.h"
Jorge Canizales3a5253e2015-07-31 23:48:56 -070035
murgatroid9969927d62015-04-24 13:32:48 -070036#import <Foundation/Foundation.h>
37#include <grpc/grpc.h>
38#include <grpc/byte_buffer.h>
39#include <grpc/support/alloc.h>
Jorge Canizales3a5253e2015-07-31 23:48:56 -070040
Jorge Canizalese8543b02015-08-01 17:37:40 -070041#import "GRPCCompletionQueue.h"
42#import "GRPCHost.h"
murgatroid9930b7d4e2015-04-24 10:36:43 -070043#import "NSDictionary+GRPC.h"
44#import "NSData+GRPC.h"
45#import "NSError+GRPC.h"
murgatroid9930b7d4e2015-04-24 10:36:43 -070046
Jorge Canizales8d997752015-06-21 21:43:26 -070047@implementation GRPCOperation {
48@protected
Jorge Canizales88487042015-06-21 21:59:37 -070049 // Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being
50 // initialized to zero.
Jorge Canizales8d997752015-06-21 21:43:26 -070051 grpc_op _op;
52 void(^_handler)();
murgatroid9954e93d42015-04-27 09:29:49 -070053}
54
Jorge Canizales8d997752015-06-21 21:43:26 -070055- (void)finish {
56 if (_handler) {
57 _handler();
58 }
59}
60@end
61
62@implementation GRPCOpSendMetadata
63
murgatroid9954e93d42015-04-27 09:29:49 -070064- (instancetype)init {
65 return [self initWithMetadata:nil handler:nil];
66}
67
Jorge Canizalesf4f150f2015-11-01 22:31:12 -080068- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070069 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070070 _op.op = GRPC_OP_SEND_INITIAL_METADATA;
71 _op.data.send_initial_metadata.count = metadata.count;
72 _op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray;
murgatroid9954e93d42015-04-27 09:29:49 -070073 _handler = handler;
74 }
75 return self;
76}
77
murgatroid9933655f92015-04-29 11:15:35 -070078- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -070079 gpr_free(_op.data.send_initial_metadata.metadata);
murgatroid9933655f92015-04-29 11:15:35 -070080}
81
murgatroid9954e93d42015-04-27 09:29:49 -070082@end
83
Jorge Canizales8d997752015-06-21 21:43:26 -070084@implementation GRPCOpSendMessage
murgatroid9954e93d42015-04-27 09:29:49 -070085
86- (instancetype)init {
87 return [self initWithMessage:nil handler:nil];
88}
89
Jorge Canizales8d997752015-06-21 21:43:26 -070090- (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070091 if (!message) {
murgatroid992101a482015-04-29 11:42:26 -070092 [NSException raise:NSInvalidArgumentException format:@"message cannot be nil"];
murgatroid9954e93d42015-04-27 09:29:49 -070093 }
94 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070095 _op.op = GRPC_OP_SEND_MESSAGE;
96 _op.data.send_message = message.grpc_byteBuffer;
murgatroid9954e93d42015-04-27 09:29:49 -070097 _handler = handler;
98 }
99 return self;
100}
101
murgatroid9933655f92015-04-29 11:15:35 -0700102- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700103 gpr_free(_op.data.send_message);
murgatroid9933655f92015-04-29 11:15:35 -0700104}
105
murgatroid9954e93d42015-04-27 09:29:49 -0700106@end
107
Jorge Canizales8d997752015-06-21 21:43:26 -0700108@implementation GRPCOpSendClose
murgatroid9954e93d42015-04-27 09:29:49 -0700109
110- (instancetype)init {
111 return [self initWithHandler:nil];
112}
113
Jorge Canizales8d997752015-06-21 21:43:26 -0700114- (instancetype)initWithHandler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700115 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700116 _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
murgatroid9954e93d42015-04-27 09:29:49 -0700117 _handler = handler;
118 }
119 return self;
120}
121
murgatroid9954e93d42015-04-27 09:29:49 -0700122@end
123
Jorge Canizales8d997752015-06-21 21:43:26 -0700124@implementation GRPCOpRecvMetadata {
125 grpc_metadata_array _headers;
murgatroid9954e93d42015-04-27 09:29:49 -0700126}
127
128- (instancetype) init {
129 return [self initWithHandler:nil];
130}
131
132- (instancetype) initWithHandler:(void (^)(NSDictionary *))handler {
133 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700134 _op.op = GRPC_OP_RECV_INITIAL_METADATA;
135 grpc_metadata_array_init(&_headers);
136 _op.data.recv_initial_metadata = &_headers;
137 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700138 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700139 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700140 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700141 __strong typeof(self) strongSelf = weakSelf;
142 NSDictionary *metadata = [NSDictionary
143 grpc_dictionaryFromMetadataArray:strongSelf->_headers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700144 handler(metadata);
145 };
146 }
murgatroid9954e93d42015-04-27 09:29:49 -0700147 }
148 return self;
149}
150
murgatroid9933655f92015-04-29 11:15:35 -0700151- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700152 grpc_metadata_array_destroy(&_headers);
murgatroid9933655f92015-04-29 11:15:35 -0700153}
154
murgatroid9954e93d42015-04-27 09:29:49 -0700155@end
156
157@implementation GRPCOpRecvMessage{
Jorge Canizales8d997752015-06-21 21:43:26 -0700158 grpc_byte_buffer *_receivedMessage;
murgatroid9954e93d42015-04-27 09:29:49 -0700159}
160
161- (instancetype)init {
162 return [self initWithHandler:nil];
163}
164
murgatroid996cc46802015-04-28 09:35:48 -0700165- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700166 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700167 _op.op = GRPC_OP_RECV_MESSAGE;
168 _op.data.recv_message = &_receivedMessage;
169 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700170 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700171 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700172 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700173 __strong typeof(self) strongSelf = weakSelf;
174 handler(strongSelf->_receivedMessage);
Jorge Canizales8d997752015-06-21 21:43:26 -0700175 };
176 }
murgatroid9954e93d42015-04-27 09:29:49 -0700177 }
178 return self;
179}
180
murgatroid9954e93d42015-04-27 09:29:49 -0700181@end
182
183@implementation GRPCOpRecvStatus{
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700184 grpc_status_code _statusCode;
185 char *_details;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700186 size_t _detailsCapacity;
Jorge Canizales8d997752015-06-21 21:43:26 -0700187 grpc_metadata_array _trailers;
murgatroid9954e93d42015-04-27 09:29:49 -0700188}
189
190- (instancetype) init {
191 return [self initWithHandler:nil];
192}
193
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700194- (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700195 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700196 _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT;
197 _op.data.recv_status_on_client.status = &_statusCode;
198 _op.data.recv_status_on_client.status_details = &_details;
199 _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity;
200 grpc_metadata_array_init(&_trailers);
201 _op.data.recv_status_on_client.trailing_metadata = &_trailers;
202 if (handler) {
murgatroid99231103b2015-06-25 10:14:09 -0700203 // Prevent reference cycle with _handler
murgatroid99dbda9692015-06-24 16:55:55 -0700204 __weak typeof(self) weakSelf = self;
Jorge Canizales8d997752015-06-21 21:43:26 -0700205 _handler = ^{
murgatroid99dbda9692015-06-24 16:55:55 -0700206 __strong typeof(self) strongSelf = weakSelf;
207 NSError *error = [NSError grpc_errorFromStatusCode:strongSelf->_statusCode
208 details:strongSelf->_details];
209 NSDictionary *trailers = [NSDictionary
210 grpc_dictionaryFromMetadataArray:strongSelf->_trailers];
Jorge Canizales8d997752015-06-21 21:43:26 -0700211 handler(error, trailers);
212 };
213 }
murgatroid9954e93d42015-04-27 09:29:49 -0700214 }
215 return self;
216}
217
murgatroid9933655f92015-04-29 11:15:35 -0700218- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700219 grpc_metadata_array_destroy(&_trailers);
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700220 gpr_free(_details);
murgatroid9933655f92015-04-29 11:15:35 -0700221}
222
murgatroid9954e93d42015-04-27 09:29:49 -0700223@end
224
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700225#pragma mark GRPCWrappedCall
226
Jorge Canizalese8543b02015-08-01 17:37:40 -0700227@implementation GRPCWrappedCall {
228 GRPCCompletionQueue *_queue;
murgatroid9969927d62015-04-24 13:32:48 -0700229 grpc_call *_call;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700230}
231
232- (instancetype)init {
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700233 return [self initWithHost:nil path:nil];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700234}
235
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700236- (instancetype)initWithHost:(NSString *)host
237 path:(NSString *)path {
238 if (!path || !host) {
murgatroid996cc46802015-04-28 09:35:48 -0700239 [NSException raise:NSInvalidArgumentException
Jorge Canizales3a5253e2015-07-31 23:48:56 -0700240 format:@"path and host cannot be nil."];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700241 }
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700242
murgatroid9930b7d4e2015-04-24 10:36:43 -0700243 if (self = [super init]) {
244 static dispatch_once_t initialization;
245 dispatch_once(&initialization, ^{
246 grpc_init();
247 });
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700248
Jorge Canizalese8543b02015-08-01 17:37:40 -0700249 // Each completion queue consumes one thread. There's a trade to be made between creating and
250 // consuming too many threads and having contention of multiple calls in a single completion
251 // queue. Currently we favor latency and use one per call.
252 _queue = [GRPCCompletionQueue completionQueue];
253
254 _call = [[GRPCHost hostWithAddress:host] unmanagedCallWithPath:path completionQueue:_queue];
murgatroid9969927d62015-04-24 13:32:48 -0700255 if (_call == NULL) {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700256 return nil;
257 }
258 }
259 return self;
260}
261
murgatroid9954e93d42015-04-27 09:29:49 -0700262- (void)startBatchWithOperations:(NSArray *)operations {
263 [self startBatchWithOperations:operations errorHandler:nil];
murgatroid9969927d62015-04-24 13:32:48 -0700264}
265
murgatroid9954e93d42015-04-27 09:29:49 -0700266- (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler {
murgatroid9969927d62015-04-24 13:32:48 -0700267 size_t nops = operations.count;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700268 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op));
murgatroid9954e93d42015-04-27 09:29:49 -0700269 size_t i = 0;
Jorge Canizales8d997752015-06-21 21:43:26 -0700270 for (GRPCOperation *operation in operations) {
271 ops_array[i++] = operation.op;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700272 }
murgatroid996cc46802015-04-28 09:35:48 -0700273 grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
murgatroid996b542442015-05-08 10:40:01 -0700274 (__bridge_retained void *)(^(bool success){
275 if (!success) {
murgatroid9969927d62015-04-24 13:32:48 -0700276 if (errorHandler) {
277 errorHandler();
278 } else {
murgatroid99def47aa2015-04-29 10:28:26 -0700279 return;
murgatroid9969927d62015-04-24 13:32:48 -0700280 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700281 }
Jorge Canizales8d997752015-06-21 21:43:26 -0700282 for (GRPCOperation *operation in operations) {
murgatroid992101a482015-04-29 11:42:26 -0700283 [operation finish];
murgatroid9969927d62015-04-24 13:32:48 -0700284 }
Jorge Canizalesb3584aa2015-08-08 15:01:24 -0700285 }), NULL);
Jorge Canizales8d997752015-06-21 21:43:26 -0700286 gpr_free(ops_array);
287
murgatroid9930b7d4e2015-04-24 10:36:43 -0700288 if (error != GRPC_CALL_OK) {
murgatroid996a084f42015-04-29 10:18:05 -0700289 [NSException raise:NSInternalInconsistencyException
Jorge Canizalesbae38d92015-06-20 20:21:25 -0700290 format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i",
291 error];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700292 }
293}
294
295- (void)cancel {
Jorge Canizalesb3584aa2015-08-08 15:01:24 -0700296 grpc_call_cancel(_call, NULL);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700297}
298
299- (void)dealloc {
murgatroid9969927d62015-04-24 13:32:48 -0700300 grpc_call_destroy(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700301}
302
Jorge Canizalesbd993df2015-08-01 02:51:22 -0700303@end