Line data Source code
1 : /*
2 : Copyright 2017 Soramitsu Co., Ltd.
3 : Licensed under the Apache License, Version 2.0 (the "License");
4 : you may not use this file except in compliance with the License.
5 : You may obtain a copy of the License at
6 : http://www.apache.org/licenses/LICENSE-2.0
7 : Unless required by applicable law or agreed to in writing, software
8 : distributed under the License is distributed on an "AS IS" BASIS,
9 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 : See the License for the specific language governing permissions and
11 : limitations under the License.
12 : */
13 :
14 : #include "torii/query_client.hpp"
15 :
16 : #include "network/impl/grpc_channel_builder.hpp"
17 :
18 : namespace torii_utils {
19 :
20 : using iroha::protocol::Query;
21 : using iroha::protocol::QueryResponse;
22 :
23 : QuerySyncClient::QuerySyncClient(const std::string &ip, size_t port)
24 271 : : ip_(ip),
25 271 : port_(port),
26 271 : stub_(iroha::network::createClient<iroha::protocol::QueryService_v1>(
27 271 : ip + ":" + std::to_string(port))) {}
28 :
29 : QuerySyncClient::QuerySyncClient(const QuerySyncClient &rhs)
30 2 : : QuerySyncClient(rhs.ip_, rhs.port_) {}
31 :
32 : QuerySyncClient &QuerySyncClient::operator=(QuerySyncClient rhs) {
33 0 : swap(*this, rhs);
34 0 : return *this;
35 : }
36 :
37 : QuerySyncClient::QuerySyncClient(QuerySyncClient &&rhs) noexcept {
38 2 : swap(*this, rhs);
39 2 : }
40 :
41 : QuerySyncClient &QuerySyncClient::operator=(QuerySyncClient &&rhs) noexcept {
42 0 : swap(*this, rhs);
43 0 : return *this;
44 : }
45 :
46 : /**
47 : * requests query to a torii server and returns response (blocking, sync)
48 : * @param query
49 : * @param response
50 : * @return grpc::Status
51 : */
52 : grpc::Status QuerySyncClient::Find(const iroha::protocol::Query &query,
53 : QueryResponse &response) const {
54 165 : grpc::ClientContext context;
55 165 : return stub_->Find(&context, query, &response);
56 165 : }
57 :
58 : std::vector<iroha::protocol::BlockQueryResponse>
59 : QuerySyncClient::FetchCommits(
60 : const iroha::protocol::BlocksQuery &blocks_query) const {
61 2 : grpc::ClientContext context;
62 2 : auto reader = stub_->FetchCommits(&context, blocks_query);
63 2 : std::vector<iroha::protocol::BlockQueryResponse> responses;
64 2 : iroha::protocol::BlockQueryResponse resp;
65 4 : while (reader->Read(&resp)) {
66 2 : responses.push_back(resp);
67 : }
68 2 : reader->Finish();
69 2 : return responses;
70 2 : }
71 :
72 : void QuerySyncClient::swap(QuerySyncClient &lhs, QuerySyncClient &rhs) {
73 : using std::swap;
74 2 : swap(lhs.ip_, rhs.ip_);
75 2 : swap(lhs.port_, rhs.port_);
76 2 : swap(lhs.stub_, rhs.stub_);
77 2 : }
78 :
79 : } // namespace torii_utils
|