Skip to content

Commit

Permalink
Refactor iroha-cli:
Browse files Browse the repository at this point in the history
 - Add grpc response handler
 - Move client test to integration
  • Loading branch information
grimadas committed Aug 6, 2017
1 parent 7753ebe commit 1f38223
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 64 deletions.
1 change: 1 addition & 0 deletions iroha-cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ add_library(client
client.cpp
impl/query_response_handler.cpp
impl/transaction_response_handler.cpp
impl/grpc_response_handler.cpp
)
target_link_libraries(client
model_converters
Expand Down
35 changes: 21 additions & 14 deletions iroha-cli/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,36 @@ namespace iroha_cli {
CliClient::CliClient(std::string target_ip, int port)
: command_client_(target_ip, port), query_client_(target_ip, port) {}

CliClient::TxStatus CliClient::sendTx(std::string json_tx) {
CliClient::Response<CliClient::TxStatus> CliClient::sendTx(std::string json_tx) {
CliClient::Response<CliClient::TxStatus> response;
iroha::model::converters::JsonTransactionFactory serializer;
auto doc = iroha::model::converters::stringToJson(std::move(json_tx));
if (not doc.has_value()) {
return WRONG_FORMAT;
}
auto tx_opt = serializer.deserialize(doc.value());
if (not tx_opt.has_value()) {
return WRONG_FORMAT;
response.status = grpc::Status::OK;
response.answer = WRONG_FORMAT;
return response;
}
auto model_tx = tx_opt.value();
// Convert to protobuf
iroha::model::converters::PbTransactionFactory factory;
auto pb_tx = factory.serialize(model_tx);
// Send to iroha:
iroha::protocol::ToriiResponse response;
auto stat = command_client_.Torii(pb_tx, response);

return response.validation() ==
iroha::protocol::STATELESS_VALIDATION_SUCCESS
? OK
: NOT_VALID;
iroha::protocol::ToriiResponse toriiResponse;
response.status = command_client_.Torii(pb_tx, toriiResponse);
response.answer = toriiResponse.validation() ==
iroha::protocol::STATELESS_VALIDATION_SUCCESS
? OK
: NOT_VALID;
return response;
}

iroha::protocol::QueryResponse CliClient::sendQuery(std::string json_query) {
CliClient::Response<iroha::protocol::QueryResponse> CliClient::sendQuery(
std::string json_query) {
CliClient::Response<iroha::protocol::QueryResponse> response;
iroha::model::converters::JsonQueryFactory serializer;

auto query_opt = serializer.deserialize(std::move(json_query));
Expand All @@ -62,12 +67,14 @@ namespace iroha_cli {
iroha::protocol::ErrorResponse er;
er.set_reason(iroha::protocol::ErrorResponse::WRONG_FORMAT);
query_response.mutable_error_response()->CopyFrom(er);
return query_response;
response.status = grpc::Status::OK;
response.answer = query_response;
return response;
}

query_client_.Find(query_opt.value(), query_response);

return query_response;
response.status = query_client_.Find(query_opt.value(), query_response);
response.answer = query_response;
return response;
}

}; // namespace iroha_cli
12 changes: 9 additions & 3 deletions iroha-cli/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ namespace iroha_cli {

class CliClient {
public:
enum TxStatus { WRONG_FORMAT, NOT_VALID, OK };
template <typename T>
struct Response{
grpc::Status status;
T answer;
};

enum TxStatus { WRONG_FORMAT, NOT_VALID, OK, };

CliClient(std::string target_ip, int port);
/**
* Send transaction to Iroha-Network
* @param json_tx
* @return
*/
TxStatus sendTx(std::string json_tx);
CliClient::Response<CliClient::TxStatus> sendTx(std::string json_tx);

iroha::protocol::QueryResponse sendQuery(std::string json_query);
CliClient::Response<iroha::protocol::QueryResponse> sendQuery(std::string json_query);

private:
torii::CommandSyncClient command_client_;
Expand Down
42 changes: 42 additions & 0 deletions iroha-cli/grpc_response_handler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef IROHA_CLI_GRPC_RESPONSE_HANDLER_HPP
#define IROHA_CLI_GRPC_RESPONSE_HANDLER_HPP

#include "query_response_handler.hpp"
#include "transaction_response_handler.hpp"
#include "client.hpp"
#include "logger/logger.hpp"

namespace iroha_cli {
class GrpcResponseHandler {
public:
GrpcResponseHandler();
void handle(CliClient::Response<CliClient::TxStatus> response);
void handle(CliClient::Response<iroha::protocol::QueryResponse> response);
private:
TransactionResponseHandler tx_handler_;
QueryResponseHandler query_handler_;
void handleGrpcErrors(grpc::StatusCode code);
// Map
std::shared_ptr<spdlog::logger> log_;
std::unordered_map<int, std::string> handler_map_;
};
} // namespace iroha_cli

#endif // IROHA_GRPC_RESPONSE_HANDLER_HPP
71 changes: 71 additions & 0 deletions iroha-cli/impl/grpc_response_handler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Copyright Soramitsu Co., Ltd. 2017 All Rights Reserved.
* http://soramitsu.co.jp
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "grpc_response_handler.hpp"

using namespace grpc;
namespace iroha_cli {

GrpcResponseHandler::GrpcResponseHandler()
: log_(logger::log("GrpcResponseHandler")) {
handler_map_[CANCELLED] = "Operation canceled";
handler_map_[UNKNOWN] = "Unknown error";
handler_map_[INVALID_ARGUMENT] = "INVALID_ARGUMENT";
handler_map_[DEADLINE_EXCEEDED] = "DEADLINE_EXCEEDED";
handler_map_[NOT_FOUND] = "NOT_FOUND";
handler_map_[ALREADY_EXISTS] = "ALREADY_EXISTS";
handler_map_[PERMISSION_DENIED] = "PERMISSION_DENIED";
handler_map_[UNAUTHENTICATED] = "UNAUTHENTICATED";
handler_map_[RESOURCE_EXHAUSTED] = "RESOURCE_EXHAUSTED";
handler_map_[FAILED_PRECONDITION] = "FAILED_PRECONDITION";
handler_map_[ABORTED] = "ABORTED";
handler_map_[OUT_OF_RANGE] = "OUT_OF_RANGE";
handler_map_[UNIMPLEMENTED] = "UNIMPLEMENTED";
handler_map_[INTERNAL] = "INTERNAL";
handler_map_[UNIMPLEMENTED] = "UNIMPLEMENTED";
handler_map_[UNAVAILABLE] = "Server is unavailable";
handler_map_[DATA_LOSS] = "DATA_LOSS";
}

void GrpcResponseHandler::handle(
CliClient::Response<CliClient::TxStatus> response) {
if (response.status.ok()) {
tx_handler_.handle(response.answer);
} else {
handleGrpcErrors(response.status.error_code());
}
}

void GrpcResponseHandler::handle(
CliClient::Response<iroha::protocol::QueryResponse> response) {
if (response.status.ok()) {
query_handler_.handle(response.answer);
} else {
handleGrpcErrors(response.status.error_code());
}
}

void GrpcResponseHandler::handleGrpcErrors(grpc::StatusCode code) {
auto it = handler_map_.find(code);
if (it != handler_map_.end()) {
log_->error(it->second);
} else {
log_->error("Handler for grpc {} not implemented", code);
}
}

} // namespace iroha_cli
12 changes: 4 additions & 8 deletions iroha-cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
#include "validators.hpp"

#include "client.hpp"
#include "grpc_response_handler.hpp"
#include "impl/keys_manager_impl.hpp"
#include "logger/logger.hpp"
#include "query_response_handler.hpp"
#include "transaction_response_handler.hpp"

// ** Genesis Block and Provisioning ** //
// Reference is here (TODO: move to doc):
Expand Down Expand Up @@ -76,24 +75,21 @@ int main(int argc, char* argv[]) {
bootstrap.run_network(peers, block);
} else if (FLAGS_grpc) {
iroha_cli::CliClient client(FLAGS_address, FLAGS_torii_port);
iroha_cli::GrpcResponseHandler response_handler;
if (not FLAGS_json_transaction.empty()) {
iroha_cli::TransactionResponseHandler tx_resp_handler;
logger->info("Send transaction to {}:{} ", FLAGS_address,
FLAGS_torii_port);
std::ifstream file(FLAGS_json_transaction);
std::string str((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());

tx_resp_handler.handle(client.sendTx(str));
response_handler.handle(client.sendTx(str));
}
if (not FLAGS_json_query.empty()) {
logger->info("Send query to {}:{}", FLAGS_address, FLAGS_torii_port);
iroha_cli::QueryResponseHandler responseHandler;
std::ifstream file(FLAGS_json_query);
std::string str((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
auto response = client.sendQuery(str);
responseHandler.handle(response);
response_handler.handle(client.sendQuery(str));
}

} else {
Expand Down
2 changes: 1 addition & 1 deletion irohad/model/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ target_link_libraries(model_converters
optional
schema
logger
)
)
12 changes: 11 additions & 1 deletion test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/test_bin)

add_subdirectory(consensus)
add_subdirectory(consensus)

addtest(client_test client_test.cpp)
target_link_libraries(client_test
client
processors
server_runner
)
target_include_directories(client_test PUBLIC
${PROJECT_SOURCE_DIR}/iroha-cli
)
Loading

0 comments on commit 1f38223

Please sign in to comment.