101 lines
3.2 KiB
Java
101 lines
3.2 KiB
Java
package bdchain.api;
|
|
|
|
import bdchain.api.grpc.*;
|
|
import bdchain.api.grpc.TransactionLedgerGrpc.TransactionLedgerBlockingStub;
|
|
import com.google.protobuf.ByteString;
|
|
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;
|
|
|
|
public class TransactionLedgerClient {
|
|
|
|
private static final Logger logger = Logger.getLogger(TransactionLedgerClient.class.getName());
|
|
|
|
private final ManagedChannel channel;
|
|
private final TransactionLedgerBlockingStub blockingStub;
|
|
// private final TransactionLedgerStub asyncStub;
|
|
|
|
/** Construct client for accessing TransactionLedger server at {@code host:port}. */
|
|
public TransactionLedgerClient(String host, int port) {
|
|
this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
|
|
}
|
|
|
|
/** Construct client for accessing TransactionLedger server using the existing channel. */
|
|
public TransactionLedgerClient(ManagedChannelBuilder<?> channelBuilder) {
|
|
channel = channelBuilder.build();
|
|
blockingStub = TransactionLedgerGrpc.newBlockingStub(channel);
|
|
// asyncStub = TransactionLedgerGrpc.newStub(channel);
|
|
}
|
|
|
|
public void shutdown() throws InterruptedException {
|
|
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
|
|
}
|
|
|
|
public CreateLedgerResponse createLedger(String name) {
|
|
|
|
info("*** createLedger: name={0}", name);
|
|
|
|
CreateLedgerRequest request = CreateLedgerRequest.newBuilder().setName(name).build();
|
|
|
|
try {
|
|
return blockingStub.createLedger(request);
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public GetLedgersResponse getLedgers() {
|
|
|
|
info("*** getLedgers");
|
|
|
|
try {
|
|
return blockingStub.getLedgers(Empty.getDefaultInstance());
|
|
} catch (StatusRuntimeException e) {
|
|
warning("RPC failed: {0}", e.getStatus());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public SendTransactionResponse sendTransaction(
|
|
String ledger, TransactionType type, String from, String to, byte[] data) {
|
|
|
|
info(
|
|
"*** sendTransaction: 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) {
|
|
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);
|
|
}
|
|
}
|