From 1fbfef2a51db4a3bac9d8a5b87af94a40a913b7a Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期日, 25 四月 2021 15:33:40 +0800
Subject: [PATCH] change mqid from uuid to uint64.

---
 box/center.cpp |   32 ++++++++++++++------------------
 1 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/box/center.cpp b/box/center.cpp
index badfbfe..d920ff7 100644
--- a/box/center.cpp
+++ b/box/center.cpp
@@ -37,9 +37,9 @@
 {
 public:
 	typedef std::string ProcId;
-	typedef std::string Address;
+	typedef MQId Address;
 	typedef bhome_msg::ProcInfo ProcInfo;
-	typedef std::function<void(Address const &)> Cleaner;
+	typedef std::function<void(Address const)> Cleaner;
 
 private:
 	enum {
@@ -84,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) :
@@ -182,7 +182,7 @@
 	{
 		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};
@@ -240,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};
@@ -253,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) {
@@ -426,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);
 		};
 	};
 
@@ -473,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);
@@ -505,28 +505,24 @@
 	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("#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