main.swift 6.2 KB

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