main.swift 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 gRPC
  34. import Foundation
  35. let address = "localhost:8001"
  36. let host = "foo.test.google.fr"
  37. func usage() {
  38. print("Usage: Simple <client|server>\n")
  39. exit(0)
  40. }
  41. func main() throws {
  42. gRPC.initialize()
  43. print("gRPC version", gRPC.version())
  44. print("\(CommandLine.arguments)")
  45. if CommandLine.arguments.count != 2 {
  46. usage()
  47. }
  48. let command = CommandLine.arguments[1]
  49. switch command {
  50. case "client": try client()
  51. case "server": try server()
  52. default:
  53. usage()
  54. }
  55. }
  56. func client() throws {
  57. let message = "hello, server!".data(using: .utf8)
  58. let c = gRPC.Channel(address:address)
  59. let steps = 3
  60. for i in 0..<steps {
  61. let latch = CountDownLatch(1)
  62. let method = (i < steps-1) ? "/hello" : "/quit"
  63. print("calling " + method)
  64. let call = c.makeCall(method)
  65. let metadata = Metadata([["x": "xylophone"],
  66. ["y": "yu"],
  67. ["z": "zither"]])
  68. try! call.start(.unary, metadata:metadata, message:message) {
  69. (response) in
  70. print("status:", response.statusCode)
  71. print("statusMessage:", response.statusMessage!)
  72. if let resultData = response.resultData {
  73. print("message: \(resultData)")
  74. }
  75. let initialMetadata = response.initialMetadata!
  76. for i in 0..<initialMetadata.count() {
  77. print("INITIAL METADATA ->", initialMetadata.key(i), ":", initialMetadata.value(i))
  78. }
  79. let trailingMetadata = response.trailingMetadata!
  80. for i in 0..<trailingMetadata.count() {
  81. print("TRAILING METADATA ->", trailingMetadata.key(i), ":", trailingMetadata.value(i))
  82. }
  83. latch.signal()
  84. }
  85. latch.wait()
  86. }
  87. print("Done")
  88. }
  89. func server() throws {
  90. let server = gRPC.Server(address:address)
  91. var requestCount = 0
  92. let latch = CountDownLatch(1)
  93. server.run() {(requestHandler) in
  94. do {
  95. requestCount += 1
  96. print("\(requestCount): Received request " + requestHandler.host
  97. + " " + requestHandler.method
  98. + " from " + requestHandler.caller)
  99. let initialMetadata = requestHandler.requestMetadata
  100. for i in 0..<initialMetadata.count() {
  101. print("\(requestCount): Received initial metadata -> " + initialMetadata.key(i)
  102. + ":" + initialMetadata.value(i))
  103. }
  104. let initialMetadataToSend = Metadata([["a": "Apple"],
  105. ["b": "Banana"],
  106. ["c": "Cherry"]])
  107. try requestHandler.receiveMessage(initialMetadata:initialMetadataToSend)
  108. {(messageData) in
  109. let messageString = String(data: messageData!, encoding: .utf8)
  110. print("\(requestCount): Received message: " + messageString!)
  111. }
  112. if requestHandler.method == "/quit" {
  113. print("quitting")
  114. latch.signal()
  115. }
  116. let replyMessage = "hello, client!"
  117. let trailingMetadataToSend = Metadata([["0": "zero"],
  118. ["1": "one"],
  119. ["2": "two"]])
  120. try requestHandler.sendResponse(message:replyMessage.data(using: .utf8)!,
  121. statusCode:0,
  122. statusMessage:"OK",
  123. trailingMetadata:trailingMetadataToSend)
  124. print("------------------------------")
  125. } catch (let callError) {
  126. Swift.print("call error \(callError)")
  127. }
  128. }
  129. server.onCompletion() {
  130. print("Server Stopped")
  131. }
  132. latch.wait()
  133. }
  134. try main()