From 238b3cab1bc1c6b2f1b2f03df3a78c8d1db6c685 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期日, 25 四月 2021 15:53:08 +0800
Subject: [PATCH] 重新生成pb.go

---
 box/center.cpp |   89 +++++++++++++++++++++-----------------------
 1 files changed, 42 insertions(+), 47 deletions(-)

diff --git a/box/center.cpp b/box/center.cpp
index a95e82d..d920ff7 100644
--- a/box/center.cpp
+++ b/box/center.cpp
@@ -27,7 +27,6 @@
 
 using namespace bhome_shm;
 using namespace bhome_msg;
-using namespace bhome::msg;
 typedef BHCenter::MsgHandler Handler;
 
 namespace
@@ -38,9 +37,9 @@
 {
 public:
 	typedef std::string ProcId;
-	typedef std::string Address;
-	typedef bhome::msg::ProcInfo ProcInfo;
-	typedef std::function<void(Address const &)> Cleaner;
+	typedef MQId Address;
+	typedef bhome_msg::ProcInfo ProcInfo;
+	typedef std::function<void(Address const)> Cleaner;
 
 private:
 	enum {
@@ -85,7 +84,7 @@
 		WeakNode weak_node_;
 		bool operator<(const TopicDest &a) const { return mq_ < a.mq_; }
 	};
-	inline const std::string &SrcAddr(const BHMsgHead &head) { return head.route(0).mq_id(); }
+	inline MQId SrcAddr(const BHMsgHead &head) { return head.route(0).mq_id(); }
 	inline bool MatchAddr(std::set<Address> const &addrs, const Address &addr) { return addrs.find(addr) != addrs.end(); }
 
 	NodeCenter(const std::string &id, const Cleaner &cleaner, const int64_t offline_time, const int64_t kill_time) :
@@ -97,12 +96,9 @@
 	NodeCenter(const std::string &id, const Cleaner &cleaner, const steady_clock::duration offline_time, const steady_clock::duration kill_time) :
 	    NodeCenter(id, cleaner, duration_cast<seconds>(offline_time).count(), duration_cast<seconds>(kill_time).count()) {}
 
-	const std::string &id() const
-	{
-		return id_;
-	} // no need to lock.
+	// center name, no relative to shm.
+	const std::string &id() const { return id_; }
 
-	//TODO maybe just return serialized string.
 	MsgCommonReply Register(const BHMsgHead &head, MsgRegister &msg)
 	{
 		if (msg.proc().proc_id() != head.proc_id()) {
@@ -170,11 +166,23 @@
 		return HandleMsg<MsgCommonReply, Func>(head, op);
 	}
 
+	MsgCommonReply Unregister(const BHMsgHead &head, MsgUnregister &msg)
+	{
+		return HandleMsg(
+		    head, [&](Node node) -> MsgCommonReply {
+			    NodeInfo &ni = *node;
+			    auto now = NowSec(); // just set to offline.
+			    ni.state_.timestamp_ = now - offline_time_;
+			    ni.state_.UpdateState(now, offline_time_, kill_time_);
+			    return MakeReply(eSuccess);
+		    });
+	}
+
 	MsgCommonReply RegisterRPC(const BHMsgHead &head, MsgRegisterRPC &msg)
 	{
 		return HandleMsg(
 		    head, [&](Node node) -> MsgCommonReply {
-			    auto &src = SrcAddr(head);
+			    auto src = SrcAddr(head);
 			    auto &topics = msg.topics().topic_list();
 			    node->services_[src].insert(topics.begin(), topics.end());
 			    TopicDest dest = {src, node};
@@ -210,20 +218,17 @@
 		auto query = [&](Node self) -> MsgQueryTopicReply {
 			auto pos = service_map_.find(req.topic());
 			if (pos != service_map_.end() && !pos->second.empty()) {
-				// now just find first one.
-				const TopicDest &dest = *(pos->second.begin());
-				Node dest_node(dest.weak_node_.lock());
-				if (!dest_node) {
-					service_map_.erase(pos);
-					return MakeReply<Reply>(eOffline, "topic server offline.");
-				} else if (!Valid(*dest_node)) {
-					return MakeReply<Reply>(eNoRespond, "topic server not responding.");
-				} else {
-					MsgQueryTopicReply reply = MakeReply<Reply>(eSuccess);
-					reply.mutable_address()->set_mq_id(dest.mq_);
-					return reply;
+				auto &clients = pos->second;
+				Reply reply = MakeReply<Reply>(eSuccess);
+				for (auto &dest : clients) {
+					Node dest_node(dest.weak_node_.lock());
+					if (dest_node && Valid(*dest_node)) {
+						auto node_addr = reply.add_node_address();
+						node_addr->set_proc_id(dest_node->proc_.proc_id());
+						node_addr->mutable_addr()->set_mq_id(dest.mq_);
+					}
 				}
-
+				return reply;
 			} else {
 				return MakeReply<Reply>(eNotFound, "topic server not found.");
 			}
@@ -235,7 +240,7 @@
 	MsgCommonReply Subscribe(const BHMsgHead &head, const MsgSubscribe &msg)
 	{
 		return HandleMsg(head, [&](Node node) {
-			auto &src = SrcAddr(head);
+			auto src = SrcAddr(head);
 			auto &topics = msg.topics().topic_list();
 			node->subscriptions_[src].insert(topics.begin(), topics.end());
 			TopicDest dest = {src, node};
@@ -248,7 +253,7 @@
 	MsgCommonReply Unsubscribe(const BHMsgHead &head, const MsgUnsubscribe &msg)
 	{
 		return HandleMsg(head, [&](Node node) {
-			auto &src = SrcAddr(head);
+			auto src = SrcAddr(head);
 			auto pos = node->subscriptions_.find(src);
 
 			auto RemoveSubTopicDestRecord = [this](const Topic &topic, const TopicDest &dest) {
@@ -399,7 +404,7 @@
 
 Handler Combine(const Handler &h1, const Handler &h2)
 {
-	return [h1, h2](ShmSocket &socket, bhome_msg::MsgI &msg, bhome::msg::BHMsgHead &head) {
+	return [h1, h2](ShmSocket &socket, bhome_msg::MsgI &msg, bhome_msg::BHMsgHead &head) {
 		return h1(socket, msg, head) || h2(socket, msg, head);
 	};
 }
@@ -421,8 +426,8 @@
 	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.msg_id()));
-			auto &remote = head.route(0).mq_id();
-			socket.Send(remote.data(), reply_head, rep_body);
+			auto remote = head.route(0).mq_id();
+			socket.Send(remote, reply_head, rep_body);
 		};
 	};
 
@@ -437,6 +442,7 @@
 		switch (head.type()) {
 			CASE_ON_MSG_TYPE(Register);
 			CASE_ON_MSG_TYPE(Heartbeat);
+			CASE_ON_MSG_TYPE(Unregister);
 
 			CASE_ON_MSG_TYPE(RegisterRPC);
 			CASE_ON_MSG_TYPE(QueryTopic);
@@ -458,7 +464,6 @@
 				replyer(reply);
 			} else {
 				replyer(MakeReply(eSuccess));
-				if (!msg.EnableRefCount(socket.shm())) { return; } // no memory?
 				if (clients.empty()) { return; }
 
 				auto it = clients.begin();
@@ -468,7 +473,7 @@
 					if (node) {
 						// should also make sure that mq is not killed before msg expires.
 						// it would be ok if (kill_time - offline_time) is longer than expire time.
-						socket.Send(cli.mq_.data(), msg);
+						socket.Send(cli.mq_, msg);
 						++it;
 					} else {
 						it = clients.erase(it);
@@ -494,40 +499,30 @@
 
 } // namespace
 
-SharedMemory &BHomeShm()
-{
-	static SharedMemory shm("bhome_default_shm_v0", 1024 * 1024 * 512);
-	return shm;
-}
-
 BHCenter::CenterRecords &BHCenter::Centers()
 {
 	static CenterRecords rec;
 	return rec;
 }
 
-bool BHCenter::Install(const std::string &name, MsgHandler handler, IdleHandler idle, const std::string &mqid, const int mq_len)
+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};
 	return true;
 }
-bool BHCenter::Install(const std::string &name, MsgHandler handler, IdleHandler idle, const MQId &mqid, const int mq_len)
-{
-	return Install(name, handler, idle, std::string((const char *) &mqid, sizeof(mqid)), mq_len);
-}
 
 BHCenter::BHCenter(Socket::Shm &shm)
 {
-	auto gc = [&](const std::string &id) {
-		auto r = ShmSocket::Remove(shm, *(MQId *) id.data());
-		printf("remove mq : %s\n", r ? "ok" : "failed");
+	auto gc = [&](const MQId id) {
+		auto r = ShmSocket::Remove(shm, id);
+		printf("remove mq %ld : %s\n", id, (r ? "ok" : "failed"));
 	};
 
-	AddCenter("#center", gc);
+	AddCenter("#bhome_center", gc);
 
 	for (auto &kv : Centers()) {
 		auto &info = kv.second;
-		sockets_[info.name_] = std::make_shared<ShmSocket>(shm, *(MQId *) info.mqid_.data(), info.mq_len_);
+		sockets_[info.name_] = std::make_shared<ShmSocket>(shm, info.mqid_, info.mq_len_);
 	}
 }
 

--
Gitblit v1.8.0