Make Route Guide client and server accept channel and server builers, respectively.

This commit is contained in:
Carl Mastrangelo 2016-03-02 10:40:53 -08:00
parent 8e1fba7c90
commit c85e04698f
2 changed files with 19 additions and 18 deletions

View File

@ -58,11 +58,14 @@ public class RouteGuideClient {
private final RouteGuideBlockingStub blockingStub;
private final RouteGuideStub asyncStub;
/** Construct client for accessing RoutGuide server at {@code host:port}. */
/** Construct client for accessing RouteGuide server at {@code host:port}. */
public RouteGuideClient(String host, int port) {
channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext(true)
.build();
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext(true));
}
/** Construct client for accessing RouteGuide server using the existing channel. */
public RouteGuideClient(ManagedChannelBuilder<?> channelBuilder) {
channel = channelBuilder.build();
blockingStub = RouteGuideGrpc.newBlockingStub(channel);
asyncStub = RouteGuideGrpc.newStub(channel);
}

View File

@ -62,29 +62,27 @@ public class RouteGuideServer {
private static final Logger logger = Logger.getLogger(RouteGuideServer.class.getName());
private final int port;
private final Collection<Feature> features;
private Server server;
private final Server server;
public RouteGuideServer(int port) {
public RouteGuideServer(int port) throws IOException {
this(port, RouteGuideUtil.getDefaultFeaturesFile());
}
/** Create a RouteGuide server listening on {@code port} using {@code featureFile} database. */
public RouteGuideServer(int port, URL featureFile) {
try {
this.port = port;
features = RouteGuideUtil.parseFeatures(featureFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
public RouteGuideServer(int port, URL featureFile) throws IOException {
this(ServerBuilder.forPort(port), port, RouteGuideUtil.parseFeatures(featureFile));
}
/** Create a RouteGuide server using serverBuilder as a base and features as data. */
public RouteGuideServer(ServerBuilder<?> serverBuilder, int port, Collection<Feature> features) {
this.port = port;
server = serverBuilder.addService(RouteGuideGrpc.bindService(new RouteGuideService(features)))
.build();
}
/** Start serving requests. */
public void start() throws IOException {
server = ServerBuilder.forPort(port)
.addService(RouteGuideGrpc.bindService(new RouteGuideService(features)))
.build()
.start();
server.start();
logger.info("Server started, listening on " + port);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override