blob: ea482b29ef54a7e5fb23a8ee368e5d0ddbca254f [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);
murgatroid99ca38ddb2015-04-29 13:16:42 -070046 grpc_metadata *_sendMetadata;
murgatroid9954e93d42015-04-27 09:29:49 -070047 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]) {
murgatroid99ca38ddb2015-04-29 13:16:42 -070056 _sendMetadata = [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;
murgatroid99ca38ddb2015-04-29 13:16:42 -070066 op->data.send_initial_metadata.metadata = _sendMetadata;
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 {
murgatroid99ca38ddb2015-04-29 13:16:42 -070076 gpr_free(_sendMetadata);
murgatroid9933655f92015-04-29 11:15:35 -070077}
78
murgatroid9954e93d42015-04-27 09:29:49 -070079@end
80
81@implementation GRPCOpSendMessage{
82 void(^_handler)(void);
murgatroid99ca38ddb2015-04-29 13:16:42 -070083 grpc_byte_buffer *_byteBuffer;
murgatroid9954e93d42015-04-27 09:29:49 -070084}
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]) {
murgatroid99ca38ddb2015-04-29 13:16:42 -070095 _byteBuffer = [message grpc_byteBuffer];
murgatroid9954e93d42015-04-27 09:29:49 -070096 _handler = handler;
97 }
98 return self;
99}
100
101- (void)getOp:(grpc_op *)op {
102 op->op = GRPC_OP_SEND_MESSAGE;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700103 op->data.send_message = _byteBuffer;
murgatroid9954e93d42015-04-27 09:29:49 -0700104}
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 {
murgatroid99ca38ddb2015-04-29 13:16:42 -0700113 gpr_free(_byteBuffer);
murgatroid9933655f92015-04-29 11:15:35 -0700114}
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 *);
murgatroid99ca38ddb2015-04-29 13:16:42 -0700147 grpc_metadata_array _recvInitialMetadata;
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;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700157 grpc_metadata_array_init(&_recvInitialMetadata);
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;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700164 op->data.recv_initial_metadata = &_recvInitialMetadata;
murgatroid9954e93d42015-04-27 09:29:49 -0700165}
166
murgatroid992101a482015-04-29 11:42:26 -0700167- (void)finish {
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700168 NSDictionary *metadata = [NSDictionary grpc_dictionaryFromMetadataArray:_recvInitialMetadata];
murgatroid992101a482015-04-29 11:42:26 -0700169 if (_handler) {
170 _handler(metadata);
171 }
murgatroid9954e93d42015-04-27 09:29:49 -0700172}
173
murgatroid9933655f92015-04-29 11:15:35 -0700174- (void)dealloc {
murgatroid99ca38ddb2015-04-29 13:16:42 -0700175 grpc_metadata_array_destroy(&_recvInitialMetadata);
murgatroid9933655f92015-04-29 11:15:35 -0700176}
177
murgatroid9954e93d42015-04-27 09:29:49 -0700178@end
179
180@implementation GRPCOpRecvMessage{
murgatroid996cc46802015-04-28 09:35:48 -0700181 void(^_handler)(grpc_byte_buffer *);
murgatroid99ca38ddb2015-04-29 13:16:42 -0700182 grpc_byte_buffer *_recvMessage;
murgatroid9954e93d42015-04-27 09:29:49 -0700183}
184
185- (instancetype)init {
186 return [self initWithHandler:nil];
187}
188
murgatroid996cc46802015-04-28 09:35:48 -0700189- (instancetype)initWithHandler:(void (^)(grpc_byte_buffer *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700190 if (self = [super init]) {
191 _handler = handler;
murgatroid9954e93d42015-04-27 09:29:49 -0700192 }
193 return self;
194}
195
196- (void)getOp:(grpc_op *)op {
197 op->op = GRPC_OP_RECV_MESSAGE;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700198 op->data.recv_message = &_recvMessage;
murgatroid9954e93d42015-04-27 09:29:49 -0700199}
200
murgatroid992101a482015-04-29 11:42:26 -0700201- (void)finish {
202 if (_handler) {
murgatroid99ca38ddb2015-04-29 13:16:42 -0700203 _handler(_recvMessage);
murgatroid992101a482015-04-29 11:42:26 -0700204 }
murgatroid9954e93d42015-04-27 09:29:49 -0700205}
206
207@end
208
209@implementation GRPCOpRecvStatus{
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700210 void(^_handler)(NSError *, NSDictionary *);
211 grpc_status_code _statusCode;
212 char *_details;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700213 size_t _detailsCapacity;
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700214 grpc_metadata_array _metadata;
murgatroid9954e93d42015-04-27 09:29:49 -0700215}
216
217- (instancetype) init {
218 return [self initWithHandler:nil];
219}
220
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700221- (instancetype) initWithHandler:(void (^)(NSError *, NSDictionary *))handler {
murgatroid9954e93d42015-04-27 09:29:49 -0700222 if (self = [super init]) {
223 _handler = handler;
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700224 grpc_metadata_array_init(&_metadata);
murgatroid9954e93d42015-04-27 09:29:49 -0700225 }
226 return self;
227}
228
229- (void)getOp:(grpc_op *)op {
230 op->op = GRPC_OP_RECV_STATUS_ON_CLIENT;
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700231 op->data.recv_status_on_client.status = &_statusCode;
232 op->data.recv_status_on_client.status_details = &_details;
murgatroid99ca38ddb2015-04-29 13:16:42 -0700233 op->data.recv_status_on_client.status_details_capacity = &_detailsCapacity;
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700234 op->data.recv_status_on_client.trailing_metadata = &_metadata;
murgatroid9954e93d42015-04-27 09:29:49 -0700235}
236
murgatroid99ca38ddb2015-04-29 13:16:42 -0700237- (void)finish {
murgatroid992101a482015-04-29 11:42:26 -0700238 if (_handler) {
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700239 NSError *error = [NSError grpc_errorFromStatusCode:_statusCode details:_details];
240 NSDictionary *trailers = [NSDictionary grpc_dictionaryFromMetadataArray:_metadata];
241 _handler(error, trailers);
murgatroid992101a482015-04-29 11:42:26 -0700242 }
murgatroid9954e93d42015-04-27 09:29:49 -0700243}
244
murgatroid9933655f92015-04-29 11:15:35 -0700245- (void)dealloc {
Jorge Canizalesf3a4f2c2015-06-12 22:12:50 -0700246 grpc_metadata_array_destroy(&_metadata);
247 gpr_free(_details);
murgatroid9933655f92015-04-29 11:15:35 -0700248}
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,
murgatroid996b542442015-05-08 10:40:01 -0700300 (__bridge_retained void *)(^(bool success){
301 if (!success) {
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