Collect.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 transport = try HTTP2ClientTransport.Posix(target: self.arguments.target)
  29. let client = GRPCClient(transport: transport)
  30. try await withThrowingDiscardingTaskGroup { group in
  31. group.addTask {
  32. try await client.run()
  33. }
  34. let echo = Echo_EchoClient(wrapping: client)
  35. for _ in 0 ..< self.arguments.repetitions {
  36. let message = try await echo.collect { writer in
  37. for part in self.arguments.message.split(separator: " ") {
  38. print("collect → \(part)")
  39. try await writer.write(.with { $0.text = String(part) })
  40. }
  41. }
  42. print("collect ← \(message.text)")
  43. }
  44. client.close()
  45. }
  46. }
  47. }