From 885b95e2ea1feccf50fc8e1b5997116a9550b222 Mon Sep 17 00:00:00 2001 From: groot Date: Sun, 21 Apr 2019 11:31:36 +0800 Subject: [PATCH] refine code Former-commit-id: 5916e0c3afaa752bcc3ff3cef69d30244be0b6c7 --- cpp/test_client/src/ClientApp.cpp | 84 +++++++------------------- cpp/test_client/src/ClientSession.cpp | 86 +++++++++++++++++++++++++++ cpp/test_client/src/ClientSession.h | 31 ++++++++++ 3 files changed, 138 insertions(+), 63 deletions(-) create mode 100644 cpp/test_client/src/ClientSession.cpp create mode 100644 cpp/test_client/src/ClientSession.h diff --git a/cpp/test_client/src/ClientApp.cpp b/cpp/test_client/src/ClientApp.cpp index 41ba36ca..28b10fec 100644 --- a/cpp/test_client/src/ClientApp.cpp +++ b/cpp/test_client/src/ClientApp.cpp @@ -4,36 +4,14 @@ * Proprietary and confidential. ******************************************************************************/ #include "ClientApp.h" +#include "ClientSession.h" #include "server/ServerConfig.h" #include "Log.h" -#include -#include - -#include "thrift/gen-cpp/VecService.h" -#include "thrift/gen-cpp/VectorService_types.h" -#include "thrift/gen-cpp/VectorService_constants.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - namespace zilliz { namespace vecwise { namespace client { -using namespace ::apache::thrift; -using namespace ::apache::thrift::protocol; -using namespace ::apache::thrift::transport; -using namespace ::apache::thrift::concurrency; - void ClientApp::Run(const std::string &config_file) { server::ServerConfig& config = server::ServerConfig::GetInstance(); config.LoadConfigFile(config_file); @@ -44,54 +22,34 @@ void ClientApp::Run(const std::string &config_file) { std::string address = server_config.GetValue(server::CONFIG_SERVER_ADDRESS, "127.0.0.1"); int32_t port = server_config.GetInt32Value(server::CONFIG_SERVER_PORT, 33001); std::string protocol = server_config.GetValue(server::CONFIG_SERVER_PROTOCOL, "binary"); - std::string mode = server_config.GetValue(server::CONFIG_SERVER_MODE, "thread_pool"); + //std::string mode = server_config.GetValue(server::CONFIG_SERVER_MODE, "thread_pool"); CLIENT_LOG_INFO << "Connect to server: " << address << ":" << std::to_string(port); try { - stdcxx::shared_ptr socket_ptr(new transport::TSocket(address, port)); - stdcxx::shared_ptr transport_ptr(new TBufferedTransport(socket_ptr)); - stdcxx::shared_ptr protocol_ptr; - if(protocol == "binary") { - protocol_ptr.reset(new TBinaryProtocol(transport_ptr)); - } else if(protocol == "json") { - protocol_ptr.reset(new TJSONProtocol(transport_ptr)); - } else if(protocol == "compact") { - protocol_ptr.reset(new TCompactProtocol(transport_ptr)); - } else if(protocol == "debug") { - protocol_ptr.reset(new TDebugProtocol(transport_ptr)); - } else { - CLIENT_LOG_ERROR << "Service protocol: " << protocol << " is not supported currently"; - return; - } - - transport_ptr->open(); - VecServiceClient client(protocol_ptr); - try { - const int32_t dim = 256; - VecGroup group; - group.id = "test_group"; - group.dimension = dim; - group.index_type = 0; - client.add_group(group); - - for(int64_t k = 0; k < 10000; k++) { - VecTensor tensor; - for(int32_t i = 0; i < dim; i++) { - tensor.tensor.push_back((double)(i + k)); - } - - VecTensorIdList result; - client.add_vector(result, group.id, tensor); + ClientSession session(address, port, protocol); + + const int32_t dim = 256; + VecGroup group; + group.id = "test_group"; + group.dimension = dim; + group.index_type = 0; + session.interface()->add_group(group); + + for(int64_t k = 0; k < 10000; k++) { + VecTensor tensor; + for(int32_t i = 0; i < dim; i++) { + tensor.tensor.push_back((double)(i + k)); } - } catch (apache::thrift::TException& ex) { - printf("%s", ex.what()); + VecTensorIdList result; + session.interface()->add_vector(result, group.id, tensor); + + CLIENT_LOG_INFO << "add vector no." << k; } - transport_ptr->close(); - } catch (apache::thrift::TException& ex) { - CLIENT_LOG_ERROR << "Server encounter exception: " << ex.what(); + } catch (std::exception& ex) { + CLIENT_LOG_ERROR << "request encounter exception: " << ex.what(); } CLIENT_LOG_INFO << "Test finished"; diff --git a/cpp/test_client/src/ClientSession.cpp b/cpp/test_client/src/ClientSession.cpp new file mode 100644 index 00000000..57aa71d4 --- /dev/null +++ b/cpp/test_client/src/ClientSession.cpp @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited. + * Proprietary and confidential. + ******************************************************************************/ +#include "ClientSession.h" +#include "Log.h" + +#include "thrift/gen-cpp/VectorService_types.h" +#include "thrift/gen-cpp/VectorService_constants.h" + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace zilliz { +namespace vecwise { +namespace client { + +using namespace ::apache::thrift; +using namespace ::apache::thrift::protocol; +using namespace ::apache::thrift::transport; +using namespace ::apache::thrift::concurrency; + +ClientSession::ClientSession(const std::string &address, int32_t port, const std::string &protocol) +: client_(nullptr) { + try { + stdcxx::shared_ptr socket_ptr(new transport::TSocket(address, port)); + stdcxx::shared_ptr transport_ptr(new TBufferedTransport(socket_ptr)); + stdcxx::shared_ptr protocol_ptr; + if(protocol == "binary") { + protocol_ptr.reset(new TBinaryProtocol(transport_ptr)); + } else if(protocol == "json") { + protocol_ptr.reset(new TJSONProtocol(transport_ptr)); + } else if(protocol == "compact") { + protocol_ptr.reset(new TCompactProtocol(transport_ptr)); + } else if(protocol == "debug") { + protocol_ptr.reset(new TDebugProtocol(transport_ptr)); + } else { + CLIENT_LOG_ERROR << "Service protocol: " << protocol << " is not supported currently"; + return; + } + + transport_ptr->open(); + client_ = std::make_shared(protocol_ptr); + } catch ( std::exception& ex) { + CLIENT_LOG_ERROR << "connect encounter exception: " << ex.what(); + } + +} + +ClientSession::~ClientSession() { + try { + if(client_ != nullptr) { + auto protocol = client_->getInputProtocol(); + if(protocol != nullptr) { + auto transport = protocol->getTransport(); + if(transport != nullptr) { + transport->close(); + } + } + } + } catch ( std::exception& ex) { + CLIENT_LOG_ERROR << "disconnect encounter exception: " << ex.what(); + } +} + +VecServiceClientPtr ClientSession::interface() { + if(client_ == nullptr) { + throw std::exception(); + } + return client_; +} + +} +} +} \ No newline at end of file diff --git a/cpp/test_client/src/ClientSession.h b/cpp/test_client/src/ClientSession.h new file mode 100644 index 00000000..b61588ff --- /dev/null +++ b/cpp/test_client/src/ClientSession.h @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved + * Unauthorized copying of this file, via any medium is strictly prohibited. + * Proprietary and confidential. + ******************************************************************************/ +#pragma once + +#include "thrift/gen-cpp/VecService.h" + +#include + +namespace zilliz { +namespace vecwise { +namespace client { + + using VecServiceClientPtr = std::shared_ptr; + + +class ClientSession { +public: + ClientSession(const std::string& address, int32_t port, const std::string& protocol); + ~ClientSession(); + + VecServiceClientPtr interface(); + + VecServiceClientPtr client_; +}; + +} +} +} -- GitLab