main.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 2017, 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 Dispatch
  18. import gRPC
  19. import Commander
  20. let address = "localhost:8001"
  21. let host = "foo.test.google.fr"
  22. func client() throws {
  23. let message = "hello, server!".data(using: .utf8)
  24. let c = gRPC.Channel(address:address, secure:false)
  25. let steps = 3
  26. for i in 0..<steps {
  27. let sem = DispatchSemaphore(value: 0)
  28. let method = (i < steps-1) ? "/hello" : "/quit"
  29. print("calling " + method)
  30. let call = c.makeCall(method)
  31. let metadata = Metadata([["x": "xylophone"],
  32. ["y": "yu"],
  33. ["z": "zither"]])
  34. try! call.start(.unary, metadata:metadata, message:message) {
  35. (response) in
  36. print("status:", response.statusCode)
  37. print("statusMessage:", response.statusMessage!)
  38. if let resultData = response.resultData {
  39. print("message: \(resultData)")
  40. }
  41. let initialMetadata = response.initialMetadata!
  42. for i in 0..<initialMetadata.count() {
  43. print("INITIAL METADATA ->", initialMetadata.key(i), ":", initialMetadata.value(i))
  44. }
  45. let trailingMetadata = response.trailingMetadata!
  46. for i in 0..<trailingMetadata.count() {
  47. print("TRAILING METADATA ->", trailingMetadata.key(i), ":", trailingMetadata.value(i))
  48. }
  49. sem.signal()
  50. }
  51. _ = sem.wait(timeout: DispatchTime.distantFuture)
  52. }
  53. print("Done")
  54. }
  55. func server() throws {
  56. let server = gRPC.Server(address:address)
  57. var requestCount = 0
  58. let sem = DispatchSemaphore(value: 0)
  59. server.run() {(requestHandler) in
  60. do {
  61. requestCount += 1
  62. print("\(requestCount): Received request " + requestHandler.host
  63. + " " + requestHandler.method
  64. + " from " + requestHandler.caller)
  65. let initialMetadata = requestHandler.requestMetadata
  66. for i in 0..<initialMetadata.count() {
  67. print("\(requestCount): Received initial metadata -> " + initialMetadata.key(i)
  68. + ":" + initialMetadata.value(i))
  69. }
  70. let initialMetadataToSend = Metadata([["a": "Apple"],
  71. ["b": "Banana"],
  72. ["c": "Cherry"]])
  73. try requestHandler.receiveMessage(initialMetadata:initialMetadataToSend)
  74. {(messageData) in
  75. let messageString = String(data: messageData!, encoding: .utf8)
  76. print("\(requestCount): Received message: " + messageString!)
  77. }
  78. if requestHandler.method == "/quit" {
  79. print("quitting")
  80. sem.signal()
  81. }
  82. let replyMessage = "hello, client!"
  83. let trailingMetadataToSend = Metadata([["0": "zero"],
  84. ["1": "one"],
  85. ["2": "two"]])
  86. try requestHandler.sendResponse(message:replyMessage.data(using: .utf8)!,
  87. statusCode:.ok,
  88. statusMessage:"OK",
  89. trailingMetadata:trailingMetadataToSend)
  90. print("------------------------------")
  91. } catch (let callError) {
  92. Swift.print("call error \(callError)")
  93. }
  94. }
  95. server.onCompletion() {
  96. print("Server Stopped")
  97. }
  98. _ = sem.wait(timeout: DispatchTime.distantFuture)
  99. }
  100. Group {
  101. $0.command("server") {
  102. gRPC.initialize()
  103. print("gRPC version", gRPC.version())
  104. try server()
  105. }
  106. $0.command("client") {
  107. gRPC.initialize()
  108. print("gRPC version", gRPC.version())
  109. try client()
  110. }
  111. }.run()