GetFeature.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 GetFeature: AsyncParsableCommand {
  19. static let configuration = CommandConfiguration(abstract: "Gets a feature at a given location.")
  20. @Option(help: "The server's listening port")
  21. var port: Int = 31415
  22. @Option(
  23. name: [.customLong("latitude"), .customLong("lat")],
  24. help: "Latitude of the feature to get in E7 format (degrees ⨯ 1e7)"
  25. )
  26. var latitude: Int32 = 407_838_351
  27. @Option(
  28. name: [.customLong("longitude"), .customLong("lon")],
  29. help: "Longitude of the feature to get in E7 format (degrees ⨯ 1e7)"
  30. )
  31. var longitude: Int32 = -746_143_763
  32. func run() async throws {
  33. let transport = try HTTP2ClientTransport.Posix(
  34. target: .ipv4(host: "127.0.0.1", port: self.port),
  35. config: .defaults(transportSecurity: .plaintext)
  36. )
  37. let client = GRPCClient(transport: transport)
  38. try await withThrowingDiscardingTaskGroup { group in
  39. group.addTask {
  40. try await client.run()
  41. }
  42. let routeGuide = Routeguide_RouteGuide_Client(wrapping: client)
  43. let point = Routeguide_Point.with {
  44. $0.latitude = self.latitude
  45. $0.longitude = self.longitude
  46. }
  47. let feature = try await routeGuide.getFeature(point)
  48. if feature.name.isEmpty {
  49. print("No feature found at (\(self.latitude), \(self.longitude))")
  50. } else {
  51. print("Found '\(feature.name)' at (\(self.latitude), \(self.longitude))")
  52. }
  53. client.beginGracefulShutdown()
  54. }
  55. }
  56. }