main.swift 4.2 KB

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