| | |
| | | package bhomedbapi |
| | | |
| | | import ( |
| | | "basic.com/valib/bhomebus.git" |
| | | "encoding/json" |
| | | "basic.com/valib/c_bhomebus.git/api/bhsgo" |
| | | "basic.com/valib/c_bhomebus.git/proto/source/bhome_msg" |
| | | json "github.com/json-iterator/go" |
| | | "errors" |
| | | "strconv" |
| | | ) |
| | | |
| | | type SBusClient struct { |
| | | nodes []bhomebus.NetNode |
| | | nodes []*bhome_msg.MsgQueryTopicReply_BHNodeAddress |
| | | } |
| | | |
| | | type ProcInfo struct { |
| | | Name string `json:"name"` // 进程名称 |
| | | ID string `json:"id"` // 进程唯一标识 |
| | | Info string `json:"info"` // 进程的描述信息,用于区分同一进程名称下多个进程 |
| | | } |
| | | |
| | | type MsgInfo struct { |
| | | SrcProc ProcInfo `json:"srcProc"` // 源进程基本信息 |
| | | MsgType string `json:"msgType"` // 数据类型,可为请求、发布、订阅、应答等 |
| | | Topic string `json:"topic"` //请求主题 |
| | | Body []byte `json:"body"` //请求内容或者反馈结果 |
| | | } |
| | | |
| | | type Request struct { |
| | |
| | | } |
| | | |
| | | func (sc SBusClient) DoGetRequest(url string, params map[string]string, headers map[string]string) ([]byte, error) { |
| | | if sc.nodes == nil || len(sc.nodes) == 0 { |
| | | return nil, errors.New("invalid netNodes") |
| | | } |
| | | |
| | | req := Request{ |
| | | Path: url, |
| | |
| | | if err !=nil { |
| | | return nil,err |
| | | } |
| | | rMsg := MsgInfo{ |
| | | Topic: req.Path, |
| | | Body: rb, |
| | | } |
| | | rData, err := json.Marshal(rMsg) |
| | | if err != nil { |
| | | return nil, err |
| | | rMsg := bhome_msg.MsgRequestTopic{ |
| | | Topic: []byte(req.Path), |
| | | Data: rb, |
| | | } |
| | | |
| | | return busReq(rData, sc.nodes) |
| | | return busReq(&rMsg, sc.nodes) |
| | | //return doReq(req, sc.nodes) |
| | | } |
| | | |
| | | func (sc SBusClient) DoPostRequest(url string, contentType string, body map[string]interface{}, params map[string]string, headers map[string]string) ([]byte, error) { |
| | | if sc.nodes == nil || len(sc.nodes) == 0 { |
| | | return nil, errors.New("invalid port") |
| | | } |
| | | |
| | | req := Request{ |
| | | Path: url, |
| | | Method: "POST", |
| | |
| | | if contentType == CONTENT_TYPE_FORM || contentType == CONTENT_TYPE_MULFORM { |
| | | if body != nil { |
| | | req.PostFormMap = make(map[string][]string) |
| | | req.FormMap = map[string][]string{} |
| | | for k,v := range body { |
| | | switch v.(type) { |
| | | case string: |
| | |
| | | if err !=nil { |
| | | return nil,err |
| | | } |
| | | rMsg := MsgInfo{ |
| | | Topic: req.Path, |
| | | Body: rb, |
| | | } |
| | | rData, err := json.Marshal(rMsg) |
| | | if err != nil { |
| | | return nil, err |
| | | rMsg := bhome_msg.MsgRequestTopic{ |
| | | Topic: []byte(req.Path), |
| | | Data: rb, |
| | | } |
| | | |
| | | return busReq(rData, sc.nodes) |
| | | return busReq(&rMsg, sc.nodes) |
| | | } |
| | | |
| | | func (sc SBusClient) DoPutRequest(url string, contentType string, body map[string]interface{}, headers map[string]string) ([]byte, error) { |
| | | if sc.nodes == nil || len(sc.nodes) == 0 { |
| | | return nil, errors.New("invalid port") |
| | | } |
| | | |
| | | req := Request{ |
| | | Path: url, |
| | | Method: "PUT", |
| | |
| | | if err !=nil { |
| | | return nil,err |
| | | } |
| | | rMsg := MsgInfo{ |
| | | Topic: req.Path, |
| | | Body: rb, |
| | | } |
| | | rData, err := json.Marshal(rMsg) |
| | | if err != nil { |
| | | return nil, err |
| | | rMsg := bhome_msg.MsgRequestTopic{ |
| | | Topic: []byte(req.Path), |
| | | Data: rb, |
| | | } |
| | | |
| | | return busReq(rData, sc.nodes) |
| | | return busReq(&rMsg, sc.nodes) |
| | | } |
| | | |
| | | func (sc SBusClient) DoDeleteRequest(url string, contentType string, body map[string]interface{}, headers map[string]string) ([]byte, error) { |
| | | if sc.nodes == nil || len(sc.nodes) == 0 { |
| | | return nil, errors.New("invalid port") |
| | | } |
| | | |
| | | req := Request{ |
| | | Path: url, |
| | | Method: "DELETE", |
| | |
| | | if err !=nil { |
| | | return nil,err |
| | | } |
| | | rMsg := MsgInfo{ |
| | | Topic: req.Path, |
| | | Body: rb, |
| | | } |
| | | rData, err := json.Marshal(rMsg) |
| | | if err != nil { |
| | | return nil, err |
| | | rMsg := bhome_msg.MsgRequestTopic{ |
| | | Topic: []byte(req.Path), |
| | | Data: rb, |
| | | } |
| | | |
| | | return busReq(rData, sc.nodes) |
| | | return busReq(&rMsg, sc.nodes) |
| | | } |
| | | |
| | | func fillParam(req *Request,headers map[string]string, params map[string]string, body map[string]interface{}) { |
| | |
| | | } else { |
| | | req.Body = b |
| | | } |
| | | |
| | | } |
| | | |
| | | //在此处使用OpenSocket会在Ctrl-C的时候,导致socket并未成功Close,共享内存块不会释放. |
| | | //所以控制共享内存块的成功回收需要在上层做,然后调InitDoReq将函数指针传递进来 |
| | | //func doReq(req Request, nodes []bhomebus.NetNode) ([]byte,error) { |
| | | // rb, err := json.Marshal(req) |
| | | // if err !=nil { |
| | | // return nil,err |
| | | // } |
| | | // rMsg := MsgInfo{ |
| | | // Topic: req.Path, |
| | | // Body: rb, |
| | | // } |
| | | // data, err := json.Marshal(rMsg) |
| | | // if err != nil { |
| | | // return nil, err |
| | | // } |
| | | // s := bhomebus.OpenSocket() |
| | | // defer s.Close() |
| | | // var ret []bhomebus.Mesg |
| | | // if n := s.SendandrecvTimeout(nodes, data, &ret, 5000);n == 0 { //n==0表示没有请求成功 |
| | | // return nil, fmt.Errorf("doReq s.SendandrecvTimeout result n:%d", n) |
| | | // } else { |
| | | // if len(ret) > 0 { |
| | | // return ret[0].Data, nil |
| | | // } |
| | | // return nil, fmt.Errorf("no any response") |
| | | // } |
| | | //} |
| | | /** |
| | | * 新版bhs直接发起请求,不会产生共享内存泄漏的问题? |
| | | */ |
| | | func doReq(req *bhome_msg.MsgRequestTopic, destArr []bhome_msg.BHAddress) ([]byte, error) { |
| | | dest := bhome_msg.BHAddress{} |
| | | if destArr != nil && len(destArr) > 0 { |
| | | dest = destArr[0] |
| | | } |
| | | pid := "" |
| | | r := bhome_msg.MsgRequestTopicReply{} |
| | | if bhsgo.Request(&dest, req, &pid, &r, 5000) { |
| | | return r.Data, nil |
| | | } else { |
| | | logPrint("bhsgo.Request request err:", r.Errmsg) |
| | | return nil, errors.New("bhsgo.Request return false") |
| | | } |
| | | } |