RouteGuideClient.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright 2019, 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 Foundation
  18. import GRPC
  19. import NIOCore
  20. import NIOPosix
  21. import RouteGuideModel
  22. /// Loads the features from `route_guide_db.json`, assumed to be in the directory above this file.
  23. func loadFeatures() throws -> [Routeguide_Feature] {
  24. let url = URL(fileURLWithPath: #filePath)
  25. .deletingLastPathComponent() // main.swift
  26. .deletingLastPathComponent() // Client/
  27. .appendingPathComponent("route_guide_db.json")
  28. let data = try Data(contentsOf: url)
  29. return try Routeguide_Feature.array(fromJSONUTF8Data: data)
  30. }
  31. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  32. internal struct RouteGuideExample {
  33. private let routeGuide: Routeguide_RouteGuideAsyncClient
  34. private let features: [Routeguide_Feature]
  35. init(routeGuide: Routeguide_RouteGuideAsyncClient, features: [Routeguide_Feature]) {
  36. self.routeGuide = routeGuide
  37. self.features = features
  38. }
  39. func run() async {
  40. // Look for a valid feature.
  41. await self.getFeature(latitude: 409_146_138, longitude: -746_188_906)
  42. // Look for a missing feature.
  43. await self.getFeature(latitude: 0, longitude: 0)
  44. // Looking for features between 40, -75 and 42, -73.
  45. await self.listFeatures(
  46. lowLatitude: 400_000_000,
  47. lowLongitude: -750_000_000,
  48. highLatitude: 420_000_000,
  49. highLongitude: -730_000_000
  50. )
  51. // Record a few randomly selected points from the features file.
  52. await self.recordRoute(features: self.features, featuresToVisit: 10)
  53. // Send and receive some notes.
  54. await self.routeChat()
  55. }
  56. }
  57. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  58. extension RouteGuideExample {
  59. /// Get the feature at the given latitude and longitude, if one exists.
  60. private func getFeature(latitude: Int, longitude: Int) async {
  61. print("\n→ GetFeature: lat=\(latitude) lon=\(longitude)")
  62. let point: Routeguide_Point = .with {
  63. $0.latitude = numericCast(latitude)
  64. $0.longitude = numericCast(longitude)
  65. }
  66. do {
  67. let feature = try await self.routeGuide.getFeature(point)
  68. if !feature.name.isEmpty {
  69. print("Found feature called '\(feature.name)' at \(feature.location.formatted)")
  70. } else {
  71. print("Found no feature at \(feature.location.formatted)")
  72. }
  73. } catch {
  74. print("RPC failed: \(error)")
  75. }
  76. }
  77. /// List all features in the area bounded by the high and low latitude and longitudes.
  78. private func listFeatures(
  79. lowLatitude: Int,
  80. lowLongitude: Int,
  81. highLatitude: Int,
  82. highLongitude: Int
  83. ) async {
  84. print(
  85. "\n→ ListFeatures: lowLat=\(lowLatitude) lowLon=\(lowLongitude), hiLat=\(highLatitude) hiLon=\(highLongitude)"
  86. )
  87. let rectangle: Routeguide_Rectangle = .with {
  88. $0.lo = .with {
  89. $0.latitude = numericCast(lowLatitude)
  90. $0.longitude = numericCast(lowLongitude)
  91. }
  92. $0.hi = .with {
  93. $0.latitude = numericCast(highLatitude)
  94. $0.longitude = numericCast(highLongitude)
  95. }
  96. }
  97. do {
  98. var resultCount = 1
  99. for try await feature in self.routeGuide.listFeatures(rectangle) {
  100. print("Result #\(resultCount): \(feature.name) at \(feature.location.formatted)")
  101. resultCount += 1
  102. }
  103. } catch {
  104. print("RPC failed: \(error)")
  105. }
  106. }
  107. /// Record a route for `featuresToVisit` features selected randomly from `features` and print a
  108. /// summary of the route.
  109. private func recordRoute(
  110. features: [Routeguide_Feature],
  111. featuresToVisit: Int
  112. ) async {
  113. print("\n→ RecordRoute")
  114. let recordRoute = self.routeGuide.makeRecordRouteCall()
  115. do {
  116. for i in 1 ... featuresToVisit {
  117. if let feature = features.randomElement() {
  118. let point = feature.location
  119. print("Visiting point #\(i) at \(point.formatted)")
  120. try await recordRoute.requestStream.send(point)
  121. // Sleep for 0.2s ... 1.0s before sending the next point.
  122. try await Task.sleep(nanoseconds: UInt64.random(in: UInt64(2e8) ... UInt64(1e9)))
  123. }
  124. }
  125. recordRoute.requestStream.finish()
  126. let summary = try await recordRoute.response
  127. print(
  128. "Finished trip with \(summary.pointCount) points. Passed \(summary.featureCount) features. "
  129. + "Travelled \(summary.distance) meters. It took \(summary.elapsedTime) seconds."
  130. )
  131. } catch {
  132. print("RecordRoute Failed: \(error)")
  133. }
  134. }
  135. /// Record notes at given locations, printing each all other messages which have previously been
  136. /// recorded at the same location.
  137. private func routeChat() async {
  138. print("\n→ RouteChat")
  139. let notes = [
  140. ("First message", 0, 0),
  141. ("Second message", 0, 1),
  142. ("Third message", 1, 0),
  143. ("Fourth message", 1, 1),
  144. ].map { message, latitude, longitude in
  145. Routeguide_RouteNote.with {
  146. $0.message = message
  147. $0.location = .with {
  148. $0.latitude = Int32(latitude)
  149. $0.longitude = Int32(longitude)
  150. }
  151. }
  152. }
  153. do {
  154. try await withThrowingTaskGroup(of: Void.self) { group in
  155. let routeChat = self.routeGuide.makeRouteChatCall()
  156. // Add a task to send each message adding a small sleep between each.
  157. group.addTask {
  158. for note in notes {
  159. print("Sending message '\(note.message)' at \(note.location.formatted)")
  160. try await routeChat.requestStream.send(note)
  161. // Sleep for 0.2s ... 1.0s before sending the next note.
  162. try await Task.sleep(nanoseconds: UInt64.random(in: UInt64(2e8) ... UInt64(1e9)))
  163. }
  164. routeChat.requestStream.finish()
  165. }
  166. // Add a task to print each message received on the response stream.
  167. group.addTask {
  168. for try await note in routeChat.responseStream {
  169. print("Received message '\(note.message)' at \(note.location.formatted)")
  170. }
  171. }
  172. try await group.waitForAll()
  173. }
  174. } catch {
  175. print("RouteChat Failed: \(error)")
  176. }
  177. }
  178. }
  179. @main
  180. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  181. struct RouteGuide: AsyncParsableCommand {
  182. @Option(help: "The port to connect to")
  183. var port: Int = 1234
  184. func run() async throws {
  185. // Load the features.
  186. let features = try loadFeatures()
  187. let group = PlatformSupport.makeEventLoopGroup(loopCount: 1)
  188. defer {
  189. try? group.syncShutdownGracefully()
  190. }
  191. let channel = try GRPCChannelPool.with(
  192. target: .host("localhost", port: self.port),
  193. transportSecurity: .plaintext,
  194. eventLoopGroup: group
  195. )
  196. defer {
  197. try? channel.close().wait()
  198. }
  199. let routeGuide = Routeguide_RouteGuideAsyncClient(channel: channel)
  200. let example = RouteGuideExample(routeGuide: routeGuide, features: features)
  201. await example.run()
  202. }
  203. }
  204. extension Routeguide_Point {
  205. var formatted: String {
  206. return "(\(self.latitude), \(self.longitude))"
  207. }
  208. }