main.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2020, 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 Dispatch
  17. import GRPC
  18. import Logging
  19. import NIO
  20. import NIOExtras
  21. // Parse the command line args.
  22. var args = CommandLine.arguments
  23. guard args.count == 3, let port = Int(args[2]) else {
  24. let usage = """
  25. Usage: \(args[0]) SERVER_HOST SERVER_PORT
  26. Note: you can start a server from the root of the gRPC Swift directory by running:
  27. $ swift run Echo server 0
  28. """
  29. print(usage)
  30. exit(1)
  31. }
  32. let host = args[1]
  33. // Create a logger.
  34. let logger = Logger(label: "gRPC PCAP Demo")
  35. // Closing file sinks is blocking, it therefore can't be done on an EventLoop.
  36. let fileSinkCloseQueue = DispatchQueue(label: "io.grpc")
  37. let fileSinkCloseGroup = DispatchGroup()
  38. defer {
  39. // Make sure we wait for all file sinks to be closed before we exit.
  40. fileSinkCloseGroup.wait()
  41. logger.info("Done!")
  42. }
  43. /// Adds a `NIOWritePCAPHandler` to the given channel.
  44. ///
  45. /// A file sink will also be created to write the PCAP to `./channel-{ID}.pcap` where `{ID}` is
  46. /// an identifier created from the given `channel`. The file sink will be closed when the channel
  47. /// closes and will notify the `fileSinkCloseGroup` when it has been closed.
  48. ///
  49. /// - Parameter channel: The channel to add the PCAP handler to.
  50. /// - Returns: An `EventLoopFuture` indicating whether the PCAP handler was successfully added.
  51. func addPCAPHandler(toChannel channel: Channel) -> EventLoopFuture<Void> {
  52. // The debug initializer can be called multiple times. We'll use the object ID of the channel
  53. // to disambiguate between the files.
  54. let channelID = ObjectIdentifier(channel)
  55. let path = "./channel-\(channelID).pcap"
  56. logger.info("Creating fileSink for path '\(path)'")
  57. do {
  58. // Create a file sink.
  59. let fileSink = try NIOWritePCAPHandler.SynchronizedFileSink
  60. .fileSinkWritingToFile(path: path) { error in
  61. logger.error("💥 Failed to write with error '\(error)' for path '\(path)'")
  62. }
  63. logger.info("✅ Successfully created fileSink for path '\(path)'")
  64. // We need to close the file sink when we're done. It can't be closed from the event loop so
  65. // we'll use a dispatch queue instead.
  66. fileSinkCloseGroup.enter()
  67. channel.closeFuture.whenComplete { _ in
  68. fileSinkCloseQueue.async {
  69. do {
  70. try fileSink.syncClose()
  71. } catch {
  72. logger.error("💥 Failed to close fileSink with error '\(error)' for path '\(path)'")
  73. }
  74. }
  75. fileSinkCloseGroup.leave()
  76. }
  77. // Add the handler to the pipeline.
  78. let handler = NIOWritePCAPHandler(mode: .client, fileSink: fileSink.write(buffer:))
  79. // We're not using TLS in this example so ".first" is the right place.
  80. return channel.pipeline.addHandler(handler, position: .first)
  81. } catch {
  82. logger.error("💥 Failed to create fileSink with error '\(error)' for path '\(path)'")
  83. return channel.eventLoop.makeFailedFuture(error)
  84. }
  85. }
  86. // Create an `EventLoopGroup`.
  87. let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
  88. defer {
  89. try! group.syncShutdownGracefully()
  90. }
  91. // Create a channel.
  92. let channel = ClientConnection.insecure(group: group)
  93. // Set the debug initializer: it will add a handler to each created channel to write a PCAP when
  94. // the channel is closed.
  95. .withDebugChannelInitializer(addPCAPHandler(toChannel:))
  96. // We're connecting to our own server here; we'll disable connection re-establishment.
  97. .withConnectionReestablishment(enabled: false)
  98. // Connect!
  99. .connect(host: host, port: port)
  100. // Create a client.
  101. let echo = Echo_EchoClient(channel: channel)
  102. // Start an RPC.
  103. let update = echo.update { response in
  104. logger.info("Received response '\(response.text)'")
  105. }
  106. // Send some requests.
  107. for text in ["foo", "bar", "baz", "thud", "grunt", "gorp"] {
  108. update.sendMessage(.with { $0.text = text }).whenSuccess {
  109. logger.info("Sent request '\(text)'")
  110. }
  111. }
  112. // Close the request stream.
  113. update.sendEnd(promise: nil)
  114. // Once the RPC finishes close the connection.
  115. let closed = update.status.flatMap { status -> EventLoopFuture<Void> in
  116. if status.isOk {
  117. logger.info("✅ RPC completed successfully")
  118. } else {
  119. logger.error("💥 RPC failed with status '\(status)'")
  120. }
  121. logger.info("Closing channel")
  122. return channel.close()
  123. }
  124. // Wait for the channel to be closed.
  125. try closed.wait()