From c6964d5af25d4ec7ed9dbe7674dc4e3896b36ead Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期五, 16 四月 2021 16:10:02 +0800
Subject: [PATCH] node remove mq if never registered; refactor.

---
 src/topic_node.h |   66 +++++++++++++++++++--------------
 1 files changed, 38 insertions(+), 28 deletions(-)

diff --git a/src/topic_node.h b/src/topic_node.h
index 8852af1..5a3b86e 100644
--- a/src/topic_node.h
+++ b/src/topic_node.h
@@ -19,7 +19,6 @@
 #define TOPIC_NODE_YVKWA6TF
 
 #include "msg.h"
-#include "pubsub.h"
 #include "socket.h"
 #include <memory>
 
@@ -30,41 +29,48 @@
 class TopicNode
 {
 	SharedMemory &shm_;
-	MsgRegister info_;
+	ProcInfo info_;
+
+	SharedMemory &shm() { return shm_; }
 
 public:
+	typedef std::function<void(std::string &proc_id, const void *data, const int len)> DataCB;
 	TopicNode(SharedMemory &shm);
 	~TopicNode();
-	bool Register(const MsgRegister &body, MsgCommonReply &reply, const int timeout_ms);
-	bool RegisterRPC(const MsgRegisterRPC &body, MsgCommonReply &reply, const int timeout_ms);
+
+	// topic node
+	bool Register(ProcInfo &proc, MsgCommonReply &reply_body, const int timeout_ms);
+	bool Heartbeat(ProcInfo &proc, MsgCommonReply &reply_body, const int timeout_ms);
+	bool Heartbeat(const int timeout_ms);
 
 	// topic rpc server
-	typedef std::function<bool(const std::string &topic, const std::string &data, std::string &reply)> OnRequest;
-	bool ServerStart(OnRequest const &cb, const int nworker = 2);
-	bool ServerStop();
-	bool ServerRecvRequest(void *&src_info, std::string &topic, std::string &data, const int timeout_ms);
-	bool ServerSendReply(void *src_info, const std::string &data, const int timeout_ms);
+	typedef std::function<bool(const std::string &client_proc_id, const MsgRequestTopic &request, MsgRequestTopicReply &reply)> ServerCB;
+	bool ServerStart(ServerCB const &cb, const int nworker = 2);
+	bool ServerRegisterRPC(MsgTopicList &topics, MsgCommonReply &reply, const int timeout_ms);
+	bool ServerRecvRequest(void *&src_info, std::string &proc_id, MsgRequestTopic &request, const int timeout_ms);
+	bool ServerSendReply(void *src_info, const MsgRequestTopicReply &reply);
 
 	// topic client
-	typedef std::function<void(const std::string &data)> RequestResultCB;
+	typedef std::function<void(const BHMsgHead &head, const MsgRequestTopicReply &reply)> RequestResultCB;
 	bool ClientStartWorker(RequestResultCB const &cb, const int nworker = 2);
-	bool ClientStopWorker();
-	bool ClientAsyncRequest(const Topic &topic, const void *data, const size_t size, const int timeout_ms, const RequestResultCB &rrcb = RequestResultCB());
-	bool ClientAsyncRequest(const Topic &topic, const std::string &data, const int timeout_ms, const RequestResultCB &rrcb = RequestResultCB())
-	{
-		return ClientAsyncRequest(topic, data.data(), data.size(), timeout_ms, rrcb);
-	}
-	bool ClientSyncRequest(const Topic &topic, const void *data, const size_t size, std::string &out, const int timeout_ms);
-	bool ClientSyncRequest(const Topic &topic, const std::string &data, std::string &out, const int timeout_ms)
-	{
-		return ClientSyncRequest(topic, data.data(), data.size(), out, timeout_ms);
-	}
+	bool ClientAsyncRequest(const MsgRequestTopic &request, std::string &msg_id, const RequestResultCB &rrcb = RequestResultCB());
+	bool ClientSyncRequest(const MsgRequestTopic &request, std::string &proc_id, MsgRequestTopicReply &reply, const int timeout_ms);
 
-	void StopAll();
+	// publish
+	bool Publish(const MsgPublish &pub, const int timeout_ms);
+
+	// subscribe
+	typedef std::function<void(const std::string &proc_id, const MsgPublish &data)> SubDataCB;
+	bool SubscribeStartWorker(const SubDataCB &tdcb, int nworker = 2);
+	bool Subscribe(MsgTopicList &topics, MsgCommonReply &reply_body, const int timeout_ms);
+	bool RecvSub(std::string &proc_id, MsgPublish &pub, const int timeout_ms);
+
+	void Start(ServerCB const &server_cb, SubDataCB const &sub_cb, RequestResultCB &client_cb, int nworker = 2);
+	void Stop();
 
 private:
 	bool ClientQueryRPCTopic(const Topic &topic, bhome::msg::BHAddress &addr, const int timeout_ms);
-	const std::string &proc_id() { return info_.proc().proc_id(); }
+	const std::string &proc_id() { return info_.proc_id(); }
 
 	typedef bhome_msg::BHAddress Address;
 	class TopicQueryCache
@@ -106,14 +112,18 @@
 	// some sockets may be the same one, using functions make it easy to change.
 
 	auto &SockNode() { return sock_node_; }
+	auto &SockPub() { return SockNode(); }
 	auto &SockSub() { return sock_sub_; }
-	auto &SockRequest() { return sock_request_; }
-	auto &SockReply() { return sock_reply_; }
+	auto &SockClient() { return sock_client_; }
+	auto &SockServer() { return sock_server_; }
+	bool IsRegistered() const { return registered_.load(); }
 
 	ShmSocket sock_node_;
-	ShmSocket sock_request_;
-	ShmSocket sock_reply_;
-	SocketSubscribe sock_sub_;
+	ShmSocket sock_client_;
+	ShmSocket sock_server_;
+	ShmSocket sock_sub_;
+	std::atomic<bool> registered_;
+	std::atomic<bool> registered_ever_;
 
 	TopicQueryCache topic_query_cache_;
 };

--
Gitblit v1.8.0