main.swift 5.7 KB

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