main.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright 2017, 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 Foundation
  17. import Dispatch
  18. import gRPC
  19. import Commander
  20. // Common flags and options
  21. let sslFlag = Flag("ssl", description:"if true, use SSL for connections")
  22. func addressOption(_ address:String) -> Option<String> {
  23. return Option("address", default:address, description:"address of server")
  24. }
  25. let portOption = Option("port", default:"8081",
  26. description:"port of server")
  27. let messageOption = Option("message", default:"Testing 1 2 3",
  28. description:"message to send")
  29. // Helper function for client actions
  30. func buildEchoService(_ ssl:Bool, _ address:String, _ port:String, _ message:String)
  31. -> Echo_EchoService {
  32. var service : Echo_EchoService
  33. if ssl {
  34. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  35. let certificates = try! String(contentsOf: certificateURL)
  36. service = Echo_EchoService(address:address + ":" + port,
  37. certificates:certificates,
  38. host:"example.com")
  39. service.host = "example.com"
  40. } else {
  41. service = Echo_EchoService(address:address + ":" + port, secure:false)
  42. }
  43. service.metadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
  44. "x-ios-bundle-identifier":"io.grpc.echo"])
  45. return service
  46. }
  47. Group {
  48. $0.command("serve",
  49. sslFlag,
  50. addressOption("0.0.0.0"),
  51. portOption,
  52. description:"Run an echo server.")
  53. { (ssl, address, port) in
  54. let sem = DispatchSemaphore(value: 0)
  55. let echoProvider = EchoProvider()
  56. if ssl {
  57. print("Starting secure server")
  58. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  59. let keyURL = URL(fileURLWithPath:"ssl.key")
  60. if let echoServer = Echo_EchoServer(address:address + ":" + port,
  61. certificateURL:certificateURL,
  62. keyURL:keyURL,
  63. provider:echoProvider) {
  64. echoServer.start()
  65. }
  66. } else {
  67. print("Starting insecure server")
  68. let echoServer = Echo_EchoServer(address:address + ":" + port,
  69. provider:echoProvider)
  70. echoServer.start()
  71. }
  72. // This blocks to keep the main thread from finishing while the server runs,
  73. // but the server never exits. Kill the process to stop it.
  74. _ = sem.wait(timeout: DispatchTime.distantFuture)
  75. }
  76. $0.command("get", sslFlag, addressOption("localhost"), portOption, messageOption,
  77. description: "Perform a unary get().")
  78. {(ssl, address, port, message) in
  79. let service = buildEchoService(ssl, address, port, message)
  80. var requestMessage = Echo_EchoRequest()
  81. requestMessage.text = message
  82. print("Sending: " + requestMessage.text)
  83. let responseMessage = try service.get(requestMessage)
  84. print("get received: " + responseMessage.text)
  85. }
  86. $0.command("expand", sslFlag, addressOption("localhost"), portOption, messageOption,
  87. description: "Perform a server-streaming expand().")
  88. {(ssl, address, port, message) in
  89. let service = buildEchoService(ssl, address, port, message)
  90. var requestMessage = Echo_EchoRequest()
  91. requestMessage.text = message
  92. print("Sending: " + requestMessage.text)
  93. let sem = DispatchSemaphore(value: 0)
  94. let expandCall = try service.expand(requestMessage) {result in
  95. print("result \(result)")
  96. sem.signal()
  97. }
  98. _ = sem.wait(timeout: DispatchTime.distantFuture)
  99. var running = true
  100. while running {
  101. do {
  102. let responseMessage = try expandCall.receive()
  103. print("Received: \(responseMessage.text)")
  104. } catch Echo_EchoClientError.endOfStream {
  105. print("expand closed")
  106. running = false
  107. }
  108. }
  109. }
  110. $0.command("collect", sslFlag, addressOption("localhost"), portOption, messageOption,
  111. description: "Perform a client-streaming collect().")
  112. {(ssl, address, port, message) in
  113. let service = buildEchoService(ssl, address, port, message)
  114. let sem = DispatchSemaphore(value: 0)
  115. let collectCall = try service.collect() {result in
  116. print("result \(result)")
  117. sem.signal()
  118. }
  119. _ = sem.wait(timeout: DispatchTime.distantFuture)
  120. let parts = message.components(separatedBy:" ")
  121. for part in parts {
  122. var requestMessage = Echo_EchoRequest()
  123. requestMessage.text = part
  124. print("Sending: " + part)
  125. try collectCall.send(requestMessage) {error in print(error)}
  126. sleep(1)
  127. }
  128. let responseMessage = try collectCall.closeAndReceive()
  129. print("Received: \(responseMessage.text)")
  130. }
  131. $0.command("update", sslFlag, addressOption("localhost"), portOption, messageOption,
  132. description: "Perform a bidirectional-streaming update().")
  133. {(ssl, address, port, message) in
  134. let service = buildEchoService(ssl, address, port, message)
  135. let sem = DispatchSemaphore(value: 0)
  136. let updateCall = try service.update() {result in
  137. print("result \(result)")
  138. sem.signal()
  139. }
  140. _ = sem.wait(timeout: DispatchTime.distantFuture)
  141. DispatchQueue.global().async {
  142. var running = true
  143. while running {
  144. do {
  145. let responseMessage = try updateCall.receive()
  146. print("Received: \(responseMessage.text)")
  147. } catch Echo_EchoClientError.endOfStream {
  148. print("update closed")
  149. sem.signal()
  150. running = false
  151. } catch (let error) {
  152. print("error: \(error)")
  153. }
  154. }
  155. }
  156. let parts = message.components(separatedBy:" ")
  157. for part in parts {
  158. var requestMessage = Echo_EchoRequest()
  159. requestMessage.text = part
  160. print("Sending: " + requestMessage.text)
  161. try updateCall.send(requestMessage) {error in print(error)}
  162. sleep(1)
  163. }
  164. try updateCall.closeSend()
  165. _ = sem.wait(timeout: DispatchTime.distantFuture)
  166. }
  167. }.run()