From 365c864a587365fe443b11cc0cd7cfc8f8f8eb81 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期二, 01 六月 2021 11:19:22 +0800
Subject: [PATCH] refactor, clean up useless code.
---
box/center_main.cc | 112 ++++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 97 insertions(+), 15 deletions(-)
diff --git a/box/center_main.cc b/box/center_main.cc
index 5baa409..e7715f8 100644
--- a/box/center_main.cc
+++ b/box/center_main.cc
@@ -19,36 +19,118 @@
#include "box.h"
#include "center.h"
#include "defs.h"
+#include "log.h"
#include "signalhandle.h"
+#include <boost/interprocess/sync/named_mutex.hpp>
#include <chrono>
#include <thread>
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_.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();
+ LOG_DEBUG() << "old: " << *time_stamp << ", now: " << 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[])
{
AppArg args(argc, argv);
if (args.Has("remove")) {
- BHomeShm().Remove();
+ SharedMemory::Remove(BHomeShmName());
return 0;
}
- bool run = true;
- auto showStatus = [&]() {
- auto init = BHomeShm().get_free_memory();
- uint64_t idx = 0;
- while (run) {
- std::this_thread::sleep_for(1s);
- printf("%8d shared memory: avail : %ld / %ld\n", ++idx, BHomeShm().get_free_memory(), init);
- }
- };
- std::thread t(showStatus);
+ 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); }
- BHCenter center(BHomeShm());
+ auto &shm = BHomeShm();
+ if (!CenterInit(shm)) {
+ auto msg = "init memory error.";
+ LOG_FATAL() << msg;
+ printf("%s\n", msg);
+ exit(0);
+ }
+ GlobalInit(shm);
+
+ InstanceFlag inst(shm, kCenterRunningFlag);
+ if (!inst.TryStartAsFirstInstance()) {
+ auto msg = "another instance is running, exit.";
+ LOG_INFO() << msg;
+ printf("%s\n", msg);
+ return 0;
+ }
+
+ if (args.Has("daemon") || args.Has("d")) {
+ int r = daemon(0, 0); // TODO center control msg to close itself.
+ }
+
+ BHCenter center(shm);
center.Start();
- printf("center started ...\n");
+
+ auto msg = "center started ...";
+ LOG_INFO() << msg;
+ printf("%s\n", msg);
WaitForSignals({SIGINT, SIGTERM});
- run = false;
- t.join();
+ center.Stop();
+ LOG_INFO() << "center stopped.";
return 0;
}
--
Gitblit v1.8.0