163 lines
4.3 KiB
C++
163 lines
4.3 KiB
C++
#include "client.h"
|
|
#include <iostream>
|
|
#include <of_str.h>
|
|
|
|
using namespace ofen;
|
|
CClient::CClient(const std::shared_ptr<spdlog::logger>& logger) : logger_(logger)
|
|
{
|
|
client_ = std::make_shared<CTcpClient>(io_context_, logger_);
|
|
supported_.push_back("Get");
|
|
}
|
|
|
|
CClient::~CClient()
|
|
{
|
|
}
|
|
|
|
void CClient::run()
|
|
{
|
|
if (!client_->connect("127.0.0.1", "8080")) {
|
|
logger_->info("{} connect err.", __FUNCTION__);
|
|
return;
|
|
}
|
|
client_->register_func([&](CFrameBuffer* buf) { handle_frame(buf); });
|
|
client_->async_recv();
|
|
std::thread thread([&]() { io_context_.run(); });
|
|
logger_->warn("SupportCmd:Get|Up|Down|Cancel");
|
|
char line[512]{};
|
|
while (std::cin.getline(line, 512)) {
|
|
std::string cmd_input(line);
|
|
if (std::strstr(line, "end")) {
|
|
break;
|
|
}
|
|
auto vec = COfStr::split(cmd_input, " ");
|
|
if (vec.size() < 1) {
|
|
logger_->error("input's invalid format.");
|
|
continue;
|
|
}
|
|
std::string cmd{};
|
|
std::string param{};
|
|
if (vec.size() == 1) {
|
|
cmd = vec[0];
|
|
} else {
|
|
cmd = vec[0];
|
|
param = vec[1];
|
|
}
|
|
if (cmd == "Get") {
|
|
get_task_list();
|
|
continue;
|
|
}
|
|
if (cmd == "Down") {
|
|
int key = param.empty() ? -1 : std::stoi(param);
|
|
if (task_list_.count(key)) {
|
|
down_task();
|
|
} else {
|
|
logger_->error("no task number find.");
|
|
}
|
|
continue;
|
|
}
|
|
if (cmd == "Up") {
|
|
up_task(cmd_input);
|
|
continue;
|
|
}
|
|
if (cmd == "Cancel") {
|
|
cancel_task();
|
|
continue;
|
|
}
|
|
logger_->error("No matched cmd.");
|
|
}
|
|
client_->disconnect();
|
|
thread.join();
|
|
logger_->info("{} exit.", __FUNCTION__);
|
|
}
|
|
|
|
bool CClient::get_task_list()
|
|
{
|
|
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
|
buf->type_ = TYPE_GET_LIST;
|
|
return send_frame(buf.get());
|
|
}
|
|
|
|
bool CClient::down_task()
|
|
{
|
|
// if (send_frame(198, ))
|
|
return false;
|
|
}
|
|
|
|
bool CClient::up_task(const std::string& cmd)
|
|
{
|
|
auto list = CFileOpr::get_file_list(cmd);
|
|
std::string msg;
|
|
for (const auto& item : list) {
|
|
if (msg.empty()) {
|
|
msg.append(item);
|
|
} else {
|
|
msg.append("|" + item);
|
|
}
|
|
}
|
|
if (msg.empty()) {
|
|
logger_->warn("{} msg empty.", __FUNCTION__);
|
|
return false;
|
|
}
|
|
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
|
buf->type_ = TYPE_UP_LIST;
|
|
buf->data_ = new char[msg.size()];
|
|
std::memset(buf->data_, 0x0, msg.size());
|
|
buf->len_ = std::snprintf(buf->data_, msg.size(), "%s", msg.data());
|
|
return send_frame(buf.get());
|
|
}
|
|
|
|
bool CClient::cancel_task()
|
|
{
|
|
std::shared_ptr<CFrameBuffer> buf = std::make_shared<CFrameBuffer>();
|
|
buf->type_ = TYPE_CANCEL_LIST;
|
|
return send_frame(buf.get());
|
|
}
|
|
|
|
bool CClient::send_frame(CFrameBuffer* buf)
|
|
{
|
|
char* out_buf{};
|
|
int out_len{};
|
|
if (!CTransProtocal::pack(buf, &out_buf, out_len)) {
|
|
logger_->error("{} pack failed.", __FUNCTION__);
|
|
return false;
|
|
}
|
|
if (!client_->send(out_buf, out_len)) {
|
|
logger_->error("{} send failed.", __FUNCTION__);
|
|
delete[] out_buf;
|
|
return false;
|
|
}
|
|
delete[] out_buf;
|
|
return true;
|
|
}
|
|
|
|
void CClient::handle_frame(CFrameBuffer* buf)
|
|
{
|
|
if (buf == nullptr) {
|
|
logger_->error("{} nullptr.", __FUNCTION__);
|
|
return;
|
|
}
|
|
// logger_->debug("type: {}", buf->type_);
|
|
// logger_->debug("len: {}", buf->len_);
|
|
|
|
if (buf->type_ == 199) {
|
|
task_list_.clear();
|
|
std::string source(buf->data_);
|
|
auto vec = COfStr::split(source, "\n");
|
|
for (const auto& item : vec) {
|
|
if (item.find("[") == std::string::npos) {
|
|
logger_->info("FILE ==> {}", item);
|
|
} else {
|
|
logger_->debug("***********************************************");
|
|
logger_->debug("{}", item);
|
|
}
|
|
}
|
|
// int index = 0;
|
|
// auto vec = COfStr::split(source, "|");
|
|
// for (const auto& item : vec) {
|
|
// task_list_[index] = item;
|
|
// ++index;
|
|
// logger_->warn("{}:{}", index, item);
|
|
// }
|
|
}
|
|
}
|