Update APIs returning futures

This commit is contained in:
Nex
2018-09-12 10:00:00 +08:00
parent 1eac2150b1
commit 5a76d9114b
3 changed files with 201 additions and 20 deletions

View File

@@ -2,6 +2,8 @@ package bdchain.api;
import bdchain.api.grpc.*;
import bdchain.api.grpc.TransactionLedgerGrpc.TransactionLedgerBlockingStub;
import bdchain.api.grpc.TransactionLedgerGrpc.TransactionLedgerFutureStub;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.protobuf.ByteString;
import com.google.protobuf.Empty;
import io.grpc.ManagedChannel;
@@ -19,6 +21,7 @@ public class TransactionLedgerClient {
private static final Logger logger = Logger.getLogger(TransactionLedgerClient.class.getName());
private final ManagedChannel channel;
private final TransactionLedgerFutureStub futureStub;
private final TransactionLedgerBlockingStub blockingStub;
// private final TransactionLedgerStub asyncStub;
@@ -30,6 +33,7 @@ public class TransactionLedgerClient {
/** Construct client for accessing TransactionLedger server using the existing channel. */
public TransactionLedgerClient(ManagedChannelBuilder<?> channelBuilder) {
channel = channelBuilder.build();
futureStub = TransactionLedgerGrpc.newFutureStub(channel);
blockingStub = TransactionLedgerGrpc.newBlockingStub(channel);
// asyncStub = TransactionLedgerGrpc.newStub(channel);
}
@@ -38,12 +42,26 @@ public class TransactionLedgerClient {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
public CreateLedgerResponse createLedger(String name) {
public ListenableFuture<CreateLedgerResponse> createLedger(String name) {
info("*** createLedger: name={0}", name);
CreateLedgerRequest request = CreateLedgerRequest.newBuilder().setName(name).build();
try {
return futureStub.createLedger(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
public CreateLedgerResponse createLedgerSync(String name) {
info("*** createLedgerSync: name={0}", name);
CreateLedgerRequest request = CreateLedgerRequest.newBuilder().setName(name).build();
try {
return blockingStub.createLedger(request);
} catch (StatusRuntimeException e) {
@@ -52,10 +70,22 @@ public class TransactionLedgerClient {
}
}
public GetLedgersResponse getLedgers() {
public ListenableFuture<GetLedgersResponse> getLedgers() {
info("*** getLedgers");
try {
return futureStub.getLedgers(Empty.getDefaultInstance());
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
public GetLedgersResponse getLedgersSync() {
info("*** getLedgersSync");
try {
return blockingStub.getLedgers(Empty.getDefaultInstance());
} catch (StatusRuntimeException e) {
@@ -64,7 +94,7 @@ public class TransactionLedgerClient {
}
}
public SendTransactionResponse sendTransaction(
public ListenableFuture<SendTransactionResponse> sendTransaction(
String ledger, TransactionType type, String from, String to, byte[] data) {
info(
@@ -82,6 +112,32 @@ public class TransactionLedgerClient {
.setData(ByteString.copyFrom(data)))
.build();
try {
return futureStub.sendTransaction(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
public SendTransactionResponse sendTransactionSync(
String ledger, TransactionType type, String from, String to, byte[] data) {
info(
"*** sendTransactionSync: ledger={0} type={1} from={2} to={3} data={4}",
ledger, type, from, to, data);
SendTransactionRequest request =
SendTransactionRequest.newBuilder()
.setLedger(ledger)
.setTransaction(
SendTransactionRequest.Transaction.newBuilder()
.setType(type)
.setFrom(ByteString.copyFrom(Utils.hexStringToByteArray(from)))
.setTo(ByteString.copyFrom(Utils.hexStringToByteArray(from)))
.setData(ByteString.copyFrom(data)))
.build();
try {
return blockingStub.sendTransaction(request);
} catch (StatusRuntimeException e) {