Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Side by Side Diff: third_party/grpc/examples/csharp/route_guide/RouteGuideServer/RouteGuideImpl.cs

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 using System;
31 using System.Collections.Concurrent;
32 using System.Collections.Generic;
33 using System.Diagnostics;
34 using System.Linq;
35 using System.Text;
36 using System.Threading.Tasks;
37
38 using Grpc.Core.Utils;
39
40 namespace Routeguide
41 {
42 /// <summary>
43 /// Example implementation of RouteGuide server.
44 /// </summary>
45 public class RouteGuideImpl : RouteGuide.IRouteGuide
46 {
47 readonly List<Feature> features;
48 readonly object myLock = new object();
49 readonly Dictionary<Point, List<RouteNote>> routeNotes = new Dictionary< Point, List<RouteNote>>();
50
51 public RouteGuideImpl(List<Feature> features)
52 {
53 this.features = features;
54 }
55
56 /// <summary>
57 /// Gets the feature at the requested point. If no feature at that locat ion
58 /// exists, an unnammed feature is returned at the provided location.
59 /// </summary>
60 public Task<Feature> GetFeature(Point request, Grpc.Core.ServerCallConte xt context)
61 {
62 return Task.FromResult(CheckFeature(request));
63 }
64
65 /// <summary>
66 /// Gets all features contained within the given bounding rectangle.
67 /// </summary>
68 public async Task ListFeatures(Rectangle request, Grpc.Core.IServerStrea mWriter<Feature> responseStream, Grpc.Core.ServerCallContext context)
69 {
70 var responses = features.FindAll( (feature) => feature.Exists() && r equest.Contains(feature.Location) );
71 foreach (var response in responses)
72 {
73 await responseStream.WriteAsync(response);
74 }
75 }
76
77 /// <summary>
78 /// Gets a stream of points, and responds with statistics about the "tri p": number of points,
79 /// number of known features visited, total distance traveled, and total time spent.
80 /// </summary>
81 public async Task<RouteSummary> RecordRoute(Grpc.Core.IAsyncStreamReader <Point> requestStream, Grpc.Core.ServerCallContext context)
82 {
83 int pointCount = 0;
84 int featureCount = 0;
85 int distance = 0;
86 Point previous = null;
87 var stopwatch = new Stopwatch();
88 stopwatch.Start();
89
90 while (await requestStream.MoveNext())
91 {
92 var point = requestStream.Current;
93 pointCount++;
94 if (CheckFeature(point).Exists())
95 {
96 featureCount++;
97 }
98 if (previous != null)
99 {
100 distance += (int) previous.GetDistance(point);
101 }
102 previous = point;
103 }
104
105 stopwatch.Stop();
106
107 return new RouteSummary
108 {
109 PointCount = pointCount,
110 FeatureCount = featureCount,
111 Distance = distance,
112 ElapsedTime = (int)(stopwatch.ElapsedMilliseconds / 1000)
113 };
114 }
115
116 /// <summary>
117 /// Receives a stream of message/location pairs, and responds with a str eam of all previous
118 /// messages at each of those locations.
119 /// </summary>
120 public async Task RouteChat(Grpc.Core.IAsyncStreamReader<RouteNote> requ estStream, Grpc.Core.IServerStreamWriter<RouteNote> responseStream, Grpc.Core.Se rverCallContext context)
121 {
122 while (await requestStream.MoveNext())
123 {
124 var note = requestStream.Current;
125 List<RouteNote> prevNotes = AddNoteForLocation(note.Location, no te);
126 foreach (var prevNote in prevNotes)
127 {
128 await responseStream.WriteAsync(prevNote);
129 }
130 }
131 }
132
133 /// <summary>
134 /// Adds a note for location and returns a list of pre-existing notes fo r that location (not containing the newly added note).
135 /// </summary>
136 private List<RouteNote> AddNoteForLocation(Point location, RouteNote not e)
137 {
138 lock (myLock)
139 {
140 List<RouteNote> notes;
141 if (!routeNotes.TryGetValue(location, out notes)) {
142 notes = new List<RouteNote>();
143 routeNotes.Add(location, notes);
144 }
145 var preexistingNotes = new List<RouteNote>(notes);
146 notes.Add(note);
147 return preexistingNotes;
148 }
149 }
150
151 /// <summary>
152 /// Gets the feature at the given point.
153 /// </summary>
154 /// <param name="location">the location to check</param>
155 /// <returns>The feature object at the point Note that an empty name ind icates no feature.</returns>
156 private Feature CheckFeature(Point location)
157 {
158 var result = features.FirstOrDefault((feature) => feature.Location.E quals(location));
159 if (result == null)
160 {
161 // No feature was found, return an unnamed feature.
162 return new Feature { Name = "", Location = location };
163 }
164 return result;
165 }
166 }
167 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698