Skip to content

Commit

Permalink
Improve c++ example client and clang-format with sane options.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrick committed Jan 26, 2016
1 parent 2e04ab8 commit 0ace7ec
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions rpc/documentation/clientusage.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func main() {
<a name="cpp"/>
## C++

**Note:** Protocol Buffers and gRPC require at least C++11. The example client
is written using C++14.
**Note:** Protocol Buffers and gRPC require at least C++11. The
example client is written using modern C++ as well.

**Note:** The following instructions assume the client is being written on a
Unix-like platform (with instructions using the `sh` shell and Unix-isms in the
Expand Down Expand Up @@ -127,55 +127,55 @@ thread on all gRPC IO.

#include "api.grpc.pb.h"

using namespace std::string_literals;

struct NoHomeDirectoryException : std::exception {
char const* what() const noexcept override {
return "Failed to lookup home directory";
}
char const *what() const noexcept override { return "Failed to lookup home directory"; }
};

auto read_file(std::string const& file_path) -> std::string {
auto unix_home_directory() -> std::string {
auto const pw = getpwuid(getuid());
if (pw == nullptr || pw->pw_dir == nullptr) {
throw NoHomeDirectoryException{};
}
return std::string{pw->pw_dir};
}

auto read_file(std::string const &file_path) -> std::string {
std::ifstream in{file_path};
std::stringstream ss{};
ss << in.rdbuf();
return ss.str();
}

auto main() -> int {
// Before the gRPC native library (gRPC Core) is lazily loaded and
// initialized, an environment variable must be set so OpenSSL is
// configured to use ECDSA TLS certificates (required by btcwallet).
// Before the gRPC native library is lazily loaded and initialized, an environment variable must
// be set so OpenSSL is configured to use ECDSA TLS certificates (required by btcwallet).
setenv("GRPC_SSL_CIPHER_SUITES", "HIGH+ECDSA", 1);

// Note: This path is operating system-dependent. This can be created
// portably using boost::filesystem or the experimental filesystem class
// expected to ship in C++17.
auto wallet_tls_cert_file = []{
auto pw = getpwuid(getuid());
if (pw == nullptr || pw->pw_dir == nullptr) {
throw NoHomeDirectoryException{};
}
return pw->pw_dir + "/.btcwallet/rpc.cert"s;
auto const stub = [] {
grpc::SslCredentialsOptions const cred_options{
// Note: This path used in this example is specific to non-OS X Unix systems.
// On Windows, this should be: %LOCALAPPDATA%\Btcwallet\rpc.cert
// On Mac OS X, this should be: $HOME/Library/Application Support/Btcwallet/rpc.cert
.pem_root_certs = read_file(unix_home_directory() + "/.btcwallet/rpc.cert"),
};
auto const creds = grpc::SslCredentials(cred_options);
auto const channel = grpc::CreateChannel("localhost:18332", creds);
return walletrpc::WalletService::NewStub(channel);
}();

grpc::SslCredentialsOptions cred_options{
.pem_root_certs = read_file(wallet_tls_cert_file),
};
auto creds = grpc::SslCredentials(cred_options);
auto channel = grpc::CreateChannel("localhost:18332", creds);
auto stub = walletrpc::WalletService::NewStub(channel);

grpc::ClientContext context{};

walletrpc::BalanceRequest request{};
request.set_account_number(0);
request.set_required_confirmations(1);
auto const request = [] {
walletrpc::BalanceRequest request{};
request.set_account_number(0);
request.set_required_confirmations(1);
return request;
}();

walletrpc::BalanceResponse response{};
auto status = stub->Balance(&context, request, &response);
auto const status = stub->Balance(&context, request, &response);
if (!status.ok()) {
std::cout << status.error_message() << std::endl;
std::cerr << status.error_code() << ": " << status.error_message() << std::endl;
} else {
std::cout << "Spendable balance: " << response.spendable() << " Satoshis" << std::endl;
}
Expand All @@ -185,9 +185,9 @@ auto main() -> int {
The example can then be built with the following commands:

```bash
$ c++ -std=c++14 -I/usr/local/include -pthread -c -o api.pb.o api.pb.cc
$ c++ -std=c++14 -I/usr/local/include -pthread -c -o api.grpc.pb.o api.grpc.pb.cc
$ c++ -std=c++14 -I/usr/local/include -pthread -c -o example.o example.cc
$ c++ -std=c++11 -I/usr/local/include -pthread -c -o api.pb.o api.pb.cc
$ c++ -std=c++11 -I/usr/local/include -pthread -c -o api.grpc.pb.o api.grpc.pb.cc
$ c++ -std=c++11 -I/usr/local/include -pthread -c -o example.o example.cc
$ c++ *.o -L/usr/local/lib -lgrpc++ -lgrpc -lgpr -lprotobuf -lpthread -ldl -o example
```

Expand Down

0 comments on commit 0ace7ec

Please sign in to comment.