ListFeatures.swift 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. import ArgumentParser
  17. import GRPCHTTP2Transport
  18. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  19. struct ListFeatures: AsyncParsableCommand {
  20. static let configuration = CommandConfiguration(
  21. abstract: "List all features within a bounding rectangle."
  22. )
  23. @Option(help: "The server's listening port")
  24. var port: Int = 31415
  25. @Option(
  26. name: [.customLong("minimum-latitude"), .customLong("min-lat")],
  27. help: "Minimum latitude of the bounding rectangle to search in E7 format."
  28. )
  29. var minLatitude: Int32 = 400_000_000
  30. @Option(
  31. name: [.customLong("maximum-latitude"), .customLong("max-lat")],
  32. help: "Maximum latitude of the bounding rectangle to search in E7 format."
  33. )
  34. var maxLatitude: Int32 = 420_000_000
  35. @Option(
  36. name: [.customLong("minimum-longitude"), .customLong("min-lon")],
  37. help: "Minimum longitude of the bounding rectangle to search in E7 format."
  38. )
  39. var minLongitude: Int32 = -750_000_000
  40. @Option(
  41. name: [.customLong("maximum-longitude"), .customLong("max-lon")],
  42. help: "Maximum longitude of the bounding rectangle to search in E7 format."
  43. )
  44. var maxLongitude: Int32 = -730_000_000
  45. func run() async throws {
  46. let transport = try HTTP2ClientTransport.Posix(
  47. target: .ipv4(host: "127.0.0.1", port: self.port),
  48. config: .defaults()
  49. )
  50. let client = GRPCClient(transport: transport)
  51. try await withThrowingDiscardingTaskGroup { group in
  52. group.addTask {
  53. try await client.run()
  54. }
  55. let routeGuide = Routeguide_RouteGuideClient(wrapping: client)
  56. let boundingRectangle = Routeguide_Rectangle.with {
  57. $0.lo.latitude = self.minLatitude
  58. $0.hi.latitude = self.maxLatitude
  59. $0.lo.longitude = self.minLongitude
  60. $0.hi.longitude = self.maxLongitude
  61. }
  62. try await routeGuide.listFeatures(boundingRectangle) { response in
  63. var count = 0
  64. for try await feature in response.messages {
  65. count += 1
  66. let (lat, lon) = (feature.location.latitude, feature.location.longitude)
  67. print("(\(count)) \(feature.name) at (\(lat), \(lon))")
  68. }
  69. }
  70. client.beginGracefulShutdown()
  71. }
  72. }
  73. }