main.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright 2016, 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 gRPC
  18. import CgRPC
  19. import Dispatch
  20. print("\(CommandLine.arguments)")
  21. // server options
  22. var server : Bool = false
  23. // client options
  24. var client : String = ""
  25. var message : String = "Testing 1 2 3"
  26. var address : String = ""
  27. var port : String = ""
  28. // self-test mode
  29. var test : Bool = false
  30. // general configuration
  31. var useSSL : Bool = false
  32. var i : Int = 0
  33. while i < Int(CommandLine.argc) {
  34. let arg = CommandLine.arguments[i]
  35. i = i + 1
  36. if i == 1 {
  37. continue // skip the first argument
  38. }
  39. if arg == "test" {
  40. test = true
  41. } else if arg == "serve" {
  42. server = true
  43. } else if (arg == "get") || (arg == "expand") || (arg == "collect") || (arg == "update") {
  44. client = arg
  45. } else if arg == "-ssl" {
  46. useSSL = true
  47. } else if arg == "-m" && (i < Int(CommandLine.argc)) {
  48. message = CommandLine.arguments[i]
  49. i = i + 1
  50. } else if arg == "-a" && (i < Int(CommandLine.argc)) {
  51. address = CommandLine.arguments[i]
  52. i = i + 1
  53. } else if arg == "-p" && (i < Int(CommandLine.argc)) {
  54. port = CommandLine.arguments[i]
  55. i = i + 1
  56. }
  57. }
  58. if address == "" {
  59. if server {
  60. address = "0.0.0.0"
  61. } else {
  62. address = "localhost"
  63. }
  64. }
  65. if port == "" {
  66. if useSSL {
  67. port = "8443"
  68. } else {
  69. port = "8081"
  70. }
  71. }
  72. print(address + ":" + port + "\n")
  73. let sem = DispatchSemaphore(value: 0)
  74. gRPC.initialize()
  75. if server {
  76. let echoProvider = EchoProvider()
  77. var echoServer: Echo_EchoServer!
  78. if useSSL {
  79. print("Starting secure server")
  80. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  81. let keyURL = URL(fileURLWithPath:"ssl.key")
  82. echoServer = Echo_EchoServer(address:address + ":" + port,
  83. certificateURL:certificateURL,
  84. keyURL:keyURL,
  85. provider:echoProvider)
  86. } else {
  87. print("Starting insecure server")
  88. echoServer = Echo_EchoServer(address:address + ":" + port,
  89. provider:echoProvider)
  90. }
  91. echoServer.start()
  92. // Block to keep the main thread from finishing while the server runs.
  93. // This server never exits. Kill the process to stop it.
  94. _ = sem.wait(timeout: DispatchTime.distantFuture)
  95. }
  96. if client != "" {
  97. do {
  98. print("Starting client")
  99. var service : Echo_EchoService
  100. if useSSL {
  101. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  102. let certificates = try! String(contentsOf: certificateURL)
  103. service = Echo_EchoService(address:address + ":" + port, certificates:certificates, host:"example.com")
  104. service.host = "example.com" // sample override
  105. } else {
  106. service = Echo_EchoService(address:address + ":" + port)
  107. }
  108. service.metadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
  109. "x-ios-bundle-identifier":"com.google.echo"])
  110. // Unary
  111. if client == "get" {
  112. var requestMessage = Echo_EchoRequest()
  113. requestMessage.text = message
  114. print("Sending: " + requestMessage.text)
  115. let responseMessage = try service.get(requestMessage)
  116. print("get received: " + responseMessage.text)
  117. }
  118. // Server streaming
  119. if client == "expand" {
  120. var requestMessage = Echo_EchoRequest()
  121. requestMessage.text = message
  122. print("Sending: " + requestMessage.text)
  123. let expandCall = try service.expand(requestMessage) {result in }
  124. var running = true
  125. while running {
  126. do {
  127. let responseMessage = try expandCall.receive()
  128. print("Received: \(responseMessage.text)")
  129. } catch Echo_EchoClientError.endOfStream {
  130. print("expand closed")
  131. running = false
  132. }
  133. }
  134. }
  135. // Client streaming
  136. if client == "collect" {
  137. let collectCall = try service.collect() {result in }
  138. let parts = message.components(separatedBy:" ")
  139. for part in parts {
  140. var requestMessage = Echo_EchoRequest()
  141. requestMessage.text = part
  142. print("Sending: " + part)
  143. try collectCall.send(requestMessage) {error in print(error)}
  144. sleep(1)
  145. }
  146. let responseMessage = try collectCall.closeAndReceive()
  147. print("Received: \(responseMessage.text)")
  148. }
  149. // Bidirectional streaming
  150. if client == "update" {
  151. let updateCall = try service.update() {result in}
  152. DispatchQueue.global().async {
  153. var running = true
  154. while running {
  155. do {
  156. let responseMessage = try updateCall.receive()
  157. print("Received: \(responseMessage.text)")
  158. } catch Echo_EchoClientError.endOfStream {
  159. print("update closed")
  160. sem.signal()
  161. running = false
  162. } catch (let error) {
  163. print("error: \(error)")
  164. }
  165. }
  166. }
  167. let parts = message.components(separatedBy:" ")
  168. for part in parts {
  169. var requestMessage = Echo_EchoRequest()
  170. requestMessage.text = part
  171. print("Sending: " + requestMessage.text)
  172. try updateCall.send(requestMessage) {error in print(error)}
  173. sleep(1)
  174. }
  175. try updateCall.closeSend()
  176. // Wait for the call to complete.
  177. _ = sem.wait(timeout: DispatchTime.distantFuture)
  178. }
  179. } catch let error {
  180. print("error:\(error)")
  181. }
  182. }
  183. if test {
  184. print("self test")
  185. }