blob: 8db62264538b9e957a2db53e291f87c373fc3d7a [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]) {
murgatroid99a571ceb2015-04-29 10:58:24 -070056 _send_metadata = [metadata grpc_metadataArray];
murgatroid996a084f42015-04-29 10:18:05 -070057 _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
murgatroid992101a482015-04-29 11:42:26 -070069- (void)finish {
70 if (_handler) {
71 _handler();
72 }
murgatroid9954e93d42015-04-27 09:29:49 -070073}
74
murgatroid9933655f92015-04-29 11:15:35 -070075- (void)dealloc {
76 gpr_free(_send_metadata);
77}
78
murgatroid9954e93d42015-04-27 09:29:49 -070079@end
80
81@implementation GRPCOpSendMessage{
82 void(^_handler)(void);
83 grpc_byte_buffer *_byte_buffer;
84}
85
86- (instancetype)init {
87 return [self initWithMessage:nil handler:nil];
88}
89
90- (instancetype)initWithMessage:(NSData *)message handler:(void (^)(void))handler {
91 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]) {
95 _byte_buffer = [message grpc_byteBuffer];
96 _handler = handler;
97 }
98 return self;
99}
100
101- (void)getOp:(grpc_op *)op {
102 op->op = GRPC_OP_SEND_MESSAGE;
103 op->data.send_message = _byte_buffer;
104}
105
murgatroid992101a482015-04-29 11:42:26 -0700106- (void)finish {
107 if (_handler) {
108 _handler();
109 }
murgatroid9954e93d42015-04-27 09:29:49 -0700110}
111
murgatroid9933655f92015-04-29 11:15:35 -0700112- (void)dealloc {
113 gpr_free(_byte_buffer);
114}
115
murgatroid9954e93d42015-04-27 09:29:49 -0700116@end
117
118@implementation GRPCOpSendClose{
119 void(^_handler)(void);
120}
121
122- (instancetype)init {
123 return [self initWithHandler:nil];
124}
125
126- (instancetype)initWithHandler:(void (^)(void))handler {
127 if (self = [super init]) {
128 _handler = handler;
129 }
130 return self;
131}
132
133- (void)getOp:(grpc_op *)op {
134 op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT;
135}
136
murgatroid992101a482015-04-29 11:42:26 -0700137- (void)finish {
138 if (_handler) {
139 _handler();
140 }
murgatroid9954e93d42015-04-27 09:29:49 -0700141}
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
murgatroid992101a482015-04-29 11:42:26 -0700167- (void)finish {
168 NSDictionary *metadata = [NSDictionary
169 grpc_dictionaryFromMetadata:_recv_initial_metadata.metadata
170 count:_recv_initial_metadata.count];
171 if (_handler) {
172 _handler(metadata);
173 }
murgatroid9954e93d42015-04-27 09:29:49 -0700174}
175
murgatroid9933655f92015-04-29 11:15:35 -0700176- (void)dealloc {
177 grpc_metadata_array_destroy(&_recv_initial_metadata);
178}
179
murgatroid9954e93d42015-04-27 09:29:49 -0700180@end
181
182@implementation GRPCOpRecvMessage{
murgatroid996cc46802015-04-28 09:35:48 -0700183 void(^_handler)(grpc_byte_buffer *);
murgatroid996a084f42015-04-29 10:18:05 -0700184 grpc_byte_buffer *_recv_message;
murgatroid9954e93d42015-04-27 09:29:49 -0700185}
186
187- (instancetype)init {
188 return [self initWithHandler:nil];
189}
190
murgatroid996cc46802015-04-28 09:35:48 -0700191- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700192 if (self = [super init]) {
193 _handler = handler;
murgatroid9954e93d42015-04-27 09:29:49 -0700194 }
195 return self;
196}
197
198- (void)getOp:(grpc_op *)op {
199 op->op = GRPC_OP_RECV_MESSAGE;
murgatroid996a084f42015-04-29 10:18:05 -0700200 op->data.recv_message = &_recv_message;
murgatroid9954e93d42015-04-27 09:29:49 -0700201}
202
murgatroid992101a482015-04-29 11:42:26 -0700203- (void)finish {
204 if (_handler) {
205 _handler(_recv_message);
206 }
murgatroid9954e93d42015-04-27 09:29:49 -0700207}
208
209@end
210
211@implementation GRPCOpRecvStatus{
212 void(^_handler)(NSError *);
murgatroid996a084f42015-04-29 10:18:05 -0700213 size_t _details_capacity;
214 grpc_status _status;
murgatroid9954e93d42015-04-27 09:29:49 -0700215}
216
217- (instancetype) init {
218 return [self initWithHandler:nil];
219}
220
221- (instancetype) initWithHandler:(void (^)(NSError *))handler {
222 if (self = [super init]) {
223 _handler = handler;
murgatroid996a084f42015-04-29 10:18:05 -0700224 _status.details = NULL;
225 _details_capacity = 0;
226 grpc_metadata_array_init(&_status.metadata);
murgatroid9954e93d42015-04-27 09:29:49 -0700227 }
228 return self;
229}
230
231- (void)getOp:(grpc_op *)op {
232 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
murgatroid996a084f42015-04-29 10:18:05 -0700233 op->data.recv_status_on_client.status = &_status.status;
234 op->data.recv_status_on_client.status_details = &_status.details;
235 op->data.recv_status_on_client.status_details_capacity = &_details_capacity;
236 op->data.recv_status_on_client.trailing_metadata = &_status.metadata;
murgatroid9954e93d42015-04-27 09:29:49 -0700237}
238
murgatroid992101a482015-04-29 11:42:26 -0700239- (void)opProcessor {
240 if (_handler) {
241 NSError *error = [NSError grpc_errorFromStatus:&_status];
242 _handler(error);
243 }
murgatroid9954e93d42015-04-27 09:29:49 -0700244}
245
murgatroid9933655f92015-04-29 11:15:35 -0700246- (void)dealloc {
247 grpc_metadata_array_destroy(&_status.metadata);
248}
249
murgatroid9954e93d42015-04-27 09:29:49 -0700250@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];
murgatroid99fe2c0c62015-04-29 10:59:43 -0700276 if (!_queue) {
277 return nil;
278 }
murgatroid996cc46802015-04-28 09:35:48 -0700279 _call = grpc_channel_create_call(channel.unmanagedChannel, _queue.unmanagedQueue,
280 method.UTF8String, host.UTF8String, gpr_inf_future);
murgatroid9969927d62015-04-24 13:32:48 -0700281 if (_call == NULL) {
murgatroid9930b7d4e2015-04-24 10:36:43 -0700282 return nil;
283 }
284 }
285 return self;
286}
287
murgatroid9954e93d42015-04-27 09:29:49 -0700288- (void)startBatchWithOperations:(NSArray *)operations {
289 [self startBatchWithOperations:operations errorHandler:nil];
murgatroid9969927d62015-04-24 13:32:48 -0700290}
291
murgatroid9954e93d42015-04-27 09:29:49 -0700292- (void)startBatchWithOperations:(NSArray *)operations errorHandler:(void (^)())errorHandler {
murgatroid9969927d62015-04-24 13:32:48 -0700293 size_t nops = operations.count;
murgatroid9930b7d4e2015-04-24 10:36:43 -0700294 grpc_op *ops_array = gpr_malloc(nops * sizeof(grpc_op));
murgatroid9954e93d42015-04-27 09:29:49 -0700295 size_t i = 0;
296 for (id op in operations) {
murgatroid99a571ceb2015-04-29 10:58:24 -0700297 [op getOp:&ops_array[i++]];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700298 }
murgatroid996cc46802015-04-28 09:35:48 -0700299 grpc_call_error error = grpc_call_start_batch(_call, ops_array, nops,
300 (__bridge_retained void *)(^(grpc_op_error error){
murgatroid9930b7d4e2015-04-24 10:36:43 -0700301 if (error != GRPC_OP_OK) {
murgatroid9969927d62015-04-24 13:32:48 -0700302 if (errorHandler) {
303 errorHandler();
304 } else {
murgatroid99def47aa2015-04-29 10:28:26 -0700305 return;
murgatroid9969927d62015-04-24 13:32:48 -0700306 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700307 }
murgatroid992101a482015-04-29 11:42:26 -0700308 for (id<GRPCOp> operation in operations) {
309 [operation finish];
murgatroid9969927d62015-04-24 13:32:48 -0700310 }
murgatroid9930b7d4e2015-04-24 10:36:43 -0700311 }));
murgatroid9954e93d42015-04-27 09:29:49 -0700312
murgatroid9930b7d4e2015-04-24 10:36:43 -0700313 if (error != GRPC_CALL_OK) {
murgatroid996a084f42015-04-29 10:18:05 -0700314 [NSException raise:NSInternalInconsistencyException
315 format:@"A precondition for calling grpc_call_start_batch wasn't met"];
murgatroid9930b7d4e2015-04-24 10:36:43 -0700316 }
317}
318
319- (void)cancel {
murgatroid9969927d62015-04-24 13:32:48 -0700320 grpc_call_cancel(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700321}
322
323- (void)dealloc {
murgatroid9969927d62015-04-24 13:32:48 -0700324 grpc_call_destroy(_call);
murgatroid9930b7d4e2015-04-24 10:36:43 -0700325}
326
327@end