/// <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); RouteGuide.Point request = new RouteGuide.Point { Latitude = lat, Longitude = lon }; RouteGuide.Feature feature = client.GetFeature(request); if (feature.Exists()) { Log("Found feature called \"{0}\" at {1}, {2}", feature.Name, feature.Location.GetLatitude(), feature.Location.GetLongitude()); } else { Log("Found no feature at {0}, {1}", feature.Location.GetLatitude(), feature.Location.GetLongitude()); } } catch (RpcException e) { Log("RPC failed " + e); throw; } }
/// <summary> /// Gets the feature at the given point. /// </summary> /// <param name="location">the location to check</param> /// <returns>The feature object at the point Note that an empty name indicates no feature.</returns> private RouteGuide.Feature CheckFeature(RouteGuide.Point location) { var result = features.FirstOrDefault((feature) => feature.Location.Equals(location)); if (result == null) { // No feature was found, return an unnamed feature. return(new RouteGuide.Feature { Name = "", Location = location }); } return(result); }
/// <summary> /// Adds a note for location and returns a list of pre-existing notes for that location (not containing the newly added note). /// </summary> private List <RouteGuide.RouteNote> AddNoteForLocation(RouteGuide.Point location, RouteGuide.RouteNote note) { lock (myLock) { List <RouteGuide.RouteNote> notes; if (!routeNotes.TryGetValue(location, out notes)) { notes = new List <RouteGuide.RouteNote>(); routeNotes.Add(location, notes); } var preexistingNotes = new List <RouteGuide.RouteNote>(notes); notes.Add(note); return(preexistingNotes); } }
/// <summary> /// Client-streaming example. Sends numPoints randomly chosen points from features /// with a variable delay in between. Prints the statistics when they are sent from the server. /// </summary> public async Task RecordRoute(List <RouteGuide.Feature> features, int numPoints) { try { Log("*** RecordRoute"); using (var call = client.RecordRoute()) { // Send numPoints points randomly selected from the features list. StringBuilder numMsg = new StringBuilder(); Random rand = new Random(); for (int i = 0; i < numPoints; ++i) { int index = rand.Next(features.Count); RouteGuide.Point point = features[index].Location; Log("Visiting point {0}, {1}", point.GetLatitude(), point.GetLongitude()); await call.RequestStream.WriteAsync(point); // A bit of delay before sending the next one. await Task.Delay(rand.Next(1000) + 500); } await call.RequestStream.CompleteAsync(); RouteGuide.RouteSummary summary = await call.ResponseAsync; Log("Finished trip with {0} points. Passed {1} features. " + "Travelled {2} meters. It took {3} seconds.", summary.PointCount, summary.FeatureCount, summary.Distance, summary.ElapsedTime); Log("Finished RecordRoute"); } } catch (RpcException e) { Log("RPC failed", e); throw; } }
/// <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 override async Task <RouteGuide.RouteSummary> RecordRoute(IAsyncStreamReader <RouteGuide.Point> requestStream, ServerCallContext context) { int pointCount = 0; int featureCount = 0; int distance = 0; RouteGuide.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 RouteGuide.RouteSummary { PointCount = pointCount, FeatureCount = featureCount, Distance = distance, ElapsedTime = (int)(stopwatch.ElapsedMilliseconds / 1000) }); }
/// <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 override Task <RouteGuide.Feature> GetFeature(RouteGuide.Point request, ServerCallContext context) { return(Task.FromResult(CheckFeature(request))); }