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

big refacting of communitation architect

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