Skip to content

Commit 8b8eccd

Browse files
authored
Add FeatureDatabase to non-streaming start_here proto (#16)
1 parent aa70365 commit 8b8eccd

File tree

5 files changed

+61
-15
lines changed

5 files changed

+61
-15
lines changed

codelabs/Getting_Started_with_gRPC_Java/complete/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,6 @@
1616

1717
package io.grpc.examples.routeguide;
1818

19-
import static java.lang.Math.atan2;
20-
import static java.lang.Math.cos;
21-
import static java.lang.Math.max;
22-
import static java.lang.Math.min;
23-
import static java.lang.Math.sin;
24-
import static java.lang.Math.sqrt;
25-
import static java.lang.Math.toRadians;
26-
import static java.util.concurrent.TimeUnit.NANOSECONDS;
27-
2819
import io.grpc.Grpc;
2920
import io.grpc.InsecureServerCredentials;
3021
import io.grpc.Server;

codelabs/Getting_Started_with_gRPC_Java/start_here/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import io.grpc.InsecureServerCredentials;
66
import io.grpc.Server;
77
import io.grpc.ServerBuilder;
8+
import io.grpc.stub.StreamObserver;
89
import java.io.IOException;
910
import java.net.URL;
11+
import java.util.Collection;
1012
import java.util.concurrent.TimeUnit;
1113
import java.util.logging.Logger;
1214

codelabs/Getting_Started_with_gRPC_Java/start_here/src/main/proto/routeguide/route_guide.proto

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ package routeguide;
1212
/////////////////////////////////////////////////////////////////////////////
1313

1414

15-
message Point {
15+
message Feature {
1616
/////////////////////////////////////////////////////////////////////////////
17-
// Codelab Hint: Define the Point message's contents here
17+
// Codelab Hint: Define the Feature message's contents here
1818
/////////////////////////////////////////////////////////////////////////////
1919
}
2020

2121
/////////////////////////////////////////////////////////////////////////////
22-
// Codelab Hint: Define the Feature message here
22+
// Codelab Hint: Define the Point message here
2323
/////////////////////////////////////////////////////////////////////////////
2424

25+
// Not used in the RPC. Instead, this is here for the form serialized to disk.
26+
message FeatureDatabase {
27+
repeated Feature feature = 1;
28+
}

codelabs/Getting_Started_with_gRPC_Java_Streaming/start_here/src/main/java/io/grpc/examples/routeguide/RouteGuideClient.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
import io.grpc.Status;
99
import io.grpc.StatusRuntimeException;
1010
import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideBlockingStub;
11-
import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideStub; // For Phase 2
12-
import io.grpc.stub.StreamObserver; // For Phase 2
11+
import io.grpc.examples.routeguide.RouteGuideGrpc.RouteGuideStub;
12+
import io.grpc.stub.StreamObserver;
1313
import java.io.IOException;
14+
import java.util.Iterator;
1415
import java.util.List;
16+
import java.util.Random;
17+
import java.util.concurrent.CountDownLatch;
1518
import java.util.concurrent.TimeUnit;
1619
import java.util.logging.Level;
1720
import java.util.logging.Logger;
@@ -24,6 +27,7 @@ public class RouteGuideClient {
2427

2528
private final RouteGuideBlockingStub blockingStub;
2629
private final RouteGuideStub asyncStub;
30+
private Random random = new Random();
2731

2832
/** Construct client stub for accessing RouteGuide server using the existing channel. */
2933
public RouteGuideClient(Channel channel) {
@@ -133,8 +137,8 @@ public void onCompleted() {
133137
* Codelab Hint: Start the recordRoute RPC using the asyncStub
134138
* - Put the return value into requestObserver
135139
*
136-
StreamObserver<Point> requestObserver =
137140
****************************************************************/
141+
StreamObserver<Point> requestObserver = null; // ***** TODO Replace null ******
138142
try {
139143
// Send numPoints points randomly selected from the features list.
140144
for (int i = 0; i < numPoints; ++i) {
@@ -279,4 +283,9 @@ private void warning(String msg, Object... params) {
279283
logger.log(Level.WARNING, msg, params);
280284
}
281285

286+
private RouteNote newNote(String message, int lat, int lon) {
287+
return RouteNote.newBuilder().setMessage(message)
288+
.setLocation(Point.newBuilder().setLatitude(lat).setLongitude(lon).build()).build();
289+
}
290+
282291
}

codelabs/Getting_Started_with_gRPC_Java_Streaming/start_here/src/main/java/io/grpc/examples/routeguide/RouteGuideServer.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
11

22
package io.grpc.examples.routeguide;
33

4+
import static java.lang.Math.atan2;
5+
import static java.lang.Math.cos;
6+
import static java.lang.Math.max;
7+
import static java.lang.Math.min;
8+
import static java.lang.Math.sin;
9+
import static java.lang.Math.sqrt;
10+
import static java.lang.Math.toRadians;
11+
import static java.util.concurrent.TimeUnit.NANOSECONDS;
12+
413
import io.grpc.Grpc;
514
import io.grpc.InsecureServerCredentials;
615
import io.grpc.Server;
716
import io.grpc.ServerBuilder;
17+
import io.grpc.stub.StreamObserver;
818
import java.io.IOException;
919
import java.net.URL;
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.Collections;
23+
import java.util.List;
24+
import java.util.concurrent.ConcurrentHashMap;
25+
import java.util.concurrent.ConcurrentMap;
1026
import java.util.concurrent.TimeUnit;
27+
import java.util.logging.Level;
1128
import java.util.logging.Logger;
1229

1330
/**
@@ -266,6 +283,29 @@ private Feature checkFeature(Point location) {
266283
return Feature.newBuilder().setName("").setLocation(location).build();
267284
}
268285

286+
/**
287+
* Calculate the distance between two points using the "haversine" formula.
288+
* The formula is based on http://mathforum.org/library/drmath/view/51879.html.
289+
*
290+
* @param start The starting point
291+
* @param end The end point
292+
* @return The distance between the points in meters
293+
*/
294+
private static int calcDistance(Point start, Point end) {
295+
int r = 6371000; // earth radius in meters
296+
double lat1 = toRadians(RouteGuideUtil.getLatitude(start));
297+
double lat2 = toRadians(RouteGuideUtil.getLatitude(end));
298+
double lon1 = toRadians(RouteGuideUtil.getLongitude(start));
299+
double lon2 = toRadians(RouteGuideUtil.getLongitude(end));
300+
double deltaLat = lat2 - lat1;
301+
double deltaLon = lon2 - lon1;
302+
303+
double a = sin(deltaLat / 2) * sin(deltaLat / 2)
304+
+ cos(lat1) * cos(lat2) * sin(deltaLon / 2) * sin(deltaLon / 2);
305+
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
306+
307+
return (int) (r * c);
308+
}
269309
}
270310
}
271311

0 commit comments

Comments
 (0)