From 015c1a5d8b803dbd867f31426dd1391cd51b0ee3 Mon Sep 17 00:00:00 2001 From: liuxiaolong <liuxiaolong@aiotlink.com> Date: 星期二, 08 三月 2022 14:25:26 +0800 Subject: [PATCH] NewClient add Remote call options --- client.go | 10 +++- clientFactory.go | 62 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index ecda831..d7824d5 100644 --- a/client.go +++ b/client.go @@ -48,10 +48,14 @@ return nil } +var n_getNetNode = func(topic string) []*bhome_msg.MsgQueryTopicReply_BHNodeAddress{ + return nil +} + func InitGetNetNode(fn func(name string)[]*bhome_msg.MsgQueryTopicReply_BHNodeAddress) { - //if fn != nil { - // getNetNode = fn - //} + if fn != nil { + n_getNetNode = fn + } } var busReq = func(req *bhome_msg.MsgRequestTopic,dest []*bhome_msg.MsgQueryTopicReply_BHNodeAddress) ([]byte, error) { diff --git a/clientFactory.go b/clientFactory.go index 8e190b6..5120fba 100644 --- a/clientFactory.go +++ b/clientFactory.go @@ -5,18 +5,35 @@ ) //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) + if !checkRemoteNetNode(opt.ip, &opt.nodes) { + return nil + } } return &SBusClient{ nodes: opt.nodes, } } + type Option struct { - nodes []*bhome_msg.MsgQueryTopicReply_BHNodeAddress + nodes []*bhome_msg.MsgQueryTopicReply_BHNodeAddress + topic string + devId string + ip string + port int32 } type IOption interface { @@ -43,8 +60,51 @@ }) } +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 + }) +} + func defaultOption() Option { 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