mirror of https://github.com/grpc/grpc.git
The C based gRPC (C++, Python, Ruby, Objective-C, PHP, C#)
https://grpc.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
154 lines
5.6 KiB
154 lines
5.6 KiB
8 years ago
|
// Copyright 2015 gRPC authors.
|
||
9 years ago
|
//
|
||
8 years ago
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
9 years ago
|
//
|
||
8 years ago
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
9 years ago
|
//
|
||
8 years ago
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
9 years ago
|
|
||
|
using System;
|
||
10 years ago
|
using System.Collections.Concurrent;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Diagnostics;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using System.Threading.Tasks;
|
||
|
|
||
9 years ago
|
using Grpc.Core;
|
||
9 years ago
|
using Grpc.Core.Utils;
|
||
|
|
||
9 years ago
|
namespace Routeguide
|
||
10 years ago
|
{
|
||
|
/// <summary>
|
||
|
/// Example implementation of RouteGuide server.
|
||
|
/// </summary>
|
||
9 years ago
|
public class RouteGuideImpl : RouteGuide.RouteGuideBase
|
||
10 years ago
|
{
|
||
|
readonly List<Feature> features;
|
||
9 years ago
|
readonly object myLock = new object();
|
||
9 years ago
|
readonly Dictionary<Point, List<RouteNote>> routeNotes = new Dictionary<Point, List<RouteNote>>();
|
||
10 years ago
|
|
||
|
public RouteGuideImpl(List<Feature> features)
|
||
|
{
|
||
|
this.features = features;
|
||
|
}
|
||
|
|
||
|
/// <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>
|
||
9 years ago
|
public override Task<Feature> GetFeature(Point request, ServerCallContext context)
|
||
10 years ago
|
{
|
||
|
return Task.FromResult(CheckFeature(request));
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets all features contained within the given bounding rectangle.
|
||
|
/// </summary>
|
||
9 years ago
|
public override async Task ListFeatures(Rectangle request, IServerStreamWriter<Feature> responseStream, ServerCallContext context)
|
||
10 years ago
|
{
|
||
9 years ago
|
var responses = features.FindAll( (feature) => feature.Exists() && request.Contains(feature.Location) );
|
||
9 years ago
|
foreach (var response in responses)
|
||
|
{
|
||
|
await responseStream.WriteAsync(response);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
/// <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>
|
||
9 years ago
|
public override async Task<RouteSummary> RecordRoute(IAsyncStreamReader<Point> requestStream, ServerCallContext context)
|
||
10 years ago
|
{
|
||
|
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++;
|
||
9 years ago
|
if (CheckFeature(point).Exists())
|
||
10 years ago
|
{
|
||
|
featureCount++;
|
||
|
}
|
||
|
if (previous != null)
|
||
|
{
|
||
9 years ago
|
distance += (int) previous.GetDistance(point);
|
||
10 years ago
|
}
|
||
|
previous = point;
|
||
|
}
|
||
|
|
||
|
stopwatch.Stop();
|
||
9 years ago
|
|
||
|
return new RouteSummary
|
||
|
{
|
||
|
PointCount = pointCount,
|
||
|
FeatureCount = featureCount,
|
||
|
Distance = distance,
|
||
|
ElapsedTime = (int)(stopwatch.ElapsedMilliseconds / 1000)
|
||
|
};
|
||
10 years ago
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Receives a stream of message/location pairs, and responds with a stream of all previous
|
||
|
/// messages at each of those locations.
|
||
|
/// </summary>
|
||
9 years ago
|
public override async Task RouteChat(IAsyncStreamReader<RouteNote> requestStream, IServerStreamWriter<RouteNote> responseStream, ServerCallContext context)
|
||
10 years ago
|
{
|
||
|
while (await requestStream.MoveNext())
|
||
|
{
|
||
|
var note = requestStream.Current;
|
||
9 years ago
|
List<RouteNote> prevNotes = AddNoteForLocation(note.Location, note);
|
||
10 years ago
|
foreach (var prevNote in prevNotes)
|
||
|
{
|
||
|
await responseStream.WriteAsync(prevNote);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
9 years ago
|
/// Adds a note for location and returns a list of pre-existing notes for that location (not containing the newly added note).
|
||
10 years ago
|
/// </summary>
|
||
9 years ago
|
private List<RouteNote> AddNoteForLocation(Point location, RouteNote note)
|
||
10 years ago
|
{
|
||
9 years ago
|
lock (myLock)
|
||
|
{
|
||
|
List<RouteNote> notes;
|
||
|
if (!routeNotes.TryGetValue(location, out notes)) {
|
||
|
notes = new List<RouteNote>();
|
||
|
routeNotes.Add(location, notes);
|
||
|
}
|
||
|
var preexistingNotes = new List<RouteNote>(notes);
|
||
|
notes.Add(note);
|
||
|
return preexistingNotes;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
|
/// <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 Feature CheckFeature(Point location)
|
||
|
{
|
||
9 years ago
|
var result = features.FirstOrDefault((feature) => feature.Location.Equals(location));
|
||
|
if (result == null)
|
||
10 years ago
|
{
|
||
9 years ago
|
// No feature was found, return an unnamed feature.
|
||
|
return new Feature { Name = "", Location = location };
|
||
10 years ago
|
}
|
||
9 years ago
|
return result;
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|