EchoViewController.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. import AppKit
  34. import gRPC
  35. import QuickProto
  36. class EchoViewController : NSViewController, NSTextFieldDelegate {
  37. @IBOutlet weak var messageField: NSTextField!
  38. @IBOutlet weak var outputField: NSTextField!
  39. @IBOutlet weak var addressField: NSTextField!
  40. @IBOutlet weak var streamingButton: NSButton!
  41. @IBOutlet weak var TLSButton: NSButton!
  42. private var streaming = false
  43. var client: Client!
  44. var call: Call!
  45. var fileDescriptorSet : FileDescriptorSet
  46. required init?(coder:NSCoder) {
  47. fileDescriptorSet = FileDescriptorSet(filename: "echo.out")
  48. super.init(coder:coder)
  49. }
  50. var enabled = false
  51. @IBAction func messageReturnPressed(sender: NSTextField) {
  52. if enabled {
  53. callServer(address:addressField.stringValue)
  54. }
  55. }
  56. @IBAction func addressReturnPressed(sender: NSTextField) {
  57. if (streaming) {
  58. self.sendClose()
  59. }
  60. }
  61. @IBAction func buttonValueChanged(sender: NSButton) {
  62. if (streaming && (sender.intValue == 0)) {
  63. self.sendClose()
  64. }
  65. }
  66. override func viewDidLoad() {
  67. gRPC.initialize()
  68. }
  69. override func viewDidAppear() {
  70. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  71. self.enabled = true
  72. }
  73. }
  74. func createClient(address: String, host: String) {
  75. if (TLSButton.intValue == 0) {
  76. client = Client(address:address)
  77. } else {
  78. let certificateURL = Bundle.main.url(forResource: "ssl", withExtension: "crt")!
  79. let certificates = try! String(contentsOf: certificateURL)
  80. client = Client(address:address, certificates:certificates, host:host)
  81. }
  82. }
  83. func callServer(address:String) {
  84. let requestHost = "example.com"
  85. let requestMetadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
  86. "x-ios-bundle-identifier":Bundle.main.bundleIdentifier!])
  87. if (self.streamingButton.intValue == 0) {
  88. // NONSTREAMING
  89. if let requestMessage = self.fileDescriptorSet.createMessage("EchoRequest") {
  90. requestMessage.addField("text", value:self.messageField.stringValue)
  91. let requestMessageData = requestMessage.data()
  92. createClient(address:address, host:requestHost)
  93. call = client.createCall(host: requestHost,
  94. method: "/echo.Echo/Get",
  95. timeout: 30.0)
  96. call.performNonStreamingCall(messageData: requestMessageData,
  97. metadata: requestMetadata)
  98. { (response) in
  99. print("Received status: \(response.status) " + response.statusDetails)
  100. if let messageData = response.messageData,
  101. let responseMessage = self.fileDescriptorSet.readMessage("EchoResponse",
  102. data:messageData) {
  103. responseMessage.forOneField("text") {(field) in
  104. DispatchQueue.main.async {
  105. self.outputField.stringValue = field.string()
  106. }
  107. }
  108. } else {
  109. DispatchQueue.main.async {
  110. self.outputField.stringValue = "No message received. gRPC Status \(response.status) "
  111. + response.statusDetails
  112. }
  113. }
  114. }
  115. }
  116. }
  117. else {
  118. // STREAMING
  119. if (!streaming) {
  120. createClient(address:address, host:requestHost)
  121. call = client.createCall(host: requestHost,
  122. method: "/echo.Echo/Update",
  123. timeout: 30.0)
  124. call.start(metadata:requestMetadata)
  125. self.receiveMessage()
  126. streaming = true
  127. }
  128. self.sendMessage()
  129. }
  130. }
  131. func sendMessage() {
  132. let requestMessage = self.fileDescriptorSet.createMessage("EchoRequest")!
  133. requestMessage.addField("text", value:self.messageField.stringValue)
  134. let messageData = requestMessage.data()
  135. call.sendMessage(data:messageData)
  136. }
  137. func receiveMessage() {
  138. call.receiveMessage() {(data) in
  139. let responseMessage = self.fileDescriptorSet.readMessage("EchoResponse", data:data)!
  140. responseMessage.forOneField("text") {(field) in
  141. DispatchQueue.main.async {
  142. self.outputField.stringValue = field.string()
  143. }
  144. self.receiveMessage()
  145. }
  146. }
  147. }
  148. func sendClose() {
  149. call.close() {
  150. self.streaming = false
  151. }
  152. }
  153. }