main.swift 5.7 KB

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