From 01580ae36ab8a0f67bd24178035a94bdb014b43a Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 13:43:08 +0200 Subject: [PATCH 01/10] Remove StopServer rpc method There is no actual requirement for this method, so removing it. It also helps improve the implementation by removing the need for the static 'instance' field. --- .../java/bisq/core/grpc/BisqGrpcServer.java | 22 ------------------- proto/src/main/proto/grpc.proto | 15 ------------- 2 files changed, 37 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java index 9ce22b72db8..1147389c2ea 100644 --- a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java @@ -42,9 +42,6 @@ import bisq.proto.grpc.PlaceOfferGrpc; import bisq.proto.grpc.PlaceOfferReply; import bisq.proto.grpc.PlaceOfferRequest; -import bisq.proto.grpc.StopServerGrpc; -import bisq.proto.grpc.StopServerReply; -import bisq.proto.grpc.StopServerRequest; import io.grpc.Server; import io.grpc.ServerBuilder; @@ -58,15 +55,11 @@ import lombok.extern.slf4j.Slf4j; -/** - * gRPC server. Gets a instance of BisqFacade passed to access data from the running Bisq instance. - */ @Slf4j public class BisqGrpcServer { private Server server; - private static BisqGrpcServer instance; private static Config config; private static CoreApi coreApi; @@ -157,25 +150,11 @@ public void placeOffer(PlaceOfferRequest req, StreamObserver re } } - static class StopServerImpl extends StopServerGrpc.StopServerImplBase { - @Override - public void stopServer(StopServerRequest req, StreamObserver responseObserver) { - StopServerReply reply = StopServerReply.newBuilder().build(); - responseObserver.onNext(reply); - responseObserver.onCompleted(); - - instance.stop(); - } - } - - /////////////////////////////////////////////////////////////////////////////////////////// // Constructor /////////////////////////////////////////////////////////////////////////////////////////// public BisqGrpcServer(Config config, CoreApi coreApi) { - instance = this; - BisqGrpcServer.config = config; BisqGrpcServer.coreApi = coreApi; @@ -214,7 +193,6 @@ private void start() throws IOException { .addService(new GetOffersImpl()) .addService(new GetPaymentAccountsImpl()) .addService(new PlaceOfferImpl()) - .addService(new StopServerImpl()) .intercept(new PasswordAuthInterceptor(config.apiPassword)) .build() .start(); diff --git a/proto/src/main/proto/grpc.proto b/proto/src/main/proto/grpc.proto index 6a013daa8b1..b1e7d084a65 100644 --- a/proto/src/main/proto/grpc.proto +++ b/proto/src/main/proto/grpc.proto @@ -127,18 +127,3 @@ message PlaceOfferRequest { message PlaceOfferReply { bool result = 1; } - -/////////////////////////////////////////////////////////////////////////////////////////// -// StopServer -/////////////////////////////////////////////////////////////////////////////////////////// - -service StopServer { - rpc StopServer (StopServerRequest) returns (StopServerReply) { - } -} - -message StopServerRequest { -} - -message StopServerReply { -} From f2028f2cef6ab0262e922daec19a260897b5d239 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 13:46:23 +0200 Subject: [PATCH 02/10] Remove section comments from BisqGrpcServer --- .../java/bisq/core/grpc/BisqGrpcServer.java | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java index 1147389c2ea..0eae1128c2a 100644 --- a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java @@ -54,7 +54,6 @@ import lombok.extern.slf4j.Slf4j; - @Slf4j public class BisqGrpcServer { @@ -63,11 +62,6 @@ public class BisqGrpcServer { private static Config config; private static CoreApi coreApi; - - /////////////////////////////////////////////////////////////////////////////////////////// - // Services - /////////////////////////////////////////////////////////////////////////////////////////// - static class GetVersionImpl extends GetVersionGrpc.GetVersionImplBase { @Override public void getVersion(GetVersionRequest req, StreamObserver responseObserver) { @@ -150,10 +144,6 @@ public void placeOffer(PlaceOfferRequest req, StreamObserver re } } - /////////////////////////////////////////////////////////////////////////////////////////// - // Constructor - /////////////////////////////////////////////////////////////////////////////////////////// - public BisqGrpcServer(Config config, CoreApi coreApi) { BisqGrpcServer.config = config; BisqGrpcServer.coreApi = coreApi; @@ -166,21 +156,12 @@ public BisqGrpcServer(Config config, CoreApi coreApi) { } } - - /////////////////////////////////////////////////////////////////////////////////////////// - // API - /////////////////////////////////////////////////////////////////////////////////////////// - public void stop() { if (server != null) { server.shutdown(); } } - /////////////////////////////////////////////////////////////////////////////////////////// - // Private - /////////////////////////////////////////////////////////////////////////////////////////// - private void start() throws IOException { // TODO add to options int port = 9998; From a52be594cbfad770b9e1eefed06d9e7c87994407 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 13:55:31 +0200 Subject: [PATCH 03/10] Eliminate static fields in BisqGrpcServer --- .../java/bisq/core/grpc/BisqGrpcServer.java | 94 +++++++++++-------- 1 file changed, 55 insertions(+), 39 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java index 0eae1128c2a..064fbe37479 100644 --- a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java @@ -43,7 +43,6 @@ import bisq.proto.grpc.PlaceOfferReply; import bisq.proto.grpc.PlaceOfferRequest; -import io.grpc.Server; import io.grpc.ServerBuilder; import io.grpc.stub.StreamObserver; @@ -57,12 +56,13 @@ @Slf4j public class BisqGrpcServer { - private Server server; + static class GetVersionImpl extends GetVersionGrpc.GetVersionImplBase { + private final CoreApi coreApi; - private static Config config; - private static CoreApi coreApi; + public GetVersionImpl(CoreApi coreApi) { + this.coreApi = coreApi; + } - static class GetVersionImpl extends GetVersionGrpc.GetVersionImplBase { @Override public void getVersion(GetVersionRequest req, StreamObserver responseObserver) { GetVersionReply reply = GetVersionReply.newBuilder().setVersion(coreApi.getVersion()).build(); @@ -72,6 +72,12 @@ public void getVersion(GetVersionRequest req, StreamObserver re } static class GetBalanceImpl extends GetBalanceGrpc.GetBalanceImplBase { + private final CoreApi coreApi; + + public GetBalanceImpl(CoreApi coreApi) { + this.coreApi = coreApi; + } + @Override public void getBalance(GetBalanceRequest req, StreamObserver responseObserver) { GetBalanceReply reply = GetBalanceReply.newBuilder().setBalance(coreApi.getAvailableBalance()).build(); @@ -81,6 +87,12 @@ public void getBalance(GetBalanceRequest req, StreamObserver re } static class GetTradeStatisticsImpl extends GetTradeStatisticsGrpc.GetTradeStatisticsImplBase { + private final CoreApi coreApi; + + public GetTradeStatisticsImpl(CoreApi coreApi) { + this.coreApi = coreApi; + } + @Override public void getTradeStatistics(GetTradeStatisticsRequest req, StreamObserver responseObserver) { @@ -94,6 +106,12 @@ public void getTradeStatistics(GetTradeStatisticsRequest req, } static class GetOffersImpl extends GetOffersGrpc.GetOffersImplBase { + private final CoreApi coreApi; + + public GetOffersImpl(CoreApi coreApi) { + this.coreApi = coreApi; + } + @Override public void getOffers(GetOffersRequest req, StreamObserver responseObserver) { @@ -108,6 +126,12 @@ public void getOffers(GetOffersRequest req, StreamObserver respo } static class GetPaymentAccountsImpl extends GetPaymentAccountsGrpc.GetPaymentAccountsImplBase { + private final CoreApi coreApi; + + public GetPaymentAccountsImpl(CoreApi coreApi) { + this.coreApi = coreApi; + } + @Override public void getPaymentAccounts(GetPaymentAccountsRequest req, StreamObserver responseObserver) { @@ -123,6 +147,12 @@ public void getPaymentAccounts(GetPaymentAccountsRequest req, } static class PlaceOfferImpl extends PlaceOfferGrpc.PlaceOfferImplBase { + private final CoreApi coreApi; + + public PlaceOfferImpl(CoreApi coreApi) { + this.coreApi = coreApi; + } + @Override public void placeOffer(PlaceOfferRequest req, StreamObserver responseObserver) { TransactionResultHandler resultHandler = transaction -> { @@ -145,45 +175,31 @@ public void placeOffer(PlaceOfferRequest req, StreamObserver re } public BisqGrpcServer(Config config, CoreApi coreApi) { - BisqGrpcServer.config = config; - BisqGrpcServer.coreApi = coreApi; try { - start(); + // TODO add to options + int port = 9998; + + var server = ServerBuilder.forPort(port) + .addService(new GetVersionImpl(coreApi)) + .addService(new GetBalanceImpl(coreApi)) + .addService(new GetTradeStatisticsImpl(coreApi)) + .addService(new GetOffersImpl(coreApi)) + .addService(new GetPaymentAccountsImpl(coreApi)) + .addService(new PlaceOfferImpl(coreApi)) + .intercept(new PasswordAuthInterceptor(config.apiPassword)) + .build() + .start(); + + log.info("Server started, listening on " + port); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + log.error("Shutting down gRPC server"); + server.shutdown(); + log.error("Server shut down"); + })); } catch (IOException e) { log.error(e.toString(), e); } } - - public void stop() { - if (server != null) { - server.shutdown(); - } - } - - private void start() throws IOException { - // TODO add to options - int port = 9998; - - // Config services - server = ServerBuilder.forPort(port) - .addService(new GetVersionImpl()) - .addService(new GetBalanceImpl()) - .addService(new GetTradeStatisticsImpl()) - .addService(new GetOffersImpl()) - .addService(new GetPaymentAccountsImpl()) - .addService(new PlaceOfferImpl()) - .intercept(new PasswordAuthInterceptor(config.apiPassword)) - .build() - .start(); - - log.info("Server started, listening on " + port); - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - // Use stderr here since the logger may have been reset by its JVM shutdown hook. - log.error("Shutting down gRPC server"); - BisqGrpcServer.this.stop(); - log.error("Server shut down"); - })); - } } From 376d11dd1e1d8c1fe5e20022b7b113589b9a64bb Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 13:59:28 +0200 Subject: [PATCH 04/10] Sort static inner classes at bottom --- .../java/bisq/core/grpc/BisqGrpcServer.java | 57 +++++++++---------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java index 064fbe37479..dadbe586d1f 100644 --- a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java @@ -56,6 +56,34 @@ @Slf4j public class BisqGrpcServer { + public BisqGrpcServer(Config config, CoreApi coreApi) { + try { + // TODO add to options + int port = 9998; + + var server = ServerBuilder.forPort(port) + .addService(new GetVersionImpl(coreApi)) + .addService(new GetBalanceImpl(coreApi)) + .addService(new GetTradeStatisticsImpl(coreApi)) + .addService(new GetOffersImpl(coreApi)) + .addService(new GetPaymentAccountsImpl(coreApi)) + .addService(new PlaceOfferImpl(coreApi)) + .intercept(new PasswordAuthInterceptor(config.apiPassword)) + .build() + .start(); + + log.info("Server started, listening on " + port); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + log.error("Shutting down gRPC server"); + server.shutdown(); + log.error("Server shut down"); + })); + + } catch (IOException e) { + log.error(e.toString(), e); + } + } + static class GetVersionImpl extends GetVersionGrpc.GetVersionImplBase { private final CoreApi coreApi; @@ -173,33 +201,4 @@ public void placeOffer(PlaceOfferRequest req, StreamObserver re resultHandler); } } - - public BisqGrpcServer(Config config, CoreApi coreApi) { - - try { - // TODO add to options - int port = 9998; - - var server = ServerBuilder.forPort(port) - .addService(new GetVersionImpl(coreApi)) - .addService(new GetBalanceImpl(coreApi)) - .addService(new GetTradeStatisticsImpl(coreApi)) - .addService(new GetOffersImpl(coreApi)) - .addService(new GetPaymentAccountsImpl(coreApi)) - .addService(new PlaceOfferImpl(coreApi)) - .intercept(new PasswordAuthInterceptor(config.apiPassword)) - .build() - .start(); - - log.info("Server started, listening on " + port); - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - log.error("Shutting down gRPC server"); - server.shutdown(); - log.error("Server shut down"); - })); - - } catch (IOException e) { - log.error(e.toString(), e); - } - } } From edbc81c5b41bb3007e3153542c3379ec97661956 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 14:02:16 +0200 Subject: [PATCH 05/10] Rename gRPC service classes from *{Impl=>Service} --- .../java/bisq/core/grpc/BisqGrpcServer.java | 36 +++++++++---------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java index dadbe586d1f..b805519cc84 100644 --- a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java @@ -62,12 +62,12 @@ public BisqGrpcServer(Config config, CoreApi coreApi) { int port = 9998; var server = ServerBuilder.forPort(port) - .addService(new GetVersionImpl(coreApi)) - .addService(new GetBalanceImpl(coreApi)) - .addService(new GetTradeStatisticsImpl(coreApi)) - .addService(new GetOffersImpl(coreApi)) - .addService(new GetPaymentAccountsImpl(coreApi)) - .addService(new PlaceOfferImpl(coreApi)) + .addService(new GetVersionService(coreApi)) + .addService(new GetBalanceService(coreApi)) + .addService(new GetTradeStatisticsService(coreApi)) + .addService(new GetOffersService(coreApi)) + .addService(new GetPaymentAccountsService(coreApi)) + .addService(new PlaceOfferService(coreApi)) .intercept(new PasswordAuthInterceptor(config.apiPassword)) .build() .start(); @@ -84,10 +84,10 @@ public BisqGrpcServer(Config config, CoreApi coreApi) { } } - static class GetVersionImpl extends GetVersionGrpc.GetVersionImplBase { + static class GetVersionService extends GetVersionGrpc.GetVersionImplBase { private final CoreApi coreApi; - public GetVersionImpl(CoreApi coreApi) { + public GetVersionService(CoreApi coreApi) { this.coreApi = coreApi; } @@ -99,10 +99,10 @@ public void getVersion(GetVersionRequest req, StreamObserver re } } - static class GetBalanceImpl extends GetBalanceGrpc.GetBalanceImplBase { + static class GetBalanceService extends GetBalanceGrpc.GetBalanceImplBase { private final CoreApi coreApi; - public GetBalanceImpl(CoreApi coreApi) { + public GetBalanceService(CoreApi coreApi) { this.coreApi = coreApi; } @@ -114,10 +114,10 @@ public void getBalance(GetBalanceRequest req, StreamObserver re } } - static class GetTradeStatisticsImpl extends GetTradeStatisticsGrpc.GetTradeStatisticsImplBase { + static class GetTradeStatisticsService extends GetTradeStatisticsGrpc.GetTradeStatisticsImplBase { private final CoreApi coreApi; - public GetTradeStatisticsImpl(CoreApi coreApi) { + public GetTradeStatisticsService(CoreApi coreApi) { this.coreApi = coreApi; } @@ -133,10 +133,10 @@ public void getTradeStatistics(GetTradeStatisticsRequest req, } } - static class GetOffersImpl extends GetOffersGrpc.GetOffersImplBase { + static class GetOffersService extends GetOffersGrpc.GetOffersImplBase { private final CoreApi coreApi; - public GetOffersImpl(CoreApi coreApi) { + public GetOffersService(CoreApi coreApi) { this.coreApi = coreApi; } @@ -153,10 +153,10 @@ public void getOffers(GetOffersRequest req, StreamObserver respo } } - static class GetPaymentAccountsImpl extends GetPaymentAccountsGrpc.GetPaymentAccountsImplBase { + static class GetPaymentAccountsService extends GetPaymentAccountsGrpc.GetPaymentAccountsImplBase { private final CoreApi coreApi; - public GetPaymentAccountsImpl(CoreApi coreApi) { + public GetPaymentAccountsService(CoreApi coreApi) { this.coreApi = coreApi; } @@ -174,10 +174,10 @@ public void getPaymentAccounts(GetPaymentAccountsRequest req, } } - static class PlaceOfferImpl extends PlaceOfferGrpc.PlaceOfferImplBase { + static class PlaceOfferService extends PlaceOfferGrpc.PlaceOfferImplBase { private final CoreApi coreApi; - public PlaceOfferImpl(CoreApi coreApi) { + public PlaceOfferService(CoreApi coreApi) { this.coreApi = coreApi; } From 1f8839e3ac3c404aa3337cb7d6fe5df289c31fcd Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 14:10:28 +0200 Subject: [PATCH 06/10] Make gRPC service classes non-static So they can easily access the enclosing BisqGrpcServer#coreApi field. --- .../java/bisq/core/grpc/BisqGrpcServer.java | 64 +++++-------------- 1 file changed, 16 insertions(+), 48 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java index b805519cc84..706d44a5d79 100644 --- a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java @@ -56,18 +56,22 @@ @Slf4j public class BisqGrpcServer { + private final CoreApi coreApi; + public BisqGrpcServer(Config config, CoreApi coreApi) { + this.coreApi = coreApi; + try { // TODO add to options int port = 9998; var server = ServerBuilder.forPort(port) - .addService(new GetVersionService(coreApi)) - .addService(new GetBalanceService(coreApi)) - .addService(new GetTradeStatisticsService(coreApi)) - .addService(new GetOffersService(coreApi)) - .addService(new GetPaymentAccountsService(coreApi)) - .addService(new PlaceOfferService(coreApi)) + .addService(new GetVersionService()) + .addService(new GetBalanceService()) + .addService(new GetTradeStatisticsService()) + .addService(new GetOffersService()) + .addService(new GetPaymentAccountsService()) + .addService(new PlaceOfferService()) .intercept(new PasswordAuthInterceptor(config.apiPassword)) .build() .start(); @@ -84,13 +88,7 @@ public BisqGrpcServer(Config config, CoreApi coreApi) { } } - static class GetVersionService extends GetVersionGrpc.GetVersionImplBase { - private final CoreApi coreApi; - - public GetVersionService(CoreApi coreApi) { - this.coreApi = coreApi; - } - + class GetVersionService extends GetVersionGrpc.GetVersionImplBase { @Override public void getVersion(GetVersionRequest req, StreamObserver responseObserver) { GetVersionReply reply = GetVersionReply.newBuilder().setVersion(coreApi.getVersion()).build(); @@ -99,13 +97,7 @@ public void getVersion(GetVersionRequest req, StreamObserver re } } - static class GetBalanceService extends GetBalanceGrpc.GetBalanceImplBase { - private final CoreApi coreApi; - - public GetBalanceService(CoreApi coreApi) { - this.coreApi = coreApi; - } - + class GetBalanceService extends GetBalanceGrpc.GetBalanceImplBase { @Override public void getBalance(GetBalanceRequest req, StreamObserver responseObserver) { GetBalanceReply reply = GetBalanceReply.newBuilder().setBalance(coreApi.getAvailableBalance()).build(); @@ -114,13 +106,7 @@ public void getBalance(GetBalanceRequest req, StreamObserver re } } - static class GetTradeStatisticsService extends GetTradeStatisticsGrpc.GetTradeStatisticsImplBase { - private final CoreApi coreApi; - - public GetTradeStatisticsService(CoreApi coreApi) { - this.coreApi = coreApi; - } - + class GetTradeStatisticsService extends GetTradeStatisticsGrpc.GetTradeStatisticsImplBase { @Override public void getTradeStatistics(GetTradeStatisticsRequest req, StreamObserver responseObserver) { @@ -133,13 +119,7 @@ public void getTradeStatistics(GetTradeStatisticsRequest req, } } - static class GetOffersService extends GetOffersGrpc.GetOffersImplBase { - private final CoreApi coreApi; - - public GetOffersService(CoreApi coreApi) { - this.coreApi = coreApi; - } - + class GetOffersService extends GetOffersGrpc.GetOffersImplBase { @Override public void getOffers(GetOffersRequest req, StreamObserver responseObserver) { @@ -153,13 +133,7 @@ public void getOffers(GetOffersRequest req, StreamObserver respo } } - static class GetPaymentAccountsService extends GetPaymentAccountsGrpc.GetPaymentAccountsImplBase { - private final CoreApi coreApi; - - public GetPaymentAccountsService(CoreApi coreApi) { - this.coreApi = coreApi; - } - + class GetPaymentAccountsService extends GetPaymentAccountsGrpc.GetPaymentAccountsImplBase { @Override public void getPaymentAccounts(GetPaymentAccountsRequest req, StreamObserver responseObserver) { @@ -174,13 +148,7 @@ public void getPaymentAccounts(GetPaymentAccountsRequest req, } } - static class PlaceOfferService extends PlaceOfferGrpc.PlaceOfferImplBase { - private final CoreApi coreApi; - - public PlaceOfferService(CoreApi coreApi) { - this.coreApi = coreApi; - } - + class PlaceOfferService extends PlaceOfferGrpc.PlaceOfferImplBase { @Override public void placeOffer(PlaceOfferRequest req, StreamObserver responseObserver) { TransactionResultHandler resultHandler = transaction -> { From 2f7d0f6320196c9494ed4acc7fa54fefec5a87a8 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 14:20:02 +0200 Subject: [PATCH 07/10] Use var declarations where appropriate --- .../java/bisq/core/grpc/BisqGrpcServer.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java index 706d44a5d79..12c0d2fd73b 100644 --- a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java @@ -48,7 +48,6 @@ import java.io.IOException; -import java.util.List; import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; @@ -91,7 +90,7 @@ public BisqGrpcServer(Config config, CoreApi coreApi) { class GetVersionService extends GetVersionGrpc.GetVersionImplBase { @Override public void getVersion(GetVersionRequest req, StreamObserver responseObserver) { - GetVersionReply reply = GetVersionReply.newBuilder().setVersion(coreApi.getVersion()).build(); + var reply = GetVersionReply.newBuilder().setVersion(coreApi.getVersion()).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); } @@ -100,7 +99,7 @@ public void getVersion(GetVersionRequest req, StreamObserver re class GetBalanceService extends GetBalanceGrpc.GetBalanceImplBase { @Override public void getBalance(GetBalanceRequest req, StreamObserver responseObserver) { - GetBalanceReply reply = GetBalanceReply.newBuilder().setBalance(coreApi.getAvailableBalance()).build(); + var reply = GetBalanceReply.newBuilder().setBalance(coreApi.getAvailableBalance()).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); } @@ -110,10 +109,12 @@ class GetTradeStatisticsService extends GetTradeStatisticsGrpc.GetTradeStatistic @Override public void getTradeStatistics(GetTradeStatisticsRequest req, StreamObserver responseObserver) { - List tradeStatistics = coreApi.getTradeStatistics().stream() + + var tradeStatistics = coreApi.getTradeStatistics().stream() .map(TradeStatistics2::toProtoTradeStatistics2) .collect(Collectors.toList()); - GetTradeStatisticsReply reply = GetTradeStatisticsReply.newBuilder().addAllTradeStatistics(tradeStatistics).build(); + + var reply = GetTradeStatisticsReply.newBuilder().addAllTradeStatistics(tradeStatistics).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); } @@ -123,11 +124,11 @@ class GetOffersService extends GetOffersGrpc.GetOffersImplBase { @Override public void getOffers(GetOffersRequest req, StreamObserver responseObserver) { - List tradeStatistics = coreApi.getOffers().stream() + var tradeStatistics = coreApi.getOffers().stream() .map(Offer::toProtoMessage) .collect(Collectors.toList()); - GetOffersReply reply = GetOffersReply.newBuilder().addAllOffers(tradeStatistics).build(); + var reply = GetOffersReply.newBuilder().addAllOffers(tradeStatistics).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); } @@ -138,11 +139,11 @@ class GetPaymentAccountsService extends GetPaymentAccountsGrpc.GetPaymentAccount public void getPaymentAccounts(GetPaymentAccountsRequest req, StreamObserver responseObserver) { - List tradeStatistics = coreApi.getPaymentAccounts().stream() + var tradeStatistics = coreApi.getPaymentAccounts().stream() .map(PaymentAccount::toProtoMessage) .collect(Collectors.toList()); - GetPaymentAccountsReply reply = GetPaymentAccountsReply.newBuilder().addAllPaymentAccounts(tradeStatistics).build(); + var reply = GetPaymentAccountsReply.newBuilder().addAllPaymentAccounts(tradeStatistics).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); } From a33e030e0402b6dff51ec4cb7b3c02974bdfe8d6 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 14:21:02 +0200 Subject: [PATCH 08/10] Rename BisqGrpcServer => GrpcServer --- .../bisq/core/grpc/{BisqGrpcServer.java => GrpcServer.java} | 4 ++-- daemon/src/main/java/bisq/daemon/app/BisqDaemonMain.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) rename core/src/main/java/bisq/core/grpc/{BisqGrpcServer.java => GrpcServer.java} (98%) diff --git a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java b/core/src/main/java/bisq/core/grpc/GrpcServer.java similarity index 98% rename from core/src/main/java/bisq/core/grpc/BisqGrpcServer.java rename to core/src/main/java/bisq/core/grpc/GrpcServer.java index 12c0d2fd73b..e684f53732e 100644 --- a/core/src/main/java/bisq/core/grpc/BisqGrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/GrpcServer.java @@ -53,11 +53,11 @@ import lombok.extern.slf4j.Slf4j; @Slf4j -public class BisqGrpcServer { +public class GrpcServer { private final CoreApi coreApi; - public BisqGrpcServer(Config config, CoreApi coreApi) { + public GrpcServer(Config config, CoreApi coreApi) { this.coreApi = coreApi; try { diff --git a/daemon/src/main/java/bisq/daemon/app/BisqDaemonMain.java b/daemon/src/main/java/bisq/daemon/app/BisqDaemonMain.java index 1e19ed23c5e..698f8ebe92d 100644 --- a/daemon/src/main/java/bisq/daemon/app/BisqDaemonMain.java +++ b/daemon/src/main/java/bisq/daemon/app/BisqDaemonMain.java @@ -20,7 +20,7 @@ import bisq.core.app.BisqHeadlessAppMain; import bisq.core.app.BisqSetup; import bisq.core.app.CoreModule; -import bisq.core.grpc.BisqGrpcServer; +import bisq.core.grpc.GrpcServer; import bisq.core.grpc.CoreApi; import bisq.common.UserThread; @@ -99,6 +99,6 @@ protected void onApplicationStarted() { super.onApplicationStarted(); CoreApi coreApi = injector.getInstance(CoreApi.class); - new BisqGrpcServer(config, coreApi); + new GrpcServer(config, coreApi); } } From 82f1b668180d97edd44b01b430f6b4d2e282419f Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 14:26:50 +0200 Subject: [PATCH 09/10] Refine GrpcServer log output --- core/src/main/java/bisq/core/grpc/GrpcServer.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/bisq/core/grpc/GrpcServer.java b/core/src/main/java/bisq/core/grpc/GrpcServer.java index e684f53732e..b9bf162c522 100644 --- a/core/src/main/java/bisq/core/grpc/GrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/GrpcServer.java @@ -75,11 +75,10 @@ public GrpcServer(Config config, CoreApi coreApi) { .build() .start(); - log.info("Server started, listening on " + port); + log.info("listening on port {}", port); Runtime.getRuntime().addShutdownHook(new Thread(() -> { - log.error("Shutting down gRPC server"); server.shutdown(); - log.error("Server shut down"); + log.info("shutdown complete"); })); } catch (IOException e) { From 3badf299e34cea25d7d5e87f74a7caecbeb44a54 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Sat, 25 Apr 2020 14:33:44 +0200 Subject: [PATCH 10/10] Add 'apiPort' configuration option --- common/src/main/java/bisq/common/config/Config.java | 9 +++++++++ core/src/main/java/bisq/core/grpc/GrpcServer.java | 5 ++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/bisq/common/config/Config.java b/common/src/main/java/bisq/common/config/Config.java index b04c58e73ef..fb5f16659ec 100644 --- a/common/src/main/java/bisq/common/config/Config.java +++ b/common/src/main/java/bisq/common/config/Config.java @@ -117,6 +117,7 @@ public class Config { public static final String DUMP_DELAYED_PAYOUT_TXS = "dumpDelayedPayoutTxs"; public static final String ALLOW_FAULTY_DELAYED_TXS = "allowFaultyDelayedTxs"; public static final String API_PASSWORD = "apiPassword"; + public static final String API_PORT = "apiPort"; // Default values for certain options public static final int UNSPECIFIED_PORT = -1; @@ -201,6 +202,7 @@ public class Config { public final boolean dumpDelayedPayoutTxs; public final boolean allowFaultyDelayedTxs; public final String apiPassword; + public final int apiPort; // Properties derived from options but not exposed as options themselves public final File torDir; @@ -622,6 +624,12 @@ public Config(String defaultAppName, File defaultUserDataDir, String... args) { .withRequiredArg() .defaultsTo(""); + ArgumentAcceptingOptionSpec apiPortOpt = + parser.accepts(API_PORT, "gRPC API port") + .withRequiredArg() + .ofType(Integer.class) + .defaultsTo(9998); + try { CompositeOptionSet options = new CompositeOptionSet(); @@ -735,6 +743,7 @@ public Config(String defaultAppName, File defaultUserDataDir, String... args) { this.dumpDelayedPayoutTxs = options.valueOf(dumpDelayedPayoutTxsOpt); this.allowFaultyDelayedTxs = options.valueOf(allowFaultyDelayedTxsOpt); this.apiPassword = options.valueOf(apiPasswordOpt); + this.apiPort = options.valueOf(apiPortOpt); } catch (OptionException ex) { throw new ConfigException("problem parsing option '%s': %s", ex.options().get(0), diff --git a/core/src/main/java/bisq/core/grpc/GrpcServer.java b/core/src/main/java/bisq/core/grpc/GrpcServer.java index b9bf162c522..0ae8ae9ac85 100644 --- a/core/src/main/java/bisq/core/grpc/GrpcServer.java +++ b/core/src/main/java/bisq/core/grpc/GrpcServer.java @@ -56,14 +56,13 @@ public class GrpcServer { private final CoreApi coreApi; + private final int port; public GrpcServer(Config config, CoreApi coreApi) { this.coreApi = coreApi; + this.port = config.apiPort; try { - // TODO add to options - int port = 9998; - var server = ServerBuilder.forPort(port) .addService(new GetVersionService()) .addService(new GetBalanceService())