ListFeatures.swift 2.7 KB

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