Skip to content

Commit 9e17f1f

Browse files
author
Alexander Furer
committed

File tree

9 files changed

+1997
-0
lines changed

9 files changed

+1997
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.lognet.springboot.grpc.demo.routeguide;
2+
3+
import io.grpc.examples.routeguide.Point;
4+
import io.grpc.examples.routeguide.RouteGuideGrpc;
5+
import io.grpc.examples.routeguide.RouteNote;
6+
import io.grpc.stub.StreamObserver;
7+
import lombok.extern.slf4j.Slf4j;
8+
import org.lognet.springboot.grpc.GRpcService;
9+
10+
import java.util.ArrayList;
11+
import java.util.Collections;
12+
import java.util.List;
13+
import java.util.concurrent.ConcurrentHashMap;
14+
import java.util.concurrent.ConcurrentMap;
15+
16+
@Slf4j
17+
@GRpcService
18+
public class RouteGuideService extends RouteGuideGrpc.RouteGuideImplBase{
19+
20+
private final ConcurrentMap<Point, List<RouteNote>> routeNotes = new ConcurrentHashMap<>();
21+
/**
22+
* Receives a stream of message/location pairs, and responds with a stream of all previous
23+
* messages at each of those locations.
24+
*
25+
* @param responseObserver an observer to receive the stream of previous messages.
26+
* @return an observer to handle requested message/location pairs.
27+
*/
28+
@Override
29+
public StreamObserver<RouteNote> routeChat(final StreamObserver<RouteNote> responseObserver) {
30+
return new StreamObserver<RouteNote>() {
31+
@Override
32+
public void onNext(RouteNote note) {
33+
List<RouteNote> notes = getOrCreateNotes(note.getLocation());
34+
35+
// Respond with all previous notes at this location.
36+
for (RouteNote prevNote : notes.toArray(new RouteNote[0])) {
37+
responseObserver.onNext(prevNote);
38+
}
39+
40+
// Now add the new note to the list
41+
notes.add(note);
42+
}
43+
44+
@Override
45+
public void onError(Throwable t) {
46+
log.warn( "routeChat cancelled");
47+
}
48+
49+
@Override
50+
public void onCompleted() {
51+
responseObserver.onCompleted();
52+
}
53+
};
54+
}
55+
56+
/**
57+
* Get the notes list for the given location. If missing, create it.
58+
*/
59+
private List<RouteNote> getOrCreateNotes(Point location) {
60+
List<RouteNote> notes = Collections.synchronizedList(new ArrayList<RouteNote>());
61+
List<RouteNote> prevNotes = routeNotes.putIfAbsent(location, notes);
62+
return prevNotes != null ? prevNotes : notes;
63+
}
64+
65+
66+
67+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2015 The gRPC Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
syntax = "proto3";
15+
16+
option java_multiple_files = true;
17+
option java_package = "io.grpc.examples.routeguide";
18+
option java_outer_classname = "RouteGuideProto";
19+
option objc_class_prefix = "RTG";
20+
21+
package routeguide;
22+
23+
// Interface exported by the server.
24+
service RouteGuide {
25+
26+
27+
// A Bidirectional streaming RPC.
28+
//
29+
// Accepts a stream of RouteNotes sent while a route is being traversed,
30+
// while receiving other RouteNotes (e.g. from other users).
31+
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
32+
}
33+
34+
// Points are represented as latitude-longitude pairs in the E7 representation
35+
// (degrees multiplied by 10**7 and rounded to the nearest integer).
36+
// Latitudes should be in the range +/- 90 degrees and longitude should be in
37+
// the range +/- 180 degrees (inclusive).
38+
message Point {
39+
int32 latitude = 1;
40+
int32 longitude = 2;
41+
}
42+
43+
44+
// A RouteNote is a message sent while at a given point.
45+
message RouteNote {
46+
// The location from which the message is sent.
47+
Point location = 1;
48+
49+
// The message to be sent.
50+
string message = 2;
51+
}

0 commit comments

Comments
 (0)