mirror of
https://github.com/bisq-network/bisq.git
synced 2025-02-24 07:07:43 +01:00
Eliminate static fields in BisqGrpcServer
This commit is contained in:
parent
f2028f2cef
commit
a52be594cb
1 changed files with 56 additions and 40 deletions
|
@ -43,7 +43,6 @@ import bisq.proto.grpc.PlaceOfferGrpc;
|
||||||
import bisq.proto.grpc.PlaceOfferReply;
|
import bisq.proto.grpc.PlaceOfferReply;
|
||||||
import bisq.proto.grpc.PlaceOfferRequest;
|
import bisq.proto.grpc.PlaceOfferRequest;
|
||||||
|
|
||||||
import io.grpc.Server;
|
|
||||||
import io.grpc.ServerBuilder;
|
import io.grpc.ServerBuilder;
|
||||||
import io.grpc.stub.StreamObserver;
|
import io.grpc.stub.StreamObserver;
|
||||||
|
|
||||||
|
@ -57,12 +56,13 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class BisqGrpcServer {
|
public class BisqGrpcServer {
|
||||||
|
|
||||||
private Server server;
|
|
||||||
|
|
||||||
private static Config config;
|
|
||||||
private static CoreApi coreApi;
|
|
||||||
|
|
||||||
static class GetVersionImpl extends GetVersionGrpc.GetVersionImplBase {
|
static class GetVersionImpl extends GetVersionGrpc.GetVersionImplBase {
|
||||||
|
private final CoreApi coreApi;
|
||||||
|
|
||||||
|
public GetVersionImpl(CoreApi coreApi) {
|
||||||
|
this.coreApi = coreApi;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getVersion(GetVersionRequest req, StreamObserver<GetVersionReply> responseObserver) {
|
public void getVersion(GetVersionRequest req, StreamObserver<GetVersionReply> responseObserver) {
|
||||||
GetVersionReply reply = GetVersionReply.newBuilder().setVersion(coreApi.getVersion()).build();
|
GetVersionReply reply = GetVersionReply.newBuilder().setVersion(coreApi.getVersion()).build();
|
||||||
|
@ -72,6 +72,12 @@ public class BisqGrpcServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
static class GetBalanceImpl extends GetBalanceGrpc.GetBalanceImplBase {
|
static class GetBalanceImpl extends GetBalanceGrpc.GetBalanceImplBase {
|
||||||
|
private final CoreApi coreApi;
|
||||||
|
|
||||||
|
public GetBalanceImpl(CoreApi coreApi) {
|
||||||
|
this.coreApi = coreApi;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getBalance(GetBalanceRequest req, StreamObserver<GetBalanceReply> responseObserver) {
|
public void getBalance(GetBalanceRequest req, StreamObserver<GetBalanceReply> responseObserver) {
|
||||||
GetBalanceReply reply = GetBalanceReply.newBuilder().setBalance(coreApi.getAvailableBalance()).build();
|
GetBalanceReply reply = GetBalanceReply.newBuilder().setBalance(coreApi.getAvailableBalance()).build();
|
||||||
|
@ -81,6 +87,12 @@ public class BisqGrpcServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
static class GetTradeStatisticsImpl extends GetTradeStatisticsGrpc.GetTradeStatisticsImplBase {
|
static class GetTradeStatisticsImpl extends GetTradeStatisticsGrpc.GetTradeStatisticsImplBase {
|
||||||
|
private final CoreApi coreApi;
|
||||||
|
|
||||||
|
public GetTradeStatisticsImpl(CoreApi coreApi) {
|
||||||
|
this.coreApi = coreApi;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getTradeStatistics(GetTradeStatisticsRequest req,
|
public void getTradeStatistics(GetTradeStatisticsRequest req,
|
||||||
StreamObserver<GetTradeStatisticsReply> responseObserver) {
|
StreamObserver<GetTradeStatisticsReply> responseObserver) {
|
||||||
|
@ -94,6 +106,12 @@ public class BisqGrpcServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
static class GetOffersImpl extends GetOffersGrpc.GetOffersImplBase {
|
static class GetOffersImpl extends GetOffersGrpc.GetOffersImplBase {
|
||||||
|
private final CoreApi coreApi;
|
||||||
|
|
||||||
|
public GetOffersImpl(CoreApi coreApi) {
|
||||||
|
this.coreApi = coreApi;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getOffers(GetOffersRequest req, StreamObserver<GetOffersReply> responseObserver) {
|
public void getOffers(GetOffersRequest req, StreamObserver<GetOffersReply> responseObserver) {
|
||||||
|
|
||||||
|
@ -108,6 +126,12 @@ public class BisqGrpcServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
static class GetPaymentAccountsImpl extends GetPaymentAccountsGrpc.GetPaymentAccountsImplBase {
|
static class GetPaymentAccountsImpl extends GetPaymentAccountsGrpc.GetPaymentAccountsImplBase {
|
||||||
|
private final CoreApi coreApi;
|
||||||
|
|
||||||
|
public GetPaymentAccountsImpl(CoreApi coreApi) {
|
||||||
|
this.coreApi = coreApi;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void getPaymentAccounts(GetPaymentAccountsRequest req,
|
public void getPaymentAccounts(GetPaymentAccountsRequest req,
|
||||||
StreamObserver<GetPaymentAccountsReply> responseObserver) {
|
StreamObserver<GetPaymentAccountsReply> responseObserver) {
|
||||||
|
@ -123,6 +147,12 @@ public class BisqGrpcServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
static class PlaceOfferImpl extends PlaceOfferGrpc.PlaceOfferImplBase {
|
static class PlaceOfferImpl extends PlaceOfferGrpc.PlaceOfferImplBase {
|
||||||
|
private final CoreApi coreApi;
|
||||||
|
|
||||||
|
public PlaceOfferImpl(CoreApi coreApi) {
|
||||||
|
this.coreApi = coreApi;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void placeOffer(PlaceOfferRequest req, StreamObserver<PlaceOfferReply> responseObserver) {
|
public void placeOffer(PlaceOfferRequest req, StreamObserver<PlaceOfferReply> responseObserver) {
|
||||||
TransactionResultHandler resultHandler = transaction -> {
|
TransactionResultHandler resultHandler = transaction -> {
|
||||||
|
@ -145,45 +175,31 @@ public class BisqGrpcServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
public BisqGrpcServer(Config config, CoreApi coreApi) {
|
public BisqGrpcServer(Config config, CoreApi coreApi) {
|
||||||
BisqGrpcServer.config = config;
|
|
||||||
BisqGrpcServer.coreApi = coreApi;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
start();
|
|
||||||
|
|
||||||
} 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
|
// TODO add to options
|
||||||
int port = 9998;
|
int port = 9998;
|
||||||
|
|
||||||
// Config services
|
var server = ServerBuilder.forPort(port)
|
||||||
server = ServerBuilder.forPort(port)
|
.addService(new GetVersionImpl(coreApi))
|
||||||
.addService(new GetVersionImpl())
|
.addService(new GetBalanceImpl(coreApi))
|
||||||
.addService(new GetBalanceImpl())
|
.addService(new GetTradeStatisticsImpl(coreApi))
|
||||||
.addService(new GetTradeStatisticsImpl())
|
.addService(new GetOffersImpl(coreApi))
|
||||||
.addService(new GetOffersImpl())
|
.addService(new GetPaymentAccountsImpl(coreApi))
|
||||||
.addService(new GetPaymentAccountsImpl())
|
.addService(new PlaceOfferImpl(coreApi))
|
||||||
.addService(new PlaceOfferImpl())
|
|
||||||
.intercept(new PasswordAuthInterceptor(config.apiPassword))
|
.intercept(new PasswordAuthInterceptor(config.apiPassword))
|
||||||
.build()
|
.build()
|
||||||
.start();
|
.start();
|
||||||
|
|
||||||
log.info("Server started, listening on " + port);
|
log.info("Server started, listening on " + port);
|
||||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
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");
|
log.error("Shutting down gRPC server");
|
||||||
BisqGrpcServer.this.stop();
|
server.shutdown();
|
||||||
log.error("Server shut down");
|
log.error("Server shut down");
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(e.toString(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue