From 01dfd9dc8de7b19f9dfa4284722e01bbd5837801 Mon Sep 17 00:00:00 2001 From: zhangmeng <775834166@qq.com> Date: 星期五, 19 一月 2024 09:10:30 +0800 Subject: [PATCH] replace json to json-iterator --- clientFactory.go | 71 +++++++++++++++++++++++++++++++++-- 1 files changed, 67 insertions(+), 4 deletions(-) diff --git a/clientFactory.go b/clientFactory.go index 0afe032..1b3711a 100644 --- a/clientFactory.go +++ b/clientFactory.go @@ -1,20 +1,40 @@ -package dbapi +package bhomedbapi -import "basic.com/valib/bhomebus.git" +import ( + "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg" +) //Factory new client +/* + 璺ㄦ満璇锋眰鏃讹紝topic devId ip蹇呴』闈炵┖ +*/ func NewClient(opts ...IOption) Client { opt := defaultOption() for _,o := range opts { o.apply(&opt) + } + if opt.ip != "" { //璺ㄦ満璇锋眰 + if opt.topic == "" { + return nil + } + opt.nodes = n_getNetNode(opt.topic) + logPrint("NewClient ip:", opt.ip, " topic:", opt.topic, " nodes:", opt.nodes) + if !checkRemoteNetNode(opt.ip, &opt.nodes) { + return nil + } } return &SBusClient{ nodes: opt.nodes, } } + type Option struct { - nodes []bhomebus.NetNode + nodes []*bhome_msg.MsgQueryTopicReply_BHNodeAddress + topic string + devId string + ip string + port int32 } type IOption interface { @@ -35,9 +55,33 @@ } } -func WithNodes(nodeArr []bhomebus.NetNode) IOption { +func WithNodes(nodeArr []*bhome_msg.MsgQueryTopicReply_BHNodeAddress) IOption { return newFuncOption(func(o *Option) { o.nodes = nodeArr + }) +} + +func WithTopic(topic string) IOption { + return newFuncOption(func(o *Option){ + o.topic = topic + }) +} + +func WithIp(ip string) IOption { + return newFuncOption(func(o *Option) { + o.ip = ip + }) +} + +func WithDevId(devId string) IOption { + return newFuncOption(func(o *Option) { + o.devId = devId + }) +} + +func WithPort(port int32) IOption { + return newFuncOption(func(o *Option) { + o.port = port }) } @@ -45,4 +89,23 @@ return Option{ } +} + +func checkRemoteNetNode(rip string, netNode *[]*bhome_msg.MsgQueryTopicReply_BHNodeAddress) bool { + if netNode == nil || len(*netNode) == 0 { + return false + } + + for _,n := range *netNode { + if n.Addr != nil && string(n.Addr.Ip) == rip && n.Addr.Port >0 { + *netNode = append([]*bhome_msg.MsgQueryTopicReply_BHNodeAddress{}, &bhome_msg.MsgQueryTopicReply_BHNodeAddress{ + Addr: &bhome_msg.BHAddress{ + Ip: []byte(rip), + Port: n.Addr.Port, + }, + }) + return true + } + } + return false } \ No newline at end of file -- Gitblit v1.8.0