main.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2016, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import Foundation
  17. import gRPC
  18. import CgRPC
  19. import Dispatch
  20. print("\(CommandLine.arguments)")
  21. // server options
  22. var server : Bool = false
  23. // client options
  24. var client : String = ""
  25. var message : String = "Testing 1 2 3"
  26. // self-test mode
  27. var test : Bool = false
  28. // general configuration
  29. var useSSL : Bool = false
  30. var i : Int = 0
  31. while i < Int(CommandLine.argc) {
  32. let arg = CommandLine.arguments[i]
  33. i = i + 1
  34. if i == 1 {
  35. continue // skip the first argument
  36. }
  37. if arg == "test" {
  38. test = true
  39. } else if arg == "serve" {
  40. server = true
  41. } else if (arg == "get") || (arg == "expand") || (arg == "collect") || (arg == "update") {
  42. client = arg
  43. } else if arg == "-ssl" {
  44. useSSL = true
  45. } else if arg == "-m" && (i < Int(CommandLine.argc)) {
  46. message = CommandLine.arguments[i]
  47. i = i + 1
  48. }
  49. }
  50. var latch = CountDownLatch(1)
  51. gRPC.initialize()
  52. if server {
  53. let echoProvider = EchoProvider()
  54. var echoServer: Echo_EchoServer!
  55. if useSSL {
  56. print("Starting secure server")
  57. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  58. let keyURL = URL(fileURLWithPath:"ssl.key")
  59. echoServer = Echo_EchoServer(address:"localhost:8443",
  60. certificateURL:certificateURL,
  61. keyURL:keyURL,
  62. provider:echoProvider)
  63. } else {
  64. print("Starting insecure server")
  65. echoServer = Echo_EchoServer(address:"localhost:8081",
  66. provider:echoProvider)
  67. }
  68. echoServer.start()
  69. // Block to keep the main thread from finishing while the server runs.
  70. // This server never exits. Kill the process to stop it.
  71. latch.wait()
  72. }
  73. if client != "" {
  74. print("Starting client")
  75. var service : Echo_EchoService
  76. if useSSL {
  77. let certificateURL = URL(fileURLWithPath:"ssl.crt")
  78. let certificates = try! String(contentsOf: certificateURL)
  79. service = Echo_EchoService(address:"localhost:8443", certificates:certificates, host:"example.com")
  80. service.host = "example.com" // sample override
  81. } else {
  82. service = Echo_EchoService(address:"localhost:8081")
  83. }
  84. let requestMetadata = Metadata(["x-goog-api-key":"YOUR_API_KEY",
  85. "x-ios-bundle-identifier":"com.google.echo"])
  86. // Unary
  87. if client == "get" {
  88. var requestMessage = Echo_EchoRequest()
  89. requestMessage.text = message
  90. print("Sending: " + requestMessage.text)
  91. let responseMessage = try service.get(requestMessage)
  92. print("get received: " + responseMessage.text)
  93. }
  94. // Server streaming
  95. if client == "expand" {
  96. var requestMessage = Echo_EchoRequest()
  97. requestMessage.text = message
  98. print("Sending: " + requestMessage.text)
  99. let expandCall = try service.expand(requestMessage) {result in }
  100. var running = true
  101. while running {
  102. do {
  103. let responseMessage = try expandCall.receive()
  104. print("Received: \(responseMessage.text)")
  105. } catch Echo_EchoClientError.endOfStream {
  106. print("expand closed")
  107. running = false
  108. }
  109. }
  110. }
  111. // Client streaming
  112. if client == "collect" {
  113. let collectCall = try service.collect() {result in }
  114. let parts = message.components(separatedBy:" ")
  115. for part in parts {
  116. var requestMessage = Echo_EchoRequest()
  117. requestMessage.text = part
  118. print("Sending: " + part)
  119. try collectCall.send(requestMessage) {error in print(error)}
  120. sleep(1)
  121. }
  122. let responseMessage = try collectCall.closeAndReceive()
  123. print("Received: \(responseMessage.text)")
  124. }
  125. // Bidirectional streaming
  126. if client == "update" {
  127. let updateCall = try service.update() {result in}
  128. DispatchQueue.global().async {
  129. var running = true
  130. while running {
  131. do {
  132. let responseMessage = try updateCall.receive()
  133. print("Received: \(responseMessage.text)")
  134. } catch Echo_EchoClientError.endOfStream {
  135. print("update closed")
  136. latch.signal()
  137. break
  138. } catch (let error) {
  139. print("error: \(error)")
  140. }
  141. }
  142. }
  143. let parts = message.components(separatedBy:" ")
  144. for part in parts {
  145. var requestMessage = Echo_EchoRequest()
  146. requestMessage.text = part
  147. print("Sending: " + requestMessage.text)
  148. try updateCall.send(requestMessage) {error in print(error)}
  149. sleep(1)
  150. }
  151. try updateCall.closeSend()
  152. // Wait for the call to complete.
  153. latch.wait()
  154. }
  155. }
  156. if test {
  157. print("self test")
  158. }