提交 021c83b8 authored 作者: blu's avatar blu

big refacting of communitation architect

上级 b21a0ebf
......@@ -83,23 +83,14 @@ class EvDaemon{
this->peerData["status"][peerId] = 0;
this->peerData["config"][peerId] = v;
pid_t pid;
if( (pid = fork()) == -1 ) {
spdlog::error("evdamon {} failed to fork subsytem - evmgr", this->devSn);
}else if(pid == 0) {
ret += setenv("GID", peerId.c_str(), 1);
ret += setenv("DR_PORT", to_string(portRouter).c_str(), 1);
if(ret < 0) {
spdlog::error("evdaemon {} failed to set env", this->devSn);
return -3;
}
execl("./evmgr", NULL, NULL, NULL);
spdlog::error("evdaemon {} failed to startup evmgr", this->devSn);
}else{
// parent
this->peerData["pids"][peerId] = pid;
spdlog::info("evdaemon {} created evmgr", this->devSn);
}
ret = zmqhelper::forkSubsystem(devSn, peerId, portRouter, pid);
if(ret != 0) {
spdlog::error("evdaemon {} failed to fork subsystem: {}", devSn, peerId);
// TODO:
exit(1);
}
this->peerData["pids"][peerId] = pid;
spdlog::info("evdaemon {} created subsystem {}", devSn, peerId);
}
// startup other submodules
......@@ -113,7 +104,13 @@ class EvDaemon{
continue;
}
if(m["enabled"].get<int>() != 0) {
string peerName;
ret = cfgutils::getPeerId(mn, m, peerId, peerName);
if(ret != 0) {
// TODO: cleanup and reload
}else{
}
}
}
}
......@@ -151,7 +148,7 @@ class EvDaemon{
selfId = body2str(body[0]);
bool eventConn = false;
// XTF2BJR9:evslicer:1
auto sp = cloudutils::split(selfId, ':');
auto sp = strutils::split(selfId, ':');
if(sp.size() != 3) {
spdlog::warn("evdaemon {} inproper peer id: {}", devSn, selfId);
return -1;
......
......@@ -122,7 +122,7 @@ private:
selfId = body2str(body[0]);
bool eventConn = false;
// XTF2BJR9:evslicer:1
auto sp = cloudutils::split(selfId, ':');
auto sp = strutils::split(selfId, ':');
if(sp.size() != 3) {
spdlog::warn("evmg {} inproper peer id: {}", devSn, selfId);
return -1;
......@@ -289,7 +289,7 @@ public:
strEnv = getenv("GID");
if(strEnv != NULL) {
ident = strEnv;
auto v = cloudutils::split(ident, ':');
auto v = strutils::split(ident, ':');
if(v.size() != 3||v[1] != "evmgr" || v[2] != "0") {
spdlog::error("evmgr received invalid gid: {}", ident);
exit(1);
......
......@@ -17,19 +17,6 @@ using namespace httplib;
// cloudutils
namespace cloudutils
{
vector<string> split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
/// [deprecated] ref: ../config.json
json registry(json &conf, string sn, string module) {
json ret;
......@@ -111,9 +98,46 @@ json reqConfig(json &info){
return ret;
}
} // namespace cloudutils
} // namespace cloudutils
///
namespace strutils{
vector<string> split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
}//namespace strutils
namespace cfgutils {
int getPeerId(string modName, json& modElem, string &peerId, string &peerName) {
try {
if(modName == "evmgr") {
peerId = modElem["sn"].get<string>() + ":evmgr:0";
peerName = modName;
}else if(modName == "evml") {
peerId = modElem["sn"].get<string>() + ":evml" + modElem["type"].get<string>() + ":" + modElem["iid"].get<string>();
peerName = modName + modElem["type"].get<string>();
}else{
peerId = modElem["sn"].get<string>() + ":" + modName + ":" + modElem["iid"].get<string>();
peerName = modName;
}
}catch(exception &e) {
spdlog::error("failed to get gid for {} in {}: {}", modName, modElem.dump(), e.what());
return -1;
}
return 0;
}
}
struct StrException : public std::exception
......
......@@ -16,6 +16,7 @@ update: 2019/08/23
#include <spdlog/spdlog.h>
#include "json.hpp"
#include "utils.hpp"
// #include <unistd.h>
using namespace std;
using namespace nlohmann;
......@@ -190,6 +191,30 @@ int recvConfigMsg(void *s, json &config, string addr, string ident){
}
int forkSubsystem(string devSn, string peerId, int drPort, pid_t &pid){
int ret = 0;
auto v = strutils::split(peerId, ':');
string modName = v[1];
string sn = v[0];
if( (pid = fork()) == -1 ) {
spdlog::error("evdamon {} failed to fork subsytem - evmgr", devSn);
return -1;
}else if(pid == 0) {
ret += setenv("GID", peerId.c_str(), 1);
ret += setenv("DR_PORT", to_string(drPort).c_str(), 1);
if(ret < 0) {
spdlog::error("evdaemon {} failed to set env", devSn);
return -2;
}
execl((string("./") + modName).c_str(), NULL, NULL, NULL);
spdlog::error("evdaemon {} failed to startup evmgr", devSn);
}else{
// parent
}
return 0;
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论