ListFeatures.swift 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 GRPCCore
  18. import GRPCNIOTransportHTTP2
  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. try await withGRPCClient(
  47. transport: .http2NIOPosix(
  48. target: .ipv4(host: "127.0.0.1", port: self.port),
  49. transportSecurity: .plaintext
  50. )
  51. ) { client in
  52. let routeGuide = Routeguide_RouteGuide.Client(wrapping: client)
  53. let boundingRectangle = Routeguide_Rectangle.with {
  54. $0.lo.latitude = self.minLatitude
  55. $0.hi.latitude = self.maxLatitude
  56. $0.lo.longitude = self.minLongitude
  57. $0.hi.longitude = self.maxLongitude
  58. }
  59. try await routeGuide.listFeatures(boundingRectangle) { response in
  60. var count = 0
  61. for try await feature in response.messages {
  62. count += 1
  63. let (lat, lon) = (feature.location.latitude, feature.location.longitude)
  64. print("(\(count)) \(feature.name) at (\(lat), \(lon))")
  65. }
  66. }
  67. }
  68. }
  69. }