提交 b21a0ebf authored 作者: blu's avatar blu

big refacting of communitation architect

上级 fb286dbd
......@@ -305,28 +305,14 @@ public:
string addr = string("tcp://127.0.0.1:") + to_string(config["dr-port"]);
ident = config["sn"].get<string>() + ":evmgr:0";
int ret = zmqhelper::setupDealer(&pCtxDealer, &pDealer, addr, ident);
bool bConfigGot = false;
while(!bConfigGot){
auto v = zmqhelper::z_recv_multiple(pDealer);
if(v.size() != 3) {
spdlog::error("evmgr {} invalid msg from daemon: {}", ident, addr);
//continue;
if(ret != 0) {
spdlog::error("evmgr {} failed to setup dealer {}", devSn, addr);
exit(1);
}
spdlog::info("evmgr {} msg received: {} {} {}", ident, body2str(v[0]), body2str(v[1]), body2str(v[2]));
try{
string sMeta = json::parse(body2str(v[1]))["type"];
if(sMeta != EV_MSG_META_CONFIG) {
throw StrException("meta type is:" + sMeta + ", but expecting " + EV_MSG_META_CONFIG);
}
config = json::parse(body2str(v[2]));
bConfigGot = true;
}catch(exception &e) {
spdlog::error("evmgr {} invalid config msg from daemon {}, {}", ident, addr, e.what());
exit(1);
}
ret = zmqhelper::recvConfigMsg(pDealer, config, addr, ident);
if(ret != 0) {
spdlog::error("evmgr {} failed to receive configration message {}", devSn , addr);
}
init();
......
......@@ -2889,7 +2889,7 @@ inline std::shared_ptr<Response> Client::Get(const char *path, const Headers &he
query += "=";
query += detail::encode_url(it->second);
}
req.path = string(path) + "?" + query;
req.path = std::string(path) + "?" + query;
auto res = std::make_shared<Response>();
return send(req, *res) ? res : nullptr;
......@@ -2971,7 +2971,7 @@ inline std::shared_ptr<Response> Client::Post(const char *path,
query += detail::encode_url(it->second);
}
req.path = string(path) + "?" + query;
req.path = std::string(path) + "?" + query;
req.headers.emplace("Content-Type", content_type);
req.body = body;
......
......@@ -14,8 +14,11 @@ update: 2019/08/23
#include "zmq.h"
#include <vector>
#include <spdlog/spdlog.h>
#include "json.hpp"
#include "utils.hpp"
using namespace std;
using namespace nlohmann;
namespace zmqhelper {
#define EV_HEARTBEAT_SECONDS 30
......@@ -139,6 +142,7 @@ int setupRouter(void **ctx, void **s, string addr){
}
/// setup dealer
/// @return 0 success, otherwise failed
int setupDealer(void **ctx, void **s, string addr, string ident) {
int ret = 0;
*ctx = zmq_ctx_new();
......@@ -157,6 +161,34 @@ int setupDealer(void **ctx, void **s, string addr, string ident) {
return ret;
}
/// recv config msg:
/// @return 0 success, otherwise failed.
int recvConfigMsg(void *s, json &config, string addr, string ident){
bool bConfigGot = false;
while(!bConfigGot){
auto v = zmqhelper::z_recv_multiple(s);
if(v.size() != 3) {
spdlog::error("{} invalid msg from daemon: {}", ident, addr);
return -1;
}
spdlog::info("evmgr {} msg received: {} {} {}", ident, body2str(v[0]), body2str(v[1]), body2str(v[2]));
try{
string sMeta = json::parse(body2str(v[1]))["type"];
if(sMeta != EV_MSG_META_CONFIG) {
throw StrException("meta type is:" + sMeta + ", but expecting " + EV_MSG_META_CONFIG);
}
config = json::parse(body2str(v[2]));
bConfigGot = true;
}catch(exception &e) {
spdlog::error("{} invalid config msg from daemon {}, {}", ident, addr, e.what());
return -1;
}
}
return 0;
}
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论