/// <summary> /// Gets a stream of points, and responds with statistics about the "trip": number of points, /// number of known features visited, total distance traveled, and total time spent. /// </summary> public async Task<RouteSummary> RecordRoute(Grpc.Core.ServerCallContext context, Grpc.Core.IAsyncStreamReader<Point> requestStream) { int pointCount = 0; int featureCount = 0; int distance = 0; Point previous = null; var stopwatch = new Stopwatch(); stopwatch.Start(); while (await requestStream.MoveNext()) { var point = requestStream.Current; pointCount++; if (RouteGuideUtil.Exists(CheckFeature(point))) { featureCount++; } if (previous != null) { distance += (int) CalcDistance(previous, point); } previous = point; } stopwatch.Stop(); return RouteSummary.CreateBuilder().SetPointCount(pointCount) .SetFeatureCount(featureCount).SetDistance(distance) .SetElapsedTime((int) (stopwatch.ElapsedMilliseconds / 1000)).Build(); }
/// <summary> /// Gets a stream of points, and responds with statistics about the "trip": number of points, /// number of known features visited, total distance traveled, and total time spent. /// </summary> public async Task<RouteSummary> RecordRoute(Grpc.Core.IAsyncStreamReader<Point> requestStream, Grpc.Core.ServerCallContext context) { int pointCount = 0; int featureCount = 0; int distance = 0; Point previous = null; var stopwatch = new Stopwatch(); stopwatch.Start(); while (await requestStream.MoveNext()) { var point = requestStream.Current; pointCount++; if (CheckFeature(point).Exists()) { featureCount++; } if (previous != null) { distance += (int) previous.GetDistance(point); } previous = point; } stopwatch.Stop(); return new RouteSummary { PointCount = pointCount, FeatureCount = featureCount, Distance = distance, ElapsedTime = (int)(stopwatch.ElapsedMilliseconds / 1000) }; }
/// <summary> /// Gets all features contained within the given bounding rectangle. /// </summary> public async Task ListFeatures(Rectangle request, Grpc.Core.IServerStreamWriter<Feature> responseStream, Grpc.Core.ServerCallContext context) { var responses = features.FindAll( (feature) => feature.Exists() && request.Contains(feature.Location) ); foreach (var response in responses) { await responseStream.WriteAsync(response); } }
/// <summary> /// Gets all features contained within the given bounding rectangle. /// </summary> public async Task ListFeatures(Grpc.Core.ServerCallContext context, Rectangle request, Grpc.Core.IServerStreamWriter<Feature> responseStream) { int left = Math.Min(request.Lo.Longitude, request.Hi.Longitude); int right = Math.Max(request.Lo.Longitude, request.Hi.Longitude); int top = Math.Max(request.Lo.Latitude, request.Hi.Latitude); int bottom = Math.Min(request.Lo.Latitude, request.Hi.Latitude); foreach (var feature in features) { if (!RouteGuideUtil.Exists(feature)) { continue; } int lat = feature.Location.Latitude; int lon = feature.Location.Longitude; if (lon >= left && lon <= right && lat >= bottom && lat <= top) { await responseStream.WriteAsync(feature); } } }
/// <summary> /// Gets the feature at the requested point. If no feature at that location /// exists, an unnammed feature is returned at the provided location. /// </summary> public Task<Feature> GetFeature(Point request, Grpc.Core.ServerCallContext context) { return Task.FromResult(CheckFeature(request)); }
/// <summary> /// Receives a stream of message/location pairs, and responds with a stream of all previous /// messages at each of those locations. /// </summary> public async Task RouteChat(Grpc.Core.IAsyncStreamReader<RouteNote> requestStream, Grpc.Core.IServerStreamWriter<RouteNote> responseStream, Grpc.Core.ServerCallContext context) { while (await requestStream.MoveNext()) { var note = requestStream.Current; List<RouteNote> prevNotes = AddNoteForLocation(note.Location, note); foreach (var prevNote in prevNotes) { await responseStream.WriteAsync(prevNote); } } }
/// <summary> /// Receives a stream of message/location pairs, and responds with a stream of all previous /// messages at each of those locations. /// </summary> public async Task RouteChat(Grpc.Core.ServerCallContext context, Grpc.Core.IAsyncStreamReader<RouteNote> requestStream, Grpc.Core.IServerStreamWriter<RouteNote> responseStream) { while (await requestStream.MoveNext()) { var note = requestStream.Current; List<RouteNote> notes = GetOrCreateNotes(note.Location); List<RouteNote> prevNotes; lock (notes) { prevNotes = new List<RouteNote>(notes); } foreach (var prevNote in prevNotes) { await responseStream.WriteAsync(prevNote); } lock (notes) { notes.Add(note); } } }