blob: 4a2372e24089a812768b646022b4abaaf3495ef3 [file] [log] [blame]
Jorge Canizales2779ccb2015-04-20 23:42:46 -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
34#import "ProtoRPC.h"
35
Jorge Canizales6482d5d2015-04-21 01:30:00 -070036#import <ProtocolBuffers/ProtocolBuffers.h>
Jorge Canizales2779ccb2015-04-20 23:42:46 -070037#import <RxLibrary/GRXWriteable.h>
Jorge Canizales6482d5d2015-04-21 01:30:00 -070038#import <RxLibrary/GRXWriter.h>
39#import <RxLibrary/GRXWriter+Transformations.h>
Jorge Canizales2779ccb2015-04-20 23:42:46 -070040
41@implementation ProtoRPC {
42 id<GRXWriteable> _responseWriteable;
43}
44
45- (instancetype)initWithHost:(NSString *)host
46 method:(GRPCMethodName *)method
47 requestsWriter:(id<GRXWriter>)requestWriter {
48 return [self initWithHost:host
49 method:method
50 requestsWriter:requestsWriter
51 responseClass:nil
52 responsesWriteable:nil];
53}
54
55// Designated initializer
56- (instancetype)initWithHost:(NSString *)host
57 method:(GRPCMethodName *)method
58 requestsWriter:(id<GRXWriter>)requestsWriter
59 responseClass:(Class)responseClass
60 responsesWriteable:(id<GRXWriteable>)responsesWriteable {
61 // Because we can't tell the type system to constrain the class, we need to check at runtime:
62 if (![class respondsToSelector:@selector(parseFromData:)]) {
63 [NSException raise:NSInvalidArgumentException
64 format:@"A protobuf class to parse the responses must be provided."];
65 }
Jorge Canizales6482d5d2015-04-21 01:30:00 -070066 // A writer that serializes the proto messages to send.
67 id<GRXWriter> bytesWriter =
68 [[[GRXWriter alloc] initWithWriter:requestsWriter] map:^id(PBGeneratedMessage *proto) {
69 return [proto data];
70 }];
71 if ((self = [super initWithHost:host method:method requestsWriter:bytesWriter])) {
72 // A writeable that parses the proto messages received.
Jorge Canizales2779ccb2015-04-20 23:42:46 -070073 _responseWriteable = [[GRXWriteable alloc] initWithValueHandler:^(NSData *value) {
74 [responsesWriteable didReceiveValue:[responseClass parseFromData:value]];
75 } completionHandler:^(NSError *errorOrNil) {
76 [responsesWriteable didFinishWithError:errorOrNil];
77 }];
78 }
79 return self;
80}
81
82- (void)start {
83 [self startWithWriteable:_responseWriteable];
84}
85
86- (void)startWithWriteable:(id<GRXWriteable>)writeable {
87 [super startWithWriteable:writeable];
Jorge Canizales6482d5d2015-04-21 01:30:00 -070088 // Break retain cycles.
Jorge Canizales2779ccb2015-04-20 23:42:46 -070089 _responseWriteable = nil;
90}
91@end