add:更新列表任务功能初次代码提交。
This commit is contained in:
parent
d1d918c0a5
commit
1ab3eee884
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -130,6 +130,7 @@
|
||||
"cwctype": "cpp",
|
||||
"memory_resource": "cpp",
|
||||
"random": "cpp",
|
||||
"cinttypes": "cpp"
|
||||
"cinttypes": "cpp",
|
||||
"regex": "cpp"
|
||||
}
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
#include "client.h"
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <of_path.h>
|
||||
#include <of_str.h>
|
||||
#include <of_util.h>
|
||||
#include <regex>
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
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();
|
||||
}
|
||||
|
||||
/* 清单文件,内容格式为:
|
||||
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)
|
||||
{
|
||||
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<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:
|
||||
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;
|
||||
|
@ -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<std::string, std::string>& files);
|
||||
bool down_update_file(std::map<std::string, std::string>& files);
|
||||
|
||||
private:
|
||||
bool send_frame(CFrameBuffer* buf);
|
||||
@ -73,4 +76,9 @@ private:
|
||||
std::vector<std::thread> ths_;
|
||||
std::map<std::string, std::shared_ptr<TransInfomation>> up_;
|
||||
std::thread th_down_active_;
|
||||
|
||||
private:
|
||||
std::string update_list_content_;
|
||||
std::string list_file_;
|
||||
std::string list_serve_id_;
|
||||
};
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user