1 Star 5 Fork 5

杰神 / iot-noob

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
agent.cpp 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
杰神 提交于 2021-11-19 23:27 . 补充
#include "agent.hpp"
//using namespace std;
typedef void (*AgentHandler)(cJSON *req, cJSON *resp);
class AgentNode
{
public:
std::vector<AgentHandler> handlers;
//AgentHandler _handler;
std::map<std::string, AgentNode *> childrens;
public:
void Route(std::string path, AgentHandler handler)
{
if (path == "")
{
handlers.push_back(handler);
//_handler = handler;
return;
}
int c = path.find_first_of('/');
std::string node = path.substr(0, c);
std::string next = path.substr(c);
if (childrens.find(node) == childrens.end())
{
childrens[node] = new AgentNode();
}
childrens[node]->Route(next, handler);
}
void Walk(std::string path, cJSON* req, cJSON* resp)
{
if (path == "")
{
//handlers.push_back(handler);
for (auto iter = handlers.cbegin();
iter != handlers.cend(); iter++)
{
(*iter)(req, resp);
}
return;
}
int c = path.find_first_of('/');
std::string node = path.substr(0, c);
std::string next = path.substr(c);
auto iter = childrens.find(node);
if (iter == childrens.end())
{
return;
}
iter->second->Walk(next, req, resp);
}
};
AgentNode root;
void sysInfo(cJSON* req, cJSON* resp)
{
}
void Agent_Init()
{
root.Route("sys/info", sysInfo);
}
void AgentHandle() //(Stream &stream)
{
//return output;
}
C++
1
https://gitee.com/god-jason/iot-noob.git
git@gitee.com:god-jason/iot-noob.git
god-jason
iot-noob
iot-noob
main

搜索帮助