zhangmeng
2022-04-11 f552a0ea788225d8fe80942d6e8bd6500f788393
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
#include "interface_bus_api.h"
#include "nng_wrap.h"
using namespace nng_wrap;
 
#include "common.h"
#include <tuple>
using namespace std;
 
#include "bhome_msg.pb.h"
#include "bhome_msg_api.pb.h"
using namespace bhome_msg;
 
using bus = tuple<array<thread, 0>, _ps, _ps_sub, vector<tuple<float>>, deque<map<string, void*>>, _sv, _rr, unordered_map<void*, unordered_set<string*>>, string>;
void* bus_register(const void *proc_info,
               const int proc_info_len,
               void **reply,
               int *reply_len,
               const int timeout_ms)
{
    if (!proc_info || proc_info_len <= 0) return NULL;
 
    void* out{};
    int out_len{};
    auto ret = simple_request(get_url(URLReg), proc_info, proc_info_len, &out, &out_len, timeout_ms);
    if (ret){
        constexpr int port_size = sizeof(unsigned short);
        unsigned short port = *(unsigned short*)(out);
        copy_memory(reply, reply_len, (char*)out + port_size, out_len - port_size);
        bus_free(out, out_len);
/////////////////////////////////////////////////////////////////////////
        bus *b = new bus;
        bhome_msg::ProcInfo pi;
        if (pi.ParseFromArray(proc_info, proc_info_len)) get<8>(*b) = pi.proc_id();
 
        const auto& url_hb = get_url(URLHeartBeat);
        respond_survey(url_hb,string{(char*)proc_info,(size_t)proc_info_len},&get<5>(*b));
 
        const auto& url_pub_proxy = get_url(URLPubProxy);
        publish(url_pub_proxy, NULL, 0, &get<1>(*b));
        const auto& url_sub_queue = get_url(URLSubQueue);
        subscribe_center(url_sub_queue, &get<2>(*b));
 
        // temporary
        // port = 0;
        start_reply(get<8>(*b), port, &get<6>(*b));
 
        return b;
    }
    return NULL;
}
 
static bus* get(void* h){
    return static_cast<bus*>(h);
}
 
int bus_unregister(void* handle, const void *proc_info,
                 const int proc_info_len,
                 void **reply,
                 int *reply_len,
                 const int timeout_ms)
{
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
    if (!proc_info || proc_info_len <= 0) {
        PRNTVITAG("proc_info is null");
        return false;
    }
 
    return simple_request(get_url(URLDeReg), proc_info, proc_info_len, reply, reply_len, timeout_ms);
}
 
int bus_register_topics(void* handle, const void *topics,
                     const int topics_len,
                     void **reply,
                     int *reply_len,
                     const int timeout_ms)
{
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
    if (!topics || topics_len <= 0) {
        PRNTVITAG("topics is null");
        return false;
    }
 
    if (get<8>(*b).empty()) {
        PRNTVITAG("proc_id is null");
        return false;
    }
 
    bhome_msg::MsgTopicList mtl;
    if (!mtl.ParseFromArray(topics, topics_len)){
        PRNTVITAG("MsgTopicList ParseFromArray failed");
        return false;
    }
 
    bhome_msg::MsgTopicList mtl2;
    mtl2.add_topic_list(get<8>(*b));
    for(int i = 0; i < mtl.topic_list_size(); i++){
        mtl2.add_topic_list(mtl.topic_list(i));
    }
 
    string msg(mtl2.SerializeAsString());
    const auto& url = get_url(URLRegTopic);
    if (url.empty()) {
        set_last_error("bus_register_topics url empty");
        return false;
    }
    return simple_request(url, msg.data(), msg.size(), reply, reply_len, timeout_ms);
}
 
int bus_query_topic_address(void* handle, const void *remote,
                        const int remote_len,
                        const void *topics,
                        const int topics_len,
                        void **reply,
                        int *reply_len,
                        const int timeout_ms)
{
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
    if (!topics || topics_len <= 0) {
        PRNTVITAG("topics is null");
        return false;
    }
 
    auto url(get_url(URLQueryTopic));
    if (url.empty()) {
        set_last_error("bus_query_topic_address url empty");
        return false;
    }
 
    // if (remote && remote_len > 0){
    //     BHAddress addr;
    //     if (addr.ParseFromArray(remote, remote_len)){
    //         if (!addr.ip().empty() && addr.port() > 0){
    //             // url = "tcp://" + addr.ip() + ":" + to_string(addr.port());
    //             printf("======>> bus_query_topic_address use remote address %s\n", url.c_str());
    //         }
    //     }
    // }
 
    return simple_request(url, topics, topics_len, reply, reply_len, timeout_ms);
}
 
int bus_query_procs(void* handle, const void *remote,
                 const int remote_len,
                 const void *query,
                 const int query_len,
                 void **reply,
                 int *reply_len,
                 const int timeout_ms)
{
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
    if (!query || query_len <= 0) {
        PRNTVITAG("query is null");
        return false;
    }
 
    auto url(get_url(URLQueryProcs));
    if (url.empty()) {
        set_last_error("bus_query_procs url empty");
        return false;
    }
    // if (remote && remote_len > 0){
    //     BHAddress addr;
    //     if (addr.ParseFromArray(remote, remote_len)){
    //         if (!addr.ip().empty() && addr.port() > 0){
    //             // url = "tcp://" + addr.ip() + ":" + to_string(addr.port());
    //             printf("======>> bus_query_procs use remote address %s\n", url.c_str());
    //         }
    //     }
    // }
 
    return simple_request(url, query, query_len, reply, reply_len, timeout_ms);
}
 
static int sub(const string& url,void* handle, const void *topics,
                      const int topics_len,
                      void **reply,
                      int *reply_len,
                      const int timeout_ms){
 
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
    if (!topics || topics_len <= 0) {
        PRNTVITAG("topics is null");
        return false;
    }
 
    bhome_msg::MsgTopicList mtl;
    if (!mtl.ParseFromArray(topics, topics_len)){
        PRNTVITAG("bus_subscribe_topics ParseFromArray failed");
        return false;
    }
    if (mtl.topic_list_size() == 0){
        PRNTVITAG("bus_subscribe_topics topic_list_size zero");
        return false;
    }
 
    for(int i = 0; i < mtl.topic_list_size(); i ++){
        subscribe_topic(mtl.topic_list(i), &get<2>(*b));
    }
 
///////////////////////////////////////////////////
    auto ret = simple_request(url, topics, topics_len, reply, reply_len, timeout_ms);
    if (!ret){
        printf("bus_subscribe_topics simple_request failed\n");
    }
    return ret;
 
}
 
 
int bus_subscribe_topics(void* handle, const void *topics,
                      const int topics_len,
                      void **reply,
                      int *reply_len,
                      const int timeout_ms)
{
    return sub(get_url(URLSubLocal), handle, topics, topics_len, reply, reply_len, timeout_ms);
}
int bus_subscribe_topics_net(void* handle, const void *topics,
                         const int topics_len,
                         void **reply,
                         int *reply_len,
                         const int timeout_ms)
{
    return sub(get_url(URLSubNet), handle, topics, topics_len, reply, reply_len, timeout_ms);
}
 
int bus_heartbeat_easy(void* handle, const int timeout_ms){ return 1; }
int bus_heartbeat(void* handle, const void *proc_info,const int proc_info_len,void **reply,int *reply_len,const int timeout_ms){ return 1; }
 
int bus_publish(void* handle, const void *msgpub,
              const int msgpub_len,
              const int timeout_ms)
{
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
    if (get<8>(*b).empty()){
        PRNTVITAG("proc_id is null");
        return false;
    }
    if (!msgpub || msgpub_len <= 0) {
        PRNTVITAG("msgpub is null");
        return false;
    }
 
    bhome_msg::MsgPublish pub;
    if (!pub.ParseFromArray(msgpub, msgpub_len)){
        PRNTVITAG("MsgPublish is null");
        return false;
    }
 
    // msgpublish(msgpublish(msg))
    MsgPublish newPub;
    newPub.set_topic(get<8>(*b));
    newPub.set_data(string{(const char*)msgpub, (const size_t)msgpub_len});
 
    string msg(newPub.SerializeAsString());
    auto ret = publish(pub.topic(), msg.data(), msg.size(), &get<1>(*b));
    if (ret > 0) return true;
    return false;
}
 
int bus_recv_pubmsg(void* handle, void **proc_id,
              int *proc_id_len,
              void **msgpub,
              int *msgpub_len,
              const int timeout_ms)
{
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
 
    string topic, msg;
    auto ret = subscribe_read(&topic, &msg, timeout_ms, &get<2>(*b));
    if (ret < 0) return false;
 
    MsgPublish newPub;
    if (newPub.ParseFromString(msg)){
        copy_memory(proc_id, proc_id_len, newPub.topic().data(), newPub.topic().size());
        copy_memory(msgpub, msgpub_len, newPub.data().data(), newPub.data().size());
        return true;
    }
 
    return false;
}
 
extern int get_proc_id_from_MsgRequestTopic(const void* request, const int request_len, const int timeout_ms, string* proc_id);
int bus_request(void* handle, const void *remote,
              const int remote_len,
              const void *request,
              const int request_len,
              void **proc_id,
              int *proc_id_len,
              void **reply,
              int *reply_len,
              const int timeout_ms)
{
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
 
    if (!request || request_len <= 0) {
        PRNTVITAG("request is null");
        return false;
    }
 
    string url{};
    // BHQueryTopicAddress获取proc_id
    string procid{};
    if (get_proc_id_from_MsgRequestTopic(request, request_len, timeout_ms, &procid)){
        // PRNTVITAG("get_proc_id_from_MsgRequestTopic failed");
        // return false;
        url = "ipc:///tmp/" + procid;
    }
    if (remote && remote_len > 0){
        BHAddress addr;
        if (addr.ParseFromArray(remote, remote_len)){
            if (!addr.ip().empty() && addr.port() > 0){
                url = "tcp://" + addr.ip() + ":" + to_string(addr.port());
                // printf("======>> BHRequest use remote address %s\n", url.c_str());
            }
        }
    }
    if (url.empty()) {
        set_last_error("bus_request url empty");
        return false;
    }
    int rc = request2(url, request, request_len, reply, reply_len, timeout_ms);
    if (rc < 0) return false;
    copy_memory(proc_id, proc_id_len, procid.data(), procid.size());
 
    return true;
}
 
int bus_recv_request(void* handle, void **proc_id,
                  int *proc_id_len,
                  void **request,
                  int *request_len,
                  void **src,
                  const int timeout_ms)
{
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
 
    string msg;
    auto ret = read_request(src, &msg, timeout_ms, &get<6>(*b));
    // printf("bus_recv_request read_request ret %d msg %s\n", ret, msg.c_str());
    if (ret != 0) return false;
 
    string procid{};
    if (get_proc_id_from_MsgRequestTopic(msg.data(), msg.size(), timeout_ms, &procid)){
        // PRNTVITAG("get_proc_id_from_MsgRequestTopic failed");
        // return false;
        copy_memory(proc_id, proc_id_len, procid.data(), procid.size());
    }
 
    copy_memory(request, request_len, msg.data(), msg.size());
 
    return true;
 
}
 
int bus_send_reply(void* handle, void *src,
                const void *reply,
                const int reply_len)
{
    TAG;
    bus* b = get(handle);
    if (!b){
        PRNTVITAG("handle is null");
        return false;
    }
    if (!src || !reply || reply_len <= 0) {
        PRNTVITAG("src or reply is null");
        return false;
    }
 
    auto ret = send_reply(src, reply, reply_len, &get<6>(*b));
 
    if (ret < 0) return false;
    return true;
 
}
 
void bus_cleanup(void* handle){
    bus *b = get(handle);
    if (b) delete b;
}
 
void bus_free(void *buf, int size){
    free_nng(buf, size);
}
 
int bus_last_error(void **msg, int *msg_len){
    int ec;
    get_last_error(&ec, msg, msg_len);
    return true;
}