main.swift 4.1 KB

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