init
This commit is contained in:
commit
70d467d052
97 files changed
+10386
No files matched your search
@@ -0,0 +1,110 @@
|
||||
#pragma once
|
||||
#include <us/Thread.hpp>
|
||||
#include <us/ITask.hpp>
|
||||
#include <us/HttpServer.hpp>
|
||||
namespace qing {
|
||||
namespace http = boost::beast::http;
|
||||
/* ----------------
|
||||
* HttpTask类 - 超文本服务器任务 */
|
||||
class HttpTask: public ITask {
|
||||
public:
|
||||
|
||||
/* 接受一个回调函数,其接受pmc::net::HttpServer对象的引用,在回调函数中对服务器进行配置 */
|
||||
HttpTask(int port, std::function<void(pmc::net::HttpServer& server)> handler);
|
||||
|
||||
/* 在一个线程中判断线程是否正在有效运行 */
|
||||
bool isRunning() override;
|
||||
|
||||
/* 启动该项任务 */
|
||||
void start() override;
|
||||
|
||||
/* 停止该项任务 */
|
||||
void stop() override;
|
||||
|
||||
private:
|
||||
int port;
|
||||
std::function<void(pmc::net::HttpServer& server)> handler;
|
||||
std::unique_ptr<Thread> th; /* ---- 可控线程指针 */
|
||||
std::unique_ptr<pmc::net::HttpServer> server; /* ---- HTTP服务器的指针 ---- */
|
||||
|
||||
/* 初始化线程,定义线程的行为(Http服务器) */
|
||||
void init_thread();
|
||||
};
|
||||
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
|
||||
/* ================
|
||||
* 进程托管http服务器
|
||||
* ================
|
||||
* 对操作进程列表的http服务器进行配置,即是输入回调函数 */
|
||||
namespace process_http_server {
|
||||
|
||||
/* ---- 注册POST路由 ---- */
|
||||
void registerRoutes(pmc::net::HttpServer& server);
|
||||
|
||||
/**
|
||||
* @brief 解析请求体中的JSON数据
|
||||
* @param body 请求体字符串
|
||||
* @return JSON解析后的属性树
|
||||
*/
|
||||
boost::property_tree::ptree parseJsonBody(const std::string& body);
|
||||
|
||||
/**
|
||||
* @brief 构建JSON响应
|
||||
* @param success 是否成功
|
||||
* @param message 消息
|
||||
* @param data 附加数据
|
||||
* @return JSON字符串
|
||||
*/
|
||||
std::string buildJsonResponse(bool success, const std::string& message,
|
||||
const boost::property_tree::ptree& data = boost::property_tree::ptree());
|
||||
|
||||
/**
|
||||
* @brief 从请求中提取target参数
|
||||
* @param params 查询参数
|
||||
* @return target值(空字符串表示本机)
|
||||
*/
|
||||
std::string getTarget(const std::unordered_map<std::string, std::string>& params);
|
||||
|
||||
/**
|
||||
* @brief 从请求中提取exec参数
|
||||
* @param params 查询参数
|
||||
* @return 要执行的指令
|
||||
*/
|
||||
std::string getExecCommand(const std::unordered_map<std::string, std::string>& params);
|
||||
|
||||
/**
|
||||
* @brief 从请求中提取kill参数
|
||||
* @param params 查询参数
|
||||
* @return 目标进程号
|
||||
*/
|
||||
int getPid(const std::unordered_map<std::string, std::string>& params);
|
||||
|
||||
/* POST方法处理器 */
|
||||
http::response<http::string_body> handleList(
|
||||
const http::request<http::string_body>& req,
|
||||
const std::unordered_map<std::string, std::string>& params);
|
||||
|
||||
http::response<http::string_body> handleExec(
|
||||
const http::request<http::string_body>& req,
|
||||
const std::unordered_map<std::string, std::string>& params);
|
||||
|
||||
http::response<http::string_body> handleKill(
|
||||
const http::request<http::string_body>& req,
|
||||
const std::unordered_map<std::string, std::string>& params);
|
||||
|
||||
/** 内部实现函数 */
|
||||
std::string listProcesses(const std::string& target);
|
||||
std::string execCommand(const std::string& command, const std::string& target);
|
||||
bool killProcess(int pid, const std::string& target);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <list>
|
||||
#include "main/ProcessTask.hh"
|
||||
|
||||
namespace qing {
|
||||
|
||||
|
||||
|
||||
/* -----------------
|
||||
* subprocess pool 子进程池
|
||||
* ----------------
|
||||
* FIXME: 将列表(序列化)功能放到这里来 */
|
||||
class PPool {
|
||||
public:
|
||||
|
||||
/* CREATE PROCESS - 创建进程
|
||||
*
|
||||
* cmd: 启动命令 */
|
||||
void crtp(const std::string& cmd);
|
||||
|
||||
|
||||
/* CLEAR 清理 - 删除所有已经停下的进程 */
|
||||
void clr();
|
||||
|
||||
|
||||
/* 获取进程池的大小 */
|
||||
size_t size() { return pool.size(); }
|
||||
|
||||
|
||||
/* 删除一个进程,需要输入进程的下标。
|
||||
*
|
||||
* FIXME: 输入进程下标似乎不太自然 */
|
||||
void kill(int idx);
|
||||
|
||||
/* 取下标运算符重载 - 取元素对象 */
|
||||
ProcessTask& operator[](const size_t idx);
|
||||
|
||||
|
||||
/* TODO: 实现迭代器和begin()、end()
|
||||
* ...... */
|
||||
|
||||
private:
|
||||
|
||||
/* 进程任务的shared指针的容器 */
|
||||
std::list<std::shared_ptr<ProcessTask>> pool{};
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
#include <boost/process.hpp>
|
||||
#include <us/ITask.hpp>
|
||||
|
||||
namespace qing {
|
||||
|
||||
/* ----------------
|
||||
* 【子进程任务】
|
||||
* ----------------
|
||||
* 输入要执行的命令,构造子进程任务
|
||||
* TODO: 我们可以重定向子进程的输入和输出,实时交互 */
|
||||
class ProcessTask: public ITask {
|
||||
public:
|
||||
|
||||
/* 输入一个字符串(要执行的命令)(但是并没有执行起来) */
|
||||
ProcessTask(std::string cmd): cmd(cmd) {}
|
||||
|
||||
/* 任务是否在运行中 */
|
||||
bool isRunning() override { return p && p->running(); }
|
||||
|
||||
/* 启动任务 */
|
||||
void start() override {
|
||||
if (!isRunning())
|
||||
this->p = std::make_unique<boost::process::child>(cmd); }
|
||||
|
||||
/* 终止该任务(并且重置子进程指针) */
|
||||
void stop() override {
|
||||
if (isRunning()) p->terminate();
|
||||
p.reset();
|
||||
}
|
||||
|
||||
/* 获取该任务(子进程)的命令 */
|
||||
std::string check() { return this->cmd; }
|
||||
|
||||
/* 获取该子进程的pid(进程编号) */
|
||||
pid_t pid() { return p->id(); }
|
||||
|
||||
private:
|
||||
std::unique_ptr<boost::process::child> p; /* 子进程指针 */
|
||||
std::string cmd; /* 该进程执行的命令 */
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
#ifdef __NEW_VER
|
||||
/* ---- [子进程任务]boost.process库升级到了v2 ---- */
|
||||
class ProcessTask: public ITask {
|
||||
public:
|
||||
ProcessTask(std::string cmd): cmd(cmd) {}
|
||||
bool isRunning() override;
|
||||
int getExitCode();
|
||||
void start() override;
|
||||
/* ---- NOTE(202606025-qing): 杀死进程失败只是打印错误信息 ---- */
|
||||
void stop() override;
|
||||
std::string check() {
|
||||
return this->cmd;
|
||||
}
|
||||
pid_t pid() {
|
||||
return p->id();
|
||||
}
|
||||
|
||||
private:
|
||||
boost::asio::io_context ctx; /* 异步上下文 */
|
||||
std::unique_ptr<boost::process::v2::process> p; /* 子进程指针 */
|
||||
boost::system::error_code exit_code; /* 进程退出的代码 */
|
||||
std::string cmd; /* 要执行的命令 */
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,58 @@
|
||||
#pragma onces
|
||||
#include "tt/Mq.h"
|
||||
namespace qing {
|
||||
|
||||
|
||||
/*
|
||||
* TRANSMIT TASK 通信任务
|
||||
* (创建一个通信伺服器,它收到消息就执行回调)
|
||||
* (长时间伺服运行会不会有问题)
|
||||
*/
|
||||
class Tttask: public ITask {
|
||||
public:
|
||||
|
||||
/* 构造【通信任务】
|
||||
* msglen: 单条消息的长度
|
||||
* msgcnt: 消息伺服长度
|
||||
* callback: 回调函数 */
|
||||
Tttask(const size_t msglen, size_t msgcnt, const sf_t callback);
|
||||
|
||||
/* 启动该通信伺服任务 */
|
||||
void start() override;
|
||||
|
||||
/* 终止这个通信伺服任务
|
||||
*
|
||||
* TODO: 研究一下要不要释放一些资源
|
||||
* TODO: 增加超时 */
|
||||
void stop() override {
|
||||
if (this->isRunning())
|
||||
th->WaitClose();
|
||||
}
|
||||
|
||||
|
||||
/* 该任务是否在运行
|
||||
*
|
||||
* FIXME: 对START状态的处理还应该考虑
|
||||
* FIXME: 如果在STOP状态而没有stop()任务,可能会造成线程未关闭,一直占用资源? */
|
||||
bool isRunning() override;
|
||||
|
||||
|
||||
private:
|
||||
std::unique_ptr<Thread> th; /* 可控线程指针 */
|
||||
std::unique_ptr<sf_t> callback; /* 回调行为(接收之后)指针 */
|
||||
std::unique_ptr<Mq> mq; /* 消息队列指针 */
|
||||
size_t msglen=0, msgcnt=0; /* 消息的缓存规格 */
|
||||
std::string name; /* 消息队列名(进程号) */
|
||||
|
||||
|
||||
|
||||
|
||||
/* 初始化通信伺服线程,
|
||||
* 初始化方法就是通过传入一些函数,
|
||||
* 构建线程 */
|
||||
void init_thread();
|
||||
|
||||
|
||||
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
namespace qing {
|
||||
/* ----------------
|
||||
* 进程托管服务方法集
|
||||
* ----------------
|
||||
* 直接对进程组列表进行操作
|
||||
*
|
||||
*/
|
||||
namespace pmc_mtd
|
||||
{
|
||||
auto list() -> std::string; /* 列出所有的进程 */
|
||||
auto kill(const int pid) -> bool; /* 杀死一个进程,需提供PID */
|
||||
auto exec(const std::string& cmd) -> bool; /* 执行一个进程,输入要执行的命令行 */
|
||||
auto parse_self_init_list(std::string &path) -> void; /* 解析自启动脚本 */
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
namespace qing {
|
||||
|
||||
/* ----------------
|
||||
* 子系统调用
|
||||
* ----------------
|
||||
* 通过进程间通信与子系统进行通讯,调用不同的功能
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
namespace subsys_call
|
||||
{
|
||||
std::string send_list_and_recv_pipe(const std::string& name_mq);
|
||||
std::string send_start_and_recv_pipe(const std::string& name_mq, const std::string& exec);
|
||||
std::string send_kill_and_recv_pipe(const std::string& name_mq, const int kill);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user