main.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. let getCall = service.get()
  96. var requestMessage = Echo_EchoRequest(text:message)
  97. print("Sending: " + requestMessage.text)
  98. getCall.perform(request:requestMessage) {(callResult, responseMessage) in
  99. if let responseMessage = responseMessage {
  100. print("Received: " + responseMessage.text)
  101. } else {
  102. print("No message received. gRPC Status \(callResult.statusCode): \(callResult.statusMessage)")
  103. }
  104. print("get closed")
  105. done.lock()
  106. done.signal()
  107. done.unlock()
  108. }
  109. // Wait for the call to complete.
  110. done.lock()
  111. done.wait()
  112. done.unlock()
  113. }
  114. // Server streaming
  115. if client == "expand" {
  116. let expandCall = service.expand()
  117. func receiveExpandMessage() throws -> Void {
  118. try expandCall.receiveMessage() {(responseMessage) in
  119. if let responseMessage = responseMessage {
  120. try receiveExpandMessage() // prepare to receive the next message in the stream
  121. print("Received: " + responseMessage.text)
  122. } else {
  123. print("expand closed")
  124. done.lock()
  125. done.signal()
  126. done.unlock()
  127. }
  128. }
  129. }
  130. let requestMessage = Echo_EchoRequest(text:message)
  131. print("Sending: " + requestMessage.text)
  132. expandCall.perform(request:requestMessage) {(callResult, response) in /* do nothing */}
  133. try receiveExpandMessage()
  134. // Wait for the call to complete.
  135. done.lock()
  136. done.wait()
  137. done.unlock()
  138. }
  139. // Client streaming
  140. if client == "collect" {
  141. let collectCall = service.collect()
  142. func sendCollectMessage(message: String) {
  143. let requestMessage = Echo_EchoRequest(text:message)
  144. print("Sending: " + requestMessage.text)
  145. _ = collectCall.sendMessage(message:requestMessage)
  146. }
  147. func sendCollectClose() {
  148. print("Closing")
  149. _ = try! collectCall.close(completion:{})
  150. }
  151. func receiveCollectMessage() throws -> Void {
  152. try collectCall.receiveMessage() {(responseMessage) in
  153. if let responseMessage = responseMessage {
  154. print("Received: " + responseMessage.text)
  155. }
  156. }
  157. }
  158. try collectCall.start(metadata:requestMetadata) {
  159. // this is called when the server closes the connection
  160. print("collect closed")
  161. done.lock()
  162. done.signal()
  163. done.unlock()
  164. }
  165. try receiveCollectMessage()
  166. let parts = message.components(separatedBy:" ")
  167. for part in parts {
  168. sendCollectMessage(message:part)
  169. sleep(1)
  170. }
  171. sendCollectClose()
  172. // Wait for the call to complete.
  173. done.lock()
  174. done.wait()
  175. done.unlock()
  176. }
  177. // Bidirectional streaming
  178. if client == "update" {
  179. let updateCall = service.update()
  180. func sendUpdateMessage(message: String) {
  181. let requestMessage = Echo_EchoRequest(text:message)
  182. print("Sending: " + requestMessage.text)
  183. _ = updateCall.sendMessage(message:requestMessage)
  184. }
  185. func sendUpdateClose() {
  186. print("Closing")
  187. _ = try! updateCall.close(completion:{})
  188. }
  189. func receiveUpdateMessage() throws -> Void {
  190. try updateCall.receiveMessage() {(responseMessage) in
  191. try receiveUpdateMessage() // prepare to receive the next message
  192. if let responseMessage = responseMessage {
  193. print("Received: " + responseMessage.text)
  194. }
  195. }
  196. }
  197. try updateCall.start(metadata:requestMetadata) {
  198. // this is called when the server closes the connection
  199. print("update closed")
  200. done.lock()
  201. done.signal()
  202. done.unlock()
  203. }
  204. try receiveUpdateMessage()
  205. let parts = message.components(separatedBy:" ")
  206. for part in parts {
  207. sendUpdateMessage(message:part)
  208. sleep(1)
  209. }
  210. sendUpdateClose()
  211. // Wait for the call to complete.
  212. done.lock()
  213. done.wait()
  214. done.unlock()
  215. }
  216. }