From 903b27f875e5f2a872c1b309f354b18c0450f35a Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 04 六月 2021 11:46:18 +0800
Subject: [PATCH] allow tcp request with no dest, auto query topic.

---
 box/node_center.cpp |   33 +++++++++++++++++++++++++++++----
 1 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/box/node_center.cpp b/box/node_center.cpp
index b285c9f..5c24409 100644
--- a/box/node_center.cpp
+++ b/box/node_center.cpp
@@ -222,12 +222,37 @@
 	return node;
 }
 
-bool NodeCenter::PassRemoteRequestToLocal(const MQInfo &dest, BHMsgHead &head, const std::string &body_content, ShmSocket::RecvCB &&cb)
+bool NodeCenter::PassRemoteRequestToLocal(MQInfo dest, BHMsgHead &head, const std::string &body_content, ShmSocket::RecvCB &&cb)
 {
-	Node node(GetNode(dest.id_));
-	if (!node || !Valid(*node)) {
-		LOG_ERROR() << id() << " pass remote request, dest not found.";
+	Node node;
+
+	auto FindDest = [&]() {
+		auto pos = service_map_.find(head.topic());
+		if (pos != service_map_.end() && !pos->second.empty()) {
+			auto &clients = pos->second;
+			for (auto &cli : clients) {
+				node = cli.weak_node_.lock();
+				if (node && Valid(*node)) {
+					dest.id_ = cli.mq_id_;
+					dest.offset_ = cli.mq_abs_addr_;
+					return true;
+				}
+			}
+		}
 		return false;
+	};
+
+	if (dest.id_ == 0) {
+		if (!FindDest()) {
+			LOG_ERROR() << id() << " pass remote request, topic dest not found.";
+			return false;
+		}
+	} else {
+		node = GetNode(dest.id_);
+		if (!node || !Valid(*node)) {
+			LOG_ERROR() << id() << " pass remote request, dest not found.";
+			return false;
+		}
 	}
 
 	ShmSocket &sender(DefaultSender(node->shm_));

--
Gitblit v1.8.0