From 913063a73b9ffb0d0a1cf3515cec9326102e86e1 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 29 十二月 2020 16:34:51 +0800
Subject: [PATCH] 添加请求注册中心的RequestCenter方法,以及获取本机所有已注册进程列表

---
 message.go   |   13 ++++++++++++-
 micronode.go |   24 ++++++++++++++++++++++++
 hbusc.go     |   23 +++++++++++++++++++++++
 3 files changed, 59 insertions(+), 1 deletions(-)

diff --git a/hbusc.go b/hbusc.go
index 8136821..6ff4175 100644
--- a/hbusc.go
+++ b/hbusc.go
@@ -441,6 +441,29 @@
 	return nil
 }
 
+func (h *BHBus) RequestCenter(req *MsgInfo) (*CommonReply, error) {
+	data, err := json.Marshal(*req)
+	if err != nil {
+		return nil, err
+	}
+	rNodes := append([]bhomebus.NetNode{}, bhomebus.NetNode{
+		Key: KEY_QUERY,
+	})
+	h.mtxWorker.Lock()
+	defer h.mtxWorker.Unlock()
+	var ret []bhomebus.Mesg
+	if n := h.sockWorker.sock.SendandrecvTimeout(rNodes, data,&ret, h.conf.sendTimeOut);n == 0 {
+		if len(ret) > 0 {
+			var cr *CommonReply
+			if err = json.Unmarshal(ret[0].Data, cr); err == nil {
+				return cr, nil
+			}
+		}
+	}
+	return nil, fmt.Errorf("request center err")
+}
+
+
 //鍚戜富棰橀�氶亾涓彂甯冩秷鎭�
 func (h *BHBus) Pub(nodes []bhomebus.NetNode, msg *MsgInfo) error {
 	data,err := json.Marshal(*msg)
diff --git a/message.go b/message.go
index 0f252a9..aea3e7e 100644
--- a/message.go
+++ b/message.go
@@ -4,7 +4,7 @@
 	PORT_DEFAULTPROXY int = 5000
 	KEY_REGISTER      int = 101 //澶勭悊TOPIC_REGISTER / TOPIC_LEAVE鍜孴OPIC_UPDATETOPIC
 	KEY_HEARTBEAT     int = 102 //澶勭悊TOPIC_HEARTBEAT
-	KEY_QUERY         int = 103 //澶勭悊TOPIC_QUERYKEY鍜孴OPIC_QUERYTOPIC
+	KEY_QUERY         int = 103 //澶勭悊TOPIC_QUERYKEY鍜孴OPIC_QUERYTOPIC鍜孴OPIC_QUERYPROC
 )
 
 const (
@@ -16,6 +16,7 @@
 	TOPIC_UPDATETOPIC        string = "Topic_UpdateTopic"
 	TOPIC_QUERYKEY           string = "Topic_QueryKey"
 	TOPIC_QUERYTOPIC         string = "Topic_QueryTopic"
+	TOPIC_QUERYPROC          string = "Topic_QueryProc"
 )
 
 const (
@@ -104,4 +105,14 @@
 	SvrInfo   		ServerInfo     		`json:"svrInfo"`        //鑺傜偣鍩烘湰淇℃伅
 	Topic2Key 		map[string]int 		`json:"topic2Key"`      //topic-replyKey鐨勫搴斿叧绯�
 	Status    		int            		`json:"status"`			//鑺傜偣鐘舵��
+}
+
+
+//宸叉敞鍐岀殑Proc杩涚▼淇℃伅
+type RegisteredClient struct {
+	Info           RegisterInfo 		`json:"info"`
+	ReplyKey       int					`json:"replyKey"`
+	HeartbeatCount int					`json:"heartbeatCount"`
+	DeadCount      int					`json:"deadCount"`
+	Status         int					`json:"status"`
 }
\ No newline at end of file
diff --git a/micronode.go b/micronode.go
index 87e9fad..e9048cd 100644
--- a/micronode.go
+++ b/micronode.go
@@ -191,6 +191,30 @@
 	return netNodes
 }
 
+func (ms *MicroNode) GetRegisteredClient() ([]RegisteredClient,error) {
+	r := MsgInfo{
+		SrcProc: *ms.procInfo,
+		MsgType: MesgType_ReqRep,
+		Topic: TOPIC_QUERYPROC,
+	}
+	cr, err := ms.handle.RequestCenter(&r)
+	if err != nil {
+		return nil, err
+	}
+	if cr.Status == REPLY_SUCCESS && cr.Body != nil {
+		var list []RegisteredClient
+		err = json.Unmarshal(cr.Body, &list)
+		if err == nil {
+			return list, nil
+		} else {
+			ms.printLog("unmarshal to RegisteredClient list err:", err)
+		}
+	} else {
+		ms.printLog("request center failed,status:", cr.Status, "desc:", cr.Desc)
+	}
+	return nil, fmt.Errorf("GetRegisteredClient list failed")
+}
+
 func (ms *MicroNode) serve(msgR *MsgInfo, p int) {
 	var reqBody Request
 	err := json.Unmarshal(msgR.Body, &reqBody)

--
Gitblit v1.8.0