1/* 2 * 3 * Copyright 2019 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 <GRPCClient/GRPCCallOptions.h> 23#import "src/objective-c/tests/RemoteTestClient/Messages.pbobjc.h" 24#import "src/objective-c/tests/RemoteTestClient/Test.pbrpc.h" 25 26NSString *const kRemoteHost = @"grpc-test.sandbox.googleapis.com"; 27const int32_t kMessageSize = 100; 28 29@interface ViewController : UIViewController <GRPCProtoResponseHandler> 30@property(strong, nonatomic) UILabel *callStatusLabel; 31@property(strong, nonatomic) UILabel *callCountLabel; 32@end 33 34@implementation ViewController { 35 RMTTestService *_service; 36 dispatch_queue_t _dispatchQueue; 37 GRPCStreamingProtoCall *_call; 38 int _calls_completed; 39} 40- (instancetype)init { 41 self = [super init]; 42 _calls_completed = 0; 43 return self; 44} 45 46- (void)viewDidLoad { 47 [super viewDidLoad]; 48 _dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL); 49 _callStatusLabel = (UILabel *)[self.view viewWithTag:1]; 50 _callCountLabel = (UILabel *)[self.view viewWithTag:2]; 51} 52 53- (void)startUnaryCall { 54 if (_service == nil) { 55 _service = [RMTTestService serviceWithHost:kRemoteHost]; 56 } 57 dispatch_async(dispatch_get_main_queue(), ^{ 58 self->_callStatusLabel.text = @""; 59 }); 60 61 // Set up request proto message 62 RMTSimpleRequest *request = [RMTSimpleRequest message]; 63 request.responseType = RMTPayloadType_Compressable; 64 request.responseSize = kMessageSize; 65 request.payload.body = [NSMutableData dataWithLength:kMessageSize]; 66 67 GRPCUnaryProtoCall *call = [_service unaryCallWithMessage:request 68 responseHandler:self 69 callOptions:nil]; 70 71 [call start]; 72} 73 74- (IBAction)tapUnaryCall:(id)sender { 75 NSLog(@"In tapUnaryCall"); 76 [self startUnaryCall]; 77} 78 79- (IBAction)tap10UnaryCalls:(id)sender { 80 NSLog(@"In tap10UnaryCalls"); 81 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { 82 // Background thread 83 for (int i = 0; i < 10; ++i) { 84 [self startUnaryCall]; 85 [NSThread sleepForTimeInterval:0.5]; 86 } 87 }); 88} 89 90- (IBAction)resetCounter:(id)sender { 91 _calls_completed = 0; 92 dispatch_async(dispatch_get_main_queue(), ^{ 93 self->_callCountLabel.text = 94 [NSString stringWithFormat:@"Calls completed: %d", self->_calls_completed]; 95 self->_callStatusLabel.text = @""; 96 }); 97} 98 99- (IBAction)tapStreamingCallStart:(id)sender { 100 NSLog(@"In tapStreamingCallStart"); 101 if (_service == nil) { 102 _service = [RMTTestService serviceWithHost:kRemoteHost]; 103 } 104 dispatch_async(dispatch_get_main_queue(), ^{ 105 self->_callStatusLabel.text = @""; 106 }); 107 108 // Set up request proto message 109 RMTStreamingOutputCallRequest *request = RMTStreamingOutputCallRequest.message; 110 RMTResponseParameters *parameters = [RMTResponseParameters message]; 111 parameters.size = kMessageSize; 112 [request.responseParametersArray addObject:parameters]; 113 request.payload.body = [NSMutableData dataWithLength:kMessageSize]; 114 115 GRPCStreamingProtoCall *call = [_service fullDuplexCallWithResponseHandler:self callOptions:nil]; 116 [call start]; 117 _call = call; 118 // display something to confirm the tester the call is started 119} 120 121- (IBAction)tapStreamingCallSend:(id)sender { 122 NSLog(@"In tapStreamingCallSend"); 123 if (_call == nil) return; 124 125 RMTStreamingOutputCallRequest *request = RMTStreamingOutputCallRequest.message; 126 RMTResponseParameters *parameters = [RMTResponseParameters message]; 127 parameters.size = kMessageSize; 128 [request.responseParametersArray addObject:parameters]; 129 request.payload.body = [NSMutableData dataWithLength:kMessageSize]; 130 131 [_call writeMessage:request]; 132} 133 134- (IBAction)tapStreamingCallStop:(id)sender { 135 NSLog(@"In tapStreamingCallStop"); 136 if (_call == nil) return; 137 138 [_call finish]; 139 _call = nil; 140} 141 142- (void)didReceiveInitialMetadata:(NSDictionary *)initialMetadata { 143 NSLog(@"Recv initial metadata: %@", initialMetadata); 144} 145 146- (void)didReceiveProtoMessage:(GPBMessage *)message { 147 NSLog(@"Recv message: %@", message); 148} 149 150- (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata 151 error:(nullable NSError *)error { 152 NSLog(@"Recv trailing metadata: %@, error: %@", trailingMetadata, error); 153 if (error == nil) { 154 dispatch_async(dispatch_get_main_queue(), ^{ 155 self->_callStatusLabel.text = @"Call done"; 156 }); 157 } else { 158 dispatch_async(dispatch_get_main_queue(), ^{ 159 self->_callStatusLabel.text = @"Call failed"; 160 }); 161 } 162 ++_calls_completed; 163 dispatch_async(dispatch_get_main_queue(), ^{ 164 self->_callCountLabel.text = 165 [NSString stringWithFormat:@"Calls completed: %d", self->_calls_completed]; 166 }); 167} 168 169- (dispatch_queue_t)dispatchQueue { 170 return _dispatchQueue; 171} 172 173@end 174