aboutsummaryrefslogtreecommitdiff
path: root/src/objective-c/GRPCClient/GRPCInterceptor.m
blob: d4fd45edce3fb93a65b6e11cae7ddae2b8714355 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 *
 * Copyright 2019 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#import <Foundation/Foundation.h>

#import "GRPCInterceptor.h"
#import "private/GRPCTransport+Private.h"

@interface GRPCInterceptorManager () <GRPCInterceptorInterface, GRPCResponseHandler>

@end

@implementation GRPCInterceptorManager {
  id<GRPCInterceptorInterface> _nextInterceptor;
  id<GRPCResponseHandler> _previousInterceptor;
  GRPCInterceptor *_thisInterceptor;
  dispatch_queue_t _dispatchQueue;
  NSArray<id<GRPCInterceptorFactory>> *_factories;
  GRPCTransportID _transportID;
  BOOL _shutDown;
}

- (instancetype)initWithFactories:(NSArray<id<GRPCInterceptorFactory>> *)factories
              previousInterceptor:(id<GRPCResponseHandler>)previousInterceptor
                      transportID:(nonnull GRPCTransportID)transportID {
  if ((self = [super init])) {
    if (factories.count == 0) {
      [NSException raise:NSInternalInconsistencyException
                  format:@"Interceptor manager must have factories"];
    }
    _thisInterceptor = [factories[0] createInterceptorWithManager:self];
    if (_thisInterceptor == nil) {
      return nil;
    }
    _previousInterceptor = previousInterceptor;
    _factories = factories;
    // Generate interceptor
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000 || __MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
    if (@available(iOS 8.0, macOS 10.10, *)) {
      _dispatchQueue = dispatch_queue_create(
          NULL,
          dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_DEFAULT, 0));
    } else {
#else
    {
#endif
      _dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
    }
    dispatch_set_target_queue(_dispatchQueue, _thisInterceptor.dispatchQueue);
    _transportID = transportID;
  }
  return self;
}

- (void)shutDown {
  // immediately releases reference; should not queue to dispatch queue.
  _nextInterceptor = nil;
  _previousInterceptor = nil;
  _thisInterceptor = nil;
  _shutDown = YES;
}

- (void)createNextInterceptor {
  NSAssert(_nextInterceptor == nil, @"Starting the next interceptor more than once");
  NSAssert(_factories.count > 0, @"Interceptor manager of transport cannot start next interceptor");
  if (_nextInterceptor != nil) {
    NSLog(@"Starting the next interceptor more than once");
    return;
  }
  NSMutableArray<id<GRPCInterceptorFactory>> *interceptorFactories = [NSMutableArray
      arrayWithArray:[_factories subarrayWithRange:NSMakeRange(1, _factories.count - 1)]];
  while (_nextInterceptor == nil) {
    if (interceptorFactories.count == 0) {
      _nextInterceptor = [[GRPCTransportManager alloc] initWithTransportID:_transportID
                                                       previousInterceptor:self];
      break;
    } else {
      _nextInterceptor = [[GRPCInterceptorManager alloc] initWithFactories:interceptorFactories
                                                       previousInterceptor:self
                                                               transportID:_transportID];
      if (_nextInterceptor == nil) {
        [interceptorFactories removeObjectAtIndex:0];
      }
    }
  }
  NSAssert(_nextInterceptor != nil, @"Failed to create interceptor or transport.");
  if (_nextInterceptor == nil) {
    NSLog(@"Failed to create interceptor or transport.");
  }
}

- (void)startNextInterceptorWithRequest:(GRPCRequestOptions *)requestOptions
                            callOptions:(GRPCCallOptions *)callOptions {
  if (_nextInterceptor == nil && !_shutDown) {
    [self createNextInterceptor];
  }
  if (_nextInterceptor == nil) {
    return;
  }
  id<GRPCInterceptorInterface> copiedNextInterceptor = _nextInterceptor;
  dispatch_async(copiedNextInterceptor.dispatchQueue, ^{
    [copiedNextInterceptor startWithRequestOptions:requestOptions callOptions:callOptions];
  });
}

- (void)writeNextInterceptorWithData:(id)data {
  if (_nextInterceptor == nil && !_shutDown) {
    [self createNextInterceptor];
  }
  if (_nextInterceptor == nil) {
    return;
  }
  id<GRPCInterceptorInterface> copiedNextInterceptor = _nextInterceptor;
  dispatch_async(copiedNextInterceptor.dispatchQueue, ^{
    [copiedNextInterceptor writeData:data];
  });
}

- (void)finishNextInterceptor {
  if (_nextInterceptor == nil && !_shutDown) {
    [self createNextInterceptor];
  }
  if (_nextInterceptor == nil) {
    return;
  }
  id<GRPCInterceptorInterface> copiedNextInterceptor = _nextInterceptor;
  dispatch_async(copiedNextInterceptor.dispatchQueue, ^{
    [copiedNextInterceptor finish];
  });
}

- (void)cancelNextInterceptor {
  if (_nextInterceptor == nil && !_shutDown) {
    [self createNextInterceptor];
  }
  if (_nextInterceptor == nil) {
    return;
  }
  id<GRPCInterceptorInterface> copiedNextInterceptor = _nextInterceptor;
  dispatch_async(copiedNextInterceptor.dispatchQueue, ^{
    [copiedNextInterceptor cancel];
  });
}

/** Notify the next interceptor in the chain to receive more messages */
- (void)receiveNextInterceptorMessages:(NSUInteger)numberOfMessages {
  if (_nextInterceptor == nil && !_shutDown) {
    [self createNextInterceptor];
  }
  if (_nextInterceptor == nil) {
    return;
  }
  id<GRPCInterceptorInterface> copiedNextInterceptor = _nextInterceptor;
  dispatch_async(copiedNextInterceptor.dispatchQueue, ^{
    [copiedNextInterceptor receiveNextMessages:numberOfMessages];
  });
}

// Methods to forward GRPCResponseHandler callbacks to the previous object

/** Forward initial metadata to the previous interceptor in the chain */
- (void)forwardPreviousInterceptorWithInitialMetadata:(NSDictionary *)initialMetadata {
  if (_previousInterceptor == nil) {
    return;
  }
  id<GRPCResponseHandler> copiedPreviousInterceptor = _previousInterceptor;
  dispatch_async(copiedPreviousInterceptor.dispatchQueue, ^{
    [copiedPreviousInterceptor didReceiveInitialMetadata:initialMetadata];
  });
}

/** Forward a received message to the previous interceptor in the chain */
- (void)forwardPreviousInterceptorWithData:(id)data {
  if (_previousInterceptor == nil) {
    return;
  }
  id<GRPCResponseHandler> copiedPreviousInterceptor = _previousInterceptor;
  dispatch_async(copiedPreviousInterceptor.dispatchQueue, ^{
    [copiedPreviousInterceptor didReceiveData:data];
  });
}

/** Forward call close and trailing metadata to the previous interceptor in the chain */
- (void)forwardPreviousInterceptorCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata
                                                      error:(NSError *)error {
  if (_previousInterceptor == nil) {
    return;
  }
  id<GRPCResponseHandler> copiedPreviousInterceptor = _previousInterceptor;
  // no more callbacks should be issued to the previous interceptor
  _previousInterceptor = nil;
  dispatch_async(copiedPreviousInterceptor.dispatchQueue, ^{
    [copiedPreviousInterceptor didCloseWithTrailingMetadata:trailingMetadata error:error];
  });
}

/** Forward write completion to the previous interceptor in the chain */
- (void)forwardPreviousInterceptorDidWriteData {
  if (_previousInterceptor == nil) {
    return;
  }
  id<GRPCResponseHandler> copiedPreviousInterceptor = _previousInterceptor;
  dispatch_async(copiedPreviousInterceptor.dispatchQueue, ^{
    [copiedPreviousInterceptor didWriteData];
  });
}

- (dispatch_queue_t)dispatchQueue {
  return _dispatchQueue;
}

- (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions
                    callOptions:(GRPCCallOptions *)callOptions {
  // retain this interceptor until the method exit to prevent deallocation of the interceptor within
  // the interceptor's method
  GRPCInterceptor *thisInterceptor = _thisInterceptor;
  [thisInterceptor startWithRequestOptions:requestOptions callOptions:callOptions];
}

- (void)writeData:(id)data {
  // retain this interceptor until the method exit to prevent deallocation of the interceptor within
  // the interceptor's method
  GRPCInterceptor *thisInterceptor = _thisInterceptor;
  [thisInterceptor writeData:data];
}

- (void)finish {
  // retain this interceptor until the method exit to prevent deallocation of the interceptor within
  // the interceptor's method
  GRPCInterceptor *thisInterceptor = _thisInterceptor;
  [thisInterceptor finish];
}

- (void)cancel {
  // retain this interceptor until the method exit to prevent deallocation of the interceptor within
  // the interceptor's method
  GRPCInterceptor *thisInterceptor = _thisInterceptor;
  [thisInterceptor cancel];
}

- (void)receiveNextMessages:(NSUInteger)numberOfMessages {
  // retain this interceptor until the method exit to prevent deallocation of the interceptor within
  // the interceptor's method
  GRPCInterceptor *thisInterceptor = _thisInterceptor;
  [thisInterceptor receiveNextMessages:numberOfMessages];
}

- (void)didReceiveInitialMetadata:(nullable NSDictionary *)initialMetadata {
  if ([_thisInterceptor respondsToSelector:@selector(didReceiveInitialMetadata:)]) {
    // retain this interceptor until the method exit to prevent deallocation of the interceptor
    // within the interceptor's method
    GRPCInterceptor *thisInterceptor = _thisInterceptor;
    [thisInterceptor didReceiveInitialMetadata:initialMetadata];
  }
}

- (void)didReceiveData:(id)data {
  if ([_thisInterceptor respondsToSelector:@selector(didReceiveData:)]) {
    // retain this interceptor until the method exit to prevent deallocation of the interceptor
    // within the interceptor's method
    GRPCInterceptor *thisInterceptor = _thisInterceptor;
    [thisInterceptor didReceiveData:data];
  }
}

- (void)didCloseWithTrailingMetadata:(nullable NSDictionary *)trailingMetadata
                               error:(nullable NSError *)error {
  if ([_thisInterceptor respondsToSelector:@selector(didCloseWithTrailingMetadata:error:)]) {
    // retain this interceptor until the method exit to prevent deallocation of the interceptor
    // within the interceptor's method
    GRPCInterceptor *thisInterceptor = _thisInterceptor;
    [thisInterceptor didCloseWithTrailingMetadata:trailingMetadata error:error];
  }
}

- (void)didWriteData {
  if ([_thisInterceptor respondsToSelector:@selector(didWriteData)]) {
    // retain this interceptor until the method exit to prevent deallocation of the interceptor
    // within the interceptor's method
    GRPCInterceptor *thisInterceptor = _thisInterceptor;
    [thisInterceptor didWriteData];
  }
}

@end

@implementation GRPCInterceptor {
  GRPCInterceptorManager *_manager;
  dispatch_queue_t _dispatchQueue;
}

- (instancetype)initWithInterceptorManager:(GRPCInterceptorManager *)interceptorManager
                             dispatchQueue:(dispatch_queue_t)dispatchQueue {
  if ((self = [super init])) {
    _manager = interceptorManager;
    _dispatchQueue = dispatchQueue;
  }

  return self;
}

- (dispatch_queue_t)dispatchQueue {
  return _dispatchQueue;
}

- (void)startWithRequestOptions:(GRPCRequestOptions *)requestOptions
                    callOptions:(GRPCCallOptions *)callOptions {
  [_manager startNextInterceptorWithRequest:requestOptions callOptions:callOptions];
}

- (void)writeData:(id)data {
  [_manager writeNextInterceptorWithData:data];
}

- (void)finish {
  [_manager finishNextInterceptor];
}

- (void)cancel {
  [_manager cancelNextInterceptor];
  [_manager
      forwardPreviousInterceptorCloseWithTrailingMetadata:nil
                                                    error:[NSError
                                                              errorWithDomain:kGRPCErrorDomain
                                                                         code:GRPCErrorCodeCancelled
                                                                     userInfo:@{
                                                                       NSLocalizedDescriptionKey :
                                                                           @"Canceled"
                                                                     }]];
  [_manager shutDown];
}

- (void)receiveNextMessages:(NSUInteger)numberOfMessages {
  [_manager receiveNextInterceptorMessages:numberOfMessages];
}

- (void)didReceiveInitialMetadata:(NSDictionary *)initialMetadata {
  [_manager forwardPreviousInterceptorWithInitialMetadata:initialMetadata];
}

- (void)didReceiveRawMessage:(id)message {
  NSAssert(NO,
           @"The method didReceiveRawMessage is deprecated and cannot be used with interceptor");
  NSLog(@"The method didReceiveRawMessage is deprecated and cannot be used with interceptor");
  abort();
}

- (void)didReceiveData:(id)data {
  [_manager forwardPreviousInterceptorWithData:data];
}

- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata error:(NSError *)error {
  [_manager forwardPreviousInterceptorCloseWithTrailingMetadata:trailingMetadata error:error];
  [_manager shutDown];
}

- (void)didWriteData {
  [_manager forwardPreviousInterceptorDidWriteData];
}

@end