Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -95,10 +95,6 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
response: RPCWriter<Routeguide_RouteNote>,
context: ServerContext
) async throws {
for try await note in request {
let notes = self.receivedNotes.recordNote(note)
try await response.write(contentsOf: notes)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -95,6 +125,10 @@ struct RouteGuideService: Routeguide_RouteGuide.SimpleServiceProtocol {
response: RPCWriter<Routeguide_RouteNote>,
context: ServerContext
) async throws {
for try await note in request {
let notes = self.receivedNotes.recordNote(note)
try await response.write(contentsOf: notes)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ extension RouteGuide {
}
}

private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws {
private func getFeature(
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
) async throws {
print("→ Calling 'GetFeature'")

let point = Routeguide_Point.with {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ extension RouteGuide {
}
}

private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws {
private func getFeature(
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
) async throws {
print("→ Calling 'GetFeature'")

let point = Routeguide_Point.with {
Expand All @@ -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<some ClientTransport>
) async throws {
print("→ Calling 'ListFeatures'")

let boundingRectangle = Routeguide_Rectangle.with {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ extension RouteGuide {
}
}

private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws {
private func getFeature(
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
) async throws {
print("→ Calling 'GetFeature'")

let point = Routeguide_Point.with {
Expand All @@ -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<some ClientTransport>
) async throws {
print("→ Calling 'ListFeatures'")

let boundingRectangle = Routeguide_Rectangle.with {
Expand All @@ -51,7 +55,9 @@ extension RouteGuide {
}
}

private func recordRoute(using routeGuide: Routeguide_RouteGuide.Client) async throws {
private func recordRoute(
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
) async throws {
print("→ Calling 'RecordRoute'")

let features = try self.loadFeatures()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ extension RouteGuide {
}
}

private func getFeature(using routeGuide: Routeguide_RouteGuide.Client) async throws {
private func getFeature(
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
) async throws {
print("→ Calling 'GetFeature'")

let point = Routeguide_Point.with {
Expand All @@ -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<some ClientTransport>
) async throws {
print("→ Calling 'ListFeatures'")

let boundingRectangle = Routeguide_Rectangle.with {
Expand All @@ -52,7 +56,9 @@ extension RouteGuide {
}
}

private func recordRoute(using routeGuide: Routeguide_RouteGuide.Client) async throws {
private func recordRoute(
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
) async throws {
print("→ Calling 'RecordRoute'")

let features = try self.loadFeatures()
Expand All @@ -74,7 +80,9 @@ extension RouteGuide {
)
}

private func routeChat(using routeGuide: Routeguide_RouteGuide.Client) async throws {
private func routeChat(
using routeGuide: Routeguide_RouteGuide.Client<some ClientTransport>
) async throws {
print("→ Calling 'RouteChat'")

try await routeGuide.routeChat { writer in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -300,15 +300,15 @@
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 {
To implement the RPC we iterate the request notes, call our helper class to record each
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")
}
}
}
Expand Down