2
0

main.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 SwiftGRPC
  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. let arguments: [Channel.Argument] = [.sslTargetNameOverride("example.com")]
  39. service = Echo_EchoServiceClient(address: address + ":" + port,
  40. certificates: certificates,
  41. arguments: arguments)
  42. service.host = "example.com"
  43. } else {
  44. service = Echo_EchoServiceClient(address: address + ":" + port, secure: false)
  45. }
  46. service.metadata = try! Metadata([
  47. "x-goog-api-key": "YOUR_API_KEY",
  48. "x-ios-bundle-identifier": "io.grpc.echo"
  49. ])
  50. return service
  51. }
  52. Group {
  53. $0.command("serve",
  54. sslFlag,
  55. addressOption("0.0.0.0"),
  56. portOption,
  57. description: "Run an echo server.") { ssl, address, port in
  58. let sem = DispatchSemaphore(value: 0)
  59. let echoProvider = EchoProvider()
  60. var echoServer: Echo_EchoServer?
  61. if ssl {
  62. print("starting secure server")
  63. let certificateURL = URL(fileURLWithPath: "ssl.crt")
  64. let keyURL = URL(fileURLWithPath: "ssl.key")
  65. echoServer = Echo_EchoServer(address: address + ":" + port,
  66. certificateURL: certificateURL,
  67. keyURL: keyURL,
  68. provider: echoProvider)
  69. echoServer?.start()
  70. } else {
  71. print("starting insecure server")
  72. echoServer = Echo_EchoServer(address: address + ":" + port,
  73. provider: echoProvider)
  74. echoServer?.start()
  75. }
  76. // This blocks to keep the main thread from finishing while the server runs,
  77. // but the server never exits. Kill the process to stop it.
  78. _ = sem.wait()
  79. // This suppresses a "variable echoServer was written to, but never read" warning.
  80. _ = echoServer
  81. // And this ensures that echoServer doesn't get deallocated right after it is created.
  82. echoServer = nil
  83. }
  84. $0.command("get", sslFlag, addressOption("localhost"), portOption, messageOption,
  85. description: "Perform a unary get().") { ssl, address, port, message in
  86. print("calling get")
  87. let service = buildEchoService(ssl, address, port, message)
  88. var requestMessage = Echo_EchoRequest()
  89. requestMessage.text = message
  90. print("get sending: " + requestMessage.text)
  91. let responseMessage = try service.get(requestMessage)
  92. print("get received: " + responseMessage.text)
  93. }
  94. $0.command("expand", sslFlag, addressOption("localhost"), portOption, messageOption,
  95. description: "Perform a server-streaming expand().") { ssl, address, port, message in
  96. print("calling expand")
  97. let service = buildEchoService(ssl, address, port, message)
  98. var requestMessage = Echo_EchoRequest()
  99. requestMessage.text = message
  100. print("expand sending: " + requestMessage.text)
  101. let sem = DispatchSemaphore(value: 0)
  102. var callResult : CallResult?
  103. let expandCall = try service.expand(requestMessage) { result in
  104. callResult = result
  105. sem.signal()
  106. }
  107. while true {
  108. guard let responseMessage = try expandCall.receive()
  109. else { break } // End of stream
  110. print("expand received: \(responseMessage.text)")
  111. }
  112. _ = sem.wait()
  113. if let statusCode = callResult?.statusCode {
  114. print("expand completed with code \(statusCode)")
  115. }
  116. }
  117. $0.command("collect", sslFlag, addressOption("localhost"), portOption, messageOption,
  118. description: "Perform a client-streaming collect().") { ssl, address, port, message in
  119. print("calling collect")
  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 parts = message.components(separatedBy: " ")
  128. for part in parts {
  129. var requestMessage = Echo_EchoRequest()
  130. requestMessage.text = part
  131. print("collect sending: " + part)
  132. try collectCall.send(requestMessage) { error in
  133. if let error = error {
  134. print("collect send error \(error)")
  135. }
  136. }
  137. }
  138. collectCall.waitForSendOperationsToFinish()
  139. let responseMessage = try collectCall.closeAndReceive()
  140. print("collect received: \(responseMessage.text)")
  141. _ = sem.wait()
  142. if let statusCode = callResult?.statusCode {
  143. print("collect completed with code \(statusCode)")
  144. }
  145. }
  146. $0.command("update", sslFlag, addressOption("localhost"), portOption, messageOption,
  147. description: "Perform a bidirectional-streaming update().") { ssl, address, port, message in
  148. print("calling update")
  149. let service = buildEchoService(ssl, address, port, message)
  150. let sem = DispatchSemaphore(value: 0)
  151. var callResult : CallResult?
  152. let updateCall = try service.update { result in
  153. callResult = result
  154. sem.signal()
  155. }
  156. let parts = message.components(separatedBy: " ")
  157. for part in parts {
  158. var requestMessage = Echo_EchoRequest()
  159. requestMessage.text = part
  160. print("update sending: " + requestMessage.text)
  161. try updateCall.send(requestMessage) { error in
  162. if let error = error {
  163. print("update send error \(error)")
  164. }
  165. }
  166. }
  167. updateCall.waitForSendOperationsToFinish()
  168. try updateCall.closeSend()
  169. while true {
  170. guard let responseMessage = try updateCall.receive()
  171. else { break } // End of stream
  172. print("update received: \(responseMessage.text)")
  173. }
  174. _ = sem.wait()
  175. if let statusCode = callResult?.statusCode {
  176. print("update completed with code \(statusCode)")
  177. }
  178. }
  179. }.run()