示例#1
0
        /// <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)
            };
        }
示例#2
0
        /// <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();
        }
示例#3
0
 /// <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);
         }
     }
 }
示例#4
0
        /// <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);
                }
            }
        }