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) {

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) {

View File

@ -12,28 +12,28 @@ public class Test {
final int index = 0;
TransactionLedgerClient tlClient = new TransactionLedgerClient("localhost", 8004);
System.out.println(tlClient.createLedger(ledger).getOk());
GetLedgersResponse r = tlClient.getLedgers();
System.out.println(tlClient.createLedgerSync(ledger).getOk());
GetLedgersResponse r = tlClient.getLedgersSync();
for (int i = 0; i < r.getLedgersCount(); i++) {
System.out.println(r.getLedgers(i));
}
System.out.println(
tlClient
.sendTransaction(ledger, TransactionType.RECORD, "0x0", "0x1", "data".getBytes())
.sendTransactionSync(ledger, TransactionType.RECORD, "0x0", "0x1", "data".getBytes())
.getHash());
tlClient.shutdown();
AccountingChainClient acClient = new AccountingChainClient("localhost", 8013);
System.out.println(acClient.blockNumber(ledger).getBlockNumber());
System.out.println(acClient.getBlockByNumber(ledger, block_number, false).toString());
System.out.println(acClient.getBlockByNumber(ledger, block_number, true).toString());
System.out.println(acClient.getBlockByHash(ledger, block_hash, false).toString());
System.out.println(acClient.getBlockByHash(ledger, block_hash, true).toString());
System.out.println(acClient.getTransactionByHash(ledger, "0x0").toString());
System.out.println(acClient.blockNumberSync(ledger).getBlockNumber());
System.out.println(acClient.getBlockByNumberSync(ledger, block_number, false).toString());
System.out.println(acClient.getBlockByNumberSync(ledger, block_number, true).toString());
System.out.println(acClient.getBlockByHashSync(ledger, block_hash, false).toString());
System.out.println(acClient.getBlockByHashSync(ledger, block_hash, true).toString());
System.out.println(acClient.getTransactionByHashSync(ledger, "0x0").toString());
System.out.println(
acClient.getTransactionByBlockNumberAndIndex(ledger, block_number, index).toString());
acClient.getTransactionByBlockNumberAndIndexSync(ledger, block_number, index).toString());
System.out.println(
acClient.getTransactionByBlockHashAndIndex(ledger, block_hash, index).toString());
acClient.getTransactionByBlockHashAndIndexSync(ledger, block_hash, index).toString());
acClient.shutdown();
}
}