Collect.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright 2024, 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. internal import ArgumentParser
  17. private import GRPCCore
  18. private import GRPCHTTP2Core
  19. private import GRPCHTTP2TransportNIOPosix
  20. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  21. struct Collect: AsyncParsableCommand {
  22. static let configuration = CommandConfiguration(
  23. abstract: "Makes a client streaming RPC to the echo server."
  24. )
  25. @OptionGroup
  26. var arguments: ClientArguments
  27. func run() async throws {
  28. let client = GRPCClient(
  29. transport: try .http2NIOPosix(
  30. target: self.arguments.target,
  31. config: .defaults()
  32. )
  33. )
  34. try await withThrowingDiscardingTaskGroup { group in
  35. group.addTask {
  36. try await client.run()
  37. }
  38. let echo = Echo_EchoClient(wrapping: client)
  39. for _ in 0 ..< self.arguments.repetitions {
  40. let message = try await echo.collect { writer in
  41. for part in self.arguments.message.split(separator: " ") {
  42. print("collect → \(part)")
  43. try await writer.write(.with { $0.text = String(part) })
  44. }
  45. }
  46. print("collect ← \(message.text)")
  47. }
  48. client.close()
  49. }
  50. }
  51. }