This is an automated email from the ASF dual-hosted git repository. gosonzhang pushed a commit to branch tubemq-client-cpp in repository https://gitbox.apache.org/repos/asf/incubator-tubemq.git
The following commit(s) were added to refs/heads/tubemq-client-cpp by this push: new 7da8b49 [TUBEMQ-252] Create C/C++ Metadata classes (#186) 7da8b49 is described below commit 7da8b497ed2a69c163aba4901da89cc8bc650947 Author: gosonzhang <4675...@qq.com> AuthorDate: Mon Jul 6 01:40:20 2020 +0000 [TUBEMQ-252] Create C/C++ Metadata classes (#186) Co-authored-by: gosonzhang <gosonzh...@tencent.com> --- .../tubemq-client-cpp/inc/const_config.h | 7 +- .../tubemq-client-cpp/inc/meta_info.h | 94 ++++++++++++++ tubemq-client-twins/tubemq-client-cpp/inc/utils.h | 5 +- .../tubemq-client-cpp/src/file_ini.cc | 4 +- .../tubemq-client-cpp/src/meta_info.cc | 139 +++++++++++++++++++++ tubemq-client-twins/tubemq-client-cpp/src/utils.cc | 15 ++- 6 files changed, 256 insertions(+), 8 deletions(-) diff --git a/tubemq-client-twins/tubemq-client-cpp/inc/const_config.h b/tubemq-client-twins/tubemq-client-cpp/inc/const_config.h index 1cc2b60..80d8b3b 100644 --- a/tubemq-client-twins/tubemq-client-cpp/inc/const_config.h +++ b/tubemq-client-twins/tubemq-client-cpp/inc/const_config.h @@ -51,7 +51,6 @@ static const int kFilterItemMaxCount = 500; // max session key length static const int kSessionKeyMaxLength = 1024; - // max subscribe info report times static const int kSubInfoReportMaxIntervalTimes = 6; // default message not found response wait period @@ -68,6 +67,11 @@ static const int kMaxIntValue = 0x7fffffff; // max long value static const long kMaxLongValue = 0x7fffffffffffffffL; +// default broker port +static const int kBrokerPortDef = 8123; +// default broker TLS port +static const int kBrokerTlsPortDef = 8124; + // invalid value static const int kInvalidValue = -2; @@ -76,6 +80,7 @@ static const int kInvalidValue = -2; namespace delimiter { + static const string kDelimiterDot = "."; static const string kDelimiterEqual = "="; static const string kDelimiterAnd = "&"; static const string kDelimiterComma = ","; diff --git a/tubemq-client-twins/tubemq-client-cpp/inc/meta_info.h b/tubemq-client-twins/tubemq-client-cpp/inc/meta_info.h new file mode 100644 index 0000000..55baa4c --- /dev/null +++ b/tubemq-client-twins/tubemq-client-cpp/inc/meta_info.h @@ -0,0 +1,94 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 _TUBEMQ_CLIENT_META_INFO_H_ +#define _TUBEMQ_CLIENT_META_INFO_H_ + +#include <string> + +namespace tubemq { + +using namespace std; + + + +class NodeInfo { + public: + NodeInfo(); + NodeInfo(bool is_broker, const string& node_info); + NodeInfo(const string& node_host, int node_port); + NodeInfo(int node_id, const string& node_host, int node_port); + ~NodeInfo(); + NodeInfo& operator=(const NodeInfo& target); + bool operator== (const NodeInfo& target); + bool operator< (const NodeInfo& target) const; + const int GetNodeId() const; + const string& GetHost() const; + const int GetPort() const; + const string& GetAddrInfo() const; + const string& GetNodeInfo() const; + + private: + void buildStrInfo(); + + private: + int node_id_; + string node_host_; + int node_port_; + // ip:port + string addr_info_; + // id:ip:port + string node_info_; +}; + + +class Partition { + public: + Partition(); + Partition(const string& partition_info); + Partition(const NodeInfo& broker_info, const string& partStr); + Partition(const NodeInfo& broker_info, const string& topic, int partition_id); + ~Partition(); + Partition& operator=(const Partition& target); + bool operator== (const Partition& target); + const int GetBrokerId() const; + const string& GetBrokerHost() const; + const int GetBrokerPort() const; + const string& GetPartitionKey() const; + const string& GetTopic() const; + const NodeInfo& GetBrokerInfo() const; + const int GetPartitionId() const; + const string& ToString() const; + + private: + void buildPartitionKey(); + + private: + string topic_; + NodeInfo broker_info_; + int partition_id_; + string partition_key_; + string partition_info_; +}; + + +} + +#endif + diff --git a/tubemq-client-twins/tubemq-client-cpp/inc/utils.h b/tubemq-client-twins/tubemq-client-cpp/inc/utils.h index 0709bb8..efb22d7 100644 --- a/tubemq-client-twins/tubemq-client-cpp/inc/utils.h +++ b/tubemq-client-twins/tubemq-client-cpp/inc/utils.h @@ -47,8 +47,9 @@ class Utils { const string& group_name, string& tgt_group_name); static bool ValidFilterItem(string& err_info, const string& src_filteritem, string& tgt_filteritem); - static string int2str(int data); - static string long2str(long data); + static string Int2str(int data); + static string Long2str(long data); + static int IpToInt(const string& ipv4_addr); static long GetCurrentTimeMillis(); }; diff --git a/tubemq-client-twins/tubemq-client-cpp/src/file_ini.cc b/tubemq-client-twins/tubemq-client-cpp/src/file_ini.cc index 822b803..df0cdf3 100644 --- a/tubemq-client-twins/tubemq-client-cpp/src/file_ini.cc +++ b/tubemq-client-twins/tubemq-client-cpp/src/file_ini.cc @@ -135,7 +135,7 @@ bool Fileini::GetValue(string& err_info, const string& sector, bool Fileini::GetValue(string& err_info, const string& sector, const string& key, int& value, const int def) { string val_str; - string def_str = Utils::int2str(def); + string def_str = Utils::Int2str(def); bool result = GetValue(err_info, sector, key, val_str, def_str); if (!result) { return result; @@ -144,8 +144,6 @@ bool Fileini::GetValue(string& err_info, const string& sector, return true; } - - } diff --git a/tubemq-client-twins/tubemq-client-cpp/src/meta_info.cc b/tubemq-client-twins/tubemq-client-cpp/src/meta_info.cc new file mode 100644 index 0000000..c744b47 --- /dev/null +++ b/tubemq-client-twins/tubemq-client-cpp/src/meta_info.cc @@ -0,0 +1,139 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 <sstream> +#include <stdlib.h> +#include "utils.h" +#include "meta_info.h" +#include "const_config.h" + + +namespace tubemq { + + +NodeInfo::NodeInfo() { + this->node_id_ = config::kInvalidValue; + this->node_host_ = " "; + this->node_port_ = config::kInvalidValue; + buildStrInfo(); +} + +NodeInfo::NodeInfo(bool is_broker, const string& node_info) { + vector<string> result; + Utils::Split(node_info, result, delimiter::kDelimiterColon); + if (is_broker) { + this->node_id_ = atoi(result[0].c_str()); + this->node_host_ = result[1]; + this->node_port_ = config::kBrokerPortDef; + if(result.size() >= 3){ + this->node_port_ = atoi(result[2].c_str()); + } + } else { + this->node_id_ = config::kInvalidValue; + this->node_host_ = result[0]; + this->node_port_ = config::kBrokerPortDef; + if (result.size() >= 2) { + this->node_port_ = atoi(result[1].c_str()); + } + } + buildStrInfo(); +} + +NodeInfo::NodeInfo(const string& node_host, int node_port) { + this->node_id_ = config::kInvalidValue; + this->node_host_ = node_host; + this->node_port_ = node_port; + buildStrInfo(); + +} + +NodeInfo::NodeInfo(int node_id, const string& node_host, int node_port) { + this->node_id_ = node_id; + this->node_host_ = node_host; + this->node_port_ = node_port; + buildStrInfo(); +} + +NodeInfo::~NodeInfo() { + +} + +NodeInfo& NodeInfo::operator=(const NodeInfo& target) { + if (this != &target){ + this->node_id_ = target.node_id_; + this->node_host_ = target.node_host_; + this->node_port_ = target.node_port_; + this->addr_info_ = target.addr_info_; + this->node_info_ = target.node_info_; + } + return *this; +} + +bool NodeInfo::operator== (const NodeInfo& target) { + if (this == &target) { + return true; + } + if (this->node_info_ == target.node_info_) { + return true; + } + return false; + +} + +bool NodeInfo::operator< (const NodeInfo& target) const { + return this->node_id_ < target.node_id_; +} + +const int NodeInfo::GetNodeId() const { + return this->node_id_; +} + +const string& NodeInfo::GetHost() const { + return this->node_host_; +} + +const int NodeInfo::GetPort() const { + return this->node_port_; +} + +const string& NodeInfo::GetAddrInfo() const { + return this->addr_info_; +} + +const string& NodeInfo::GetNodeInfo() const { + return this->node_info_; +} + +void NodeInfo::buildStrInfo() { + stringstream ss1; + ss1 << this->node_host_; + ss1 << delimiter::kDelimiterColon; + ss1 << this->node_port_; + this->addr_info_ = ss1.str(); + + stringstream ss2; + ss2 << this->node_id_; + ss2 << delimiter::kDelimiterColon; + ss2 << this->addr_info_; + this->node_info_ = ss2.str(); +} + + +} + diff --git a/tubemq-client-twins/tubemq-client-cpp/src/utils.cc b/tubemq-client-twins/tubemq-client-cpp/src/utils.cc index cd60982..d1fac8f 100644 --- a/tubemq-client-twins/tubemq-client-cpp/src/utils.cc +++ b/tubemq-client-twins/tubemq-client-cpp/src/utils.cc @@ -239,18 +239,29 @@ bool Utils::ValidFilterItem(string& err_info, } -string Utils::int2str(int data) { +string Utils::Int2str(int data) { stringstream ss; ss<<data; return ss.str(); } -string Utils::long2str(long data) { +string Utils::Long2str(long data) { stringstream ss; ss<<data; return ss.str(); } +int Utils::IpToInt(const string& ipv4_addr) { + int result = 0; + vector<string> result_vec; + + Utils::Split(ipv4_addr, result_vec, delimiter::kDelimiterDot); + result = ((char) atoi(result_vec[3].c_str())) & 0xFF; + result |= ((char) atoi(result_vec[2].c_str()) << 8) & 0xFF00; + result |= ((char) atoi(result_vec[1].c_str()) << 16) & 0xFF0000; + result |= ((char) atoi(result_vec[0].c_str()) << 24) & 0xFF000000; + return result; +} long Utils::GetCurrentTimeMillis() { struct timeval tv;