main.swift 4.8 KB

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