liuxiaolong
2021-01-26 cb76e3197809c7f6e3e27269271bff97e5252ada
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package bhomedbapi
 
import (
    "basic.com/valib/bhomebus.git"
    "encoding/json"
    "errors"
    "strconv"
)
 
type SBusClient struct {
    nodes []bhomebus.NetNode
}
 
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 {
    Path                    string                  `json:"path"`
    Method                  string                  `json:"method"`
    ContentType             string                  `json:"contentType"`
    HeaderMap               map[string][]string     `json:"headerMap"`
    QueryMap                map[string][]string     `json:"queryMap"`
    FormMap                 map[string][]string     `json:"formMap"`
    PostFormMap             map[string][]string     `json:"postFormMap"`
    Body                    []byte                  `json:"body"`
}
 
type reply struct {
    Success             bool                         `json:"success"`
    Msg                 string                         `json:"msg"`
    Data                 interface{}                 `json:"data"`
}
 
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,
        Method:      "GET",
        ContentType: CONTENT_TYPE_JSON,
    }
    fillParam(&req, headers, params, nil)
 
    rb, err := json.Marshal(req)
    if err !=nil {
        return nil,err
    }
    rMsg := MsgInfo{
        Topic: req.Path,
        Body: rb,
    }
    rData, err := json.Marshal(rMsg)
    if err != nil {
        return nil, err
    }
 
    return busReq(rData, 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",
        ContentType: contentType,
    }
    fillParam(&req, headers, params, body)
    if contentType == CONTENT_TYPE_FORM || contentType == CONTENT_TYPE_MULFORM {
        if body != nil {
            req.PostFormMap = make(map[string][]string)
            for k,v := range body {
                switch v.(type) {
                case string:
                    req.FormMap[k] = []string{v.(string)}
                    req.PostFormMap[k] = []string{v.(string)}
                case int:
                    req.FormMap[k] = []string{strconv.Itoa(v.(int))}
                    req.PostFormMap[k] = []string{strconv.Itoa(v.(int))}
                case bool:
                    req.FormMap[k] = []string{strconv.FormatBool(v.(bool))}
                    req.PostFormMap[k] = []string{strconv.FormatBool(v.(bool))}
                default:
                    logPrint("fill FORM or MultiForm,unknown value type,type is ", v)
                }
 
            }
        }
    } else if contentType == CONTENT_TYPE_JSON {
        bs, err := json.Marshal(body)
        if err != nil {
            logPrint("fill json body err:", err)
        } else {
            req.Body = bs
        }
    }
 
    rb, err := json.Marshal(req)
    if err !=nil {
        return nil,err
    }
    rMsg := MsgInfo{
        Topic: req.Path,
        Body: rb,
    }
    rData, err := json.Marshal(rMsg)
    if err != nil {
        return nil, err
    }
 
    return busReq(rData, 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",
        ContentType: contentType,
    }
    fillParam(&req, headers, nil, body)
 
    rb, err := json.Marshal(req)
    if err !=nil {
        return nil,err
    }
    rMsg := MsgInfo{
        Topic: req.Path,
        Body: rb,
    }
    rData, err := json.Marshal(rMsg)
    if err != nil {
        return nil, err
    }
 
    return busReq(rData, 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",
        ContentType: contentType,
    }
    fillParam(&req, headers, nil, body)
 
    rb, err := json.Marshal(req)
    if err !=nil {
        return nil,err
    }
    rMsg := MsgInfo{
        Topic: req.Path,
        Body: rb,
    }
    rData, err := json.Marshal(rMsg)
    if err != nil {
        return nil, err
    }
 
    return busReq(rData, sc.nodes)
}
 
func fillParam(req *Request,headers map[string]string, params map[string]string, body map[string]interface{}) {
    headerMap := make(map[string][]string)
    queryMap := make(map[string][]string)
    if headers != nil {
        for k,v := range headers {
            headerMap[k] = []string{v}
        }
    }
    if params != nil {
        for k,v := range params {
            queryMap[k] = []string{v}
        }
    }
    req.HeaderMap = headerMap
    req.QueryMap = queryMap
    b, err := json.Marshal(body)
    if err != nil {
        logPrint("marshal body err:", err)
    } 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")
//    }
//}