diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step04-unary.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step04-unary.swift index 75a19a7e..2779b3dd 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step04-unary.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step04-unary.swift @@ -22,8 +22,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { context: ServerContext ) async throws -> Routeguide_Feature { let feature = self.findFeature( - latitude: request.message.latitude, - longitude: request.message.longitude + latitude: request.latitude, + longitude: request.longitude ) } diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step05-unary.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step05-unary.swift index d23cc14e..561725fb 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step05-unary.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step05-unary.swift @@ -22,8 +22,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { context: ServerContext ) async throws -> Routeguide_Feature { let feature = self.findFeature( - latitude: request.message.latitude, - longitude: request.message.longitude + latitude: request.latitude, + longitude: request.longitude ) if let feature { @@ -33,8 +33,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { let unknownFeature = Routeguide_Feature.with { $0.name = "" $0.location = .with { - $0.latitude = request.message.latitude - $0.longitude = request.message.longitude + $0.latitude = request.latitude + $0.longitude = request.longitude } } return unknownFeature diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step06-server-streaming.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step06-server-streaming.swift index c912c5f5..68a6ace0 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step06-server-streaming.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step06-server-streaming.swift @@ -22,8 +22,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { context: ServerContext ) async throws -> Routeguide_Feature { let feature = self.findFeature( - latitude: request.message.latitude, - longitude: request.message.longitude + latitude: request.latitude, + longitude: request.longitude ) if let feature { @@ -33,8 +33,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { let unknownFeature = Routeguide_Feature.with { $0.name = "" $0.location = .with { - $0.latitude = request.message.latitude - $0.longitude = request.message.longitude + $0.latitude = request.latitude + $0.longitude = request.longitude } } return unknownFeature diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step08-client-streaming.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step07-client-streaming.swift similarity index 96% rename from Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step08-client-streaming.swift rename to Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step07-client-streaming.swift index 72032cfb..db211514 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step08-client-streaming.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step07-client-streaming.swift @@ -23,8 +23,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { context: ServerContext ) async throws -> Routeguide_Feature { let feature = self.findFeature( - latitude: request.message.latitude, - longitude: request.message.longitude + latitude: request.latitude, + longitude: request.longitude ) if let feature { @@ -34,8 +34,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { let unknownFeature = Routeguide_Feature.with { $0.name = "" $0.location = .with { - $0.latitude = request.message.latitude - $0.longitude = request.message.longitude + $0.latitude = request.latitude + $0.longitude = request.longitude } } return unknownFeature diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step10-bidi-streaming.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step08-bidi-streaming.swift similarity index 93% rename from Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step10-bidi-streaming.swift rename to Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step08-bidi-streaming.swift index 452e1ad7..b67f418e 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step10-bidi-streaming.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step08-bidi-streaming.swift @@ -24,8 +24,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { context: ServerContext ) async throws -> Routeguide_Feature { let feature = self.findFeature( - latitude: request.message.latitude, - longitude: request.message.longitude + latitude: request.latitude, + longitude: request.longitude ) if let feature { @@ -35,8 +35,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { let unknownFeature = Routeguide_Feature.with { $0.name = "" $0.location = .with { - $0.latitude = request.message.latitude - $0.longitude = request.message.longitude + $0.latitude = request.latitude + $0.longitude = request.longitude } } return unknownFeature @@ -95,10 +95,6 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { response: RPCWriter, context: ServerContext ) async throws { - for try await note in request { - let notes = self.receivedNotes.recordNote(note) - try await response.write(contentsOf: notes) - } } } diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step09-bidi-streaming.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step09-bidi-streaming.swift index 96ecea32..4d3b66c2 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step09-bidi-streaming.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec04-step09-bidi-streaming.swift @@ -6,10 +6,40 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { /// Known features. private let features: [Routeguide_Feature] + /// Notes recorded by clients. + private let receivedNotes: Notes + + /// A thread-safe store for notes sent by clients. + private final class Notes: Sendable { + private let notes: Mutex<[Routeguide_RouteNote]> + + init() { + self.notes = Mutex([]) + } + + /// Records a note and returns all other notes recorded at the same location. + /// + /// - Parameter receivedNote: A note to record. + /// - Returns: Other notes recorded at the same location. + func recordNote(_ receivedNote: Routeguide_RouteNote) -> [Routeguide_RouteNote] { + return self.notes.withLock { notes in + var notesFromSameLocation: [Routeguide_RouteNote] = [] + for note in notes { + if note.location == receivedNote.location { + notesFromSameLocation.append(note) + } + } + notes.append(receivedNote) + return notesFromSameLocation + } + } + } + /// Creates a new route guide service. /// - Parameter features: Known features. init(features: [Routeguide_Feature]) { self.features = features + self.receivedNotes = Notes() } /// Returns the first feature found at the given location, if one exists. @@ -24,8 +54,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { context: ServerContext ) async throws -> Routeguide_Feature { let feature = self.findFeature( - latitude: request.message.latitude, - longitude: request.message.longitude + latitude: request.latitude, + longitude: request.longitude ) if let feature { @@ -35,8 +65,8 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { let unknownFeature = Routeguide_Feature.with { $0.name = "" $0.location = .with { - $0.latitude = request.message.latitude - $0.longitude = request.message.longitude + $0.latitude = request.latitude + $0.longitude = request.longitude } } return unknownFeature @@ -95,6 +125,10 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol { response: RPCWriter, context: ServerContext ) async throws { + for try await note in request { + let notes = self.receivedNotes.recordNote(note) + try await response.write(contentsOf: notes) + } } } diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec05-step04-load-features.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec05-step04-load-features.swift index 82dc23b9..628c00e2 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec05-step04-load-features.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec05-step04-load-features.swift @@ -12,7 +12,7 @@ struct RouteGuide: AsyncParsableCommand { } } - private static func loadFeatures() throws -> [Routeguide_Feature] { + func loadFeatures() throws -> [Routeguide_Feature] { guard let url = Bundle.module.url(forResource: "route_guide_db", withExtension: "json") else { throw ExitCode.failure } diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step06-get-feature.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step06-get-feature.swift index e985f818..6809b54f 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step06-get-feature.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step06-get-feature.swift @@ -14,7 +14,9 @@ extension RouteGuide { } } - private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func getFeature( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'GetFeature'") let point = Routeguide_Point.with { diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step07-list-features.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step07-list-features.swift index ba990eed..c276819d 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step07-list-features.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step07-list-features.swift @@ -15,7 +15,9 @@ extension RouteGuide { } } - private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func getFeature( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'GetFeature'") let point = Routeguide_Point.with { @@ -27,7 +29,9 @@ extension RouteGuide { print("Got feature '\(feature.name)'") } - private func listFeatures(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func listFeatures( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'ListFeatures'") let boundingRectangle = Routeguide_Rectangle.with { diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step08-record-route.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step08-record-route.swift index 8bf4f71e..12ab60af 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step08-record-route.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step08-record-route.swift @@ -16,7 +16,9 @@ extension RouteGuide { } } - private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func getFeature( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'GetFeature'") let point = Routeguide_Point.with { @@ -28,7 +30,9 @@ extension RouteGuide { print("Got feature '\(feature.name)'") } - private func listFeatures(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func listFeatures( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'ListFeatures'") let boundingRectangle = Routeguide_Rectangle.with { @@ -51,7 +55,9 @@ extension RouteGuide { } } - private func recordRoute(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func recordRoute( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'RecordRoute'") let features = try self.loadFeatures() diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step09-route-chat.swift b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step09-route-chat.swift index 81d7cfac..803d76f6 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step09-route-chat.swift +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Resources/route-guide-sec06-step09-route-chat.swift @@ -17,7 +17,9 @@ extension RouteGuide { } } - private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func getFeature( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'GetFeature'") let point = Routeguide_Point.with { @@ -29,7 +31,9 @@ extension RouteGuide { print("Got feature '\(feature.name)'") } - private func listFeatures(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func listFeatures( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'ListFeatures'") let boundingRectangle = Routeguide_Rectangle.with { @@ -52,7 +56,9 @@ extension RouteGuide { } } - private func recordRoute(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func recordRoute( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'RecordRoute'") let features = try self.loadFeatures() @@ -74,7 +80,9 @@ extension RouteGuide { ) } - private func routeChat(using routeGuide: Routeguide_RouteGuide.Client) async throws { + private func routeChat( + using routeGuide: Routeguide_RouteGuide.Client + ) async throws { print("→ Calling 'RouteChat'") try await routeGuide.routeChat { writer in diff --git a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Route-Guide.tutorial b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Route-Guide.tutorial index 63f6b7e4..839a2485 100644 --- a/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Route-Guide.tutorial +++ b/Sources/GRPCCore/Documentation.docc/Tutorials/Route-Guide/Route-Guide.tutorial @@ -291,7 +291,7 @@ After the *client* has finished sending points we populate a `Routeguide_RouteSummary` which we return in the response. - @Code(name: "Sources/RouteGuideService.swift", file: "route-guide-sec04-step08-client-streaming.swift") + @Code(name: "Sources/RouteGuideService.swift", file: "route-guide-sec04-step07-client-streaming.swift") } @Step { @@ -300,7 +300,7 @@ also need to modify the state of our service to store notes sent by the client. We'll do this with a helper class to store the notes. - @Code(name: "Sources/RouteGuideService.swift", file: "route-guide-sec04-step09-bidi-streaming.swift") + @Code(name: "Sources/RouteGuideService.swift", file: "route-guide-sec04-step08-bidi-streaming.swift") } @Step { @@ -308,7 +308,7 @@ note and get all other notes recorded in the same location. We then write each of those notes back to the client. - @Code(name: "Sources/RouteGuideService.swift", file: "route-guide-sec04-step10-bidi-streaming.swift") + @Code(name: "Sources/RouteGuideService.swift", file: "route-guide-sec04-step09-bidi-streaming.swift") } } }