main.swift 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. import Foundation
  34. import gRPC
  35. import CgRPC
  36. import Dispatch
  37. print("\(CommandLine.arguments)")
  38. // server options
  39. var server : Bool = false
  40. // client options
  41. var client : String = ""
  42. var message : String = "Testing 1 2 3"
  43. // self-test mode
  44. var test : Bool = false
  45. // general configuration
  46. var useSSL : Bool = false
  47. var i : Int = 0
  48. while i < Int(CommandLine.argc) {
  49. let arg = CommandLine.arguments[i]
  50. i = i + 1
  51. if i == 1 {
  52. continue // skip the first argument
  53. }
  54. if arg == "test" {
  55. test = true
  56. } else if arg == "serve" {
  57. server = true
  58. } else if (arg == "get") || (arg == "expand") || (arg == "collect") || (arg == "update") {
  59. client = arg
  60. } else if arg == "-ssl" {
  61. useSSL = true
  62. } else if arg == "-m" && (i < Int(CommandLine.argc)) {
  63. message = CommandLine.arguments[i]
  64. i = i + 1
  65. }
  66. }
  67. var latch = CountDownLatch(1)
  68. gRPC.initialize()
  69. if server {
  70. let echoProvider = EchoProvider()
  71. var echoServer: Echo_EchoServer!
  72. if useSSL {
  73. print("Starting secure server")
  74. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  75. let keyURL = URL(fileURLWithPath:"ssl.key")
  76. echoServer = Echo_EchoServer(address:"localhost:8443",
  77. certificateURL:certificateURL,
  78. keyURL:keyURL,
  79. provider:echoProvider)
  80. } else {
  81. print("Starting insecure server")
  82. echoServer = Echo_EchoServer(address:"localhost:8081",
  83. provider:echoProvider)
  84. }
  85. echoServer.start()
  86. // Block to keep the main thread from finishing while the server runs.
  87. // This server never exits. Kill the process to stop it.
  88. latch.wait()
  89. }
  90. if client != "" {
  91. print("Starting client")
  92. var service : Echo_EchoService
  93. if useSSL {
  94. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  95. let certificates = try! String(contentsOf: certificateURL)
  96. service = Echo_EchoService(address:"localhost:8443", certificates:certificates, host:"example.com")
  97. service.host = "example.com" // sample override
  98. } else {
  99. service = Echo_EchoService(address:"localhost:8081")
  100. }
  101. let requestMetadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
  102. "x-ios-bundle-identifier":"com.google.echo"])
  103. // Unary
  104. if client == "get" {
  105. var requestMessage = Echo_EchoRequest()
  106. requestMessage.text = message
  107. print("Sending: " + requestMessage.text)
  108. let responseMessage = try service.get(requestMessage)
  109. print("get received: " + responseMessage.text)
  110. }
  111. // Server streaming
  112. if client == "expand" {
  113. var requestMessage = Echo_EchoRequest()
  114. requestMessage.text = message
  115. print("Sending: " + requestMessage.text)
  116. let expandCall = try service.expand(requestMessage) {result in }
  117. var running = true
  118. while running {
  119. do {
  120. let responseMessage = try expandCall.receive()
  121. print("Received: \(responseMessage.text)")
  122. } catch Echo_EchoClientError.endOfStream {
  123. print("expand closed")
  124. running = false
  125. }
  126. }
  127. }
  128. // Client streaming
  129. if client == "collect" {
  130. let collectCall = try service.collect() {result in }
  131. let parts = message.components(separatedBy:" ")
  132. for part in parts {
  133. var requestMessage = Echo_EchoRequest()
  134. requestMessage.text = part
  135. print("Sending: " + part)
  136. try collectCall.send(requestMessage) {error in print(error)}
  137. sleep(1)
  138. }
  139. let responseMessage = try collectCall.closeAndReceive()
  140. print("Received: \(responseMessage.text)")
  141. }
  142. // Bidirectional streaming
  143. if client == "update" {
  144. let updateCall = try service.update() {result in}
  145. DispatchQueue.global().async {
  146. var running = true
  147. while running {
  148. do {
  149. let responseMessage = try updateCall.receive()
  150. print("Received: \(responseMessage.text)")
  151. } catch Echo_EchoClientError.endOfStream {
  152. print("update closed")
  153. latch.signal()
  154. break
  155. } catch (let error) {
  156. print("error: \(error)")
  157. }
  158. }
  159. }
  160. let parts = message.components(separatedBy:" ")
  161. for part in parts {
  162. var requestMessage = Echo_EchoRequest()
  163. requestMessage.text = part
  164. print("Sending: " + requestMessage.text)
  165. try updateCall.send(requestMessage) {error in print(error)}
  166. sleep(1)
  167. }
  168. try updateCall.closeSend()
  169. // Wait for the call to complete.
  170. latch.wait()
  171. }
  172. }
  173. if test {
  174. print("self test")
  175. }