From 69f60d8bcc5121eb952b57277c94ad5cecb8d44a Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期四, 03 六月 2021 11:03:08 +0800 Subject: [PATCH] client side update shm on center restart. --- src/bh_api.cc | 34 ++++++++++++++++++++++++++-------- 1 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/bh_api.cc b/src/bh_api.cc index 8690d5f..3dafe7a 100644 --- a/src/bh_api.cc +++ b/src/bh_api.cc @@ -1,9 +1,11 @@ #include "bh_api.h" #include "defs.h" #include "topic_node.h" +#include <chrono> #include <cstdio> #include <memory> +using namespace std::chrono_literals; using namespace bhome_shm; using namespace bhome_msg; @@ -30,17 +32,33 @@ } std::unique_ptr<TopicNode> &ProcNodePtr() { + // client side init here. static std::mutex mtx; - std::lock_guard<std::mutex> lk(mtx); + auto InitLog = []() { + ns_log::AddLog(BHLogDir() + "bhshmq_node_" + GetProcExe() + ".log", true); + return true; + }; + static bool init_log = InitLog(); + static std::string shm_name; static std::unique_ptr<TopicNode> ptr; - if (!ptr && GlobalInit(BHomeShm())) { - auto InitLog = []() { - ns_log::AddLog(BHLogDir() + "bhshmq_node_" + GetProcExe() + ".log"); - return true; - }; - static bool init_log = InitLog(); - ptr.reset(new TopicNode(BHomeShm())); + + std::lock_guard<std::mutex> lk(mtx); + if (shm_name != BHomeShmName()) { + shm_name = BHomeShmName(); + LOG_INFO() << "using shm " << shm_name; + + ptr.reset(); + // must reset/stop node before call BHomeShm() which resets shm. + + auto &shm = BHomeShm(); + for (int i = 0; !ptr && i < 3; ++i) { + if (GlobalInit(shm)) { + ptr.reset(new TopicNode(shm)); + } else { + std::this_thread::sleep_for(1s); // make sure shm init done. + } + } } return ptr; } -- Gitblit v1.8.0