blob: c79daf434eb95ca40be2121f0f6cbbdbadaeecb9 [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
murgatroid9954e93d42015-04-27 09:29:49 -070044@implementation GRPCOpSendMetadata{
45 void(^_handler)(void);
46 grpc_metadata *_send_metadata;
47 size_t _count;
48}
49
50- (instancetype)init {
51 return [self initWithMetadata:nil handler:nil];
52}
53
54- (instancetype)initWithMetadata:(NSDictionary *)metadata handler:(void (^)(void))handler {
55 if (self = [super init]) {
murgatroid996a084f42015-04-29 10:18:05 -070056 _send_metadata = [metadata grpc_getMetadataArray];
57 _count = metadata.count;
murgatroid9954e93d42015-04-27 09:29:49 -070058 _handler = handler;
59 }
60 return self;
61}
62
63- (void)getOp:(grpc_op *)op {
64 op->op = GRPC_OP_SEND_INITIAL_METADATA;
65 op->data.send_initial_metadata.count = _count;
murgatroid996a084f42015-04-29 10:18:05 -070066 op->data.send_initial_metadata.metadata = _send_metadata;
murgatroid9954e93d42015-04-27 09:29:49 -070067}
68
69- (void (^)(void))opProcessor {
70 return ^{
71 gpr_free(_send_metadata);
72 if (_handler) {
73 _handler();
74 }
75 };
76}
77
78@end
79
80@implementation GRPCOpSendMessage{
81 void(^_handler)(void);
82 grpc_byte_buffer *_byte_buffer;
83}
84
85- (instancetype)init {
86 return [self initWithMessage:nil handler:nil];
87}
88
89- (instancetype)initWithMessage:(NSData *)message handler:(void (^)(void))handler {
90 if (!message) {
91 [NSException raise:NSInvalidArgumentException format:@"message cannot be null"];
92 }
93 if (self = [super init]) {
94 _byte_buffer = [message grpc_byteBuffer];
95 _handler = handler;
96 }
97 return self;
98}
99
100- (void)getOp:(grpc_op *)op {
101 op->op = GRPC_OP_SEND_MESSAGE;
102 op->data.send_message = _byte_buffer;
103}
104
105- (void (^)(void))opProcessor {
106 return ^{
107 gpr_free(_byte_buffer);
108 if (_handler) {
109 _handler();
110 }
111 };
112}
113
114@end
115
116@implementation GRPCOpSendClose{
117 void(^_handler)(void);
118}
119
120- (instancetype)init {
121 return [self initWithHandler:nil];
122}
123
124- (instancetype)initWithHandler:(void (^)(void))handler {
125 if (self = [super init]) {
126 _handler = handler;
127 }
128 return self;
129}
130
131- (void)getOp:(grpc_op *)op {
132 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
133}
134
135- (void (^)(void))opProcessor {
136 return ^{
137 if (_handler) {
138 _handler();
139 }
140 };
141}
142
143@end
144
145@implementation GRPCOpRecvMetadata{
146 void(^_handler)(NSDictionary *);
murgatroid996a084f42015-04-29 10:18:05 -0700147 grpc_metadata_array _recv_initial_metadata;
murgatroid9954e93d42015-04-27 09:29:49 -0700148}
149
150- (instancetype) init {
151 return [self initWithHandler:nil];
152}
153
154- (instancetype) initWithHandler:(void (^)(NSDictionary *))handler {
155 if (self = [super init]) {
156 _handler = handler;
murgatroid996a084f42015-04-29 10:18:05 -0700157 grpc_metadata_array_init(&_recv_initial_metadata);
murgatroid9954e93d42015-04-27 09:29:49 -0700158 }
159 return self;
160}
161
162- (void)getOp:(grpc_op *)op {
163 op->op = GRPC_OP_RECV_INITIAL_METADATA;
murgatroid996a084f42015-04-29 10:18:05 -0700164 op->data.recv_initial_metadata = &_recv_initial_metadata;
murgatroid9954e93d42015-04-27 09:29:49 -0700165}
166
167- (void (^)(void))opProcessor {
168 return ^{
murgatroid996cc46802015-04-28 09:35:48 -0700169 NSDictionary *metadata = [NSDictionary
murgatroid996a084f42015-04-29 10:18:05 -0700170 grpc_dictionaryFromMetadata:_recv_initial_metadata.metadata
171 count:_recv_initial_metadata.count];
172 grpc_metadata_array_destroy(&_recv_initial_metadata);
murgatroid9954e93d42015-04-27 09:29:49 -0700173 if (_handler) {
174 _handler(metadata);
175 }
176 };
177}
178
179@end
180
181@implementation GRPCOpRecvMessage{
murgatroid996cc46802015-04-28 09:35:48 -0700182 void(^_handler)(grpc_byte_buffer *);
murgatroid996a084f42015-04-29 10:18:05 -0700183 grpc_byte_buffer *_recv_message;
murgatroid9954e93d42015-04-27 09:29:49 -0700184}
185
186- (instancetype)init {
187 return [self initWithHandler:nil];
188}
189
murgatroid996cc46802015-04-28 09:35:48 -0700190- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700191 if (self = [super init]) {
192 _handler = handler;
murgatroid9954e93d42015-04-27 09:29:49 -0700193 }
194 return self;
195}
196
197- (void)getOp:(grpc_op *)op {
198 op->op = GRPC_OP_RECV_MESSAGE;
murgatroid996a084f42015-04-29 10:18:05 -0700199 op->data.recv_message = &_recv_message;
murgatroid9954e93d42015-04-27 09:29:49 -0700200}
201
202- (void (^)(void))opProcessor {
203 return ^{
murgatroid9954e93d42015-04-27 09:29:49 -0700204 if (_handler) {
murgatroid996a084f42015-04-29 10:18:05 -0700205 _handler(_recv_message);
murgatroid9954e93d42015-04-27 09:29:49 -0700206 }
207 };
208}
209
210@end
211
212@implementation GRPCOpRecvStatus{
213 void(^_handler)(NSError *);
murgatroid996a084f42015-04-29 10:18:05 -0700214 size_t _details_capacity;
215 grpc_status _status;
murgatroid9954e93d42015-04-27 09:29:49 -0700216}
217
218- (instancetype) init {
219 return [self initWithHandler:nil];
220}
221
222- (instancetype) initWithHandler:(void (^)(NSError *))handler {
223 if (self = [super init]) {
224 _handler = handler;
murgatroid996a084f42015-04-29 10:18:05 -0700225 _status.details = NULL;
226 _details_capacity = 0;
227 grpc_metadata_array_init(&_status.metadata);
murgatroid9954e93d42015-04-27 09:29:49 -0700228 }
229 return self;
230}
231
232- (void)getOp:(grpc_op *)op {
233 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
murgatroid996a084f42015-04-29 10:18:05 -0700234 op->data.recv_status_on_client.status = &_status.status;
235 op->data.recv_status_on_client.status_details = &_status.details;
236 op->data.recv_status_on_client.status_details_capacity = &_details_capacity;
237 op->data.recv_status_on_client.trailing_metadata = &_status.metadata;
murgatroid9954e93d42015-04-27 09:29:49 -0700238}
239
240- (void (^)(void))opProcessor {
241 return ^{
murgatroid9954e93d42015-04-27 09:29:49 -0700242 if (_handler) {
murgatroid996a084f42015-04-29 10:18:05 -0700243 NSError *error = [NSError grpc_errorFromStatus:&_status];
244 grpc_metadata_array_destroy(&_status.metadata);
245 _handler(error);
murgatroid9954e93d42015-04-27 09:29:49 -0700246 }
247 };
248}
249
250@end
251
murgatroid9930b7d4e2015-04-24 10:36:43 -0700252@implementation GRPCWrappedCall{
murgatroid9969927d62015-04-24 13:32:48 -0700253 grpc_call *_call;
254 GRPCCompletionQueue *_queue;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700255}
256
257- (instancetype)init {
murgatroid99463a7a82015-04-24 11:41:43 -0700258 return [self initWithChannel:nil method:nil host:nil];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700259}
260
murgatroid996cc46802015-04-28 09:35:48 -0700261- (instancetype)initWithChannel:(GRPCChannel *)channel
262 method:(NSString *)method
263 host:(NSString *)host {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700264 if (!channel || !method || !host) {
murgatroid996cc46802015-04-28 09:35:48 -0700265 [NSException raise:NSInvalidArgumentException
266 format:@"channel, method, and host cannot be nil."];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700267 }
268
269 if (self = [super init]) {
270 static dispatch_once_t initialization;
271 dispatch_once(&initialization, ^{
272 grpc_init();
273 });
274
murgatroid9969927d62015-04-24 13:32:48 -0700275 _queue = [GRPCCompletionQueue completionQueue];
murgatroid996cc46802015-04-28 09:35:48 -0700276 _call = grpc_channel_create_call(channel.unmanagedChannel, _queue.unmanagedQueue,
277 method.UTF8String, host.UTF8String, gpr_inf_future);
murgatroid9969927d62015-04-24 13:32:48 -0700278 if (_call == NULL) {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700279 return nil;
280 }
281 }
282 return self;
283}
284
murgatroid9954e93d42015-04-27 09:29:49 -0700285- (void)startBatchWithOperations:(NSArray *)operations {
286 [self startBatchWithOperations:operations errorHandler:nil];
murgatroid9969927d62015-04-24 13:32:48 -0700287}
288
murgatroid9954e93d42015-04-27 09:29:49 -0700289- (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler {
290 NSMutableArray *opProcessors = [NSMutableArray array];
murgatroid9969927d62015-04-24 13:32:48 -0700291 size_t nops = operations.count;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700292 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op));
murgatroid9954e93d42015-04-27 09:29:49 -0700293 size_t i = 0;
294 for (id op in operations) {
295 [op getOp:&ops_array[i]];
296 [opProcessors addObject:[op opProcessor]];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700297 }
murgatroid996cc46802015-04-28 09:35:48 -0700298 grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
299 (__bridge_retained void *)(^(grpc_op_error error){
murgatroid9930b7d4e2015-04-24 10:36:43 -0700300 if (error != GRPC_OP_OK) {
murgatroid9969927d62015-04-24 13:32:48 -0700301 if (errorHandler) {
302 errorHandler();
303 } else {
murgatroid99def47aa2015-04-29 10:28:26 -0700304 return;
murgatroid9969927d62015-04-24 13:32:48 -0700305 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700306 }
murgatroid9954e93d42015-04-27 09:29:49 -0700307 for (void(^processor)(void) in opProcessors) {
308 processor();
murgatroid9969927d62015-04-24 13:32:48 -0700309 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700310 }));
murgatroid9954e93d42015-04-27 09:29:49 -0700311
murgatroid9930b7d4e2015-04-24 10:36:43 -0700312 if (error != GRPC_CALL_OK) {
murgatroid996a084f42015-04-29 10:18:05 -0700313 [NSException raise:NSInternalInconsistencyException
314 format:@"A precondition for calling grpc_call_start_batch wasn't met"];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700315 }
316}
317
318- (void)cancel {
murgatroid9969927d62015-04-24 13:32:48 -0700319 grpc_call_cancel(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700320}
321
322- (void)dealloc {
murgatroid9969927d62015-04-24 13:32:48 -0700323 grpc_call_destroy(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700324}
325
326@end