From 1ab3eee884043f4a74e31db8313ac8147ed93236 Mon Sep 17 00:00:00 2001 From: taynpg Date: Fri, 20 Dec 2024 16:57:44 +0800 Subject: [PATCH] =?UTF-8?q?add=EF=BC=9A=E6=9B=B4=E6=96=B0=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E4=BB=BB=E5=8A=A1=E5=8A=9F=E8=83=BD=E5=88=9D=E6=AC=A1?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=8F=90=E4=BA=A4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +- client/client.cpp | 95 ++++++++++++++++++++++++++++++++++++++++++- client/client.h | 8 ++++ util/util.h | 7 +++- 4 files changed, 110 insertions(+), 3 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index e206bc6..c836560 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -130,6 +130,7 @@ "cwctype": "cpp", "memory_resource": "cpp", "random": "cpp", - "cinttypes": "cpp" + "cinttypes": "cpp", + "regex": "cpp" } } \ No newline at end of file diff --git a/client/client.cpp b/client/client.cpp index 94c74c8..bf1c98c 100644 --- a/client/client.cpp +++ b/client/client.cpp @@ -1,9 +1,11 @@ #include "client.h" #include +#include #include #include #include #include +#include namespace fs = std::filesystem; CClient::CClient(const std::shared_ptr& logger) : logger_(logger) @@ -262,6 +264,63 @@ void CClient::report_trans_ret(TransState state, const std::string& key) 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 iterf(in); + std::istreambuf_iterator 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 buf = std::make_shared(); + 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& files) +{ + return false; +} + +bool CClient::down_update_file(std::map& files) +{ + return false; +} + bool CClient::send_frame(CFrameBuffer* buf) { char* out_buf{}; @@ -383,6 +442,40 @@ void CClient::handle_frame(CFrameBuffer* buf) report_trans_ret(TRANS_FAILED); break; } + case TYPE_REQUEST_UPDATE_LIST: { + std::string content(buf->data_, buf->len_); + std::map 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: 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); return; } - //std::this_thread::sleep_for(std::chrono::milliseconds(10)); + // std::this_thread::sleep_for(std::chrono::milliseconds(10)); } buf->type_ = TYPE_TRANS_DONE; diff --git a/client/client.h b/client/client.h index 370458c..6e65253 100644 --- a/client/client.h +++ b/client/client.h @@ -46,6 +46,9 @@ public: bool cancel_task(); bool down_one_file(const std::string& id, const std::string& file); 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& files); + bool down_update_file(std::map& files); private: bool send_frame(CFrameBuffer* buf); @@ -73,4 +76,9 @@ private: std::vector ths_; std::map> up_; std::thread th_down_active_; + +private: + std::string update_list_content_; + std::string list_file_; + std::string list_serve_id_; }; \ No newline at end of file diff --git a/util/util.h b/util/util.h index 116ea8e..1bbfbaf 100644 --- a/util/util.h +++ b/util/util.h @@ -22,7 +22,12 @@ enum FrameType : int16_t { TYPE_WAITTING, TYPE_HEARTS, 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;