EchoViewController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "EchoViewController.h"
  15. #import "echoapi/Echo.pbobjc.h"
  16. #import "echoapi/Echo.pbrpc.h"
  17. #import <GRPCClient/GRPCCall.h>
  18. #import <GRPCClient/GRPCCall+Tests.h> // this allows us to disable TLS
  19. #import <RxLibrary/GRXBufferedPipe.h>
  20. #import <ProtoRPC/ProtoRPC.h>
  21. // [START host]
  22. static NSString * const kHostAddress = @"localhost";
  23. // [END host]
  24. // = @"<IP Address>"; // GCE instance
  25. // = @"<IP Address>"; // L4 load balancer
  26. static BOOL useSSL = NO;
  27. @interface EchoViewController () <UITextFieldDelegate>
  28. @property (nonatomic, strong) IBOutlet UITextField *echoField;
  29. @property (nonatomic, strong) IBOutlet UITextField *textField;
  30. @property (nonatomic, strong) IBOutlet UISwitch *streamSwitch;
  31. @property (nonatomic, strong) NSString *addressWithPort;
  32. @property (nonatomic, strong) Echo *client;
  33. @property (nonatomic, strong) GRPCProtoCall *updateCall;
  34. @property (nonatomic, strong) GRXBufferedPipe *writer;
  35. @end
  36. @implementation EchoViewController
  37. - (void) viewDidLoad {
  38. [super viewDidLoad];
  39. self.view.backgroundColor = [UIColor darkGrayColor];
  40. [self configureNetworking];
  41. }
  42. - (void) configureNetworking {
  43. if (!useSSL) {
  44. _addressWithPort = [kHostAddress stringByAppendingString:@":8080"];
  45. // This tells the GRPC library to NOT use SSL.
  46. [GRPCCall useInsecureConnectionsForHost:_addressWithPort];
  47. } else {
  48. _addressWithPort = [kHostAddress stringByAppendingString:@":443"];
  49. // This tells the GRPC library to trust a certificate that it might not be able to validate.
  50. // Typically this would be used to trust a self-signed certificate.
  51. [GRPCCall useTestCertsPath:[[NSBundle mainBundle] pathForResource:@"ssl" ofType:@"crt"]
  52. testName:@"example.com"
  53. forHost:kHostAddress
  54. ];
  55. }
  56. _client = [[Echo alloc] initWithHost:_addressWithPort];
  57. }
  58. - (void) getStickynoteWithMessage:(NSString *) message {
  59. EchoRequest *request = [EchoRequest message];
  60. request.text = message;
  61. GRPCProtoCall *call = [_client RPCToGetWithRequest:request
  62. handler:
  63. ^(EchoResponse *response, NSError *error) {
  64. [self handleEchoResponse:response andError:error];
  65. }];
  66. [call start];
  67. }
  68. // [START openStreamingConnection]
  69. - (void) openStreamingConnection {
  70. _writer = [[GRXBufferedPipe alloc] init];
  71. _updateCall = [_client RPCToUpdateWithRequestsWriter:_writer
  72. eventHandler:^(BOOL done, EchoResponse *response, NSError *error) {
  73. [self handleEchoResponse:response andError:error];
  74. }];
  75. [_updateCall start];
  76. }
  77. // [END openStreamingConnection]
  78. - (void) closeStreamingConnection {
  79. [_writer writesFinishedWithError:nil];
  80. }
  81. - (void) handleEchoResponse:(EchoResponse *)response andError:(NSError *) error {
  82. if (error) {
  83. self.echoField.backgroundColor = [UIColor redColor];
  84. self.echoField.text = @"";
  85. NSLog(@"ERROR: %@", error);
  86. } else if (response.text) {
  87. self.echoField.backgroundColor = [UIColor whiteColor];
  88. self.echoField.text = response.text;
  89. }
  90. }
  91. - (IBAction) textFieldDidEndEditing:(UITextField *)textField
  92. {
  93. [self getStickynoteWithMessage:textField.text];
  94. }
  95. // [START textDidChange]
  96. - (IBAction)textDidChange:(UITextField *) sender {
  97. if ([_streamSwitch isOn]) {
  98. EchoRequest *request = [EchoRequest message];
  99. request.text = sender.text;
  100. [_writer writeValue:request];
  101. }
  102. }
  103. // [END textDidChange]
  104. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  105. [textField resignFirstResponder];
  106. return NO;
  107. }
  108. - (IBAction) switchValueDidChange:(UISwitch *) sender {
  109. if ([sender isOn]) {
  110. [self openStreamingConnection];
  111. } else {
  112. [self closeStreamingConnection];
  113. }
  114. }
  115. - (UIStatusBarStyle) preferredStatusBarStyle {
  116. return UIStatusBarStyleLightContent;
  117. }
  118. @end