blob: 4ccd5723c61f126232f0673e717388cf8411378e [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"
murgatroid9969927d62015-04-24 13:32:48 -070035#import <Foundation/Foundation.h>
36#include <grpc/grpc.h>
37#include <grpc/byte_buffer.h>
38#include <grpc/support/alloc.h>
murgatroid9930b7d4e2015-04-24 10:36:43 -070039#import "GRPCCompletionQueue.h"
40#import "NSDictionary+GRPC.h"
41#import "NSData+GRPC.h"
42#import "NSError+GRPC.h"
murgatroid9930b7d4e2015-04-24 10:36:43 -070043
Jorge Canizales8d997752015-06-21 21:43:26 -070044@implementation GRPCOperation {
45@protected
Jorge Canizales88487042015-06-21 21:59:37 -070046 // Most operation subclasses don't set any flags in the grpc_op, and rely on the flag member being
47 // initialized to zero.
Jorge Canizales8d997752015-06-21 21:43:26 -070048 grpc_op _op;
49 void(^_handler)();
murgatroid9954e93d42015-04-27 09:29:49 -070050}
51
Jorge Canizales8d997752015-06-21 21:43:26 -070052- (void)finish {
53 if (_handler) {
54 _handler();
55 }
56}
57@end
58
59@implementation GRPCOpSendMetadata
60
murgatroid9954e93d42015-04-27 09:29:49 -070061- (instancetype)init {
62 return [self initWithMetadata:nil handler:nil];
63}
64
Jorge Canizales8d997752015-06-21 21:43:26 -070065- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070066 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070067 _op.op = GRPC_OP_SEND_INITIAL_METADATA;
68 _op.data.send_initial_metadata.count = metadata.count;
69 _op.data.send_initial_metadata.metadata = metadata.grpc_metadataArray;
murgatroid9954e93d42015-04-27 09:29:49 -070070 _handler = handler;
71 }
72 return self;
73}
74
murgatroid9933655f92015-04-29 11:15:35 -070075- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -070076 gpr_free(_op.data.send_initial_metadata.metadata);
murgatroid9933655f92015-04-29 11:15:35 -070077}
78
murgatroid9954e93d42015-04-27 09:29:49 -070079@end
80
Jorge Canizales8d997752015-06-21 21:43:26 -070081@implementation GRPCOpSendMessage
murgatroid9954e93d42015-04-27 09:29:49 -070082
83- (instancetype)init {
84 return [self initWithMessage:nil handler:nil];
85}
86
Jorge Canizales8d997752015-06-21 21:43:26 -070087- (instancetype)initWithMessage:(NSData *)message handler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -070088 if (!message) {
murgatroid992101a482015-04-29 11:42:26 -070089 [NSException raise:NSInvalidArgumentException format:@"message cannot be nil"];
murgatroid9954e93d42015-04-27 09:29:49 -070090 }
91 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -070092 _op.op = GRPC_OP_SEND_MESSAGE;
93 _op.data.send_message = message.grpc_byteBuffer;
murgatroid9954e93d42015-04-27 09:29:49 -070094 _handler = handler;
95 }
96 return self;
97}
98
murgatroid9933655f92015-04-29 11:15:35 -070099- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700100 gpr_free(_op.data.send_message);
murgatroid9933655f92015-04-29 11:15:35 -0700101}
102
murgatroid9954e93d42015-04-27 09:29:49 -0700103@end
104
Jorge Canizales8d997752015-06-21 21:43:26 -0700105@implementation GRPCOpSendClose
murgatroid9954e93d42015-04-27 09:29:49 -0700106
107- (instancetype)init {
108 return [self initWithHandler:nil];
109}
110
Jorge Canizales8d997752015-06-21 21:43:26 -0700111- (instancetype)initWithHandler:(void (^)())handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700112 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700113 _op.op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
murgatroid9954e93d42015-04-27 09:29:49 -0700114 _handler = handler;
115 }
116 return self;
117}
118
murgatroid9954e93d42015-04-27 09:29:49 -0700119@end
120
Jorge Canizales8d997752015-06-21 21:43:26 -0700121@implementation GRPCOpRecvMetadata {
122 grpc_metadata_array _headers;
murgatroid9954e93d42015-04-27 09:29:49 -0700123}
124
125- (instancetype) init {
126 return [self initWithHandler:nil];
127}
128
129- (instancetype) initWithHandler:(void (^)(NSDictionary *))handler {
130 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700131 _op.op = GRPC_OP_RECV_INITIAL_METADATA;
132 grpc_metadata_array_init(&_headers);
133 _op.data.recv_initial_metadata = &_headers;
134 if (handler) {
135 _handler = ^{
136 NSDictionary *metadata = [NSDictionary grpc_dictionaryFromMetadataArray:_headers];
137 handler(metadata);
138 };
139 }
murgatroid9954e93d42015-04-27 09:29:49 -0700140 }
141 return self;
142}
143
murgatroid9933655f92015-04-29 11:15:35 -0700144- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700145 grpc_metadata_array_destroy(&_headers);
murgatroid9933655f92015-04-29 11:15:35 -0700146}
147
murgatroid9954e93d42015-04-27 09:29:49 -0700148@end
149
150@implementation GRPCOpRecvMessage{
Jorge Canizales8d997752015-06-21 21:43:26 -0700151 grpc_byte_buffer *_receivedMessage;
murgatroid9954e93d42015-04-27 09:29:49 -0700152}
153
154- (instancetype)init {
155 return [self initWithHandler:nil];
156}
157
murgatroid996cc46802015-04-28 09:35:48 -0700158- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700159 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700160 _op.op = GRPC_OP_RECV_MESSAGE;
161 _op.data.recv_message = &_receivedMessage;
162 if (handler) {
163 _handler = ^{
164 handler(_receivedMessage);
165 };
166 }
murgatroid9954e93d42015-04-27 09:29:49 -0700167 }
168 return self;
169}
170
murgatroid9954e93d42015-04-27 09:29:49 -0700171@end
172
173@implementation GRPCOpRecvStatus{
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700174 grpc_status_code _statusCode;
175 char *_details;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700176 size_t _detailsCapacity;
Jorge Canizales8d997752015-06-21 21:43:26 -0700177 grpc_metadata_array _trailers;
murgatroid9954e93d42015-04-27 09:29:49 -0700178}
179
180- (instancetype) init {
181 return [self initWithHandler:nil];
182}
183
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700184- (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700185 if (self = [super init]) {
Jorge Canizales8d997752015-06-21 21:43:26 -0700186 _op.op = GRPC_OP_RECV_STATUS_ON_CLIENT;
187 _op.data.recv_status_on_client.status = &_statusCode;
188 _op.data.recv_status_on_client.status_details = &_details;
189 _op.data.recv_status_on_client.status_details_capacity = &_detailsCapacity;
190 grpc_metadata_array_init(&_trailers);
191 _op.data.recv_status_on_client.trailing_metadata = &_trailers;
192 if (handler) {
193 _handler = ^{
194 NSError *error = [NSError grpc_errorFromStatusCode:_statusCode details:_details];
195 NSDictionary *trailers = [NSDictionary grpc_dictionaryFromMetadataArray:_trailers];
196 handler(error, trailers);
197 };
198 }
murgatroid9954e93d42015-04-27 09:29:49 -0700199 }
200 return self;
201}
202
murgatroid9933655f92015-04-29 11:15:35 -0700203- (void)dealloc {
Jorge Canizales8d997752015-06-21 21:43:26 -0700204 grpc_metadata_array_destroy(&_trailers);
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700205 gpr_free(_details);
murgatroid9933655f92015-04-29 11:15:35 -0700206}
207
murgatroid9954e93d42015-04-27 09:29:49 -0700208@end
209
murgatroid9930b7d4e2015-04-24 10:36:43 -0700210@implementation GRPCWrappedCall{
murgatroid9969927d62015-04-24 13:32:48 -0700211 grpc_call *_call;
212 GRPCCompletionQueue *_queue;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700213}
214
215- (instancetype)init {
murgatroid99463a7a82015-04-24 11:41:43 -0700216 return [self initWithChannel:nil method:nil host:nil];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700217}
218
murgatroid996cc46802015-04-28 09:35:48 -0700219- (instancetype)initWithChannel:(GRPCChannel *)channel
220 method:(NSString *)method
221 host:(NSString *)host {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700222 if (!channel || !method || !host) {
murgatroid996cc46802015-04-28 09:35:48 -0700223 [NSException raise:NSInvalidArgumentException
224 format:@"channel, method, and host cannot be nil."];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700225 }
226
227 if (self = [super init]) {
228 static dispatch_once_t initialization;
229 dispatch_once(&initialization, ^{
230 grpc_init();
231 });
232
murgatroid9969927d62015-04-24 13:32:48 -0700233 _queue = [GRPCCompletionQueue completionQueue];
murgatroid99fe2c0c62015-04-29 10:59:43 -0700234 if (!_queue) {
235 return nil;
236 }
murgatroid996cc46802015-04-28 09:35:48 -0700237 _call = grpc_channel_create_call(channel.unmanagedChannel, _queue.unmanagedQueue,
238 method.UTF8String, host.UTF8String, gpr_inf_future);
murgatroid9969927d62015-04-24 13:32:48 -0700239 if (_call == NULL) {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700240 return nil;
241 }
242 }
243 return self;
244}
245
murgatroid9954e93d42015-04-27 09:29:49 -0700246- (void)startBatchWithOperations:(NSArray *)operations {
247 [self startBatchWithOperations:operations errorHandler:nil];
murgatroid9969927d62015-04-24 13:32:48 -0700248}
249
murgatroid9954e93d42015-04-27 09:29:49 -0700250- (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler {
murgatroid9969927d62015-04-24 13:32:48 -0700251 size_t nops = operations.count;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700252 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op));
murgatroid9954e93d42015-04-27 09:29:49 -0700253 size_t i = 0;
Jorge Canizales8d997752015-06-21 21:43:26 -0700254 for (GRPCOperation *operation in operations) {
255 ops_array[i++] = operation.op;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700256 }
murgatroid996cc46802015-04-28 09:35:48 -0700257 grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
murgatroid996b542442015-05-08 10:40:01 -0700258 (__bridge_retained void *)(^(bool success){
259 if (!success) {
murgatroid9969927d62015-04-24 13:32:48 -0700260 if (errorHandler) {
261 errorHandler();
262 } else {
murgatroid99def47aa2015-04-29 10:28:26 -0700263 return;
murgatroid9969927d62015-04-24 13:32:48 -0700264 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700265 }
Jorge Canizales8d997752015-06-21 21:43:26 -0700266 for (GRPCOperation *operation in operations) {
murgatroid992101a482015-04-29 11:42:26 -0700267 [operation finish];
murgatroid9969927d62015-04-24 13:32:48 -0700268 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700269 }));
Jorge Canizales8d997752015-06-21 21:43:26 -0700270 gpr_free(ops_array);
271
murgatroid9930b7d4e2015-04-24 10:36:43 -0700272 if (error != GRPC_CALL_OK) {
murgatroid996a084f42015-04-29 10:18:05 -0700273 [NSException raise:NSInternalInconsistencyException
Jorge Canizalesbae38d92015-06-20 20:21:25 -0700274 format:@"A precondition for calling grpc_call_start_batch wasn't met. Error %i",
275 error];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700276 }
277}
278
279- (void)cancel {
murgatroid9969927d62015-04-24 13:32:48 -0700280 grpc_call_cancel(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700281}
282
283- (void)dealloc {
murgatroid9969927d62015-04-24 13:32:48 -0700284 grpc_call_destroy(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700285}
286
287@end