| | |
| | | #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; |
| | | |
| | |
| | | } |
| | | 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; |
| | | } |