main.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2019, 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 NIO
  20. import NIOSSL
  21. import GRPC
  22. import GRPCSampleData
  23. // Common flags and options
  24. let sslFlag = Flag("ssl", description: "if true, use SSL for connections")
  25. func addressOption(_ address: String) -> Option<String> {
  26. return Option("address", default: address, description: "address of server")
  27. }
  28. let portOption = Option("port", default: 8080)
  29. let messageOption = Option("message",
  30. default: "Testing 1 2 3",
  31. description: "message to send")
  32. func makeClientTLSConfiguration() -> ClientConnection.Configuration.TLS {
  33. let caCert = SampleCertificate.ca
  34. let clientCert = SampleCertificate.client
  35. precondition(!caCert.isExpired && !clientCert.isExpired,
  36. "SSL certificates are expired. Please submit an issue at https://github.com/grpc/grpc-swift.")
  37. return .init(certificateChain: [.certificate(clientCert.certificate)],
  38. privateKey: .privateKey(SamplePrivateKey.client),
  39. trustRoots: .certificates([caCert.certificate]),
  40. certificateVerification: .noHostnameVerification)
  41. }
  42. func makeServerTLSConfiguration() -> Server.Configuration.TLS {
  43. let caCert = SampleCertificate.ca
  44. let serverCert = SampleCertificate.server
  45. precondition(!caCert.isExpired && !serverCert.isExpired,
  46. "SSL certificates are expired. Please submit an issue at https://github.com/grpc/grpc-swift.")
  47. return .init(certificateChain: [.certificate(serverCert.certificate)],
  48. privateKey: .privateKey(SamplePrivateKey.server),
  49. trustRoots: .certificates([caCert.certificate]))
  50. }
  51. /// Create en `EchoClient` and wait for it to initialize. Returns nil if initialisation fails.
  52. func makeEchoClient(address: String, port: Int, ssl: Bool) -> Echo_EchoServiceClient? {
  53. let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  54. let configuration = ClientConnection.Configuration(
  55. target: .hostAndPort(address, port),
  56. eventLoopGroup: eventLoopGroup,
  57. tls: ssl ? makeClientTLSConfiguration() : nil)
  58. return Echo_EchoServiceClient(connection: ClientConnection(configuration: configuration))
  59. }
  60. Group {
  61. $0.command("serve",
  62. sslFlag,
  63. addressOption("localhost"),
  64. portOption,
  65. description: "Run an echo server.") { ssl, address, port in
  66. let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  67. var configuration = Server.Configuration(
  68. target: .hostAndPort(address, port),
  69. eventLoopGroup: eventLoopGroup,
  70. serviceProviders: [EchoProvider()])
  71. if ssl {
  72. print("starting secure server")
  73. configuration.tls = makeServerTLSConfiguration()
  74. } else {
  75. print("starting insecure server")
  76. }
  77. let server = try! Server.start(configuration: configuration)
  78. .wait()
  79. // This blocks to keep the main thread from finishing while the server runs,
  80. // but the server never exits. Kill the process to stop it.
  81. try server.onClose.wait()
  82. }
  83. $0.command(
  84. "get",
  85. sslFlag,
  86. addressOption("localhost"),
  87. portOption,
  88. messageOption,
  89. description: "Perform a unary get()."
  90. ) { ssl, address, port, message in
  91. print("calling get")
  92. guard let echo = makeEchoClient(address: address, port: port, ssl: ssl) else { return }
  93. var requestMessage = Echo_EchoRequest()
  94. requestMessage.text = message
  95. print("get sending: \(requestMessage.text)")
  96. let get = echo.get(requestMessage)
  97. get.response.whenSuccess { response in
  98. print("get received: \(response.text)")
  99. }
  100. get.response.whenFailure { error in
  101. print("get response failed with error: \(error)")
  102. }
  103. // wait() on the status to stop the program from exiting.
  104. do {
  105. let status = try get.status.wait()
  106. print("get completed with status: \(status)")
  107. } catch {
  108. print("get status failed with error: \(error)")
  109. }
  110. }
  111. $0.command(
  112. "expand",
  113. sslFlag,
  114. addressOption("localhost"),
  115. portOption,
  116. messageOption,
  117. description: "Perform a server-streaming expand()."
  118. ) { ssl, address, port, message in
  119. print("calling expand")
  120. guard let echo = makeEchoClient(address: address, port: port, ssl: ssl) else { return }
  121. let requestMessage = Echo_EchoRequest.with { $0.text = message }
  122. print("expand sending: \(requestMessage.text)")
  123. let expand = echo.expand(requestMessage) { response in
  124. print("expand received: \(response.text)")
  125. }
  126. // wait() on the status to stop the program from exiting.
  127. do {
  128. let status = try expand.status.wait()
  129. print("expand completed with status: \(status)")
  130. } catch {
  131. print("expand status failed with error: \(error)")
  132. }
  133. }
  134. $0.command(
  135. "collect",
  136. sslFlag,
  137. addressOption("localhost"),
  138. portOption,
  139. messageOption,
  140. description: "Perform a client-streaming collect()."
  141. ) { ssl, address, port, message in
  142. print("calling collect")
  143. guard let echo = makeEchoClient(address: address, port: port, ssl: ssl) else { return }
  144. let collect = echo.collect()
  145. var queue = collect.newMessageQueue()
  146. for part in message.components(separatedBy: " ") {
  147. var requestMessage = Echo_EchoRequest()
  148. requestMessage.text = part
  149. print("collect sending: \(requestMessage.text)")
  150. queue = queue.flatMap { collect.sendMessage(requestMessage) }
  151. }
  152. queue.whenSuccess { collect.sendEnd(promise: nil) }
  153. collect.response.whenSuccess { respone in
  154. print("collect received: \(respone.text)")
  155. }
  156. collect.response.whenFailure { error in
  157. print("collect response failed with error: \(error)")
  158. }
  159. // wait() on the status to stop the program from exiting.
  160. do {
  161. let status = try collect.status.wait()
  162. print("collect completed with status: \(status)")
  163. } catch {
  164. print("collect status failed with error: \(error)")
  165. }
  166. }
  167. $0.command(
  168. "update",
  169. sslFlag,
  170. addressOption("localhost"),
  171. portOption,
  172. messageOption,
  173. description: "Perform a bidirectional-streaming update()."
  174. ) { ssl, address, port, message in
  175. print("calling update")
  176. guard let echo = makeEchoClient(address: address, port: port, ssl: ssl) else { return }
  177. let update = echo.update { response in
  178. print("update received: \(response.text)")
  179. }
  180. var queue = update.newMessageQueue()
  181. for part in message.components(separatedBy: " ") {
  182. var requestMessage = Echo_EchoRequest()
  183. requestMessage.text = part
  184. print("update sending: \(requestMessage.text)")
  185. queue = queue.flatMap { update.sendMessage(requestMessage) }
  186. }
  187. queue.whenSuccess { update.sendEnd(promise: nil) }
  188. // wait() on the status to stop the program from exiting.
  189. do {
  190. let status = try update.status.wait()
  191. print("update completed with status: \(status)")
  192. } catch {
  193. print("update status failed with error: \(error)")
  194. }
  195. }
  196. }.run()