From 3788226ee9332945e90066b58f2b85026c2a0460 Mon Sep 17 00:00:00 2001 From: lichao <lichao@aiotlink.com> Date: 星期二, 18 五月 2021 10:56:32 +0800 Subject: [PATCH] change node init, no shm lock any more. --- box/center_main.cc | 33 +++++++++++++++++++++++++-------- 1 files changed, 25 insertions(+), 8 deletions(-) diff --git a/box/center_main.cc b/box/center_main.cc index 1f181b8..5eb21b9 100644 --- a/box/center_main.cc +++ b/box/center_main.cc @@ -19,6 +19,7 @@ #include "box.h" #include "center.h" #include "defs.h" +#include "log.h" #include "signalhandle.h" #include <boost/interprocess/sync/named_mutex.hpp> #include <chrono> @@ -44,13 +45,13 @@ return true; } - auto mtx(shm_.find_or_construct<Mutex>((name_ + "_mutex_0").c_str())()); - auto time_stamp(shm_.find_or_construct<int64_t>((name_ + "_timestamp_0").c_str())(0)); + auto mtx(shm_.FindOrCreate<Mutex>(name_ + "_mutex_0")); + auto time_stamp(shm_.FindOrCreate<int64_t>(name_ + "_timestamp_0", 0)); if (mtx && time_stamp) { Guard lock(*mtx); auto now = NowSec(); - // printf("old: %ld, now: %ld\n", *time_stamp, now); + LOG_DEBUG() << "old: " << *time_stamp << ", now: " << now; if (now > *time_stamp + 10) { *time_stamp = now; auto UpdateTime = [this, time_stamp]() { @@ -85,17 +86,31 @@ } // namespace int center_main(int argc, const char *argv[]) { - auto &shm = BHomeShm(); - AppArg args(argc, argv); if (args.Has("remove")) { - shm.Remove(); + SharedMemory::Remove(BHomeShmName()); return 0; } + ns_log::AddLog("/tmp/bhshmq_center.log"); + auto lvl = args.Get("log", "info"); + if (strcasecmp(lvl.c_str(), "trace") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::trace); } + if (strcasecmp(lvl.c_str(), "debug") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::debug); } + if (strcasecmp(lvl.c_str(), "info") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::info); } + if (strcasecmp(lvl.c_str(), "warning") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::warning); } + if (strcasecmp(lvl.c_str(), "error") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::error); } + if (strcasecmp(lvl.c_str(), "fatal") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::fatal); } + + auto &shm = BHomeShm(); + if (!CenterInit(shm)) { + LOG_FATAL() << "init memory error."; + exit(0); + } + GlobalInit(shm); + InstanceFlag inst(shm, kCenterRunningFlag); if (!inst.TryStartAsFirstInstance()) { - printf("another instance is running, exit.\n"); + LOG_INFO() << "another instance is running, exit."; return 0; } @@ -106,8 +121,10 @@ BHCenter center(shm); center.Start(); - printf("center started ...\n"); + LOG_INFO() << "center started ..."; WaitForSignals({SIGINT, SIGTERM}); + center.Stop(); + LOG_INFO() << "center stopped."; return 0; } -- Gitblit v1.8.0