aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/csharp/route_guide/RouteGuideServer/RouteGuideImpl.cs
blob: 0bdf386fc333c2239b77d21e99981657c4518082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace examples
{
    /// <summary>
    /// Example implementation of RouteGuide server.
    /// </summary>
    public class RouteGuideImpl : RouteGuide.IRouteGuide
    {
        readonly List<Feature> features;
        private readonly ConcurrentDictionary<Point, List<RouteNote>> routeNotes =
            new ConcurrentDictionary<Point, List<RouteNote>>();

        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>
        public Task<Feature> GetFeature(Grpc.Core.ServerCallContext context, Point request)
        {
            return Task.FromResult(CheckFeature(request));
        }

        /// <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);
                }
            }
        }

        /// <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();
        }

        /// <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);
                }
            }
        }

        
        /// <summary>
        /// Get the notes list for the given location. If missing, create it.
        /// </summary>
        private List<RouteNote> GetOrCreateNotes(Point location)
        {
            List<RouteNote> notes = new List<RouteNote>();
            routeNotes.TryAdd(location, notes);
            return routeNotes[location];
        }

        /// <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)
        {
            foreach (var feature in features)
            {
                if (feature.Location.Latitude == location.Latitude
                    && feature.Location.Longitude == location.Longitude)
                {
                    return feature;
                }
            }

            // No feature was found, return an unnamed feature.
            return Feature.CreateBuilder().SetName("").SetLocation(location).Build();
        }

        /// <summary>
        /// Calculate the distance between two points using the "haversine" formula.
        /// This code was taken from http://www.movable-type.co.uk/scripts/latlong.html.
        /// </summary>
        /// <param name="start">the starting point</param>
        /// <param name="end">the end point</param>
        /// <returns>the distance between the points in meters</returns>
        private static double CalcDistance(Point start, Point end)
        {
            double lat1 = RouteGuideUtil.GetLatitude(start);
            double lat2 = RouteGuideUtil.GetLatitude(end);
            double lon1 = RouteGuideUtil.GetLongitude(start);
            double lon2 = RouteGuideUtil.GetLongitude(end);
            int r = 6371000; // metres
            double φ1 = ToRadians(lat1);
            double φ2 = ToRadians(lat2);
            double Δφ = ToRadians(lat2 - lat1);
            double Δλ = ToRadians(lon2 - lon1);

            double a = Math.Sin(Δφ / 2) * Math.Sin(Δφ / 2) + Math.Cos(φ1) * Math.Cos(φ2) * Math.Sin(Δλ / 2) * Math.Sin(Δλ / 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));

            return r * c;
        }

        private static double ToRadians(double val)
        {
            return (Math.PI / 180) * val;
        }
    }
}