forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.java
198 lines (163 loc) · 6.75 KB
/
Client.java
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
198
package io.vertx.example.grpc.routeguide;
import io.grpc.ManagedChannel;
import io.grpc.Status;
import io.grpc.examples.routeguide.*;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.streams.ReadStream;
import io.vertx.core.streams.WriteStream;
import io.vertx.example.grpc.util.Runner;
import io.vertx.grpc.VertxChannelBuilder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
public class Client extends AbstractVerticle {
public static void main(String[] args) {
Runner.runExample(Client.class);
}
private Random random = new Random();
private VertxRouteGuideGrpc.RouteGuideVertxStub stub;
@Override
public void start() throws Exception {
ManagedChannel channel = VertxChannelBuilder
.forAddress(vertx, "localhost", 8080)
.usePlaintext()
.build();
stub = VertxRouteGuideGrpc.newVertxStub(channel);
List<Feature> features = Util.parseFeatures(Util.getDefaultFeaturesFile());
// Looking for a valid feature
getFeature(409146138, -746188906);
// Feature missing.
getFeature(0, 0);
// Looking for features between 40, -75 and 42, -73.
listFeatures(400000000, -750000000, 420000000, -730000000);
// Record a few randomly selected points from the features file.
recordRoute(features, 10);
routeChat();
}
/**
* Blocking unary call example. Calls getFeature and prints the response.
*/
public void getFeature(int lat, int lon) {
System.out.println("*** GetFeature: lat=" + lat + " lon=" + lon);
Point request = Point.newBuilder().setLatitude(lat).setLongitude(lon).build();
stub.getFeature(request).onComplete(ar -> {
if (ar.succeeded()) {
Feature feature = ar.result();
if (Util.exists(feature)) {
System.out.println("Found feature called " + feature.getName() +
" at " + Util.getLatitude(feature.getLocation()) + ", " + Util.getLongitude(feature.getLocation()));
} else {
System.out.println("Found no feature at " + Util.getLatitude(feature.getLocation()) + ", " +
Util.getLongitude(feature.getLocation()));
}
}
});
}
/**
* Blocking server-streaming example. Calls listFeatures with a rectangle of interest. Prints each
* response feature as it arrives.
*/
public void listFeatures(int lowLat, int lowLon, int hiLat, int hiLon) {
System.out.println("*** ListFeatures: lowLat=" + lowLat + " lowLon=" + lowLon + " hiLat=" + hiLat + " hiLon=" + hiLon);
Rectangle request =
Rectangle.newBuilder()
.setLo(Point.newBuilder().setLatitude(lowLat).setLongitude(lowLon).build())
.setHi(Point.newBuilder().setLatitude(hiLat).setLongitude(hiLon).build()).build();
ReadStream<Feature> response = stub.listFeatures(request);
List<Feature> features = Collections.synchronizedList(new ArrayList<>());
response.handler(feature -> {
System.out.println("Result #" + features.size() + ": " + feature);
features.add(feature);
});
// Neede for now as it triggers an NPE if not set
response.endHandler(v -> {
/*
Feb 14, 2017 11:08:53 PM io.grpc.internal.SerializingExecutor$TaskRunner run
SEVERE: Exception while executing runnable io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed@6668e779
java.lang.NullPointerException
at io.vertx.grpc.impl.GrpcReadStreamImpl$1.onCompleted(GrpcReadStreamImpl.java:69)
at io.grpc.stub.ClientCalls$StreamObserverToCallListenerAdapter.onClose(ClientCalls.java:390)
at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:422)
*/
});
}
class RouteSender {
List<Feature> features;
WriteStream<Point> writeStream;
RouteSender(List<Feature> features, WriteStream<Point> writeStream) {
this.features = features;
this.writeStream = writeStream;
}
void send(int numPoints) {
int index = random.nextInt(features.size());
Point point = features.get(index).getLocation();
System.out.println("Visiting point " + Util.getLatitude(point) + ", " + Util.getLongitude(point));
writeStream.write(point);
if (numPoints > 0) {
vertx.setTimer(random.nextInt(1000) + 500, id -> {
send(numPoints - 1);
});
} else {
writeStream.end();
}
}
}
/**
* Async client-streaming example. Sends {@code numPoints} randomly chosen points from {@code
* features} with a variable delay in between. Prints the statistics when they are sent from the
* server.
*/
public void recordRoute(List<Feature> features, int numPoints) {
System.out.println("*** RecordRoute");
stub.recordRoute(writeStream -> {
RouteSender sender = new RouteSender(features, writeStream);
// Send numPoints points randomly selected from the features list.
sender.send(numPoints);
}).onComplete(ar -> {
if (ar.succeeded()) {
RouteSummary summary = ar.result();
System.out.println("Finished trip with " + summary.getPointCount() + " points. Passed " + summary.getFeatureCount()
+ " features.Travelled " + summary.getDistance() + " meters. It took " + summary.getElapsedTime() + " seconds.");
System.out.println("Finished RecordRoute");
} else {
System.out.println("RecordRoute Failed: " + Status.fromThrowable(ar.cause()));
}
});
}
/**
* Bi-directional example, which can only be asynchronous. Send some chat messages, and print any
* chat messages that are sent from the server.
*/
public void routeChat() {
System.out.println("*** RouteChat");
ReadStream<RouteNote> readStream = stub.routeChat(writeStream -> {
RouteNote[] requests =
{newNote("First message", 0, 0), newNote("Second message", 0, 1),
newNote("Third message", 1, 0), newNote("Fourth message", 1, 1)};
for (RouteNote request : requests) {
System.out.println("Sending message \"" + request.getMessage() + "\" at " + request.getLocation()
.getLatitude() + ", " + request.getLocation().getLongitude());
writeStream.write(request);
}
writeStream.end();
});
readStream.handler(note -> {
System.out.println("Got message \"" + note.getMessage() + "\" at " + note.getLocation().getLatitude() +
", " + note.getLocation().getLongitude());
});
readStream.exceptionHandler(err -> {
System.out.println("RouteChat Failed: " + Status.fromThrowable(err));
});
readStream.endHandler(v -> {
System.out.println("Finished RouteChat");
});
}
private RouteNote newNote(String message, int lat, int lon) {
return RouteNote.newBuilder().setMessage(message)
.setLocation(Point.newBuilder().setLatitude(lat).setLongitude(lon).build()).build();
}
}