main.swift 6.8 KB

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