| | |
| | | package bhomeclient |
| | | |
| | | import ( |
| | | "basic.com/valib/bhshmq.git/api/bhsgo" |
| | | "basic.com/valib/bhshmq.git/proto/source/bhome_msg" |
| | | "basic.com/valib/c_bhomebus.git/api/bhsgo" |
| | | "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg" |
| | | "context" |
| | | "encoding/json" |
| | | "errors" |
| | |
| | | "sync" |
| | | "time" |
| | | "unsafe" |
| | | |
| | | "github.com/bytedance/sonic" |
| | | ) |
| | | |
| | | type MsgReq struct { |
| | |
| | | |
| | | handle.printLog("register done!" ) |
| | | |
| | | handle.wg = &sync.WaitGroup{} |
| | | |
| | | //有订阅消息才需要启动协程接收消息 |
| | | if ri.SubTopic != nil && len(ri.SubTopic) > 0 { |
| | | if len(ri.SubTopic) > 0 { |
| | | handle.printLog("sub topics") |
| | | var subList bhome_msg.MsgTopicList |
| | | for _,v := range ri.SubTopic { |
| | |
| | | } |
| | | |
| | | var subReply bhome_msg.MsgCommonReply |
| | | if bhsgo.Subscribe(&subList, &subReply, handle.conf.sendTimeOut) { |
| | | //启动订阅信息接收 |
| | | handle.wg.Add(1) |
| | | go recvSubRoutine(ctx, handle.wg, handle.ChSub, handle.printLog) |
| | | if !bhsgo.Subscribe(&subList, &subReply, handle.conf.sendTimeOut) { |
| | | handle.printLog("bhsgo.Subscribe ret false") |
| | | } |
| | | } |
| | | |
| | | if len(ri.SubNetTopic) > 0 { |
| | | handle.printLog("sub net topics") |
| | | var subNetList bhome_msg.MsgTopicList |
| | | for _,v := range ri.SubNetTopic { |
| | | subNetList.TopicList = append(subNetList.TopicList, []byte(v)) |
| | | } |
| | | var subNetReply bhome_msg.MsgCommonReply |
| | | if !bhsgo.SubscribeNet(&subNetList, &subNetReply, handle.conf.sendTimeOut) { |
| | | handle.printLog("bhsgo.SubscribeNet ret false") |
| | | } |
| | | } |
| | | |
| | | if len(ri.SubTopic) > 0 || len(ri.SubNetTopic) > 0 { |
| | | //启动订阅信息接收 |
| | | handle.wg.Add(1) |
| | | go recvSubRoutine(ctx, handle.wg, handle.ChSub, handle.printLog) |
| | | } |
| | | |
| | | return handle, nil |
| | |
| | | for { |
| | | select { |
| | | case <-ctx.Done(): |
| | | logFn("recvRoutine ctx.Done") |
| | | logFn("recvSubRoutine ctx.Done") |
| | | wg.Done() |
| | | return |
| | | default: |
| | | if bhsgo.ReadSub(&procId, &msg, 100) { |
| | | ch <- msg |
| | | logFn("ReadSub topic:", string(msg.Topic), " data:", string(msg.Data)) |
| | | logFn("ReadSub topic:", string(msg.Topic), " len(data):", len(msg.Data)) |
| | | |
| | | procId = "" |
| | | msg.Reset() |
| | |
| | | func (h *BHBus) DeRegister(dri *RegisterInfo) error { |
| | | |
| | | h.printLog("DeRegister") |
| | | req := bhome_msg.ProcInfo{ |
| | | ProcId: []byte(h.ri.Proc.ID), |
| | | Name: []byte(h.ri.Proc.Name), |
| | | } |
| | | reply := bhome_msg.MsgCommonReply{} |
| | | if !bhsgo.Unregister(&req, &reply, h.conf.sendTimeOut) { |
| | | h.printLog("Unregister false! ") |
| | | return errors.New("Unregister false! ") |
| | | } |
| | | return nil |
| | | } |
| | | |
| | |
| | | //获取topic对应的key |
| | | //如果传了serverId不为空,则获取指定机器上的topic-key |
| | | //如果server为空,则获取所有节点上topic-key |
| | | func (h *BHBus) GetNetNodeByTopic(serverId string,srcProc *ProcInfo, topic string) ([]bhome_msg.BHAddress,error) { |
| | | |
| | | return nil, nil |
| | | func (h *BHBus) GetNetNodeByTopic(serverId string,srcProc *ProcInfo, topic string) ([]*bhome_msg.MsgQueryTopicReply_BHNodeAddress,error) { |
| | | dest := bhome_msg.BHAddress{} |
| | | reqTopic := bhome_msg.MsgQueryTopic{ |
| | | Topic: []byte(topic), |
| | | } |
| | | rep := bhome_msg.MsgQueryTopicReply{} |
| | | if bhsgo.QueryTopicAddress(&dest, &reqTopic, &rep, h.conf.sendTimeOut) { |
| | | return rep.NodeAddress, nil |
| | | } |
| | | if rep.Errmsg != nil { |
| | | h.printLog("QueryTopicAddress errCode:", rep.Errmsg.ErrCode, "errMsg:", string(rep.Errmsg.ErrString)) |
| | | return nil, errors.New(string(rep.Errmsg.ErrString)) |
| | | } |
| | | return nil, errors.New("bhsgo.QueryTopicAddress ret false") |
| | | } |
| | | |
| | | func (h *BHBus) Request(serverId string, req *bhome_msg.MsgRequestTopic, milliSecs int) (*Reply, error) { |
| | |
| | | if bhsgo.Request(&dest, req, &pid, &mrt, milliSecs) { |
| | | var reply Reply |
| | | if err := json.Unmarshal(mrt.Data, &reply); err != nil { |
| | | h.printLog("bhsgo.Request ret true, but unmarshal err:", err, " mrt.Data:", string(mrt.Data)) |
| | | return nil,err |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | func (h *BHBus) RequestOnly(req *bhome_msg.MsgRequestTopic, destArr []bhome_msg.BHAddress) ([]byte, error) { |
| | | func (h *BHBus) RequestOnly(req *bhome_msg.MsgRequestTopic, destArr []*bhome_msg.MsgQueryTopicReply_BHNodeAddress) ([]byte, error) { |
| | | dest := bhome_msg.BHAddress{} |
| | | if destArr != nil && len(destArr) > 0 { |
| | | dest = destArr[0] |
| | | if destArr[0].Addr != nil { |
| | | dest = *(destArr[0].Addr) |
| | | } |
| | | } |
| | | pid := "" |
| | | r := bhome_msg.MsgRequestTopicReply{} |
| | |
| | | } |
| | | |
| | | func (h *BHBus) Reply(src unsafe.Pointer, i *Reply) error { |
| | | data,err := json.Marshal(*i) |
| | | defer func() { |
| | | i = nil |
| | | }() |
| | | |
| | | data,err := sonic.Marshal(i) |
| | | if err != nil { |
| | | return err |
| | | } |
| | | rep := bhome_msg.MsgRequestTopicReply{ |
| | | Data: data, |
| | | } |
| | | |
| | | if bhsgo.SendReply(src, &rep) { |
| | | return nil |
| | | } |