hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
async_grpc_client.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_ASYNC_GRPC_CLIENT_HPP
7 #define IROHA_ASYNC_GRPC_CLIENT_HPP
8 
9 #include <ciso646>
10 #include <thread>
11 
12 #include <google/protobuf/empty.pb.h>
13 #include <grpc++/grpc++.h>
14 #include <grpcpp/impl/codegen/async_unary_call.h>
15 #include "logger/logger.hpp"
16 
17 namespace iroha {
18  namespace network {
19 
24  template <typename Response>
26  public:
27  explicit AsyncGrpcClient(
28  logger::Logger log = logger::log("AsyncGrpcClient"))
30  log_(std::move(log)) {}
31 
36  void *got_tag;
37  auto ok = false;
38  while (cq_.Next(&got_tag, &ok)) {
39  auto call = static_cast<AsyncClientCall *>(got_tag);
40  if (not call->status.ok()) {
41  log_->warn("RPC failed: {}", call->status.error_message());
42  }
43  delete call;
44  }
45  }
46 
48  cq_.Shutdown();
49  if (thread_.joinable()) {
50  thread_.join();
51  }
52  }
53 
54  grpc::CompletionQueue cq_;
55  std::thread thread_;
57 
61  struct AsyncClientCall {
62  Response reply;
63 
64  grpc::ClientContext context;
65 
66  grpc::Status status;
67 
68  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<Response>>
70  };
71 
77  template <typename F>
78  void Call(F &&lambda) {
79  auto call = new AsyncClientCall;
80  call->response_reader = lambda(&call->context, &cq_);
81  call->response_reader->Finish(&call->reply, &call->status, call);
82  }
83  };
84  } // namespace network
85 } // namespace iroha
86 
87 #endif // IROHA_ASYNC_GRPC_CLIENT_HPP
grpc::ClientContext context
Definition: async_grpc_client.hpp:64
Definition: async_grpc_client.hpp:61
Response reply
Definition: async_grpc_client.hpp:62
std::unique_ptr< grpc::ClientAsyncResponseReaderInterface< Response > > response_reader
Definition: async_grpc_client.hpp:69
logger::Logger log_
Definition: async_grpc_client.hpp:56
Definition: peer.hpp:48
void asyncCompleteRpc()
Definition: async_grpc_client.hpp:35
std::shared_ptr< spdlog::logger > Logger
Definition: logger.hpp:26
void Call(F &&lambda)
Definition: async_grpc_client.hpp:78
grpc::Status status
Definition: async_grpc_client.hpp:66
Definition: block_query.hpp:16
AsyncGrpcClient(logger::Logger log=logger::log("AsyncGrpcClient"))
Definition: async_grpc_client.hpp:27
Logger log(const std::string &tag)
Definition: logger.cpp:48
std::thread thread_
Definition: async_grpc_client.hpp:55
Definition: async_grpc_client.hpp:25
~AsyncGrpcClient()
Definition: async_grpc_client.hpp:47
grpc::CompletionQueue cq_
Definition: async_grpc_client.hpp:54