main.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 Commander
  17. import Dispatch
  18. import Foundation
  19. import gRPC
  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",
  26. default: "8080",
  27. description: "port of server")
  28. let messageOption = Option("message",
  29. default: "Testing 1 2 3",
  30. description: "message to send")
  31. // Helper function for client actions
  32. func buildEchoService(_ ssl: Bool, _ address: String, _ port: String, _: String)
  33. -> Echo_EchoServiceClient {
  34. var service: Echo_EchoServiceClient
  35. if ssl {
  36. let certificateURL = URL(fileURLWithPath: "ssl.crt")
  37. let certificates = try! String(contentsOf: certificateURL)
  38. service = Echo_EchoServiceClient(address: address + ":" + port,
  39. certificates: certificates,
  40. host: "example.com")
  41. service.host = "example.com"
  42. } else {
  43. service = Echo_EchoServiceClient(address: address + ":" + port, secure: false)
  44. }
  45. service.metadata = Metadata([
  46. "x-goog-api-key": "YOUR_API_KEY",
  47. "x-ios-bundle-identifier": "io.grpc.echo"
  48. ])
  49. return service
  50. }
  51. Group {
  52. $0.command("serve",
  53. sslFlag,
  54. addressOption("0.0.0.0"),
  55. portOption,
  56. description: "Run an echo server.") { ssl, address, port in
  57. let sem = DispatchSemaphore(value: 0)
  58. let echoProvider = EchoProvider()
  59. var echoServer: Echo_EchoServer?
  60. if ssl {
  61. print("starting secure server")
  62. let certificateURL = URL(fileURLWithPath: "ssl.crt")
  63. let keyURL = URL(fileURLWithPath: "ssl.key")
  64. echoServer = Echo_EchoServer(address: address + ":" + port,
  65. certificateURL: certificateURL,
  66. keyURL: keyURL,
  67. provider: echoProvider)
  68. echoServer?.start()
  69. } else {
  70. print("starting insecure server")
  71. echoServer = Echo_EchoServer(address: address + ":" + port,
  72. provider: echoProvider)
  73. echoServer?.start()
  74. }
  75. // This blocks to keep the main thread from finishing while the server runs,
  76. // but the server never exits. Kill the process to stop it.
  77. _ = sem.wait(timeout: DispatchTime.distantFuture)
  78. // This suppresses a "variable echoServer was written to, but never read" warning.
  79. _ = echoServer
  80. // And this ensures that echoServer doesn't get deallocated right after it is created.
  81. echoServer = nil
  82. }
  83. $0.command("get", sslFlag, addressOption("localhost"), portOption, messageOption,
  84. description: "Perform a unary get().") { ssl, address, port, message in
  85. let service = buildEchoService(ssl, address, port, message)
  86. var requestMessage = Echo_EchoRequest()
  87. requestMessage.text = message
  88. print("get sending: " + requestMessage.text)
  89. let responseMessage = try service.get(requestMessage)
  90. print("get received: " + responseMessage.text)
  91. }
  92. $0.command("expand", sslFlag, addressOption("localhost"), portOption, messageOption,
  93. description: "Perform a server-streaming expand().") { ssl, address, port, message in
  94. let service = buildEchoService(ssl, address, port, message)
  95. var requestMessage = Echo_EchoRequest()
  96. requestMessage.text = message
  97. print("expand sending: " + requestMessage.text)
  98. let sem = DispatchSemaphore(value: 0)
  99. var callResult : CallResult?
  100. let expandCall = try service.expand(requestMessage) { result in
  101. callResult = result
  102. sem.signal()
  103. }
  104. var running = true
  105. while running {
  106. do {
  107. let responseMessage = try expandCall.receive()
  108. print("expand received: \(responseMessage.text)")
  109. } catch ClientError.endOfStream {
  110. running = false
  111. }
  112. }
  113. _ = sem.wait(timeout: DispatchTime.distantFuture)
  114. if let statusCode = callResult?.statusCode {
  115. print("expand completed with code \(statusCode)")
  116. }
  117. }
  118. $0.command("collect", sslFlag, addressOption("localhost"), portOption, messageOption,
  119. description: "Perform a client-streaming collect().") { ssl, address, port, message in
  120. let service = buildEchoService(ssl, address, port, message)
  121. let sem = DispatchSemaphore(value: 0)
  122. var callResult : CallResult?
  123. let collectCall = try service.collect { result in
  124. callResult = result
  125. sem.signal()
  126. }
  127. let sendCountMutex = Mutex()
  128. var sendCount = 0
  129. let parts = message.components(separatedBy: " ")
  130. for part in parts {
  131. var requestMessage = Echo_EchoRequest()
  132. requestMessage.text = part
  133. print("collect sending: " + part)
  134. try collectCall.send(requestMessage) {
  135. error in
  136. sendCountMutex.synchronize {
  137. sendCount = sendCount + 1
  138. }
  139. if let error = error {
  140. print("collect send error \(error)")
  141. }
  142. }
  143. }
  144. // don't close until all sends have completed
  145. var waiting = true
  146. while (waiting) {
  147. sendCountMutex.synchronize {
  148. if sendCount == parts.count {
  149. waiting = false
  150. }
  151. }
  152. }
  153. let responseMessage = try collectCall.closeAndReceive()
  154. print("collect received: \(responseMessage.text)")
  155. _ = sem.wait(timeout: DispatchTime.distantFuture)
  156. if let statusCode = callResult?.statusCode {
  157. print("collect completed with status \(statusCode)")
  158. }
  159. }
  160. $0.command("update", sslFlag, addressOption("localhost"), portOption, messageOption,
  161. description: "Perform a bidirectional-streaming update().") { ssl, address, port, message in
  162. let service = buildEchoService(ssl, address, port, message)
  163. let sem = DispatchSemaphore(value: 0)
  164. var callResult : CallResult?
  165. let updateCall = try service.update { result in
  166. callResult = result
  167. sem.signal()
  168. }
  169. let responsesMutex = Mutex()
  170. var responses : [String] = []
  171. DispatchQueue.global().async {
  172. var running = true
  173. while running {
  174. do {
  175. let responseMessage = try updateCall.receive()
  176. responsesMutex.synchronize {
  177. responses.append("update received: \(responseMessage.text)")
  178. }
  179. } catch ClientError.endOfStream {
  180. running = false
  181. } catch (let error) {
  182. responsesMutex.synchronize {
  183. responses.append("update receive error: \(error)")
  184. }
  185. }
  186. }
  187. }
  188. let parts = message.components(separatedBy: " ")
  189. for part in parts {
  190. var requestMessage = Echo_EchoRequest()
  191. requestMessage.text = part
  192. print("update sending: " + requestMessage.text)
  193. try updateCall.send(requestMessage) {
  194. error in
  195. if let error = error {
  196. print("update send error: \(error)")
  197. }
  198. }
  199. }
  200. // don't close until last update is received
  201. var waiting = true
  202. while (waiting) {
  203. responsesMutex.synchronize {
  204. if responses.count == parts.count {
  205. waiting = false
  206. }
  207. }
  208. }
  209. try updateCall.closeSend()
  210. _ = sem.wait(timeout: DispatchTime.distantFuture)
  211. for response in responses {
  212. print(response)
  213. }
  214. if let statusCode = callResult?.statusCode {
  215. print("update completed with status \(statusCode)")
  216. }
  217. }
  218. }.run()