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.AccountingChainGrpc.AccountingChainBlockingStub;
import bdchain.api.grpc.AccountingChainGrpc.AccountingChainFutureStub;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.protobuf.ByteString;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
@@ -18,6 +20,7 @@ public class AccountingChainClient {
private static final Logger logger = Logger.getLogger(AccountingChainClient.class.getName());
private final ManagedChannel channel;
private final AccountingChainFutureStub futureStub;
private final AccountingChainBlockingStub blockingStub;
// private final AccountingChainStub asyncStub;
@@ -29,6 +32,8 @@ public class AccountingChainClient {
/** Construct client for accessing AccountingChain server using the existing channel. */
public AccountingChainClient(ManagedChannelBuilder<?> channelBuilder) {
channel = channelBuilder.build();
AccountingChainGrpc.newFutureStub(channel);
futureStub = AccountingChainGrpc.newFutureStub(channel);
blockingStub = AccountingChainGrpc.newBlockingStub(channel);
// asyncStub = AccountingChainGrpc.newStub(channel);
}
@@ -37,12 +42,26 @@ public class AccountingChainClient {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
public BlockNumberResponse blockNumber(String ledger) {
public ListenableFuture<BlockNumberResponse> blockNumber(String ledger) {
info("*** blockNumber: ledger={0}", ledger);
BlockNumberRequest request = BlockNumberRequest.newBuilder().setLedger(ledger).build();
try {
return futureStub.blockNumber(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
public BlockNumberResponse blockNumberSync(String ledger) {
info("*** blockNumberSync: ledger={0}", ledger);
BlockNumberRequest request = BlockNumberRequest.newBuilder().setLedger(ledger).build();
try {
return blockingStub.blockNumber(request);
} catch (StatusRuntimeException e) {
@@ -51,7 +70,8 @@ public class AccountingChainClient {
}
}
public Block getBlockByNumber(String ledger, long number, boolean fullTransaction) {
public ListenableFuture<Block> getBlockByNumber(
String ledger, long number, boolean fullTransaction) {
info(
"*** getBlockByNumber: ledger={0} number={1} fullTransaction={2}",
@@ -64,6 +84,27 @@ public class AccountingChainClient {
.setFullTransaction(fullTransaction)
.build();
try {
return futureStub.getBlockByNumber(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
public Block getBlockByNumberSync(String ledger, long number, boolean fullTransaction) {
info(
"*** getBlockByNumberSync: ledger={0} number={1} fullTransaction={2}",
ledger, number, fullTransaction);
GetBlockByNumberRequest request =
GetBlockByNumberRequest.newBuilder()
.setLedger(ledger)
.setNumber(number)
.setFullTransaction(fullTransaction)
.build();
try {
return blockingStub.getBlockByNumber(request);
} catch (StatusRuntimeException e) {
@@ -72,7 +113,8 @@ public class AccountingChainClient {
}
}
public Block getBlockByHash(String ledger, String hash, boolean fullTransaction) {
public ListenableFuture<Block> getBlockByHash(
String ledger, String hash, boolean fullTransaction) {
info(
"*** getBlockByHash: ledger={0} hash={1} fullTransaction={2}",
@@ -85,6 +127,27 @@ public class AccountingChainClient {
.setFullTransaction(fullTransaction)
.build();
try {
return futureStub.getBlockByHash(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
public Block getBlockByHashSync(String ledger, String hash, boolean fullTransaction) {
info(
"*** getBlockByHashSync: ledger={0} hash={1} fullTransaction={2}",
ledger, hash, fullTransaction);
GetBlockByHashRequest request =
GetBlockByHashRequest.newBuilder()
.setLedger(ledger)
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
.setFullTransaction(fullTransaction)
.build();
try {
return blockingStub.getBlockByHash(request);
} catch (StatusRuntimeException e) {
@@ -93,10 +156,28 @@ public class AccountingChainClient {
}
}
public Block getTransactionByHash(String ledger, String hash) {
public ListenableFuture<Block> getTransactionByHash(String ledger, String hash) {
info("*** getTransactionByHash: ledger={0} hash={1}", ledger, hash);
GetTransactionByHashRequest request =
GetTransactionByHashRequest.newBuilder()
.setLedger(ledger)
.setHash(ByteString.copyFrom(Utils.hexStringToByteArray(hash)))
.build();
try {
return futureStub.getTransactionByHash(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
public Block getTransactionByHashSync(String ledger, String hash) {
info("*** getTransactionByHashSync: ledger={0} hash={1}", ledger, hash);
GetTransactionByHashRequest request =
GetTransactionByHashRequest.newBuilder()
.setLedger(ledger)
@@ -111,7 +192,7 @@ public class AccountingChainClient {
}
}
public Transaction getTransactionByBlockNumberAndIndex(
public ListenableFuture<Transaction> getTransactionByBlockNumberAndIndex(
String ledger, long block_number, int index) {
info(
@@ -125,6 +206,28 @@ public class AccountingChainClient {
.setIndex(index)
.build();
try {
return futureStub.getTransactionByBlockNumberAndIndex(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
public Transaction getTransactionByBlockNumberAndIndexSync(
String ledger, long block_number, int index) {
info(
"*** getTransactionByBlockNumberAndIndexSync: ledger={0} block_number={1} index={2}",
ledger, block_number, index);
GetTransactionByBlockNumberAndIndexRequest request =
GetTransactionByBlockNumberAndIndexRequest.newBuilder()
.setLedger(ledger)
.setBlockNumber(block_number)
.setIndex(index)
.build();
try {
return blockingStub.getTransactionByBlockNumberAndIndex(request);
} catch (StatusRuntimeException e) {
@@ -133,7 +236,7 @@ public class AccountingChainClient {
}
}
public Transaction getTransactionByBlockHashAndIndex(
public ListenableFuture<Transaction> getTransactionByBlockHashAndIndex(
String ledger, String block_hash, int index) {
info(
@@ -147,6 +250,28 @@ public class AccountingChainClient {
.setIndex(index)
.build();
try {
return futureStub.getTransactionByBlockHashAndIndex(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
public Transaction getTransactionByBlockHashAndIndexSync(
String ledger, String block_hash, int index) {
info(
"*** getTransactionByBlockHashAndIndexSync: ledger={0} block_hash={1} index={2}",
ledger, block_hash, index);
GetTransactionByBlockHashAndIndexRequest request =
GetTransactionByBlockHashAndIndexRequest.newBuilder()
.setLedger(ledger)
.setBlockHash(ByteString.copyFrom(Utils.hexStringToByteArray(block_hash)))
.setIndex(index)
.build();
try {
return blockingStub.getTransactionByBlockHashAndIndex(request);
} catch (StatusRuntimeException e) {