EchoViewController.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import AppKit
  17. import gRPC
  18. class EchoViewController : NSViewController, NSTextFieldDelegate {
  19. @IBOutlet weak var messageField: NSTextField!
  20. @IBOutlet weak var sentOutputField: NSTextField!
  21. @IBOutlet weak var receivedOutputField: NSTextField!
  22. @IBOutlet weak var addressField: NSTextField!
  23. @IBOutlet weak var TLSButton: NSButton!
  24. @IBOutlet weak var callSelectButton: NSSegmentedControl!
  25. @IBOutlet weak var closeButton: NSButton!
  26. private var service : Echo_EchoService?
  27. private var expandCall: Echo_EchoExpandCall?
  28. private var collectCall: Echo_EchoCollectCall?
  29. private var updateCall: Echo_EchoUpdateCall?
  30. private var nowStreaming = false
  31. required init?(coder:NSCoder) {
  32. super.init(coder:coder)
  33. }
  34. private var enabled = false
  35. @IBAction func messageReturnPressed(sender: NSTextField) {
  36. if enabled {
  37. do {
  38. try callServer(address:addressField.stringValue,
  39. host:"example.com")
  40. } catch (let error) {
  41. print(error)
  42. }
  43. }
  44. }
  45. @IBAction func addressReturnPressed(sender: NSTextField) {
  46. if (nowStreaming) {
  47. do {
  48. try self.sendClose()
  49. } catch (let error) {
  50. print(error)
  51. }
  52. }
  53. // invalidate the service
  54. service = nil
  55. }
  56. @IBAction func buttonValueChanged(sender: NSSegmentedControl) {
  57. if (nowStreaming) {
  58. do {
  59. try self.sendClose()
  60. } catch (let error) {
  61. print(error)
  62. }
  63. }
  64. }
  65. @IBAction func closeButtonPressed(sender: NSButton) {
  66. if (nowStreaming) {
  67. do {
  68. try self.sendClose()
  69. } catch (let error) {
  70. print(error)
  71. }
  72. }
  73. }
  74. override func viewDidLoad() {
  75. closeButton.isEnabled = false
  76. // prevent the UI from trying to send messages until gRPC is initialized
  77. DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
  78. self.enabled = true
  79. }
  80. }
  81. func displayMessageSent(_ message:String) {
  82. DispatchQueue.main.async {
  83. self.sentOutputField.stringValue = message
  84. }
  85. }
  86. func displayMessageReceived(_ message:String) {
  87. DispatchQueue.main.async {
  88. self.receivedOutputField.stringValue = message
  89. }
  90. }
  91. func prepareService(address: String, host: String) {
  92. if (service != nil) {
  93. return
  94. }
  95. if (TLSButton.intValue == 0) {
  96. service = Echo_EchoService(address:address, secure:false)
  97. } else {
  98. let certificateURL = Bundle.main.url(forResource: "ssl",
  99. withExtension: "crt")!
  100. let certificates = try! String(contentsOf: certificateURL)
  101. service = Echo_EchoService(address:address, certificates:certificates, host:host)
  102. }
  103. if let service = service {
  104. service.host = "example.com" // sample override
  105. service.metadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
  106. "x-ios-bundle-identifier":Bundle.main.bundleIdentifier!])
  107. }
  108. }
  109. func callServer(address:String, host:String) throws -> Void {
  110. prepareService(address:address, host:host)
  111. guard let service = service else {
  112. return
  113. }
  114. if (self.callSelectButton.selectedSegment == 0) {
  115. // NONSTREAMING
  116. var requestMessage = Echo_EchoRequest()
  117. requestMessage.text = self.messageField.stringValue
  118. self.displayMessageSent(requestMessage.text)
  119. _ = try service.get(requestMessage) {responseMessage, callResult in
  120. if let responseMessage = responseMessage {
  121. self.displayMessageReceived(responseMessage.text)
  122. } else {
  123. self.displayMessageReceived("No message received. \(callResult)")
  124. }
  125. }
  126. }
  127. else if (self.callSelectButton.selectedSegment == 1) {
  128. // STREAMING EXPAND
  129. if (!nowStreaming) {
  130. do {
  131. var requestMessage = Echo_EchoRequest()
  132. requestMessage.text = self.messageField.stringValue
  133. self.expandCall = try service.expand(requestMessage) {call in
  134. print("Started expand \(call)")
  135. }
  136. try self.receiveExpandMessages()
  137. self.displayMessageSent(requestMessage.text)
  138. } catch (let error) {
  139. self.displayMessageReceived("No message received. \(error)")
  140. }
  141. }
  142. }
  143. else if (self.callSelectButton.selectedSegment == 2) {
  144. // STREAMING COLLECT
  145. do {
  146. if (!self.nowStreaming) {
  147. let collectCall = try service.collect() {call in
  148. print("Started collect \(call)")
  149. }
  150. self.collectCall = collectCall
  151. self.nowStreaming = true
  152. DispatchQueue.main.async {
  153. self.closeButton.isEnabled = true
  154. }
  155. }
  156. try self.sendCollectMessage()
  157. } catch (let error) {
  158. self.displayMessageReceived("No message received. \(error)")
  159. }
  160. }
  161. else if (self.callSelectButton.selectedSegment == 3) {
  162. // STREAMING UPDATE
  163. do {
  164. if (!self.nowStreaming) {
  165. let updateCall = try service.update() {call in
  166. print("Started update \(call)")
  167. }
  168. self.updateCall = updateCall
  169. self.nowStreaming = true
  170. try self.receiveUpdateMessages()
  171. DispatchQueue.main.async {
  172. self.closeButton.isEnabled = true
  173. }
  174. }
  175. try self.sendUpdateMessage()
  176. } catch (let error) {
  177. self.displayMessageReceived("No message received. \(error)")
  178. }
  179. }
  180. }
  181. func receiveExpandMessages() throws -> Void {
  182. guard let expandCall = expandCall else {
  183. return
  184. }
  185. try expandCall.receive() {response, error in
  186. if let responseMessage = response {
  187. self.displayMessageReceived(responseMessage.text)
  188. try! self.receiveExpandMessages()
  189. } else if let error = error {
  190. switch error {
  191. case .endOfStream:
  192. self.displayMessageReceived("Done.")
  193. default:
  194. self.displayMessageReceived("No message received. \(error)")
  195. }
  196. }
  197. }
  198. }
  199. func sendCollectMessage() throws {
  200. if let collectCall = collectCall {
  201. var requestMessage = Echo_EchoRequest()
  202. requestMessage.text = self.messageField.stringValue
  203. self.displayMessageSent(requestMessage.text)
  204. try collectCall.send(requestMessage) {error in print(error)}
  205. }
  206. }
  207. func sendUpdateMessage() throws {
  208. if let updateCall = updateCall {
  209. var requestMessage = Echo_EchoRequest()
  210. requestMessage.text = self.messageField.stringValue
  211. self.displayMessageSent(requestMessage.text)
  212. try updateCall.send(requestMessage) {error in print(error)}
  213. }
  214. }
  215. func receiveUpdateMessages() throws -> Void {
  216. guard let updateCall = updateCall else {
  217. return
  218. }
  219. try updateCall.receive() {response, error in
  220. if let responseMessage = response {
  221. self.displayMessageReceived(responseMessage.text)
  222. try! self.receiveUpdateMessages()
  223. } else if let error = error {
  224. switch error {
  225. case .endOfStream:
  226. self.displayMessageReceived("Done.")
  227. default:
  228. self.displayMessageReceived("No message received. \(error)")
  229. }
  230. }
  231. }
  232. }
  233. func sendClose() throws {
  234. if let updateCall = updateCall {
  235. try updateCall.closeSend() {
  236. self.updateCall = nil
  237. self.nowStreaming = false
  238. DispatchQueue.main.async {
  239. self.closeButton.isEnabled = false
  240. }
  241. }
  242. }
  243. if let collectCall = collectCall {
  244. do {
  245. try collectCall.closeAndReceive() {response, error in
  246. if let response = response {
  247. self.displayMessageReceived(response.text)
  248. } else if let error = error {
  249. self.displayMessageReceived("No message received. \(error)")
  250. }
  251. self.collectCall = nil
  252. self.nowStreaming = false
  253. DispatchQueue.main.async {
  254. self.closeButton.isEnabled = false
  255. }
  256. }
  257. }
  258. }
  259. }
  260. }