main.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: "8081",
  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_EchoService {
  34. var service: Echo_EchoService
  35. if ssl {
  36. let certificateURL = URL(fileURLWithPath: "ssl.crt")
  37. let certificates = try! String(contentsOf: certificateURL)
  38. service = Echo_EchoService(address: address + ":" + port,
  39. certificates: certificates,
  40. host: "example.com")
  41. service.host = "example.com"
  42. } else {
  43. service = Echo_EchoService(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", sslFlag, addressOption("0.0.0.0"), portOption,
  53. description: "Run an echo server.") { 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().") { ssl, address, port, message in
  78. let service = buildEchoService(ssl, address, port, message)
  79. var requestMessage = Echo_EchoRequest()
  80. requestMessage.text = message
  81. print("Sending: " + requestMessage.text)
  82. let responseMessage = try service.get(requestMessage)
  83. print("get received: " + responseMessage.text)
  84. }
  85. $0.command("expand", sslFlag, addressOption("localhost"), portOption, messageOption,
  86. description: "Perform a server-streaming expand().") { ssl, address, port, message in
  87. let service = buildEchoService(ssl, address, port, message)
  88. var requestMessage = Echo_EchoRequest()
  89. requestMessage.text = message
  90. print("Sending: " + requestMessage.text)
  91. let sem = DispatchSemaphore(value: 0)
  92. let expandCall = try service.expand(requestMessage) { result in
  93. print("result \(result)")
  94. sem.signal()
  95. }
  96. _ = sem.wait(timeout: DispatchTime.distantFuture)
  97. var running = true
  98. while running {
  99. do {
  100. let responseMessage = try expandCall.receive()
  101. print("Received: \(responseMessage.text)")
  102. } catch Echo_EchoClientError.endOfStream {
  103. print("expand closed")
  104. running = false
  105. }
  106. }
  107. }
  108. $0.command("collect", sslFlag, addressOption("localhost"), portOption, messageOption,
  109. description: "Perform a client-streaming collect().") { ssl, address, port, message in
  110. let service = buildEchoService(ssl, address, port, message)
  111. let sem = DispatchSemaphore(value: 0)
  112. let collectCall = try service.collect { result in
  113. print("result \(result)")
  114. sem.signal()
  115. }
  116. _ = sem.wait(timeout: DispatchTime.distantFuture)
  117. let parts = message.components(separatedBy: " ")
  118. for part in parts {
  119. var requestMessage = Echo_EchoRequest()
  120. requestMessage.text = part
  121. print("Sending: " + part)
  122. try collectCall.send(requestMessage) { error in print(error) }
  123. sleep(1)
  124. }
  125. let responseMessage = try collectCall.closeAndReceive()
  126. print("Received: \(responseMessage.text)")
  127. }
  128. $0.command("update", sslFlag, addressOption("localhost"), portOption, messageOption,
  129. description: "Perform a bidirectional-streaming update().") { ssl, address, port, message in
  130. let service = buildEchoService(ssl, address, port, message)
  131. let sem = DispatchSemaphore(value: 0)
  132. let updateCall = try service.update { result in
  133. print("result \(result)")
  134. sem.signal()
  135. }
  136. _ = sem.wait(timeout: DispatchTime.distantFuture)
  137. DispatchQueue.global().async {
  138. var running = true
  139. while running {
  140. do {
  141. let responseMessage = try updateCall.receive()
  142. print("Received: \(responseMessage.text)")
  143. } catch Echo_EchoClientError.endOfStream {
  144. print("update closed")
  145. sem.signal()
  146. running = false
  147. } catch (let error) {
  148. print("error: \(error)")
  149. }
  150. }
  151. }
  152. let parts = message.components(separatedBy: " ")
  153. for part in parts {
  154. var requestMessage = Echo_EchoRequest()
  155. requestMessage.text = part
  156. print("Sending: " + requestMessage.text)
  157. try updateCall.send(requestMessage) { error in print(error) }
  158. sleep(1)
  159. }
  160. try updateCall.closeSend()
  161. _ = sem.wait(timeout: DispatchTime.distantFuture)
  162. }
  163. }.run()