main.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_get : Bool = false
  41. var client_expand : Bool = false
  42. var client_collect : Bool = false
  43. var client_update : Bool = false
  44. // other network configuration
  45. var useSSL : Bool = false
  46. var i : Int = 0
  47. while i < Int(CommandLine.argc) {
  48. let arg = CommandLine.arguments[i]
  49. i = i + 1
  50. if i == 1 {
  51. continue // skip the first argument
  52. }
  53. if arg == "serve" {
  54. server = true
  55. } else if arg == "get" {
  56. client_get = true
  57. } else if arg == "expand" {
  58. client_expand = true
  59. } else if arg == "collect" {
  60. client_collect = true
  61. } else if arg == "update" {
  62. client_update = true
  63. } else if arg == "-ssl" {
  64. useSSL = true
  65. }
  66. }
  67. var insecureEchoServer: EchoServer!
  68. var secureEchoServer: EchoServer!
  69. var done = NSCondition()
  70. gRPC.initialize()
  71. if server {
  72. if useSSL {
  73. print("Starting secure server")
  74. secureEchoServer = EchoServer(address:"localhost:8443", secure:true)
  75. secureEchoServer.start()
  76. } else {
  77. print("Starting insecure server")
  78. insecureEchoServer = EchoServer(address:"localhost:8081", secure:false)
  79. insecureEchoServer.start()
  80. }
  81. // we never actually exit the server; kill the process to stop it.
  82. done.lock()
  83. done.wait()
  84. done.unlock()
  85. }
  86. if client_get || client_expand || client_collect || client_update {
  87. print("Starting client")
  88. var service : EchoService
  89. if useSSL {
  90. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  91. let certificates = try! String(contentsOf: certificateURL)
  92. service = EchoService(address:"localhost:8443", certificates:certificates, host:"example.com")
  93. service.channel.host = "example.com" // sample override
  94. } else {
  95. service = 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. if client_get {
  100. let getCall = service.get()
  101. var requestMessage = Echo_EchoRequest()
  102. requestMessage.text = "Hello!!!"
  103. print("Sending: " + requestMessage.text)
  104. getCall.perform(request:requestMessage) {(callResult, responseMessage) in
  105. if let responseMessage = responseMessage {
  106. print("Received: " + responseMessage.text)
  107. } else {
  108. print("No message received. gRPC Status \(callResult.statusCode): \(callResult.statusMessage)")
  109. }
  110. done.lock()
  111. done.signal()
  112. done.unlock()
  113. }
  114. done.lock()
  115. done.wait()
  116. done.unlock()
  117. }
  118. if client_expand {
  119. let expandCall = service.expand()
  120. func receiveExpandMessage() throws -> Void {
  121. try expandCall.receiveMessage() {(responseMessage) in
  122. if let responseMessage = responseMessage {
  123. try receiveExpandMessage() // prepare to receive the next message
  124. print(responseMessage.text)
  125. } else {
  126. print("expand closed")
  127. done.lock()
  128. done.signal()
  129. done.unlock()
  130. }
  131. }
  132. }
  133. var requestMessage = Echo_EchoRequest()
  134. requestMessage.text = "Testing One Two Three"
  135. print("Sending: " + requestMessage.text)
  136. expandCall.perform(request:requestMessage) {(callResult, response) in}
  137. try receiveExpandMessage()
  138. done.lock()
  139. done.wait()
  140. done.unlock()
  141. }
  142. if client_collect {
  143. let collectCall = service.collect()
  144. func sendCollectMessage() {
  145. var requestMessage = Echo_EchoRequest()
  146. requestMessage.text = "hello"
  147. print("Sending: " + requestMessage.text)
  148. _ = collectCall.sendMessage(message:requestMessage)
  149. }
  150. func receiveCollectMessage() throws -> Void {
  151. try collectCall.receiveMessage() {(responseMessage) in
  152. if let responseMessage = responseMessage {
  153. print("Received: " + responseMessage.text)
  154. } else {
  155. print("collect closed")
  156. done.lock()
  157. done.signal()
  158. done.unlock()
  159. }
  160. }
  161. }
  162. try collectCall.start(metadata:requestMetadata)
  163. try receiveCollectMessage()
  164. sendCollectMessage()
  165. done.lock()
  166. done.wait()
  167. done.unlock()
  168. }
  169. if client_update {
  170. let updateCall = service.update()
  171. func sendUpdateMessage() {
  172. var requestMessage = Echo_EchoRequest()
  173. requestMessage.text = "hello"
  174. print("Sending: " + requestMessage.text)
  175. _ = updateCall.sendMessage(message:requestMessage)
  176. }
  177. func receiveUpdateMessage() throws -> Void {
  178. try updateCall.receiveMessage() {(responseMessage) in
  179. try receiveUpdateMessage() // prepare to receive the next message
  180. if let responseMessage = responseMessage {
  181. print("Received: " + responseMessage.text)
  182. } else {
  183. print("update closed")
  184. done.lock()
  185. done.signal()
  186. done.unlock()
  187. }
  188. }
  189. }
  190. try updateCall.start(metadata:requestMetadata)
  191. try receiveUpdateMessage()
  192. sendUpdateMessage()
  193. done.lock()
  194. done.wait()
  195. done.unlock()
  196. }
  197. }