From c2cc00574415c612e82f0955523d422f59594912 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期四, 06 五月 2021 19:40:38 +0800
Subject: [PATCH] disable center console log.

---
 box/center.cpp |  100 +++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 74 insertions(+), 26 deletions(-)

diff --git a/box/center.cpp b/box/center.cpp
index 4bb9ea1..c57d34d 100644
--- a/box/center.cpp
+++ b/box/center.cpp
@@ -18,6 +18,7 @@
 #include "center.h"
 #include "bh_util.h"
 #include "defs.h"
+#include "log.h"
 #include "shm.h"
 #include <chrono>
 #include <set>
@@ -60,9 +61,7 @@
 		void UpdateState(const int64_t now, const int64_t offline_time, const int64_t kill_time)
 		{
 			auto diff = now - timestamp_;
-#ifndef NDEBUG
-			printf("state %p diff: %ld\n", this, diff);
-#endif
+			LOG_DEBUG() << "state " << this << " diff: " << diff;
 			if (diff < offline_time) {
 				flag_ = kStateNormal;
 			} else if (diff < kill_time) {
@@ -103,7 +102,22 @@
 
 	// center name, no relative to shm.
 	const std::string &id() const { return id_; }
+	void OnNodeInit(const int64_t msg)
+	{
+		MQId ssn = msg;
+		auto UpdateRegInfo = [&](Node &node) {
+			for (int i = 0; i < 10; ++i) {
+				node->addrs_.insert(ssn + i);
+			}
+			node->state_.timestamp_ = NowSec() - offline_time_;
+			node->state_.UpdateState(NowSec(), offline_time_, kill_time_);
+		};
 
+		Node node(new NodeInfo);
+		UpdateRegInfo(node);
+		nodes_[ssn] = node;
+		LOG_INFO() << "new node ssn (" << ssn << ") init";
+	}
 	MsgCommonReply Register(const BHMsgHead &head, MsgRegister &msg)
 	{
 		if (msg.proc().proc_id() != head.proc_id()) {
@@ -112,8 +126,7 @@
 
 		try {
 			MQId ssn = head.ssn_id();
-			// use src_addr as session id.
-			// when node restart, src_addr will change,
+			// when node restart, ssn will change,
 			// and old node will be removed after timeout.
 			auto UpdateRegInfo = [&](Node &node) {
 				node->addrs_.insert(SrcAddr(head));
@@ -133,16 +146,19 @@
 				Node node(new NodeInfo);
 				UpdateRegInfo(node);
 				nodes_[ssn] = node;
+			}
+			LOG_DEBUG() << "node (" << head.proc_id() << ") ssn (" << ssn << ")";
 
-				auto old = node_addr_map_.find(head.proc_id());
-				if (old != node_addr_map_.end()) { // old session
-					auto &old_ssn = old->second;
+			auto old = online_node_addr_map_.find(head.proc_id());
+			if (old != online_node_addr_map_.end()) { // old session
+				auto &old_ssn = old->second;
+				if (old_ssn != ssn) {
 					nodes_[old_ssn]->state_.PutOffline(offline_time_);
-					printf("put %s %ld offline\n", nodes_[old_ssn]->proc_.proc_id().c_str(), old->second);
+					LOG_DEBUG() << "put node (" << nodes_[old_ssn]->proc_.proc_id() << ") ssn (" << old->second << ") offline";
 					old_ssn = ssn;
-				} else {
-					node_addr_map_.emplace(head.proc_id(), ssn);
 				}
+			} else {
+				online_node_addr_map_.emplace(head.proc_id(), ssn);
 			}
 			return MakeReply(eSuccess);
 		} catch (...) {
@@ -200,6 +216,10 @@
 			    TopicDest dest = {src, node};
 			    for (auto &topic : topics) {
 				    service_map_[topic].insert(dest);
+			    }
+			    LOG_DEBUG() << "node " << node->proc_.proc_id() << " ssn " << *node->addrs_.begin() << " serve " << topics.size() << " topics:\n";
+			    for (auto &topic : topics) {
+				    LOG_DEBUG() << "\t" << topic;
 			    }
 			    return MakeReply(eSuccess);
 		    });
@@ -387,11 +407,19 @@
 		};
 		EraseMapRec(service_map_, node->services_);
 		EraseMapRec(subscribe_map_, node->subscriptions_);
-		node_addr_map_.erase(node->proc_.proc_id());
+
+		// remove online record.
+		auto pos = online_node_addr_map_.find(node->proc_.proc_id());
+		if (pos != online_node_addr_map_.end()) {
+			if (node->addrs_.find(pos->second) != node->addrs_.end()) {
+				online_node_addr_map_.erase(pos);
+			}
+		}
 
 		for (auto &addr : node->addrs_) {
 			cleaner_(addr);
 		}
+
 		node->addrs_.clear();
 	}
 	std::string id_; // center proc id;
@@ -399,7 +427,7 @@
 	std::unordered_map<Topic, Clients> service_map_;
 	std::unordered_map<Topic, Clients> subscribe_map_;
 	std::unordered_map<Address, Node> nodes_;
-	std::unordered_map<std::string, Address> node_addr_map_;
+	std::unordered_map<std::string, Address> online_node_addr_map_;
 	Cleaner cleaner_; // remove mqs.
 	int64_t offline_time_;
 	int64_t kill_time_;
@@ -434,16 +462,24 @@
 		    msg, head, [&](auto &body) { return center->MsgTag(head, body); }, replyer); \
 		return true;
 
-bool AddCenter(const std::string &id, const NodeCenter::Cleaner &cleaner)
+auto MakeReplyer(ShmSocket &socket, BHMsgHead &head, const std::string &proc_id)
 {
-	auto center_ptr = std::make_shared<Synced<NodeCenter>>(id, cleaner, 60s, 60s * 2);
-	auto MakeReplyer = [](ShmSocket &socket, BHMsgHead &head, const std::string &proc_id) {
-		return [&](auto &&rep_body) {
-			auto reply_head(InitMsgHead(GetType(rep_body), proc_id, head.ssn_id(), head.msg_id()));
-			auto remote = head.route(0).mq_id();
-			socket.Send(remote, reply_head, rep_body);
-		};
+	return [&](auto &&rep_body) {
+		auto reply_head(InitMsgHead(GetType(rep_body), proc_id, head.ssn_id(), head.msg_id()));
+		auto remote = head.route(0).mq_id();
+		socket.Send(remote, reply_head, rep_body);
 	};
+}
+
+bool AddCenter(std::shared_ptr<Synced<NodeCenter>> center_ptr)
+{
+	auto OnNodeInit = [center_ptr](ShmSocket &socket, MsgI &msg) {
+		auto &center = *center_ptr;
+		center->OnNodeInit(msg.Offset());
+	};
+	auto Nothing = [](ShmSocket &socket) {};
+
+	BHCenter::Install("#centetr.Init", OnNodeInit, Nothing, BHInitAddress(), 16);
 
 	auto OnCenterIdle = [center_ptr](ShmSocket &socket) {
 		auto &center = *center_ptr;
@@ -463,6 +499,7 @@
 		default: return false;
 		}
 	};
+	BHCenter::Install("#center.main", OnCenter, OnCenterIdle, BHTopicCenterAddress(), 1000);
 
 	auto OnBusIdle = [=](ShmSocket &socket) {};
 	auto OnPubSub = [=](ShmSocket &socket, MsgI &msg, BHMsgHead &head) -> bool {
@@ -503,7 +540,6 @@
 		}
 	};
 
-	BHCenter::Install("#center.reg", OnCenter, OnCenterIdle, BHTopicCenterAddress(), 1000);
 	BHCenter::Install("#center.bus", OnPubSub, OnBusIdle, BHTopicBusAddress(), 1000);
 
 	return true;
@@ -521,7 +557,12 @@
 
 bool BHCenter::Install(const std::string &name, MsgHandler handler, IdleHandler idle, const MQId mqid, const int mq_len)
 {
-	Centers()[name] = CenterInfo{name, handler, idle, mqid, mq_len};
+	Centers()[name] = CenterInfo{name, handler, MsgIHandler(), idle, mqid, mq_len};
+	return true;
+}
+bool BHCenter::Install(const std::string &name, MsgIHandler handler, IdleHandler idle, const MQId mqid, const int mq_len)
+{
+	Centers()[name] = CenterInfo{name, MsgHandler(), handler, idle, mqid, mq_len};
 	return true;
 }
 
@@ -529,10 +570,13 @@
 {
 	auto gc = [&](const MQId id) {
 		auto r = ShmSocket::Remove(shm, id);
-		printf("remove mq %ld : %s\n", id, (r ? "ok" : "failed"));
+		if (r) {
+			LOG_DEBUG() << "remove mq " << id << " ok\n";
+		}
 	};
 
-	AddCenter("#bhome_center", gc);
+	auto center_ptr = std::make_shared<Synced<NodeCenter>>("#bhome_center", gc, 6s, 6s * 2);
+	AddCenter(center_ptr);
 
 	for (auto &kv : Centers()) {
 		auto &info = kv.second;
@@ -544,7 +588,11 @@
 {
 	for (auto &kv : Centers()) {
 		auto &info = kv.second;
-		sockets_[info.name_]->Start(info.handler_, info.idle_);
+		if (info.handler_) {
+			sockets_[info.name_]->Start(info.handler_, info.idle_);
+		} else {
+			sockets_[info.name_]->Start(info.raw_handler_, info.idle_);
+		}
 	}
 
 	return true;

--
Gitblit v1.8.0