Skip to content

Commit 31b7551

Browse files
authored
Update RouteGuide examples (#1426)
1 parent 32e290b commit 31b7551

File tree

5 files changed

+64
-74
lines changed

5 files changed

+64
-74
lines changed

Sources/Examples/HelloWorld/Client/main.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,4 @@ struct HelloWorld: AsyncParsableCommand {
6767
}
6868
}
6969
}
70-
7170
#endif // compiler(>=5.6)

Sources/Examples/HelloWorld/Server/main.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,4 @@ struct HelloWorld: AsyncParsableCommand {
4343
try await server.onClose.get()
4444
}
4545
}
46-
4746
#endif // compiler(>=5.6)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright 2022, 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+
17+
// This file exists to workaround https://github.com/apple/swift/issues/55127.

Sources/Examples/RouteGuide/Client/main.swift renamed to Sources/Examples/RouteGuide/Client/RouteGuideClient.swift

Lines changed: 34 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

Sources/Examples/RouteGuide/Server/main.swift renamed to Sources/Examples/RouteGuide/Server/RouteGuideServer.swift

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ func loadFeatures() throws -> [Routeguide_Feature] {
3333
return try Routeguide_Feature.array(fromJSONUTF8Data: data)
3434
}
3535

36+
@main
3637
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
37-
struct RouteGuide: ParsableCommand {
38+
struct RouteGuide: AsyncParsableCommand {
3839
@Option(help: "The port to listen on for new connections")
3940
var port = 1234
4041

41-
func run() throws {
42+
func run() async throws {
4243
// Create an event loop group for the server to run on.
4344
let group = MultiThreadedEventLoopGroup(numberOfThreads: System.coreCount)
4445
defer {
@@ -52,28 +53,22 @@ struct RouteGuide: ParsableCommand {
5253
let provider = RouteGuideProvider(features: features)
5354

5455
// Start the server and print its address once it has started.
55-
let server = Server.insecure(group: group)
56+
let server = try await Server.insecure(group: group)
5657
.withServiceProviders([provider])
5758
.bind(host: "localhost", port: self.port)
59+
.get()
5860

59-
server.map {
60-
$0.channel.localAddress
61-
}.whenSuccess { address in
62-
print("server started on port \(address!.port!)")
63-
}
61+
print("server started on port \(server.channel.localAddress!.port!)")
6462

6563
// Wait on the server's `onClose` future to stop the program from exiting.
66-
_ = try server.flatMap {
67-
$0.onClose
68-
}.wait()
64+
try await server.onClose.get()
6965
}
7066
}
71-
72-
if #available(macOS 12, *) {
73-
RouteGuide.main()
74-
} else {
75-
fatalError("The RouteGuide example requires macOS 12 or newer.")
76-
}
7767
#else
78-
fatalError("The RouteGuide example requires Swift concurrency support.")
68+
@main
69+
enum RouteGuide {
70+
static func main() {
71+
print("This example requires Swift >= 5.6")
72+
}
73+
}
7974
#endif // compiler(>=5.6)

0 commit comments

Comments
 (0)