Files
bdledger-java-sdk/src/main/java/bdchain/api/TransactionLedgerClient.java

192 lines
6.1 KiB
Java
Raw Normal View History

2018-09-12 00:10:49 +08:00
package bdchain.api;
import bdchain.api.grpc.*;
import bdchain.api.grpc.TransactionLedgerGrpc.TransactionLedgerBlockingStub;
2018-09-12 10:00:00 +08:00
import bdchain.api.grpc.TransactionLedgerGrpc.TransactionLedgerFutureStub;
import com.google.common.util.concurrent.ListenableFuture;
2018-09-12 00:51:33 +08:00
import com.google.protobuf.ByteString;
2018-09-12 00:10:49 +08:00
import com.google.protobuf.Empty;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.StatusRuntimeException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
// import bdchain.api.grpc.TransactionLedgerGrpc.TransactionLedgerStub;
2018-09-14 10:15:36 +08:00
/**
* 事务账本客户端
*
* <p>如有更灵活的需求可直接使用{@link bdchain.api.grpc.TransactionLedgerGrpc}
*
* @see <a href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#api">事务账本API</a>
* @author nex
*/
2018-09-12 00:10:49 +08:00
public class TransactionLedgerClient {
private static final Logger logger = Logger.getLogger(TransactionLedgerClient.class.getName());
private final ManagedChannel channel;
2018-09-12 10:00:00 +08:00
private final TransactionLedgerFutureStub futureStub;
2018-09-12 00:10:49 +08:00
private final TransactionLedgerBlockingStub blockingStub;
// private final TransactionLedgerStub asyncStub;
2018-09-14 10:15:36 +08:00
/** 构造客户端来访问{@code host:port}的事务账本服务。 */
2018-09-12 00:10:49 +08:00
public TransactionLedgerClient(String host, int port) {
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
}
2018-09-14 10:15:36 +08:00
/** 用已有的{@link io.grpc.Channel}对象构造客户端来访问事务账本服务。 */
2018-09-12 00:10:49 +08:00
public TransactionLedgerClient(ManagedChannelBuilder<?> channelBuilder) {
channel = channelBuilder.build();
2018-09-12 10:00:00 +08:00
futureStub = TransactionLedgerGrpc.newFutureStub(channel);
2018-09-12 00:10:49 +08:00
blockingStub = TransactionLedgerGrpc.newBlockingStub(channel);
// asyncStub = TransactionLedgerGrpc.newStub(channel);
}
2018-09-14 10:15:36 +08:00
/** 关闭客户端的网络连接。 */
2018-09-12 00:10:49 +08:00
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
2018-09-14 10:15:36 +08:00
/**
* <a href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#createledger">创建账本</a>
* 非阻塞
*/
2018-09-12 10:00:00 +08:00
public ListenableFuture<CreateLedgerResponse> createLedger(String name) {
2018-09-12 00:10:49 +08:00
info("*** createLedger: name={0}", name);
CreateLedgerRequest request = CreateLedgerRequest.newBuilder().setName(name).build();
2018-09-12 10:00:00 +08:00
try {
return futureStub.createLedger(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
2018-09-14 10:15:36 +08:00
/**
* <a href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#createledger">创建账本</a>
* 阻塞
*/
2018-09-12 10:00:00 +08:00
public CreateLedgerResponse createLedgerSync(String name) {
info("*** createLedgerSync: name={0}", name);
CreateLedgerRequest request = CreateLedgerRequest.newBuilder().setName(name).build();
2018-09-12 00:10:49 +08:00
try {
return blockingStub.createLedger(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
2018-09-14 10:15:36 +08:00
/**
* <a href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getledgers">返回账本列表</a>
* 非阻塞
*/
2018-09-12 10:00:00 +08:00
public ListenableFuture<GetLedgersResponse> getLedgers() {
2018-09-12 00:10:49 +08:00
info("*** getLedgers");
2018-09-12 10:00:00 +08:00
try {
return futureStub.getLedgers(Empty.getDefaultInstance());
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
2018-09-14 10:15:36 +08:00
/**
* <a href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#getledgers">返回账本列表</a>
* 阻塞
*/
2018-09-12 10:00:00 +08:00
public GetLedgersResponse getLedgersSync() {
info("*** getLedgersSync");
2018-09-12 00:10:49 +08:00
try {
return blockingStub.getLedgers(Empty.getDefaultInstance());
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
2018-09-14 10:15:36 +08:00
/**
* <a
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#sendtransaction">发送新事务</a>
* 非阻塞
*/
2018-09-12 10:00:00 +08:00
public ListenableFuture<SendTransactionResponse> sendTransaction(
2018-09-12 00:51:33 +08:00
String ledger, TransactionType type, String from, String to, byte[] data) {
2018-09-12 00:10:49 +08:00
2018-09-12 00:51:33 +08:00
info(
"*** sendTransaction: ledger={0} type={1} from={2} to={3} data={4}",
ledger, type, from, to, data);
2018-09-12 00:10:49 +08:00
SendTransactionRequest request =
2018-09-12 00:51:33 +08:00
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();
2018-09-12 00:10:49 +08:00
2018-09-12 10:00:00 +08:00
try {
return futureStub.sendTransaction(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
2018-09-14 10:15:36 +08:00
/**
* <a
* href="https://phabricator.internetapi.cn/w/public/bdchain/grpc-api/#sendtransaction">发送新事务</a>
* 阻塞
*/
2018-09-12 10:00:00 +08:00
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();
2018-09-12 00:10:49 +08:00
try {
return blockingStub.sendTransaction(request);
} catch (StatusRuntimeException e) {
warning("RPC failed: {0}", e.getStatus());
return null;
}
}
private void info(String msg, Object... params) {
logger.log(Level.INFO, msg, params);
}
private void warning(String msg, Object... params) {
logger.log(Level.WARNING, msg, params);
}
}