예제 #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.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());
        }
예제 #2
0
            /// <summary>
            /// Blocking unary call example.  Calls GetFeature and prints the response.
            /// </summary>
            public void GetFeature(int lat, int lon)
            {
                try
                {
                    Log("*** GetFeature: lat={0} lon={1}", lat, lon);

                    Point request = Point.CreateBuilder().SetLatitude(lat).SetLongitude(lon).Build();

                    Feature feature = client.GetFeature(request);
                    if (RouteGuideUtil.Exists(feature))
                    {
                        Log("Found feature called \"{0}\" at {1}, {2}",
                            feature.Name,
                            RouteGuideUtil.GetLatitude(feature.Location),
                            RouteGuideUtil.GetLongitude(feature.Location));
                    }
                    else
                    {
                        Log("Found no feature at {0}, {1}",
                            RouteGuideUtil.GetLatitude(feature.Location),
                            RouteGuideUtil.GetLongitude(feature.Location));
                    }
                }
                catch (RpcException e)
                {
                    Log("RPC failed " + e);
                    throw e;
                }
            }
예제 #3
0
        /// <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);
                }
            }
        }