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:
29  log_(std::move(log)) {}
30 
35  void *got_tag;
36  auto ok = false;
37  while (cq_.Next(&got_tag, &ok)) {
38  auto call = static_cast<AsyncClientCall *>(got_tag);
39  if (not call->status.ok()) {
40  log_->warn("RPC failed: {}", call->status.error_message());
41  }
42  delete call;
43  }
44  }
45 
47  cq_.Shutdown();
48  if (thread_.joinable()) {
49  thread_.join();
50  }
51  }
52 
53  grpc::CompletionQueue cq_;
54  std::thread thread_;
55 
59  struct AsyncClientCall {
60  Response reply;
61 
62  grpc::ClientContext context;
63 
64  grpc::Status status;
65 
66  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<Response>>
68  };
69 
75  template <typename F>
76  void Call(F &&lambda) {
77  auto call = new AsyncClientCall;
78  call->response_reader = lambda(&call->context, &cq_);
79  call->response_reader->Finish(&call->reply, &call->status, call);
80  }
81 
82  private:
83  logger::LoggerPtr log_;
84  };
85  } // namespace network
86 } // namespace iroha
87 
88 #endif // IROHA_ASYNC_GRPC_CLIENT_HPP
grpc::ClientContext context
Definition: async_grpc_client.hpp:62
AsyncGrpcClient(logger::LoggerPtr log)
Definition: async_grpc_client.hpp:27
Definition: async_grpc_client.hpp:59
Response reply
Definition: async_grpc_client.hpp:60
std::unique_ptr< grpc::ClientAsyncResponseReaderInterface< Response > > response_reader
Definition: async_grpc_client.hpp:67
Definition: peer.hpp:48
void asyncCompleteRpc()
Definition: async_grpc_client.hpp:34
std::shared_ptr< Logger > LoggerPtr
Definition: logger_fwd.hpp:22
void Call(F &&lambda)
Definition: async_grpc_client.hpp:76
grpc::Status status
Definition: async_grpc_client.hpp:64
Definition: block_query.hpp:16
std::thread thread_
Definition: async_grpc_client.hpp:54
Definition: async_grpc_client.hpp:25
~AsyncGrpcClient()
Definition: async_grpc_client.hpp:46
grpc::CompletionQueue cq_
Definition: async_grpc_client.hpp:53