| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#import <UIKit/UIKit.h> |
| 20 |
|
| 21 |
#import <GRPCClient/GRPCCall.h> |
| 22 |
#import <ProtoRPC/ProtoMethod.h> |
| 23 |
#import <RxLibrary/GRXBufferedPipe.h> |
| 24 |
#import <RxLibrary/GRXWriter+Immediate.h> |
| 25 |
#import <RxLibrary/GRXWriter+Transformations.h> |
| 26 |
|
| 27 |
NSString *host = @"grpc-test.sandbox.googleapis.com"; |
| 28 |
|
| 29 |
@interface ViewController : UIViewController |
| 30 |
@end |
| 31 |
|
| 32 |
@implementation ViewController |
| 33 |
- (void)viewDidLoad { |
| 34 |
[super viewDidLoad]; |
| 35 |
} |
| 36 |
|
| 37 |
- (void)reachabilityChanged:(NSNotification *)note { |
| 38 |
NSLog(@"Reachability changed\n"); |
| 39 |
} |
| 40 |
|
| 41 |
- (IBAction)tapUnary:(id)sender { |
| 42 |
// Create a unary call |
| 43 |
|
| 44 |
// A trivial proto message to generate a response |
| 45 |
char bytes[] = {0x10, 0x05, 0x1A, 0x07, 0x12, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 46 |
|
| 47 |
GRPCProtoMethod *method = [[GRPCProtoMethod alloc] initWithPackage:@"grpc.testing" |
| 48 |
service:@"TestService" |
| 49 |
method:@"UnaryCall"]; |
| 50 |
GRXWriter *loggingRequestWriter = |
| 51 |
[[GRXWriter writerWithValue:[NSData dataWithBytes:bytes |
| 52 |
length:sizeof(bytes)]] map:^id(id value) { |
| 53 |
NSLog(@"Sending request."); |
| 54 |
return value; |
| 55 |
}]; |
| 56 |
GRPCCall *call = [[GRPCCall alloc] initWithHost:host |
| 57 |
path:method.HTTPPath |
| 58 |
requestsWriter:loggingRequestWriter]; |
| 59 |
|
| 60 |
[call startWithWriteable:[GRXWriteable |
| 61 |
writeableWithEventHandler:^(BOOL done, id value, NSError *error) { |
| 62 |
if (!done) { |
| 63 |
return; |
| 64 |
} |
| 65 |
NSLog(@"Unary call finished with error: %@", error); |
| 66 |
}]]; |
| 67 |
} |
| 68 |
|
| 69 |
- (IBAction)tapStreaming:(id)sender { |
| 70 |
// Create a streaming call |
| 71 |
|
| 72 |
// A trivial proto message to generate a response |
| 73 |
char bytes[] = {0x12, 0x02, 0x08, 0x02, 0x1A, 0x04, 0x12, 0x02, 0x00, 0x00}; |
| 74 |
|
| 75 |
GRPCProtoMethod *method = [[GRPCProtoMethod alloc] initWithPackage:@"grpc.testing" |
| 76 |
service:@"TestService" |
| 77 |
method:@"FullDuplexCall"]; |
| 78 |
|
| 79 |
GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init]; |
| 80 |
|
| 81 |
[requestsBuffer writeValue:[NSData dataWithBytes:bytes length:sizeof(bytes)]]; |
| 82 |
|
| 83 |
GRPCCall *call = [[GRPCCall alloc] initWithHost:host |
| 84 |
path:method.HTTPPath |
| 85 |
requestsWriter:requestsBuffer]; |
| 86 |
|
| 87 |
[call startWithWriteable:[GRXWriteable |
| 88 |
writeableWithEventHandler:^(BOOL done, id value, NSError *error) { |
| 89 |
if (!done) { |
| 90 |
return; |
| 91 |
} |
| 92 |
NSLog(@"Streaming call finished with error: %@", error); |
| 93 |
}]]; |
| 94 |
} |
| 95 |
|
| 96 |
@end |