main.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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(text:message)
  106. print("Sending: " + requestMessage.text)
  107. let responseMessage = try service.get(requestMessage)
  108. print("get received: " + responseMessage.text)
  109. }
  110. // Server streaming
  111. if client == "expand" {
  112. let requestMessage = Echo_EchoRequest(text:message)
  113. print("Sending: " + requestMessage.text)
  114. let expandCall = try service.expand(requestMessage)
  115. var running = true
  116. while running {
  117. do {
  118. let responseMessage = try expandCall.Receive()
  119. print("Received: \(responseMessage.text)")
  120. } catch Echo_EchoClientError.endOfStream {
  121. print("expand closed")
  122. running = false
  123. }
  124. }
  125. }
  126. // Client streaming
  127. if client == "collect" {
  128. let collectCall = try service.collect()
  129. let parts = message.components(separatedBy:" ")
  130. for part in parts {
  131. let requestMessage = Echo_EchoRequest(text:part)
  132. print("Sending: " + part)
  133. try collectCall.Send(requestMessage)
  134. sleep(1)
  135. }
  136. let responseMessage = try collectCall.CloseAndReceive()
  137. print("Received: \(responseMessage.text)")
  138. }
  139. // Bidirectional streaming
  140. if client == "update" {
  141. let updateCall = try service.update()
  142. DispatchQueue.global().async {
  143. var running = true
  144. while running {
  145. do {
  146. let responseMessage = try updateCall.Receive()
  147. print("Received: \(responseMessage.text)")
  148. } catch Echo_EchoClientError.endOfStream {
  149. print("update closed")
  150. latch.signal()
  151. break
  152. } catch (let error) {
  153. print("error: \(error)")
  154. }
  155. }
  156. }
  157. let parts = message.components(separatedBy:" ")
  158. for part in parts {
  159. let requestMessage = Echo_EchoRequest(text:part)
  160. print("Sending: " + requestMessage.text)
  161. try updateCall.Send(requestMessage)
  162. sleep(1)
  163. }
  164. try updateCall.CloseSend()
  165. // Wait for the call to complete.
  166. latch.wait()
  167. }
  168. }
  169. if test {
  170. print("self test")
  171. }