This is an automated email from the ASF dual-hosted git repository.
dockerzhang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/inlong.git
The following commit(s) were added to refs/heads/master by this push:
new f7bcd93bad [INLONG-8889][SDK] Optimize CmakeList for dataproxy cpp sdk
(#8890)
f7bcd93bad is described below
commit f7bcd93badeb59d673f1123604147d9774fbc4a4
Author: doleyzi <[email protected]>
AuthorDate: Mon Sep 11 18:56:33 2023 +0800
[INLONG-8889][SDK] Optimize CmakeList for dataproxy cpp sdk (#8890)
---
.../dataproxy-sdk-cpp/CMakeLists.txt | 3 +
.../dataproxy-sdk-cpp/release/inc/sdk_conf.h | 4 +-
.../dataproxy-sdk-cpp/src/client/CMakeLists.txt | 24 +++
.../dataproxy-sdk-cpp/src/config/CMakeLists.txt | 24 +++
.../dataproxy-sdk-cpp/src/config/ini_help.h | 2 +-
.../dataproxy-sdk-cpp/src/config/sdk_conf.cc | 6 +-
.../dataproxy-sdk-cpp/src/utils/CMakeLists.txt | 24 +++
.../dataproxy-sdk-cpp/src/utils/utils.cc | 183 ++++++++++-----------
.../dataproxy-sdk-cpp/src/utils/utils.h | 106 +++++-------
9 files changed, 211 insertions(+), 165 deletions(-)
diff --git a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/CMakeLists.txt
b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/CMakeLists.txt
index b08af1d45c..317e4bed6b 100644
--- a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/CMakeLists.txt
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/CMakeLists.txt
@@ -29,6 +29,7 @@
include_directories(${PROJECT_SOURCE_DIR}/third_party/rapidjson/src/rapidjson/in
include_directories(release/inc)
include_directories(src/base)
include_directories(src/net)
+include_directories(src/utils)
link_directories(${PROJECT_SOURCE_DIR}/third_party/lib)
link_directories(${PROJECT_SOURCE_DIR}/third_party/lib64)
@@ -36,6 +37,8 @@ link_directories(${PROJECT_SOURCE_DIR}/third_party/lib64)
add_subdirectory(third_party)
add_subdirectory(src/base)
add_subdirectory(src/net)
+add_subdirectory(src/utils)
+add_subdirectory(src/config)
# add_subdirectory(test)
add_subdirectory(release)
diff --git
a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/release/inc/sdk_conf.h
b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/release/inc/sdk_conf.h
index 7c1f4a78a5..d31407dc34 100644
--- a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/release/inc/sdk_conf.h
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/release/inc/sdk_conf.h
@@ -26,7 +26,7 @@
#include <string>
#include <vector>
-namespace sdk {
+namespace inlong {
class SdkConfig {
private:
static SdkConfig *instance_;
@@ -114,6 +114,6 @@ public:
return ((6 == msg_type_) || ((msg_type_ >= 7) && (extend_field_ & 0x1)));
}
};
-} // namespace busapi
+} // namespace inlong
#endif // CAPI_BASE_CLIENT_CONFIG_H_
\ No newline at end of file
diff --git
a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/client/CMakeLists.txt
b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/client/CMakeLists.txt
new file mode 100644
index 0000000000..fad19e6a1d
--- /dev/null
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/client/CMakeLists.txt
@@ -0,0 +1,24 @@
+#
+# 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.
+#
+
+cmake_minimum_required(VERSION 3.1)
+
+aux_source_directory(. CLIENT_SRCS)
+
+add_library(inlong_client STATIC ${CLIENT_SRCS})
diff --git
a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/CMakeLists.txt
b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/CMakeLists.txt
new file mode 100644
index 0000000000..57ed3d29c0
--- /dev/null
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/CMakeLists.txt
@@ -0,0 +1,24 @@
+#
+# 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.
+#
+
+cmake_minimum_required(VERSION 3.1)
+
+aux_source_directory(. CONF_SRCS)
+
+add_library(inlong_conf STATIC ${CONF_SRCS})
diff --git
a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/ini_help.h
b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/ini_help.h
index 82f04c0e43..d846c09da9 100644
--- a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/ini_help.h
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/ini_help.h
@@ -25,7 +25,7 @@
#include <string>
#include <vector>
-namespace dataproxy_sdk
+namespace inlong
{
const char delim[] = "\n";
struct Iterm
diff --git
a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/sdk_conf.cc
b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/sdk_conf.cc
index dab7fd5b8d..fcb7d7da19 100644
--- a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/sdk_conf.cc
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/config/sdk_conf.cc
@@ -64,7 +64,7 @@ bool SdkConfig::ParseConfig(const std::string &config_path) {
InitTcpParam(doc);
OthersParam(doc);
- sdk::initLog4cplus();
+ inlong::initLog4cplus();
ShowClientConfig();
@@ -72,7 +72,7 @@ bool SdkConfig::ParseConfig(const std::string &config_path) {
}
void SdkConfig::defaultInit() {
- per_groupid_thread_nums_ = constants::kPerBidThreadNums;
+ per_groupid_thread_nums_ = constants::kPerGroupidThreadNums;
dispatch_interval_send_ = constants::kDispatchIntervalSend;
dispatch_interval_zip_ = constants::kDispatchIntervalZip;
tcp_detection_interval_ = constants::kTcpDetectionInterval;
@@ -121,7 +121,7 @@ void SdkConfig::InitThreadParam(const rapidjson::Value
&doc) {
const rapidjson::Value &obj = doc["per_groupid_thread_nums"];
per_groupid_thread_nums_ = obj.GetInt();
} else {
- per_groupid_thread_nums_ = constants::kPerBidThreadNums;
+ per_groupid_thread_nums_ = constants::kPerGroupidThreadNums;
}
if (doc.HasMember("dispatch_interval_zip") &&
doc["dispatch_interval_zip"].IsInt() &&
diff --git
a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/CMakeLists.txt
b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/CMakeLists.txt
new file mode 100644
index 0000000000..6ecf82464b
--- /dev/null
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/CMakeLists.txt
@@ -0,0 +1,24 @@
+#
+# 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.
+#
+
+cmake_minimum_required(VERSION 3.1)
+
+aux_source_directory(. UTILS_SRCS)
+
+add_library(utils STATIC ${UTILS_SRCS})
diff --git
a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/utils.cc
b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/utils.cc
index 5a80740e11..f4810e92f3 100644
--- a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/utils.cc
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/utils.cc
@@ -19,13 +19,16 @@
#include "utils.h"
+#include "logger.h"
#include <arpa/inet.h>
#include <ctime>
+#include <curl/curl.h>
#include <errno.h>
#include <fstream>
#include <iostream>
#include <iterator>
#include <net/if.h>
+#include <netdb.h>
#include <netinet/in.h>
#include <pthread.h>
#include <regex>
@@ -37,19 +40,14 @@
#include <sys/sysinfo.h>
#include <sys/time.h>
-#include "curl/curl.h"
-#include "logger.h"
+#include "api_code.h"
#include "tc_api.h"
namespace inlong {
uint16_t Utils::sequence = 0;
uint64_t Utils::last_msstamp = 0;
char Utils::snowflake_id[35] = {0};
-char base64_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
- 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
- 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
- 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
- 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2',
- '3', '4', '5', '6', '7', '8', '9', '+', '/'};
+AtomicUInt g_send_msgid{0};
+AtomicInt user_exit_flag{0};
void Utils::taskWaitTime(int32_t sec) {
struct timeval tv;
@@ -91,9 +89,10 @@ std::string Utils::getFormatTime(uint64_t data_time) {
size_t Utils::zipData(const char *input, uint32_t input_len,
std::string &zip_res) {
+ // size_t zip_res_len = snappy::MaxCompressedLength(input_len);
size_t len_after_zip = snappy::Compress((char *)input, input_len, &zip_res);
- LOG_TRACE("data zip: input len is %u, output len is %u.", input_len,
- len_after_zip);
+ // LOG_TRACE("data zip: input len is %u, output len is %u.", input_len,
+ // len_after_zip);
return len_after_zip;
}
@@ -104,7 +103,6 @@ char *Utils::getSnowflakeId() {
uint32_t pidid = static_cast<uint16_t>((getpid() & 0xFFFF));
uint32_t selfid = static_cast<uint16_t>((pthread_self() & 0xFFFF00) >> 8);
- // 22bit ms
uint64_t sequence_mask = -1LL ^ (-1LL << 22);
uint64_t time_id = 0LL;
@@ -112,23 +110,19 @@ char *Utils::getSnowflakeId() {
uint64_t since_date = 1288834974657LL; // Thu, 04 Nov 2010 01:42:54 GMT
- // 41bit ms
uint64_t msstamp = getCurrentMsTime();
uint64_t rand = 0;
uint64_t rand_mask = -1LL ^ (-1LL << (32 + 5 + 12));
- // error timestap
if (msstamp < last_msstamp) {
- LOG_ERROR("ms(%llx) time less last(%llx).", msstamp, last_msstamp);
+ LOG_ERROR("ms " << msstamp << " time less last:" << last_msstamp);
- // last ms
last_msstamp = msstamp;
srand(static_cast<uint32_t>(msstamp));
rand = random();
- // generate id
time_id = ((msstamp - since_date) << 22 | (rand & rand_mask));
snprintf(&snowflake_id[0], sizeof(snowflake_id), "0x%.16llx%.16llx",
@@ -136,7 +130,6 @@ char *Utils::getSnowflakeId() {
return &snowflake_id[0];
}
- // increase id
if (last_msstamp == msstamp) {
sequence = (sequence + 1) & sequence_mask;
@@ -151,11 +144,6 @@ char *Utils::getSnowflakeId() {
time_id = (((msstamp - since_date) << 22) | sequence);
- LOG_TRACE("ms:0x%llx, ip:0x%.16llx, seq:0x:%x, selfid:%u, "
- "local_id:0x%.16llx, time_id:0x%.16llx.",
- (msstamp - since_date) << (22), ipaddr, sequence,
- static_cast<uint32_t>(pthread_self()), local_id, time_id);
-
snprintf(&snowflake_id[0], sizeof(snowflake_id), "0x%.16llx%.16llx",
local_id,
time_id);
return &snowflake_id[0];
@@ -191,7 +179,6 @@ bool Utils::getFirstIpAddr(std::string &local_host) {
ifreq = (struct ifreq *)buf;
ip_num = ifconf.ifc_len / sizeof(struct ifreq);
for (int32_t i = 0; i < ip_num; i++, ifreq++) {
- // exclude ipv6 addr
if (ifreq->ifr_flags != AF_INET) {
continue;
}
@@ -232,7 +219,6 @@ bool Utils::bindCPU(int32_t cpu_id) {
cpu_set_t mask;
if (abs(cpu_id) > cpunum) {
- LOG_ERROR("mask<%d> more than total cpu num<%d>.", cpu_id, cpunum);
return false;
}
@@ -244,109 +230,114 @@ bool Utils::bindCPU(int32_t cpu_id) {
CPU_SET(cpucore, &mask);
if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
- LOG_ERROR("set CPU affinity<%d>/<%d> errno<%d>", cpu_id, cpunum, errno);
+ LOG_ERROR("set CPU affinity" << cpu_id << " cpunum:" << cpunum
+ << " errno: " << errno);
}
return true;
}
-std::string Utils::base64_encode(const std::string &data) {
- size_t in_len = data.size();
- size_t out_len = 4 * ((in_len + 2) / 3);
- std::string ret(out_len, '\0');
- size_t i;
- char *p = const_cast<char *>(ret.c_str());
-
- for (i = 0; i < in_len - 2; i += 3) {
- *p++ = base64_table[(data[i] >> 2) & 0x3F];
- *p++ =
- base64_table[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >>
4)];
- *p++ = base64_table[((data[i + 1] & 0xF) << 2) |
- ((int)(data[i + 2] & 0xC0) >> 6)];
- *p++ = base64_table[data[i + 2] & 0x3F];
+bool Utils::parseHost(const std::string &host, std::string &ip) {
+ bool success = false;
+ struct addrinfo *res = NULL;
+ struct addrinfo hint;
+ char ipStr[17];
+
+ bzero(ipStr, 17);
+ bzero(&hint, sizeof(hint));
+ hint.ai_family = AF_INET;
+ hint.ai_protocol = SOCK_STREAM;
+
+ int32_t ret = getaddrinfo(host.c_str(), NULL, &hint, &res);
+ if (ret) {
+ freeaddrinfo(res);
+ LOG_ERROR("fail to resolve host:" << host);
+ return false;
}
- if (i < in_len) {
- *p++ = base64_table[(data[i] >> 2) & 0x3F];
- if (i == (in_len - 1)) {
- *p++ = base64_table[((data[i] & 0x3) << 4)];
- *p++ = '=';
- } else {
- *p++ = base64_table[((data[i] & 0x3) << 4) |
- ((int)(data[i + 1] & 0xF0) >> 4)];
- *p++ = base64_table[((data[i + 1] & 0xF) << 2)];
+
+ for (struct addrinfo *ptr = res; ptr != NULL; ptr = ptr->ai_next) {
+ struct sockaddr_in *sa = (struct sockaddr_in *)ptr->ai_addr;
+ if (NULL != sa) {
+ inet_ntop(AF_INET, &sa->sin_addr.s_addr, ipStr, sizeof(ipStr));
+ ip = ipStr;
+ success = true;
+ break;
}
- *p++ = '=';
}
- return ret;
+ freeaddrinfo(res);
+ return success;
}
-std::string Utils::genBasicAuthCredential(const std::string &id,
- const std::string &key) {
- std::string credential = id + constants::kBasicAuthJoiner + key;
- std::string result = constants::kBasicAuthPrefix;
- result.append(constants::kBasicAuthSeparator);
- result.append(base64_encode(credential));
- return result;
+bool Utils::getUrlByDNS(const std::string &url, std::string &ipUrl) {
+ std::string host;
+ std::string ip;
+ size_t pos = url.find("://");
+ if (pos == std::string::npos) {
+ return false;
+ }
+
+ size_t sta = pos + 3;
+ size_t end = url.find(":", sta);
+ if (end != std::string::npos) {
+ host = url.substr(sta, end - sta);
+ if (!isalpha(host.at(0))) {
+ ipUrl = url;
+ return true;
+ }
+ if (parseHost(host, ip)) {
+ ipUrl = url;
+ ipUrl.replace(ipUrl.find(host), host.length(), ip);
+ return true;
+ }
+ }
+ return false;
}
-int32_t Utils::requestUrl(std::string &res, const HttpRequest *request) {
- CURL *curl = NULL;
- struct curl_slist *list = NULL;
+int32_t Utils::requestUrl(const std::string &url, std::string &urlByDNS,
+ std::string &res, uint32_t timeout) {
+ if (!getUrlByDNS(url, urlByDNS)) {
+ LOG_ERROR("host resolve error, fail to request url " << url);
+ return SdkCode::kErrorCURL;
+ }
+ CURL *curl = NULL;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (!curl) {
- LOG_ERROR("failed to init curl object");
- return SDKInvalidResult::kErrorCURL;
+ LOG_ERROR("failed to Init curl object");
+ return SdkCode::kErrorCURL;
}
-
- // http header
- list = curl_slist_append(list,
- "Content-Type: application/x-www-form-urlencoded");
- if (request->need_auth && !request->auth_id.empty() &&
- !request->auth_key.empty()) {
- // Authorization: Basic xxxxxxxx
- std::string auth = constants::kBasicAuthHeader;
- auth.append(constants::kBasicAuthSeparator);
- auth.append(genBasicAuthCredential(request->auth_id, request->auth_key));
- LOG_INFO("request manager, auth-header:%s", auth.c_str());
- list = curl_slist_append(list, auth.c_str());
- }
- curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
-
- // set url
- curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
- curl_easy_setopt(curl, CURLOPT_URL, request->url.c_str());
- curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request->post_data.c_str());
- curl_easy_setopt(curl, CURLOPT_TIMEOUT, request->timeout);
-
- // register callback and get res
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
+ curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
+ curl_easy_setopt(curl, CURLOPT_URL, urlByDNS.c_str());
+ curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &Utils::getUrlResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &res);
- curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
- // execute curl request
+ curl_easy_setopt(curl, CURLOPT_FRESH_CONNECT, 1L);
+ curl_easy_setopt(curl, CURLOPT_FORBID_REUSE, 1L);
+
CURLcode ret = curl_easy_perform(curl);
+ LOG_INFO("request from tdm:" << res);
if (ret != 0) {
- LOG_ERROR("%s", curl_easy_strerror(ret));
- LOG_ERROR("failed to request data from %s", request->url.c_str());
+ LOG_ERROR("failed to request data from " << urlByDNS);
if (curl)
curl_easy_cleanup(curl);
curl_global_cleanup();
- return SDKInvalidResult::kErrorCURL;
+ return SdkCode::kErrorCURL;
}
int32_t code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
if (code != 200) {
- LOG_ERROR("tdm responsed with code %d", code);
+ LOG_ERROR("tdm responsed with code " << code);
if (curl)
curl_easy_cleanup(curl);
curl_global_cleanup();
- return SDKInvalidResult::kErrorCURL;
+ return SdkCode::kErrorCURL;
}
if (res.empty()) {
@@ -355,11 +346,11 @@ int32_t Utils::requestUrl(std::string &res, const
HttpRequest *request) {
curl_easy_cleanup(curl);
curl_global_cleanup();
- return SDKInvalidResult::kErrorCURL;
+ return SdkCode::kErrorCURL;
}
- // clean work
- curl_easy_cleanup(curl);
+ if (curl)
+ curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
@@ -376,8 +367,7 @@ size_t Utils::getUrlResponse(void *buffer, size_t size,
size_t count,
bool Utils::readFile(const std::string &file_path, std::string &content) {
std::ifstream f(file_path.c_str());
if (f.fail()) {
- LOG_ERROR("fail to read file:%s, please check file path",
- file_path.c_str());
+ LOG_ERROR("fail to read file:" << file_path << "please check file path");
return false;
}
std::stringstream ss;
@@ -438,5 +428,4 @@ std::string Utils::getVectorStr(std::vector<std::string>
&vs) {
}
return res;
}
-
} // namespace inlong
diff --git a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/utils.h
b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/utils.h
index 5915ae63e1..4ccdfa0aef 100644
--- a/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/utils.h
+++ b/inlong-sdk/dataproxy-sdk-twins/dataproxy-sdk-cpp/src/utils/utils.h
@@ -1,92 +1,74 @@
/**
- * 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.
- */
+* 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 INLONG_SDK_UTILS_H
#define INLONG_SDK_UTILS_H
+#include <snappy.h>
#include <stdint.h>
-#include <string>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
+#include <string>
#include <utility>
#include <vector>
-
-#include "snappy.h"
+#include "atomic.h"
namespace inlong {
-using PAIR = std::pair<std::string, int32_t>;
-struct HttpRequest {
- std::string url;
- uint32_t timeout;
- bool need_auth;
- std::string auth_id;
- std::string auth_key;
- std::string post_data;
-};
-
+extern AtomicUInt g_send_msgid;
+extern AtomicInt user_exit_flag;
class Utils {
-private:
+ private:
static char snowflake_id[35];
static uint16_t sequence;
static uint64_t last_msstamp;
-public:
+ public:
static void taskWaitTime(int32_t sec);
static uint64_t getCurrentMsTime();
static uint64_t getCurrentWsTime();
- static std::string
- getFormatTime(uint64_t date_time); // format time: yyyymmddHHMMSS
- static size_t zipData(const char *input, uint32_t input_len,
- std::string &zip_res); // snappy data
- static char *getSnowflakeId(); // get 64bit snowflakeId
- static bool getFirstIpAddr(std::string &local_host);
- inline static bool isLegalTime(uint64_t report_time) {
+ static std::string getFormatTime(uint64_t date_time);
+ static size_t zipData(const char* input, uint32_t input_len, std::string&
zip_res);
+ static char* getSnowflakeId();
+ static bool getFirstIpAddr(std::string& local_host);
+ inline static bool isLegalTime(uint64_t report_time)
+ {
return ((report_time > 1435101567000LL) && (report_time <
4103101567000LL));
}
static bool bindCPU(int32_t cpu_id);
- static std::string base64_encode(const std::string &data);
- static std::string genBasicAuthCredential(const std::string &id,
- const std::string &key);
- static int32_t requestUrl(std::string &res, const HttpRequest *request);
- static bool readFile(const std::string &file_path,
- std::string &content); // read file content, save as
res,
- // return true is success
- static int32_t splitOperate(const std::string &source,
- std::vector<std::string> &result,
- const std::string &delimiter);
- static std::string getVectorStr(std::vector<std::string> &vs);
+ static int32_t requestUrl(const std::string& url, std::string& urlByDNS,
std::string& res,
+ uint32_t timeout);
+ static bool readFile(const std::string& file_path,
+ std::string& content);
- static bool upValueSort(const PAIR &lhs, const PAIR &rhs) {
- return lhs.second < rhs.second;
- }
- static bool downValueSort(const PAIR &lhs, const PAIR &rhs) {
- return lhs.second > rhs.second;
- }
+ static int32_t splitOperate(const std::string& source,
std::vector<std::string>& result,
+ const std::string& delimiter);
+ static std::string getVectorStr(std::vector<std::string>& vs);
-private:
- static size_t getUrlResponse(void *buffer, size_t size, size_t count,
- void *response);
+ private:
+ static size_t getUrlResponse(void* buffer, size_t size, size_t count, void*
response);
static int64_t waitNextMills(int64_t last_ms);
- static std::string trim(const std::string &source);
+ static std::string trim(const std::string& source);
+ static bool parseHost(const std::string& host, std::string& ip);
+ static bool getUrlByDNS(const std::string& url, std::string& ipUrl);
};
-} // namespace dataproxy_sdk
+} // namespace inlong
-#endif // INLONG_SDK_UTILS_H
\ No newline at end of file
+#endif // INLONG_SDK_UTILS_H
\ No newline at end of file