add:更新列表任务功能初次代码提交。

This commit is contained in:
taynpg 2024-12-20 16:57:44 +08:00
parent d1d918c0a5
commit 1ab3eee884
4 changed files with 110 additions and 3 deletions

View File

@ -130,6 +130,7 @@
"cwctype": "cpp", "cwctype": "cpp",
"memory_resource": "cpp", "memory_resource": "cpp",
"random": "cpp", "random": "cpp",
"cinttypes": "cpp" "cinttypes": "cpp",
"regex": "cpp"
} }
} }

View File

@ -1,9 +1,11 @@
#include "client.h" #include "client.h"
#include <filesystem> #include <filesystem>
#include <fstream>
#include <iostream> #include <iostream>
#include <of_path.h> #include <of_path.h>
#include <of_str.h> #include <of_str.h>
#include <of_util.h> #include <of_util.h>
#include <regex>
namespace fs = std::filesystem; namespace fs = std::filesystem;
CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger) CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger)
@ -262,6 +264,63 @@ void CClient::report_trans_ret(TransState state, const std::string& key)
t->cur_remote_id_.clear(); t->cur_remote_id_.clear();
} }
/* 清单文件,内容格式为:
D:/a.txt|/home/zhangsan/
C:/Dijava|/home/zhangsan/dia
*/
bool CClient::request_update_list(const std::string& list_file)
{
// 读取list文件
std::ifstream in(list_file);
if (!in.is_open()) {
logger_->error("Can't Open File:{}", list_file);
return false;
}
std::istreambuf_iterator<char> iterf(in);
std::istreambuf_iterator<char> iter;
std::string content(iterf, iter);
in.close();
std::regex rg(R"()");
// 校验格式是否正确
auto vec = COfStr::split(content, "\n");
bool valid = false;
std::string handled_content;
for (const auto& item : vec) {
std::string hitem = COfStr::trim(item);
if (std::regex_match(hitem, rg)) {
handled_content.append(handled_content + "\n");
continue;
}
valid = false;
break;
}
if (!valid) {
logger_->error("Judge List File {} Format Not Passed.", list_file);
return false;
}
list_file_ = list_file;
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
buf->type_ = TYPE_REQUEST_UPDATE_LIST;
buf->data_ = new char[handled_content.size() + 1]();
buf->len_ = std::snprintf(buf->data_, handled_content.size() + 1, "%s", handled_content.c_str());
return false;
}
bool CClient::check_update_list(const std::string& content, std::map<std::string, std::string>& files)
{
return false;
}
bool CClient::down_update_file(std::map<std::string, std::string>& files)
{
return false;
}
bool CClient::send_frame(CFrameBuffer* buf) bool CClient::send_frame(CFrameBuffer* buf)
{ {
char* out_buf{}; char* out_buf{};
@ -383,6 +442,40 @@ void CClient::handle_frame(CFrameBuffer* buf)
report_trans_ret(TRANS_FAILED); report_trans_ret(TRANS_FAILED);
break; break;
} }
case TYPE_REQUEST_UPDATE_LIST: {
std::string content(buf->data_, buf->len_);
std::map<std::string, std::string> files;
if (check_update_list(content, files)) {
update_list_content_ = content;
buf->type_ = TYPE_CONFIRM_UPDATE_LIST;
} else {
buf->type_ = TYPE_UNCONFIRM_UPDATE_LIST;
}
std::swap(buf->tid_, buf->fid_);
if (!send_frame(buf)) {
logger_->error("Send Failed {}.", __LINE__);
break;
}
list_serve_id_ = buf->fid_;
break;
}
case TYPE_CONFIRM_UPDATE_LIST: {
logger_->info("remote {} check {} passed!", buf->fid_, list_file_);
break;
}
case TYPE_UNCONFIRM_UPDATE_LIST: {
logger_->error("remote {} check {} not passed!", buf->fid_, list_file_);
break;
}
case TYPE_DONE_UPDATE_LIST: {
logger_->info("remote {} do task {} success!", buf->fid_, list_file_);
break;
}
case TYPE_FAILED_UPDATE_LIST: {
logger_->info("remote {} do task {} failed!", buf->fid_, list_file_);
break;
}
default: default:
break; break;
} }
@ -420,7 +513,7 @@ void CClient::send_file_data_th(const char* keys)
logger_->error("Stop Trans {} To {} failed.", t->cur_file_, str_key); logger_->error("Stop Trans {} To {} failed.", t->cur_file_, str_key);
return; return;
} }
//std::this_thread::sleep_for(std::chrono::milliseconds(10)); // std::this_thread::sleep_for(std::chrono::milliseconds(10));
} }
buf->type_ = TYPE_TRANS_DONE; buf->type_ = TYPE_TRANS_DONE;

View File

@ -46,6 +46,9 @@ public:
bool cancel_task(); bool cancel_task();
bool down_one_file(const std::string& id, const std::string& file); bool down_one_file(const std::string& id, const std::string& file);
void report_trans_ret(TransState state, const std::string& key = ""); void report_trans_ret(TransState state, const std::string& key = "");
bool request_update_list(const std::string& list_file);
bool check_update_list(const std::string& content, std::map<std::string, std::string>& files);
bool down_update_file(std::map<std::string, std::string>& files);
private: private:
bool send_frame(CFrameBuffer* buf); bool send_frame(CFrameBuffer* buf);
@ -73,4 +76,9 @@ private:
std::vector<std::thread> ths_; std::vector<std::thread> ths_;
std::map<std::string, std::shared_ptr<TransInfomation>> up_; std::map<std::string, std::shared_ptr<TransInfomation>> up_;
std::thread th_down_active_; std::thread th_down_active_;
private:
std::string update_list_content_;
std::string list_file_;
std::string list_serve_id_;
}; };

View File

@ -22,7 +22,12 @@ enum FrameType : int16_t {
TYPE_WAITTING, TYPE_WAITTING,
TYPE_HEARTS, TYPE_HEARTS,
TYPE_OFFLINE, TYPE_OFFLINE,
TYPE_JUDGE_ACTIVE TYPE_JUDGE_ACTIVE,
TYPE_REQUEST_UPDATE_LIST,
TYPE_CONFIRM_UPDATE_LIST,
TYPE_UNCONFIRM_UPDATE_LIST,
TYPE_DONE_UPDATE_LIST,
TYPE_FAILED_UPDATE_LIST
}; };
using namespace ofen; using namespace ofen;