From dc12826dd61ce18fac3a9561c5843d30a0cf9660 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 02 四月 2021 15:48:53 +0800
Subject: [PATCH] add request topic cache; refactor req/rep center.

---
 src/reqrep.h |   39 ++++++++++++++++++++++++++++++++++++++-
 1 files changed, 38 insertions(+), 1 deletions(-)

diff --git a/src/reqrep.h b/src/reqrep.h
index 2971403..e8a38f7 100644
--- a/src/reqrep.h
+++ b/src/reqrep.h
@@ -18,6 +18,7 @@
 #ifndef REQREP_ACEH09NK
 #define REQREP_ACEH09NK
 
+#include "bh_util.h"
 #include "defs.h"
 #include "msg.h"
 #include "socket.h"
@@ -58,7 +59,43 @@
 	bool QueryRPCTopic(const std::string &topic, bhome::msg::BHAddress &addr, const int timeout_ms);
 	std::unordered_map<std::string, RecvCB> async_cbs_;
 
-	std::pair<std::string, bhome::msg::BHAddress> tmp_cache_;
+	typedef bhome_msg::BHAddress Address;
+	class TopicCache
+	{
+		class Impl
+		{
+			typedef std::unordered_map<std::string, Address> Store;
+			Store store_;
+
+		public:
+			bool Find(const std::string &topic, Address &addr)
+			{
+				auto pos = store_.find(topic);
+				if (pos != store_.end()) {
+					addr = pos->second;
+					return true;
+				} else {
+					return false;
+				}
+			}
+			bool Update(const std::string &topic, const Address &addr)
+			{
+				store_[topic] = addr;
+				return true;
+			}
+		};
+		Synced<Impl> impl_;
+		// Impl &impl()
+		// {
+		// 	thread_local Impl impl;
+		// 	return impl;
+		// }
+
+	public:
+		bool Find(const std::string &topic, Address &addr) { return impl_->Find(topic, addr); }
+		bool Update(const std::string &topic, const Address &addr) { return impl_->Update(topic, addr); }
+	};
+	TopicCache topic_cache_;
 };
 
 class SocketReply : private ShmSocket

--
Gitblit v1.8.0