main.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 QuickProto
  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. var echoServer: EchoServer!
  66. if useSSL {
  67. print("Starting secure server")
  68. echoServer = EchoServer(address:"localhost:8443", secure:true)
  69. } else {
  70. print("Starting insecure server")
  71. echoServer = EchoServer(address:"localhost:8081", secure:false)
  72. }
  73. echoServer.start()
  74. // Block to keep the main thread from finishing while the server runs.
  75. // This server never exits. Kill the process to stop it.
  76. done.lock()
  77. done.wait()
  78. done.unlock()
  79. }
  80. if client != "" {
  81. print("Starting client")
  82. var service : EchoService
  83. if useSSL {
  84. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  85. let certificates = try! String(contentsOf: certificateURL)
  86. service = EchoService(address:"localhost:8443", certificates:certificates, host:"example.com")
  87. service.channel.host = "example.com" // sample override
  88. } else {
  89. service = EchoService(address:"localhost:8081")
  90. }
  91. let requestMetadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
  92. "x-ios-bundle-identifier":"com.google.echo"])
  93. // Unary
  94. if client == "get" {
  95. var requestMessage = Echo_EchoRequest(text:message)
  96. print("Sending: " + requestMessage.text)
  97. let result = service.get(requestMessage)
  98. switch result {
  99. case .Response(let responseMessage):
  100. print("get received: " + responseMessage.text)
  101. case .CallResult(let result):
  102. print("get: no message received. \(result)")
  103. case .Error(let error):
  104. print("get: no message received. \(error)")
  105. }
  106. }
  107. // Server streaming
  108. if client == "expand" {
  109. let requestMessage = Echo_EchoRequest(text:message)
  110. print("Sending: " + requestMessage.text)
  111. let expandCall = service.expand(requestMessage)
  112. var running = true
  113. while running {
  114. let result = expandCall.Recv()
  115. switch result {
  116. case .Response(let responseMessage):
  117. print("Received: \(responseMessage.text)")
  118. case .CallResult(let result):
  119. print("error: \(result)")
  120. case .Error(let error):
  121. if error == "EOM" {
  122. print("expand closed")
  123. running = false
  124. break
  125. } else {
  126. print("error: \(error)")
  127. }
  128. }
  129. }
  130. }
  131. // Client streaming
  132. if client == "collect" {
  133. let collectCall = service.collect()
  134. let parts = message.components(separatedBy:" ")
  135. for part in parts {
  136. let requestMessage = Echo_EchoRequest(text:part)
  137. print("Sending: " + part)
  138. collectCall.Send(requestMessage)
  139. sleep(1)
  140. }
  141. let result = collectCall.CloseAndRecv()
  142. switch result {
  143. case .Response(let responseMessage):
  144. print("Received: \(responseMessage.text)")
  145. case .CallResult(let result):
  146. print("error: \(result)")
  147. case .Error(let error):
  148. if error == "EOM" {
  149. print("collect closed")
  150. break
  151. } else {
  152. print("error: \(error)")
  153. }
  154. }
  155. }
  156. // Bidirectional streaming
  157. if client == "update" {
  158. let updateCall = service.update()
  159. DispatchQueue.global().async {
  160. var running = true
  161. while running {
  162. let result = updateCall.Recv()
  163. switch result {
  164. case .Response(let responseMessage):
  165. print("Received: \(responseMessage.text)")
  166. case .CallResult(let result):
  167. print("error: \(result)")
  168. case .Error(let error):
  169. if error == "EOM" {
  170. print("update closed")
  171. done.lock()
  172. done.signal()
  173. done.unlock()
  174. break
  175. } else {
  176. print("error: \(error)")
  177. }
  178. }
  179. }
  180. }
  181. let parts = message.components(separatedBy:" ")
  182. for part in parts {
  183. let requestMessage = Echo_EchoRequest(text:part)
  184. print("Sending: " + requestMessage.text)
  185. updateCall.Send(message:requestMessage)
  186. sleep(1)
  187. }
  188. updateCall.CloseSend()
  189. // Wait for the call to complete.
  190. done.lock()
  191. done.wait()
  192. done.unlock()
  193. }
  194. }