From 3c7339498c5a47e912f6e6009c197291acd7e1fd Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 03 六月 2021 11:27:39 +0800
Subject: [PATCH] change mq shm name prefix.

---
 box/center_main.cc |   79 ++-------------------------------------
 1 files changed, 5 insertions(+), 74 deletions(-)

diff --git a/box/center_main.cc b/box/center_main.cc
index e7715f8..0f582a4 100644
--- a/box/center_main.cc
+++ b/box/center_main.cc
@@ -27,70 +27,9 @@
 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")) {
-		SharedMemory::Remove(BHomeShmName());
-		return 0;
-	}
 
 	ns_log::AddLog(BHLogDir() + "bhshmq_center.log");
 	auto lvl = args.Get("log", "info");
@@ -101,22 +40,14 @@
 	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)) {
-		auto msg = "init memory error.";
+	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);
-
-	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.
@@ -125,9 +56,9 @@
 	BHCenter center(shm);
 	center.Start();
 
-	auto msg = "center started ...";
+	auto msg = "center (" + shm.name() + ") started ...";
 	LOG_INFO() << msg;
-	printf("%s\n", msg);
+	printf("%s\n", msg.c_str());
 	WaitForSignals({SIGINT, SIGTERM});
 	center.Stop();
 	LOG_INFO() << "center stopped.";

--
Gitblit v1.8.0