main.swift 5.7 KB

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