ViewController.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <RemoteTest/Messages.pbobjc.h>
  22. #import <RemoteTest/Test.pbrpc.h>
  23. NSString *const kRemoteHost = @"grpc-test.sandbox.googleapis.com";
  24. const int32_t kMessageSize = 100;
  25. @interface ViewController : UIViewController<GRPCProtoResponseHandler>
  26. @end
  27. @implementation ViewController {
  28. RMTTestService *_service;
  29. dispatch_queue_t _dispatchQueue;
  30. GRPCStreamingProtoCall *_call;
  31. }
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. _dispatchQueue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
  35. }
  36. - (IBAction)tapUnaryCall:(id)sender {
  37. if (_service == nil) {
  38. _service = [RMTTestService serviceWithHost:kRemoteHost];
  39. }
  40. // Set up request proto message
  41. RMTSimpleRequest *request = [RMTSimpleRequest message];
  42. request.responseType = RMTPayloadType_Compressable;
  43. request.responseSize = kMessageSize;
  44. request.payload.body = [NSMutableData dataWithLength:kMessageSize];
  45. GRPCUnaryProtoCall *call =
  46. [_service unaryCallWithMessage:request responseHandler:self callOptions:nil];
  47. [call start];
  48. }
  49. - (IBAction)tapStreamingCallStart:(id)sender {
  50. if (_service == nil) {
  51. _service = [RMTTestService serviceWithHost:kRemoteHost];
  52. }
  53. // Set up request proto message
  54. RMTStreamingOutputCallRequest *request = RMTStreamingOutputCallRequest.message;
  55. RMTResponseParameters *parameters = [RMTResponseParameters message];
  56. parameters.size = kMessageSize;
  57. [request.responseParametersArray addObject:parameters];
  58. request.payload.body = [NSMutableData dataWithLength:kMessageSize];
  59. GRPCStreamingProtoCall *call = [_service fullDuplexCallWithResponseHandler:self callOptions:nil];
  60. [call start];
  61. _call = call;
  62. // display something to confirm the tester the call is started
  63. NSLog(@"Started streaming call");
  64. }
  65. - (IBAction)tapStreamingCallSend:(id)sender {
  66. if (_call == nil) return;
  67. RMTStreamingOutputCallRequest *request = RMTStreamingOutputCallRequest.message;
  68. RMTResponseParameters *parameters = [RMTResponseParameters message];
  69. parameters.size = kMessageSize;
  70. [request.responseParametersArray addObject:parameters];
  71. request.payload.body = [NSMutableData dataWithLength:kMessageSize];
  72. [_call writeMessage:request];
  73. }
  74. - (IBAction)tapStreamingCallStop:(id)sender {
  75. if (_call == nil) return;
  76. [_call finish];
  77. _call = nil;
  78. }
  79. - (void)didReceiveInitialMetadata:(NSDictionary *)initialMetadata {
  80. NSLog(@"Recv initial metadata: %@", initialMetadata);
  81. }
  82. - (void)didReceiveProtoMessage:(GPBMessage *)message {
  83. NSLog(@"Recv message: %@", message);
  84. }
  85. - (void)didCloseWithTrailingMetadata:(NSDictionary *)trailingMetadata
  86. error:(nullable NSError *)error {
  87. NSLog(@"Recv trailing metadata: %@, error: %@", trailingMetadata, error);
  88. }
  89. - (dispatch_queue_t)dispatchQueue {
  90. return _dispatchQueue;
  91. }
  92. @end