@@ -32,20 +32,8 @@ func loadFeatures() throws -> [Routeguide_Feature] {
3232 return try Routeguide_Feature . array ( fromJSONUTF8Data: data)
3333}
3434
35- /// Makes a `RouteGuide` client for a service hosted on "localhost" and listening on the given port.
3635@available ( macOS 10 . 15 , iOS 13 , tvOS 13 , watchOS 6 , * )
37- func makeClient( port: Int , group: EventLoopGroup ) throws -> Routeguide_RouteGuideAsyncClient {
38- let channel = try GRPCChannelPool . with (
39- target: . host( " localhost " , port: port) ,
40- transportSecurity: . plaintext,
41- eventLoopGroup: group
42- )
43-
44- return Routeguide_RouteGuideAsyncClient ( channel: channel)
45- }
46-
47- @available ( macOS 10 . 15 , iOS 13 , tvOS 13 , watchOS 6 , * )
48- internal struct RouteGuideExample : @unchecked Sendable {
36+ internal struct RouteGuideExample {
4937 private let routeGuide : Routeguide_RouteGuideAsyncClient
5038 private let features : [ Routeguide_Feature ]
5139
@@ -54,37 +42,26 @@ internal struct RouteGuideExample: @unchecked Sendable {
5442 self . features = features
5543 }
5644
57- func runAndBlockUntilCompletion( ) {
58- let group = DispatchGroup ( )
59- group. enter ( )
60-
61- Task {
62- defer {
63- group. leave ( )
64- }
65-
66- // Look for a valid feature.
67- await self . getFeature ( latitude: 409_146_138 , longitude: - 746_188_906 )
45+ func run( ) async {
46+ // Look for a valid feature.
47+ await self . getFeature ( latitude: 409_146_138 , longitude: - 746_188_906 )
6848
69- // Look for a missing feature.
70- await self . getFeature ( latitude: 0 , longitude: 0 )
71-
72- // Looking for features between 40, -75 and 42, -73.
73- await self . listFeatures (
74- lowLatitude: 400_000_000 ,
75- lowLongitude: - 750_000_000 ,
76- highLatitude: 420_000_000 ,
77- highLongitude: - 730_000_000
78- )
49+ // Look for a missing feature.
50+ await self . getFeature ( latitude: 0 , longitude: 0 )
7951
80- // Record a few randomly selected points from the features file.
81- await self . recordRoute ( features: features, featuresToVisit: 10 )
52+ // Looking for features between 40, -75 and 42, -73.
53+ await self . listFeatures (
54+ lowLatitude: 400_000_000 ,
55+ lowLongitude: - 750_000_000 ,
56+ highLatitude: 420_000_000 ,
57+ highLongitude: - 730_000_000
58+ )
8259
83- // Send and receive some notes.
84- await self . routeChat ( )
85- }
60+ // Record a few randomly selected points from the features file.
61+ await self . recordRoute ( features: self . features, featuresToVisit: 10 )
8662
87- group. wait ( )
63+ // Send and receive some notes.
64+ await self . routeChat ( )
8865 }
8966}
9067
@@ -229,12 +206,13 @@ extension RouteGuideExample {
229206 }
230207}
231208
209+ @main
232210@available ( macOS 10 . 15 , iOS 13 , tvOS 13 , watchOS 6 , * )
233- struct RouteGuide : ParsableCommand {
211+ struct RouteGuide : AsyncParsableCommand {
234212 @Option ( help: " The port to connect to " )
235213 var port : Int = 1234
236214
237- func run( ) throws {
215+ func run( ) async throws {
238216 // Load the features.
239217 let features = try loadFeatures ( )
240218
@@ -243,15 +221,18 @@ struct RouteGuide: ParsableCommand {
243221 try ? group. syncShutdownGracefully ( )
244222 }
245223
246- let routeGuide = try makeClient ( port: self . port, group: group)
224+ let channel = try GRPCChannelPool . with (
225+ target: . host( " localhost " , port: self . port) ,
226+ transportSecurity: . plaintext,
227+ eventLoopGroup: group
228+ )
247229 defer {
248- try ? routeGuide . channel. close ( ) . wait ( )
230+ try ? channel. close ( ) . wait ( )
249231 }
250232
251- // ArgumentParser did not support async/await at the point in time this was written. Block
252- // this thread while the example runs.
233+ let routeGuide = Routeguide_RouteGuideAsyncClient ( channel: channel)
253234 let example = RouteGuideExample ( routeGuide: routeGuide, features: features)
254- example. runAndBlockUntilCompletion ( )
235+ await example. run ( )
255236 }
256237}
257238
@@ -266,12 +247,11 @@ extension Routeguide_Feature: CustomStringConvertible {
266247 return " \( self . name) at \( self . location) "
267248 }
268249}
269-
270- if #available( macOS 12 , * ) {
271- RouteGuide . main ( )
272- } else {
273- fatalError ( " The RouteGuide example requires macOS 12 or newer. " )
274- }
275250#else
276- fatalError ( " The RouteGuide example requires Swift concurrency features. " )
251+ @main
252+ enum NotAvailable {
253+ static func main( ) {
254+ print ( " This example requires Swift >= 5.6 " )
255+ }
256+ }
277257#endif // compiler(>=5.6)
0 commit comments