/
routeguide.go
197 lines (176 loc) · 5.05 KB
/
routeguide.go
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
185
186
187
188
189
190
191
192
193
194
195
196
197
package rguide
import (
context "context"
_ "embed"
"encoding/json"
"fmt"
"io"
"log"
"math"
sync "sync"
"time"
"google.golang.org/protobuf/proto"
)
//go:embed sample_data.json
var jsonDBFile []byte
func NewServer() *RGServer {
s := &RGServer{
routeNotes: make(map[string][]*RouteNote),
}
s.loadFeatures("")
return s
}
type RGServer struct {
UnimplementedRouteGuideServer
savedFeatures []*Feature // read-only after initialized
mu sync.Mutex // protects routeNotes
routeNotes map[string][]*RouteNote
}
// GetFeature returns the feature at the given point.
func (s *RGServer) GetFeature(ctx context.Context, point *Point) (*Feature, error) {
for _, feature := range s.savedFeatures {
if proto.Equal(feature.Location, point) {
return feature, nil
}
}
// No feature was found, return an unnamed feature
return &Feature{Location: point}, nil
}
// GetDefaultFeature returns the feature at the given point.
func (s *RGServer) GetDefaultFeature(ctx context.Context, point *Point) (*Feature, error) {
feature := &Feature{
Name: "home",
Location: &Point{
Lat: 333,
Long: 333,
},
}
return feature, nil
}
// ListFeatures lists all features contained within the given bounding Rectangle.
func (s *RGServer) ListFeatures(rect *Rectangle, stream RouteGuide_ListFeaturesServer) error {
for _, feature := range s.savedFeatures {
if inRange(feature.Location, rect) {
if err := stream.Send(feature); err != nil {
return err
}
}
}
return nil
}
// RecordRoute records a route composited of a sequence of points.
//
// It 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.
func (s *RGServer) RecordRoute(stream RouteGuide_RecordRouteServer) error {
var pointCount, featureCount, distance int32
var lastPoint *Point
startTime := time.Now()
for {
point, err := stream.Recv()
if err == io.EOF {
endTime := time.Now()
return stream.SendAndClose(&RouteSummary{
PointCount: pointCount,
FeatureCount: featureCount,
Distance: distance,
ElapsedTime: int32(endTime.Sub(startTime).Seconds()),
})
}
if err != nil {
return err
}
pointCount++
for _, feature := range s.savedFeatures {
if proto.Equal(feature.Location, point) {
featureCount++
}
}
if lastPoint != nil {
distance += calcDistance(lastPoint, point)
}
lastPoint = point
}
}
// RouteChat receives a stream of message/location pairs, and responds with a stream of all
// previous messages at each of those locations.
func (s *RGServer) RouteChat(stream RouteGuide_RouteChatServer) error {
for {
in, err := stream.Recv()
if err == io.EOF {
return nil
}
if err != nil {
return err
}
key := serialize(in.Location)
s.mu.Lock()
s.routeNotes[key] = append(s.routeNotes[key], in)
// Note: this copy prevents blocking other clients while serving this one.
// We don't need to do a deep copy, because elements in the slice are
// insert-only and never modified.
rn := make([]*RouteNote, len(s.routeNotes[key]))
copy(rn, s.routeNotes[key])
s.mu.Unlock()
for _, note := range rn {
if err := stream.Send(note); err != nil {
return err
}
}
}
}
// loadFeatures loads features from a JSON file.
func (s *RGServer) loadFeatures(filePath string) {
// var data []byte
// if filePath != "" {
// var err error
// data, err = ioutil.ReadFile(filePath)
// if err != nil {
// log.Fatalf("Failed to load default features: %v", err)
// }
// } else {
// data = exampleData
// }
data := jsonDBFile
if err := json.Unmarshal(data, &s.savedFeatures); err != nil {
log.Fatalf("Failed to load default features: %v", err)
}
}
func toRadians(num float64) float64 {
return num * math.Pi / float64(180)
}
// calcDistance calculates the distance between two points using the "haversine" formula.
// The formula is based on http://mathforum.org/library/drmath/view/51879.html.
func calcDistance(p1 *Point, p2 *Point) int32 {
const CordFactor float64 = 1e7
const R = float64(6371000) // earth radius in metres
lat1 := toRadians(float64(p1.Lat) / CordFactor)
lat2 := toRadians(float64(p2.Lat) / CordFactor)
lng1 := toRadians(float64(p1.Long) / CordFactor)
lng2 := toRadians(float64(p2.Long) / CordFactor)
dlat := lat2 - lat1
dlng := lng2 - lng1
a := math.Sin(dlat/2)*math.Sin(dlat/2) +
math.Cos(lat1)*math.Cos(lat2)*
math.Sin(dlng/2)*math.Sin(dlng/2)
c := 2 * math.Atan2(math.Sqrt(a), math.Sqrt(1-a))
distance := R * c
return int32(distance)
}
func inRange(point *Point, rect *Rectangle) bool {
left := math.Min(float64(rect.Lo.Long), float64(rect.Hi.Long))
right := math.Max(float64(rect.Lo.Long), float64(rect.Hi.Long))
top := math.Max(float64(rect.Lo.Lat), float64(rect.Hi.Lat))
bottom := math.Min(float64(rect.Lo.Lat), float64(rect.Hi.Lat))
if float64(point.Long) >= left &&
float64(point.Long) <= right &&
float64(point.Lat) >= bottom &&
float64(point.Lat) <= top {
return true
}
return false
}
func serialize(point *Point) string {
return fmt.Sprintf("%d %d", point.Lat, point.Long)
}