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#import "src/objective-c/GRPCClient/private/GRPCConnectivityMonitor.h" 28 29NSString *host = @"grpc-test.sandbox.googleapis.com"; 30 31@interface ViewController : UIViewController 32@end 33 34@implementation ViewController 35- (void)viewDidLoad { 36 [super viewDidLoad]; 37 38#ifndef GRPC_CFSTREAM 39 [GRPCConnectivityMonitor registerObserver:self selector:@selector(reachabilityChanged:)]; 40#endif 41} 42 43- (void)reachabilityChanged:(NSNotification *)note { 44 NSLog(@"Reachability changed\n"); 45} 46 47- (IBAction)tapUnary:(id)sender { 48 // Create a unary call 49 50 // A trivial proto message to generate a response 51 char bytes[] = {0x10, 0x05, 0x1A, 0x07, 0x12, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00}; 52 53 GRPCProtoMethod *method = [[GRPCProtoMethod alloc] initWithPackage:@"grpc.testing" 54 service:@"TestService" 55 method:@"UnaryCall"]; 56 GRXWriter *loggingRequestWriter = [[GRXWriter 57 writerWithValue:[NSData dataWithBytes:bytes length:sizeof(bytes)]] map:^id(id value) { 58 NSLog(@"Sending request."); 59 return value; 60 }]; 61 GRPCCall *call = 62 [[GRPCCall alloc] initWithHost:host path:method.HTTPPath requestsWriter:loggingRequestWriter]; 63 64 [call startWithWriteable:[GRXWriteable 65 writeableWithEventHandler:^(BOOL done, id value, NSError *error) { 66 if (!done) { 67 return; 68 } 69 NSLog(@"Unary call finished with error: %@", error); 70 }]]; 71} 72 73- (IBAction)tapStreaming:(id)sender { 74 // Create a streaming call 75 76 // A trivial proto message to generate a response 77 char bytes[] = {0x12, 0x02, 0x08, 0x02, 0x1A, 0x04, 0x12, 0x02, 0x00, 0x00}; 78 79 GRPCProtoMethod *method = [[GRPCProtoMethod alloc] initWithPackage:@"grpc.testing" 80 service:@"TestService" 81 method:@"FullDuplexCall"]; 82 83 GRXBufferedPipe *requestsBuffer = [[GRXBufferedPipe alloc] init]; 84 85 [requestsBuffer writeValue:[NSData dataWithBytes:bytes length:sizeof(bytes)]]; 86 87 GRPCCall *call = 88 [[GRPCCall alloc] initWithHost:host path:method.HTTPPath requestsWriter:requestsBuffer]; 89 90 [call startWithWriteable:[GRXWriteable 91 writeableWithEventHandler:^(BOOL done, id value, NSError *error) { 92 if (!done) { 93 return; 94 } 95 NSLog(@"Streaming call finished with error: %@", error); 96 }]]; 97} 98 99@end 100