ViewController.m 5.2 KB

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