From 056f71f24cefaf88f2a93714c6678c03ed5f1e0e Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 02 七月 2021 16:54:33 +0800
Subject: [PATCH] fixed to adapt gcc-5.4 & glibc-2.25
---
box/center_main.cc | 93 +++++++++++-----------------------------------
1 files changed, 23 insertions(+), 70 deletions(-)
diff --git a/box/center_main.cc b/box/center_main.cc
index 7f4b26b..8c840dc 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>
@@ -26,89 +27,41 @@
using namespace std::chrono_literals;
using namespace bhome_shm;
-namespace
-{
-const std::string kCenterRunningFlag = "bh_center_single_flag_0";
-
-class InstanceFlag
-{
-
-public:
- InstanceFlag(SharedMemory &shm, const std::string &name) :
- shm_(shm), name_(name), run_(false) {}
- ~InstanceFlag() { Stop(); }
-
- bool TryStartAsFirstInstance()
- {
- if (run_) {
- 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));
-
- if (mtx && time_stamp) {
- Guard lock(*mtx);
- auto now = NowSec();
- // printf("old: %ld, now: %ld\n", *time_stamp, now);
- if (now > *time_stamp + 10) {
- *time_stamp = now;
- auto UpdateTime = [this, time_stamp]() {
- while (run_) {
- std::this_thread::sleep_for(1s);
- *time_stamp = NowSec();
- }
- };
- run_.store(true);
- std::thread(UpdateTime).swap(worker_);
- return true;
- }
- }
- return false;
- }
-
-private:
- void Stop()
- {
- run_.store(false);
- if (worker_.joinable()) {
- worker_.join();
- }
- }
-
- std::thread worker_;
- SharedMemory &shm_;
- std::string name_;
- std::atomic<bool> run_;
-};
-
-} // namespace
int center_main(int argc, const char *argv[])
{
- auto &shm = BHomeShm();
- MsgI::BindShm(shm);
-
AppArg args(argc, argv);
- if (args.Has("remove")) {
- shm.Remove();
- return 0;
- }
- InstanceFlag inst(shm, kCenterRunningFlag);
- if (!inst.TryStartAsFirstInstance()) {
- printf("another instance is running, exit.\n");
- return 0;
+ ns_log::AddLog(BHLogDir() + "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); }
+
+ if (!CenterInit()) {
+ auto msg = "init memory failed, or center is already running.";
+ LOG_FATAL() << msg;
+ printf("%s\n", msg);
+ exit(0);
}
+ auto &shm = BHomeShm();
+ GlobalInit(shm);
if (args.Has("daemon") || args.Has("d")) {
- int r = daemon(0, 0); // TODO center control msg to close itself.
+ int r = daemon(0, 0); // maybe add center control msg to close itself.
}
BHCenter center(shm);
center.Start();
- printf("center started ...\n");
+ auto msg = "center (" + shm.name() + ") started ...";
+ LOG_INFO() << msg;
+ printf("%s\n", msg.c_str());
WaitForSignals({SIGINT, SIGTERM});
+ center.Stop();
+ LOG_INFO() << "center stopped.";
return 0;
}
--
Gitblit v1.8.0