modify log info.
This commit is contained in:
parent
bc186360fc
commit
24ddd30df1
@ -318,51 +318,51 @@ struct ethtool_value {
|
||||
unsigned int data;
|
||||
};
|
||||
|
||||
extern std::string GBKToUTF8(const std::string& strGBK);
|
||||
extern std::string UTFtoGBK(const char* utf8);
|
||||
extern void hexToAscii(const char* hexStr, char* asciiStr);
|
||||
extern void stringToHex(const char* str, char* hexStr);
|
||||
extern std::string GetLocalTimeWithMs(void);
|
||||
extern void InitGpio(unsigned int gpioN, unsigned int inout);
|
||||
extern int gpio_set(unsigned int gpioN, char x);
|
||||
extern int gpio_read(unsigned int gpioN);
|
||||
extern int config_uart(const char* Port, int speed);
|
||||
extern int write_data(int fd, char* buff, int len);
|
||||
extern int read_data(int fd, char* buff, int len, int timeout);
|
||||
extern int ModifyMac(char* buff);
|
||||
extern void mssleep(unsigned long microseconds);
|
||||
std::string GBKToUTF8(const std::string& strGBK);
|
||||
std::string UTFtoGBK(const char* utf8);
|
||||
void hexToAscii(const char* hexStr, char* asciiStr);
|
||||
void stringToHex(const char* str, char* hexStr);
|
||||
std::string GetLocalTimeWithMs(void);
|
||||
void InitGpio(unsigned int gpioN, unsigned int inout);
|
||||
int gpio_set(unsigned int gpioN, char x);
|
||||
int gpio_read(unsigned int gpioN);
|
||||
int config_uart(const char* Port, int speed);
|
||||
int write_data(int fd, char* buff, int len);
|
||||
int read_data(int fd, char* buff, int len, int timeout);
|
||||
int ModifyMac(char* buff);
|
||||
void mssleep(unsigned long microseconds);
|
||||
|
||||
/**
|
||||
* @brief 获取当前实时时间
|
||||
* @return 时间string 例:2018-11-18 17:16:10
|
||||
*/
|
||||
extern std::string GetCurrentTime();
|
||||
std::string GetCurrentTime();
|
||||
|
||||
extern tm* get_current_date();
|
||||
tm* get_current_date();
|
||||
/**
|
||||
* @brief 和系统交互函数
|
||||
* @param cmd 要发出的系统命令 例:ls
|
||||
* @param buf 系统返回的数据存在buf里
|
||||
* @return -1:失败 0:成功
|
||||
*/
|
||||
extern int system_custom(const char* cmd, char* buf);
|
||||
int system_custom(const char* cmd, char* buf);
|
||||
|
||||
/**
|
||||
* @brief 向日志写入内存信息
|
||||
*/
|
||||
extern void WriteMemoryInfo();
|
||||
void WriteMemoryInfo();
|
||||
|
||||
/**
|
||||
* @brief 获取当前日期
|
||||
* @return 当前日期的string
|
||||
*/
|
||||
extern std::string GetDayDate();
|
||||
std::string GetDayDate();
|
||||
|
||||
/**
|
||||
* @brief 获取当前时间 例 12:00
|
||||
* @return void
|
||||
*/
|
||||
extern void GetTime_(char* time_buff, TIME_SIZE len);
|
||||
void GetTime_(char* time_buff, TIME_SIZE len);
|
||||
|
||||
/**
|
||||
* @brief 获取当前时间戳
|
||||
@ -370,9 +370,9 @@ extern void GetTime_(char* time_buff, TIME_SIZE len);
|
||||
* @param type 0 是毫秒级的时间戳 1 秒级的时间戳
|
||||
* @return void
|
||||
*/
|
||||
extern void GetTimeNet(char* timebuf, int type);
|
||||
void GetTimeNet(char* timebuf, int type);
|
||||
|
||||
extern std::string GetRTC(char* timebuf, int& millisecond);
|
||||
std::string GetRTC(char* timebuf, int& millisecond);
|
||||
|
||||
/**
|
||||
* @brief 从配置文件中读取数据
|
||||
@ -381,7 +381,7 @@ extern std::string GetRTC(char* timebuf, int& millisecond);
|
||||
* @param option 具体配置参数
|
||||
* @return std::string 返回要获取的参数数据
|
||||
*/
|
||||
extern std::string ReadStrByOpt(std::string filename, std::string config, std::string option);
|
||||
std::string ReadStrByOpt(std::string filename, std::string config, std::string option);
|
||||
|
||||
/**
|
||||
* @brief 写配置文件
|
||||
@ -391,19 +391,19 @@ extern std::string ReadStrByOpt(std::string filename, std::string config, std::s
|
||||
* @param value 具体的数据值
|
||||
* @return int 0:配置成功
|
||||
*/
|
||||
extern int WriteStr2Config(std::string filename, std::string config, std::string option, std::string value, bool listable = false);
|
||||
int WriteStr2Config(std::string filename, std::string config, std::string option, std::string value, bool listable = false);
|
||||
|
||||
/**
|
||||
* @brief 获取设备的MAC地址做为设备的唯一标识
|
||||
* @return std::string Mac地址
|
||||
*/
|
||||
extern std::string GetLocalMac(const char* net);
|
||||
std::string GetLocalMac(const char* net);
|
||||
|
||||
/**
|
||||
* @brief 获取设备IP
|
||||
* @return std::string IP地址
|
||||
*/
|
||||
extern std::string IpAddrInit();
|
||||
std::string IpAddrInit();
|
||||
|
||||
/**
|
||||
* @brief 获取正在工作的网卡
|
||||
@ -415,31 +415,31 @@ extern std::string IpAddrInit();
|
||||
* @brief 获取系统基本信息
|
||||
* @return std::string CPU MEM DISK info
|
||||
*/
|
||||
extern std::string GetSysInfo();
|
||||
std::string GetSysInfo();
|
||||
|
||||
/**
|
||||
* @brief 去掉字符串所有空格
|
||||
* @return std::string
|
||||
*/
|
||||
extern std::string& ClearAllSpace(std::string& str);
|
||||
std::string& ClearAllSpace(std::string& str);
|
||||
|
||||
/**
|
||||
* @brief 调用接口写入data数据
|
||||
* @return void
|
||||
*/
|
||||
extern void StartWriteToDat();
|
||||
void StartWriteToDat();
|
||||
|
||||
/**
|
||||
* @brief 循环检测文件
|
||||
* @return void
|
||||
*/
|
||||
extern void BackupDatFile();
|
||||
void BackupDatFile();
|
||||
|
||||
/**
|
||||
* @brief 设置系统时间
|
||||
* @return int
|
||||
*/
|
||||
extern int SetTime(unsigned long seconds, int milliseconds = 0);
|
||||
int SetTime(unsigned long seconds, int milliseconds = 0);
|
||||
|
||||
/**
|
||||
* @brief 获取文件内容
|
||||
@ -447,45 +447,45 @@ extern int SetTime(unsigned long seconds, int milliseconds = 0);
|
||||
* @param line 第几行
|
||||
* @return std::string
|
||||
*/
|
||||
extern std::string GetFileContent(std::string filename, int line);
|
||||
std::string GetFileContent(std::string filename, int line);
|
||||
|
||||
/**
|
||||
* @brief 配置时区
|
||||
* @param zoneid 时区ID
|
||||
* @return void
|
||||
*/
|
||||
extern void ZoneConfig(std::string zoneid);
|
||||
void ZoneConfig(std::string zoneid);
|
||||
|
||||
/**
|
||||
* @brief 获取系统基本信息
|
||||
* @return std::string CPU MEM DISK info
|
||||
*/
|
||||
extern std::string GetSysStatus();
|
||||
std::string GetSysStatus();
|
||||
double GetHardDiskFree();
|
||||
extern bool CheckIP(const char* ip);
|
||||
bool CheckIP(const char* ip);
|
||||
|
||||
bool IsValidMask(std::string mask);
|
||||
// read update config file
|
||||
extern std::vector<DataNodeUpdate> ReadStrUpdate(std::string filename);
|
||||
std::vector<DataNodeUpdate> ReadStrUpdate(std::string filename);
|
||||
|
||||
extern void ReadStrConfig(std::string filename);
|
||||
extern void ImportConfig(std::string filename);
|
||||
extern int UpdataDataNodeConfig(std::string filename);
|
||||
extern char* solve(char* dest, const char* src);
|
||||
extern void swap(char* data);
|
||||
extern int hexStringToBytes(const char* hexStr, unsigned char* bytes, size_t bytesSize);
|
||||
void ReadStrConfig(std::string filename);
|
||||
void ImportConfig(std::string filename);
|
||||
int UpdataDataNodeConfig(std::string filename);
|
||||
char* solve(char* dest, const char* src);
|
||||
void swap(char* data);
|
||||
int hexStringToBytes(const char* hexStr, unsigned char* bytes, size_t bytesSize);
|
||||
|
||||
int OpenWatchDog();
|
||||
int WriteWatchDog(int fd);
|
||||
|
||||
//获取4G信号强度
|
||||
extern int getcsq();
|
||||
extern std::string GetGwIp_(const char* eth_name);
|
||||
extern std::string GetOneContent(const char* szData, int nRow, const char* szSeparate);
|
||||
extern int readStringValue(const char* section, char* key, char* val, const char* file);
|
||||
extern int writeStringVlaue(const char* section, char* key, char* val, const char* file);
|
||||
extern int readIntValue(const char* section, char* key, const char* file);
|
||||
extern int writeIntValue(const char* section, char* key, int val, const char* file);
|
||||
int getcsq();
|
||||
std::string GetGwIp_(const char* eth_name);
|
||||
std::string GetOneContent(const char* szData, int nRow, const char* szSeparate);
|
||||
int readStringValue(const char* section, char* key, char* val, const char* file);
|
||||
int writeStringVlaue(const char* section, char* key, char* val, const char* file);
|
||||
int readIntValue(const char* section, char* key, const char* file);
|
||||
int writeIntValue(const char* section, char* key, int val, const char* file);
|
||||
|
||||
int getDiskInfo(char* diskTotal, char* diskFree);
|
||||
|
||||
@ -493,9 +493,9 @@ unsigned short cal_chksum(unsigned short* addr, int len);
|
||||
|
||||
int socketHeart(const char* pSendData);
|
||||
|
||||
extern int Ping(const char* ips, int timeout);
|
||||
extern int get_netlink_status(const char* if_name);
|
||||
extern int compareVersions(const std::string& version1, const std::string& version2);
|
||||
extern void Binary_Bit(unsigned char* p_data, unsigned char position, int flag);
|
||||
int Ping(const char* ips, int timeout);
|
||||
int get_netlink_status(const char* if_name);
|
||||
int compareVersions(const std::string& version1, const std::string& version2);
|
||||
void Binary_Bit(unsigned char* p_data, unsigned char position, int flag);
|
||||
|
||||
#endif // COMMON_FUNC_HPP_
|
||||
|
||||
@ -1,8 +1,12 @@
|
||||
#include "data_trans.hpp"
|
||||
#include "dirent.h"
|
||||
#include <stdio.h>
|
||||
#include <zlog.h>
|
||||
|
||||
DataTrans::DataTrans() : m_bDebug(false) {}
|
||||
extern zlog_category_t *zct;
|
||||
extern zlog_category_t *zbt;
|
||||
|
||||
DataTrans::DataTrans() : debug_(false) {}
|
||||
|
||||
DataTrans::~DataTrans() {}
|
||||
|
||||
@ -10,13 +14,13 @@ static int OnDebug(CURL *, curl_infotype itype, char *pData, size_t size, void *
|
||||
if (itype == CURLINFO_TEXT) {
|
||||
// printf("[TEXT]%s\n", pData);
|
||||
} else if (itype == CURLINFO_HEADER_IN) {
|
||||
print_info("[HEADER_IN]%s\n", pData);
|
||||
zlog_debug(zbt, "[HEADER_IN]%s", pData);
|
||||
} else if (itype == CURLINFO_HEADER_OUT) {
|
||||
print_info("[HEADER_OUT]%s\n", pData);
|
||||
zlog_debug(zbt, "[HEADER_OUT]%s", pData);
|
||||
} else if (itype == CURLINFO_DATA_IN) {
|
||||
print_info("[DATA_IN]%s\n", pData);
|
||||
zlog_debug(zbt, "[DATA_IN]%s", pData);
|
||||
} else if (itype == CURLINFO_DATA_OUT) {
|
||||
print_info("[DATA_OUT]%s\n", pData);
|
||||
zlog_debug(zbt, "[DATA_OUT]%s", pData);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -24,6 +28,7 @@ static int OnDebug(CURL *, curl_infotype itype, char *pData, size_t size, void *
|
||||
static size_t OnWriteData(void *buffer, size_t size, size_t nmemb, void *lpVoid) {
|
||||
std::string *str = dynamic_cast<std::string *>((std::string *)lpVoid);
|
||||
if (NULL == str || NULL == buffer) {
|
||||
zlog_error(zct, "[OnWriteData] str:%p, buffer:%p", str, buffer);
|
||||
return -1;
|
||||
}
|
||||
char *pData = (char *)buffer;
|
||||
@ -32,10 +37,10 @@ static size_t OnWriteData(void *buffer, size_t size, size_t nmemb, void *lpVoid)
|
||||
}
|
||||
|
||||
static size_t my_fwrite(void *buffer, size_t size, size_t nmemb, void *stream) {
|
||||
printf("size = %d\n", size);
|
||||
zlog_debug(zct, "size = %d", size);
|
||||
struct DownloadFile *out = (struct DownloadFile *)stream;
|
||||
if (out && !out->stream) {
|
||||
out->stream = fopen(out->filename, "wb"); //打开文件进行写入
|
||||
out->stream = fopen(out->filename, "wb");
|
||||
if (!out->stream) return -1;
|
||||
}
|
||||
return fwrite(buffer, size, nmemb, out->stream);
|
||||
@ -71,16 +76,15 @@ int DataTrans::download(char *pFilename, std::string &strUrl, std::string &strRe
|
||||
//释放curl对象
|
||||
curl_easy_cleanup(curl);
|
||||
if (res != CURLE_OK) {
|
||||
std::cout << stderr << res << std::endl;
|
||||
zlog_error(zct, "curl_easy_perform ret:%d", res);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
printf("strResponse = %s\n", strResponse.c_str());
|
||||
zlog_debug(zct, "strResponse = %s", strResponse.c_str());
|
||||
if (bDownload) {
|
||||
if (dlfile.stream) {
|
||||
fclose(dlfile.stream);
|
||||
}
|
||||
} else {
|
||||
}
|
||||
|
||||
curl_global_cleanup();
|
||||
@ -98,47 +102,53 @@ int DataTrans::dl_curl_post_req(const std::string &url, const std::string &postP
|
||||
CURLcode res;
|
||||
res = curl_global_init(CURL_GLOBAL_ALL);
|
||||
if (CURLE_OK != res) {
|
||||
printf("init libcurl failed.");
|
||||
zlog_error(zct, "init libcurl failed.");
|
||||
curl_global_cleanup();
|
||||
return -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
curl = curl_easy_init();
|
||||
if (curl) {
|
||||
fp = fopen(filename.c_str(), "wb");
|
||||
res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
if (res != CURLE_OK) {
|
||||
fclose(fp);
|
||||
curl_easy_cleanup(curl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writedata2file);
|
||||
if (res != CURLE_OK) {
|
||||
fclose(fp);
|
||||
curl_easy_cleanup(curl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
|
||||
if (res != CURLE_OK) {
|
||||
fclose(fp);
|
||||
curl_easy_cleanup(curl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
|
||||
fclose(fp);
|
||||
|
||||
if (res != CURLE_OK) {
|
||||
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
|
||||
curl_easy_cleanup(curl);
|
||||
return -1;
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
if (curl == NULL) {
|
||||
zlog_error(zct, "curl_easy_init failed.");
|
||||
curl_global_cleanup();
|
||||
return 2;
|
||||
}
|
||||
|
||||
fp = fopen(filename.c_str(), "wb");
|
||||
res = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
|
||||
if (res != CURLE_OK) {
|
||||
fclose(fp);
|
||||
curl_easy_cleanup(curl);
|
||||
curl_global_cleanup();
|
||||
return 3;
|
||||
}
|
||||
|
||||
res = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writedata2file);
|
||||
if (res != CURLE_OK) {
|
||||
fclose(fp);
|
||||
curl_easy_cleanup(curl);
|
||||
curl_global_cleanup();
|
||||
return 4;
|
||||
}
|
||||
|
||||
res = curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
|
||||
if (res != CURLE_OK) {
|
||||
fclose(fp);
|
||||
curl_easy_cleanup(curl);
|
||||
curl_global_cleanup();
|
||||
return 5;
|
||||
}
|
||||
|
||||
res = curl_easy_perform(curl);
|
||||
fclose(fp);
|
||||
if (res != CURLE_OK) {
|
||||
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
|
||||
curl_easy_cleanup(curl);
|
||||
curl_global_cleanup();
|
||||
return 6;
|
||||
}
|
||||
|
||||
curl_easy_cleanup(curl);
|
||||
curl_global_cleanup();
|
||||
return 0;
|
||||
}
|
||||
@ -147,9 +157,10 @@ int DataTrans::Post(const std::string &strUrl, const std::string &strPost, std::
|
||||
CURLcode res;
|
||||
CURL *curl = curl_easy_init();
|
||||
if (NULL == curl) {
|
||||
zlog_error(zct, "curl_easy_init failed.");
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
if (m_bDebug) {
|
||||
if (debug_) {
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
||||
}
|
||||
@ -171,9 +182,10 @@ int DataTrans::Get(const std::string &strUrl, std::string &strResponse) {
|
||||
CURLcode res;
|
||||
CURL *curl = curl_easy_init();
|
||||
if (NULL == curl) {
|
||||
zlog_error(zct, "curl_easy_init failed.");
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
if (m_bDebug) {
|
||||
if (debug_) {
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
||||
}
|
||||
@ -198,10 +210,11 @@ int DataTrans::Posts(const std::string &strUrl, const std::string &strPost, std:
|
||||
CURLcode res;
|
||||
CURL *curl = curl_easy_init();
|
||||
if (NULL == curl) {
|
||||
zlog_error(zct, "curl_easy_init failed.");
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
|
||||
if (m_bDebug) {
|
||||
if (debug_) {
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
||||
}
|
||||
@ -232,9 +245,10 @@ int DataTrans::Gets(const std::string &strUrl, std::string &strResponse, const c
|
||||
CURLcode res;
|
||||
CURL *curl = curl_easy_init();
|
||||
if (NULL == curl) {
|
||||
zlog_error(zct, "curl_easy_init failed.");
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
if (m_bDebug) {
|
||||
if (debug_) {
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
||||
curl_easy_setopt(curl, CURLOPT_DEBUGFUNCTION, OnDebug);
|
||||
}
|
||||
@ -275,155 +289,79 @@ int DataTrans::upload_file(const std::string &strUrl, const std::string &filenam
|
||||
curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "submit", CURLFORM_COPYCONTENTS, "upload", CURLFORM_END);
|
||||
|
||||
curl = curl_easy_init();
|
||||
if (NULL == curl) {
|
||||
zlog_error(zct, "curl_easy_init failed.");
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
multi_handle = curl_multi_init();
|
||||
if (NULL == multi_handle) {
|
||||
zlog_error(zct, "curl_multi_init failed.");
|
||||
curl_easy_cleanup(curl);
|
||||
return CURLE_FAILED_INIT;
|
||||
}
|
||||
/* initalize custom header list (stating that Expect: 100-continue is not wanted */
|
||||
headerlist = curl_slist_append(headerlist, buf);
|
||||
if (curl && multi_handle) {
|
||||
/* what URL that receives this POST */
|
||||
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, res);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
||||
curl_multi_add_handle(multi_handle, curl);
|
||||
curl_multi_perform(multi_handle, &still_running);
|
||||
do {
|
||||
struct timeval timeout;
|
||||
int rc;
|
||||
fd_set fdread;
|
||||
fd_set fdwrite;
|
||||
fd_set fdexcep;
|
||||
int maxfd = -1;
|
||||
long curl_timeo = -1;
|
||||
FD_ZERO(&fdread);
|
||||
FD_ZERO(&fdwrite);
|
||||
FD_ZERO(&fdexcep);
|
||||
/* set a suitable timeout to play around with */
|
||||
timeout.tv_sec = 1;
|
||||
timeout.tv_usec = 0;
|
||||
curl_multi_timeout(multi_handle, &curl_timeo);
|
||||
if (curl_timeo >= 0) {
|
||||
timeout.tv_sec = curl_timeo / 1000;
|
||||
if (timeout.tv_sec > 1)
|
||||
timeout.tv_sec = 1;
|
||||
else
|
||||
timeout.tv_usec = (curl_timeo % 1000) * 1000;
|
||||
}
|
||||
|
||||
/* get file descriptors from the transfers */
|
||||
curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
|
||||
/* In a real-world program you OF COURSE check the return code of the
|
||||
function calls. On success, the value of maxfd is guaranteed to be
|
||||
greater or equal than -1. We call select(maxfd + 1, ...), specially in
|
||||
case of (maxfd == -1), we call select(0, ...), which is basically equal
|
||||
to sleep. */
|
||||
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
|
||||
switch (rc) {
|
||||
case -1: break;
|
||||
case 0:
|
||||
default:
|
||||
/* timeout or readable/writable sockets */
|
||||
print_info("perform!\n");
|
||||
curl_multi_perform(multi_handle, &still_running);
|
||||
print_info("running: %d!\n", still_running);
|
||||
break;
|
||||
}
|
||||
} while (still_running);
|
||||
curl_multi_cleanup(multi_handle);
|
||||
/* always cleanup */
|
||||
curl_easy_cleanup(curl);
|
||||
/* then cleanup the formpost chain */
|
||||
curl_formfree(formpost);
|
||||
/* free slist */
|
||||
curl_slist_free_all(headerlist);
|
||||
}
|
||||
/* what URL that receives this POST */
|
||||
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
|
||||
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, res);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
|
||||
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
|
||||
curl_multi_add_handle(multi_handle, curl);
|
||||
curl_multi_perform(multi_handle, &still_running);
|
||||
do {
|
||||
struct timeval timeout;
|
||||
int rc;
|
||||
fd_set fdread;
|
||||
fd_set fdwrite;
|
||||
fd_set fdexcep;
|
||||
int maxfd = -1;
|
||||
long curl_timeo = -1;
|
||||
FD_ZERO(&fdread);
|
||||
FD_ZERO(&fdwrite);
|
||||
FD_ZERO(&fdexcep);
|
||||
/* set a suitable timeout to play around with */
|
||||
timeout.tv_sec = 1;
|
||||
timeout.tv_usec = 0;
|
||||
curl_multi_timeout(multi_handle, &curl_timeo);
|
||||
if (curl_timeo >= 0) {
|
||||
timeout.tv_sec = curl_timeo / 1000;
|
||||
if (timeout.tv_sec > 1)
|
||||
timeout.tv_sec = 1;
|
||||
else
|
||||
timeout.tv_usec = (curl_timeo % 1000) * 1000;
|
||||
}
|
||||
|
||||
/* get file descriptors from the transfers */
|
||||
curl_multi_fdset(multi_handle, &fdread, &fdwrite, &fdexcep, &maxfd);
|
||||
/* In a real-world program you OF COURSE check the return code of the
|
||||
function calls. On success, the value of maxfd is guaranteed to be
|
||||
greater or equal than -1. We call select(maxfd + 1, ...), specially in
|
||||
case of (maxfd == -1), we call select(0, ...), which is basically equal
|
||||
to sleep. */
|
||||
rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
|
||||
switch (rc) {
|
||||
case -1: break;
|
||||
case 0:
|
||||
default:
|
||||
/* timeout or readable/writable sockets */
|
||||
zlog_info(zct, "perform!");
|
||||
curl_multi_perform(multi_handle, &still_running);
|
||||
zlog_info(zct, "running: %d!", still_running);
|
||||
break;
|
||||
}
|
||||
} while (still_running);
|
||||
|
||||
curl_multi_cleanup(multi_handle);
|
||||
/* always cleanup */
|
||||
curl_easy_cleanup(curl);
|
||||
/* then cleanup the formpost chain */
|
||||
curl_formfree(formpost);
|
||||
/* free slist */
|
||||
curl_slist_free_all(headerlist);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DataTrans::Send_file_socket(const std::string &filename) { return 0; }
|
||||
|
||||
int DataTrans::Send_Dir_socket(const char *dirname) {
|
||||
DIR *dirp;
|
||||
std::vector<std::string> v;
|
||||
std::string temp;
|
||||
struct dirent *dp;
|
||||
dirp = opendir(dirname);
|
||||
while ((dp = readdir(dirp)) != NULL) {
|
||||
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue;
|
||||
v.push_back(std::string(dp->d_name));
|
||||
}
|
||||
|
||||
int socketfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
struct sockaddr_in s_add, c_add;
|
||||
unsigned short portnum = 12345;
|
||||
int len;
|
||||
char buffer[BUFFER_SIZE];
|
||||
FILE *fp;
|
||||
int file_block_length = 0;
|
||||
if (socketfd < 0) {
|
||||
print_error("Socket create error! \n");
|
||||
return -1;
|
||||
} else {
|
||||
print_info("Socket create success!\n");
|
||||
}
|
||||
/*
|
||||
*设置地址
|
||||
*/
|
||||
|
||||
bzero(&s_add, sizeof(struct sockaddr_in));
|
||||
s_add.sin_family = AF_INET;
|
||||
s_add.sin_addr.s_addr = inet_addr(GlobalConfig::ServerIP.c_str());
|
||||
s_add.sin_port = htons(PORT);
|
||||
|
||||
if (connect(socketfd, (struct sockaddr *)(&s_add), sizeof(struct sockaddr)) < 0) {
|
||||
print_error("Connect failure!\n");
|
||||
return -1;
|
||||
} else {
|
||||
print_info("Connect Success!\n");
|
||||
}
|
||||
std::vector<std::string>::iterator iter;
|
||||
for (iter = v.begin(); iter != v.end(); ++iter) {
|
||||
temp = *iter;
|
||||
std::cout << temp.c_str() << std::endl;
|
||||
int file_name_length = (int)strlen(temp.c_str());
|
||||
if (send(socketfd, (char *)&file_name_length, sizeof(int), 0) < 0) { //发送文件名长度
|
||||
print_error("Send File_Name_Length Failed!\n");
|
||||
} else {
|
||||
print_info("Send File_Name_Length Success!\n");
|
||||
}
|
||||
//发送文件名
|
||||
if (send(socketfd, temp.c_str(), file_name_length, 0) < 0) {
|
||||
print_error("Send File_Name Failed!\n");
|
||||
} else {
|
||||
print_info("Send File_Name Success!\n");
|
||||
}
|
||||
/*
|
||||
*发送文件
|
||||
*/
|
||||
std::string filename = std::string(dirname + temp);
|
||||
fp = fopen(filename.c_str(), "r");
|
||||
if (fp == NULL) {
|
||||
print_error("File: %s Not Found!\n", filename.c_str());
|
||||
} else {
|
||||
bzero(buffer, BUFFER_SIZE);
|
||||
while ((file_block_length = fread(buffer, sizeof(char), BUFFER_SIZE, fp)) > 0) {
|
||||
print_info("file_block_length = %d\n", file_block_length);
|
||||
/*
|
||||
*把数据写入buffer
|
||||
*/
|
||||
if (send(socketfd, buffer, file_block_length, 0) < 0) {
|
||||
print_error("Send File:%s Failed!\n", filename.c_str());
|
||||
break;
|
||||
}
|
||||
bzero(buffer, sizeof(buffer));
|
||||
}
|
||||
fclose(fp);
|
||||
print_info("File: %s Transfer Finished!\n", filename.c_str());
|
||||
}
|
||||
close(socketfd);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void DataTrans::SetDebug(bool bDebug) { m_bDebug = bDebug; }
|
||||
void DataTrans::SetDebug(bool bDebug) { debug_ = bDebug; }
|
||||
|
||||
@ -84,28 +84,13 @@ public:
|
||||
|
||||
// int download_file(const std::string &strUrl, const std::string &filename, std::string &strResponse);
|
||||
|
||||
/**
|
||||
* @brief 通过socket发送文件
|
||||
* @param filename 文件名
|
||||
* @return 函数返回0执行成功
|
||||
*/
|
||||
int Send_file_socket(const std::string &filename);
|
||||
|
||||
/**
|
||||
* @brief 通过socket发送文件夹下所有文件
|
||||
* @param dirname 文件名
|
||||
* @return 函数返回0执行成功
|
||||
*/
|
||||
int Send_Dir_socket(const char *dirname);
|
||||
|
||||
int download(char *pFilename, std::string &strUrl, std::string &strResponse, bool bDownload);
|
||||
int dl_curl_post_req(const std::string &url, const std::string &postParams, std::string &filename);
|
||||
|
||||
public:
|
||||
void SetDebug(bool bDebug);
|
||||
|
||||
private:
|
||||
bool m_bDebug;
|
||||
bool debug_;
|
||||
};
|
||||
|
||||
typedef boost::container::dtl::singleton_default<DataTrans> data_trans;
|
||||
|
||||
@ -1,14 +1,11 @@
|
||||
#include "sql_db.hpp"
|
||||
#include <unistd.h>
|
||||
#include <zlog.h>
|
||||
#include "common/global.hpp"
|
||||
#include "utility/calculation.hpp"
|
||||
|
||||
bool SqlSwitch() {
|
||||
if (access("./sql", 0) >= 0) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
extern zlog_category_t *zct;
|
||||
extern zlog_category_t *zbt;
|
||||
|
||||
SqliteDB::SqliteDB() {}
|
||||
|
||||
@ -131,6 +128,7 @@ void SqliteDB::SqliteInitDel(const char *pDbName) {
|
||||
Deletetable(sztableName);
|
||||
}
|
||||
}
|
||||
|
||||
void SqliteDB::Deletetable(const char *ptableName) {
|
||||
char sql_exec[2048];
|
||||
//创建传感器数据存储表
|
||||
@ -141,6 +139,7 @@ void SqliteDB::Deletetable(const char *ptableName) {
|
||||
perror_info("sqlite3_exec");
|
||||
}
|
||||
}
|
||||
|
||||
void SqliteDB::CreatedataStatictable(const char *ptableName) {
|
||||
char sql_exec[2048];
|
||||
//创建传感器数据存储表
|
||||
@ -149,11 +148,11 @@ void SqliteDB::CreatedataStatictable(const char *ptableName) {
|
||||
T_DATASTATIC_INFO(VOLTAGE), "zigbeeSignal", "StaticIndex", T_DATASTATIC_INFO(TIMESTAMP), "sendMsg", "nodeResend", "zigbeeSignalNode", "statisticType", "timing");
|
||||
CreateTable(sql_exec, 0);
|
||||
memset(sql_exec, 0, 2048);
|
||||
sprintf(sql_exec, "CREATE INDEX %s_1 \
|
||||
ON %s (%s)",
|
||||
sprintf(sql_exec, "CREATE INDEX %s_1 ON %s (%s)",
|
||||
ptableName, ptableName, T_DATA_INFO(DATANODENO));
|
||||
CreateTable(sql_exec, 0);
|
||||
}
|
||||
|
||||
bool SqliteDB::OpenDB(const char *pDbName, bool isDB2) {
|
||||
if (isDB2 == false) {
|
||||
int ret = 0;
|
||||
@ -165,7 +164,7 @@ bool SqliteDB::OpenDB(const char *pDbName, bool isDB2) {
|
||||
sqlite3_exec(mDBAcess, "PRAGMA auto_vacuum = 1;", 0, 0, 0);
|
||||
print_info("Success To Open DataBase!\n");
|
||||
} else {
|
||||
print_error("Fail To Open DataBase!\n");
|
||||
zlog_error(zbt, "DB2 false, Fail To Open DataBase:%s, ret:%d", pDbName, ret);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
@ -178,7 +177,7 @@ bool SqliteDB::OpenDB(const char *pDbName, bool isDB2) {
|
||||
sqlite3_exec(mDb2, "PRAGMA auto_vacuum = 1;", 0, 0, 0);
|
||||
print_info("Success To Open DataBase!\n");
|
||||
} else {
|
||||
print_error("Fail To Open DataBase!\n");
|
||||
zlog_error(zbt, "DB2 true, Fail To Open DataBase:%s, ret:%d", pDbName, ret);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -197,15 +196,15 @@ sqlite3 *SqliteDB::GetDbHandle(bool isDB2) {
|
||||
|
||||
int SqliteDB::CreateTable(const char *sql, bool isDB2) {
|
||||
print_light_green("%s\n", sql);
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", sql);
|
||||
char *msg;
|
||||
int iRet = sqlite3_exec(GetDbHandle(isDB2), sql, 0, 0, &msg);
|
||||
if (iRet != SQLITE_OK) {
|
||||
print_error("sqlite3 error: code=%d msg=%s\n", iRet, msg);
|
||||
zlog_error(zbt, "[CreateTable] sqlite3 error: code=%d msg=%s sql=[%s]\n", iRet, msg, sql);
|
||||
sqlite3_free(msg);
|
||||
}
|
||||
return iRet;
|
||||
}
|
||||
|
||||
int SqliteDB::ExeSql(const char *sql, bool isDB2) {
|
||||
print_light_green("%s\n", sql);
|
||||
char *msg;
|
||||
@ -216,13 +215,13 @@ int SqliteDB::ExeSql(const char *sql, bool isDB2) {
|
||||
}
|
||||
return iRet;
|
||||
}
|
||||
|
||||
int SqliteDB::GetTableColumnCount(const char *tablename, bool isDB2) {
|
||||
std::string strSql = "select * from ";
|
||||
int count = 0;
|
||||
sqlite3_stmt *stmt;
|
||||
strSql = strSql + tablename + ";";
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
sqlite3_prepare_v2(GetDbHandle(isDB2), strSql.c_str(), -1, &stmt, 0);
|
||||
count = sqlite3_column_count(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
@ -238,7 +237,6 @@ int SqliteDB::GetTableRows(const char *tablename, const char *whereCon) {
|
||||
strSql = strSql + tablename + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||||
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
||||
@ -259,7 +257,6 @@ int SqliteDB::AlterTable(const char *tablename, const char *column, bool isAdd,
|
||||
std::string strSql = "alter table ";
|
||||
strSql = strSql + tablename + " add " + column + ";";
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, NULL);
|
||||
if (iRet != SQLITE_OK) {
|
||||
perror_info("sqlite3_exec");
|
||||
@ -278,7 +275,6 @@ vec_t SqliteDB::GetDataSingleLine(const char *tablename, const char *column, con
|
||||
strSql = strSql + column + " from " + tablename + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||||
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
||||
@ -307,7 +303,6 @@ char *SqliteDB::GetDataChar(const char *tablename, const char *column, const cha
|
||||
strSql = strSql + column + " from " + tablename + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
sqlite3_stmt *stmt;
|
||||
g_tDbMutex.Lock();
|
||||
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||||
@ -335,7 +330,6 @@ std::string SqliteDB::GetData(const char *tablename, const char *column, const c
|
||||
strSql = strSql + column + " from " + tablename + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
sqlite3_stmt *stmt;
|
||||
g_tDbMutex.Lock();
|
||||
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||||
@ -364,7 +358,6 @@ array_t SqliteDB::GetDataMultiLine(const char *tablename, const char *column, co
|
||||
}
|
||||
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
sqlite3_stmt *stmt;
|
||||
g_tDbMutex.Lock();
|
||||
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||||
@ -399,7 +392,6 @@ array_t SqliteDB::GetDataMultiLineTransaction(const char *tablename, const char
|
||||
strSql = strSql + column + " from " + tablename + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
sqlite3_exec(mDBAcess, "BEGIN", 0, 0, NULL);
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||||
@ -435,7 +427,6 @@ vec_t SqliteDB::GetDataMultiLineOfOneColumn(const char *tablename, const char *c
|
||||
strSql = strSql + column + " from " + tablename + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||||
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
||||
@ -462,7 +453,6 @@ vec_Value SqliteDB::GetDataMultiLineOfOneColumnDouble(const char *tablename, con
|
||||
strSql = strSql + column + " from " + tablename + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
sqlite3_stmt *stmt;
|
||||
if (sqlite3_prepare_v2(mDBAcess, strSql.c_str(), -1, &stmt, 0) != SQLITE_OK) {
|
||||
print_error("sqlite3_prepare_v2:%s\n", sqlite3_errmsg(mDBAcess));
|
||||
@ -488,7 +478,6 @@ int SqliteDB::DeleteTableData(const char *tablename, const char *whereCond, bool
|
||||
}
|
||||
// LOG_INFO("strSql = %s",strSql.c_str());
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
char *msg;
|
||||
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
||||
|
||||
@ -507,7 +496,6 @@ int SqliteDB::DeleteTableDataOneConditon(const char *tablename, const char *cond
|
||||
strSql = strSql + tablename + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
char *msg;
|
||||
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
||||
|
||||
@ -534,7 +522,6 @@ int SqliteDB::UpdateNodeNameData(const char *tablename, const char *updateColumn
|
||||
strSql = strSql + tablename + " set " + updateColumn + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
char *msg;
|
||||
int iRet = sqlite3_exec(GetDbHandle(isDB2), szSql, 0, 0, &msg);
|
||||
|
||||
@ -553,7 +540,6 @@ int SqliteDB::UpdateTableData(const char *tablename, const char *updateColumn, c
|
||||
strSql = strSql + tablename + " set " + updateColumn + ";";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
char *msg;
|
||||
g_tDbMutex.Lock();
|
||||
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
||||
@ -568,7 +554,6 @@ int SqliteDB::UpdateTableData(const char *tablename, const char *updateColumn, c
|
||||
|
||||
int SqliteDB::UpdateTableData(const char *directSql, bool isDB2) {
|
||||
print_light_green("%s\n", directSql);
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", directSql);
|
||||
char *msg;
|
||||
int iRet = sqlite3_exec(GetDbHandle(isDB2), directSql, 0, 0, &msg);
|
||||
if (iRet != SQLITE_OK) {
|
||||
@ -586,7 +571,6 @@ int SqliteDB::UpdateTableDataOneColumn(const char *tablename, const char *column
|
||||
strSql = strSql + tablename + " set " + columnName + "='" + columnValue + "';";
|
||||
}
|
||||
print_light_green("%s\n", strSql.c_str());
|
||||
// if (SqlSwitch()) log_system->log_write_system("[info]","%s\n", strSql.c_str());
|
||||
char *msg;
|
||||
int iRet = sqlite3_exec(GetDbHandle(isDB2), strSql.c_str(), 0, 0, &msg);
|
||||
if (iRet != SQLITE_OK) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user