lichao
2021-05-19 34cd75f77d0ca94dbdba4e6cc9451fe4d33e78b3
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
#include "bh_api.h"
#include "defs.h"
#include "topic_node.h"
#include <cstdio>
#include <memory>
 
using namespace bhome_shm;
using namespace bhome_msg;
 
namespace
{
std::string GetProcExe()
{
    auto f = fopen("/proc/self/stat", "rb");
    if (f) {
        DEFER1(fclose(f));
        char buf[100] = {0};
        int n = fread(buf, 1, sizeof(buf), f);
        if (n > 0) {
            std::string s(buf, n);
            auto start = s.find('(');
            if (start != std::string::npos) {
                ++start;
                auto end = s.find(')', start);
                return s.substr(start, end - start);
            }
        }
    }
    return std::to_string(getpid());
}
std::unique_ptr<TopicNode> &ProcNodePtr()
{
    static std::mutex mtx;
    std::lock_guard<std::mutex> lk(mtx);
 
    static std::unique_ptr<TopicNode> ptr;
    if (!ptr && GlobalInit(BHomeShm())) {
        auto InitLog = []() {
            auto id = GetProcExe();
            char path[200] = {0};
            sprintf(path, "/opt/vasystem/valog/bhshmq_node_%s.log", id.c_str());
            ns_log::AddLog(path);
            return true;
        };
        static bool init_log = InitLog();
        ptr.reset(new TopicNode(BHomeShm()));
    }
    return ptr;
}
TopicNode &ProcNode()
{
    return *ProcNodePtr();
}
 
class TmpPtr : private boost::noncopyable
{
    void *ptr_ = 0;
    size_t size_ = 0;
 
public:
    explicit TmpPtr(const size_t size) :
        ptr_(malloc(size)), size_(size) {}
    explicit TmpPtr(const std::string &str) :
        TmpPtr(str.size())
    {
        if (ptr_) {
            memcpy(ptr_, str.data(), str.size());
        }
    }
    ~TmpPtr() { free(ptr_); }
    void *get() const { return ptr_; }
    void *release()
    {
        void *tmp = ptr_;
        ptr_ = 0;
        return tmp;
    }
    size_t size() const { return size_; }
    operator bool() const { return ptr_; }
    bool ReleaseTo(void **pdata, int *psize)
    {
        if (!ptr_) {
            return false;
        }
        if (pdata && psize) {
            *psize = size();
            *pdata = release();
        }
        return true;
    }
};
 
template <class Msg>
bool PackOutput(const Msg &msg, void **out, int *out_len)
{
    if (!out || !out_len) {
        return true; // not wanted.
    }
    auto size = msg.ByteSizeLong();
    TmpPtr p(size);
    if (!p) {
        SetLastError(ENOMEM, "not enough memory.");
        return false;
    }
    msg.SerializePartialToArray(p.get(), size);
    p.ReleaseTo(out, out_len);
    return true;
}
 
template <class MsgIn, class MsgOut = MsgCommonReply>
bool BHApi_In1_Out1(bool (TopicNode::*mfunc)(MsgIn &, MsgOut &, const int),
                    const void *request, const int request_len,
                    void **reply, int *reply_len,
                    const int timeout_ms)
{
    MsgIn input;
    if (!input.ParseFromArray(request, request_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    MsgOut msg_reply;
    auto &ptr = ProcNodePtr();
    if (!ptr) {
        SetLastError(eNotFound, "center not started.");
        return 0;
    }
 
    return (ProcNode().*mfunc)(input, msg_reply, timeout_ms) &&
           PackOutput(msg_reply, reply, reply_len);
}
 
template <class MsgIn0, class MsgIn1, class MsgOut = MsgCommonReply>
bool BHApi_In2_Out1(bool (TopicNode::*mfunc)(MsgIn0 &, MsgIn1 &, MsgOut &, const int),
                    const void *in0, const int in0_len,
                    const void *in1, const int in1_len,
                    void **reply, int *reply_len,
                    const int timeout_ms)
{
    MsgIn0 input0;
    MsgIn1 input1;
    if (!input0.ParseFromArray(in0, in0_len) ||
        !input1.ParseFromArray(in1, in1_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    MsgOut msg_reply;
    return (ProcNode().*mfunc)(input0, input1, msg_reply, timeout_ms) &&
           PackOutput(msg_reply, reply, reply_len);
}
 
} // namespace
 
int BHApiIn1Out1Proxy(FBHApiIn1Out1 func,
                      const void *request,
                      const int request_len,
                      void **reply,
                      int *reply_len,
                      const int timeout_ms)
{
    return (*func)(request, request_len, reply, reply_len, timeout_ms);
}
 
int BHRegister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
{
    return BHApi_In1_Out1<ProcInfo>(&TopicNode::Register, proc_info, proc_info_len, reply, reply_len, timeout_ms);
}
int BHUnregister(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
{
    return BHApi_In1_Out1<ProcInfo>(&TopicNode::Unregister, proc_info, proc_info_len, reply, reply_len, timeout_ms);
}
 
int BHHeartbeatEasy(const int timeout_ms)
{
    return ProcNode().Heartbeat(timeout_ms);
}
 
int BHHeartbeat(const void *proc_info, const int proc_info_len, void **reply, int *reply_len, const int timeout_ms)
{
    return BHApi_In1_Out1<ProcInfo>(&TopicNode::Heartbeat, proc_info, proc_info_len, reply, reply_len, timeout_ms);
}
 
int BHRegisterTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
{
    return BHApi_In1_Out1<MsgTopicList>(&TopicNode::ServerRegisterRPC, topics, topics_len, reply, reply_len, timeout_ms);
}
 
int BHQueryTopicAddress(const void *remote, const int remote_len,
                        const void *topic, const int topic_len,
                        void **reply, int *reply_len,
                        const int timeout_ms)
{
    return BHApi_In2_Out1<BHAddress, MsgQueryTopic, MsgQueryTopicReply>(
        &TopicNode::QueryTopicAddress,
        remote, remote_len, topic, topic_len, reply, reply_len, timeout_ms);
}
 
int BHQueryProcs(const void *remote,
                 const int remote_len,
                 const void *query,
                 const int query_len,
                 void **reply,
                 int *reply_len,
                 const int timeout_ms)
{
    return BHApi_In2_Out1<BHAddress, MsgQueryProc, MsgQueryProcReply>(
        &TopicNode::QueryProcs,
        remote, remote_len, query, query_len, reply, reply_len, timeout_ms);
}
 
int BHSubscribeTopics(const void *topics, const int topics_len, void **reply, int *reply_len, const int timeout_ms)
{
    return BHApi_In1_Out1<MsgTopicList>(&TopicNode::Subscribe, topics, topics_len, reply, reply_len, timeout_ms);
}
 
int BHPublish(const void *msgpub,
              const int msgpub_len,
              const int timeout_ms)
{
    MsgPublish pub;
    if (!pub.ParseFromArray(msgpub, msgpub_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    return ProcNode().Publish(pub, timeout_ms);
}
 
int BHReadSub(void **proc_id,
              int *proc_id_len,
              void **msgpub,
              int *msgpub_len,
              const int timeout_ms)
{
    std::string proc;
    MsgPublish pub;
 
    if (ProcNode().RecvSub(proc, pub, timeout_ms)) {
        TmpPtr pproc(proc);
        if (pproc && PackOutput(pub, msgpub, msgpub_len)) {
            pproc.ReleaseTo(proc_id, proc_id_len);
            return true;
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
int BHAsyncRequest(const void *remote,
                   const int remote_len,
                   const void *request,
                   const int request_len,
                   void **msg_id,
                   int *msg_id_len)
{
    BHAddress dest;
    MsgRequestTopic req;
    if (!dest.ParseFromArray(remote, remote_len) ||
        !req.ParseFromArray(request, request_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    std::string str_msg_id;
    MsgRequestTopicReply out_msg;
    if (ProcNode().ClientAsyncRequest(dest, req, str_msg_id)) {
        if (!msg_id || !msg_id_len) {
            return true;
        }
        TmpPtr ptr(str_msg_id);
        if (ptr) {
            ptr.ReleaseTo(msg_id, msg_id_len);
            return true;
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
int BHRequest(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)
{
    BHAddress dest;
    MsgRequestTopic req;
    if (!dest.ParseFromArray(remote, remote_len) ||
        !req.ParseFromArray(request, request_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    std::string proc;
    MsgRequestTopicReply out_msg;
    if (ProcNode().ClientSyncRequest(dest, req, proc, out_msg, timeout_ms)) {
        TmpPtr pproc(proc);
        if (pproc && PackOutput(out_msg, reply, reply_len)) {
            pproc.ReleaseTo(proc_id, proc_id_len);
            return true;
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
int BHReadRequest(void **proc_id,
                  int *proc_id_len,
                  void **request,
                  int *request_len,
                  void **src,
                  const int timeout_ms)
{
    void *src_info = 0;
    std::string proc;
    MsgRequestTopic out_msg;
    if (ProcNode().ServerRecvRequest(src_info, proc, out_msg, timeout_ms)) {
        TmpPtr pproc(proc);
        if (pproc && PackOutput(out_msg, request, request_len)) {
            pproc.ReleaseTo(proc_id, proc_id_len);
            *src = src_info;
            return true;
        } else {
            SetLastError(ENOMEM, "out of mem");
        }
    }
    return false;
}
 
int BHSendReply(void *src,
                const void *reply,
                const int reply_len)
{
    MsgRequestTopicReply rep;
    if (!rep.ParseFromArray(reply, reply_len)) {
        SetLastError(eInvalidInput, "invalid input.");
        return false;
    }
    return ProcNode().ServerSendReply(src, rep);
}
 
void BHStartWorker(FServerCallback server_cb, FSubDataCallback sub_cb, FClientCallback client_cb)
{
    TopicNode::ServerAsyncCB on_req;
    TopicNode::SubDataCB on_sub;
    TopicNode::RequestResultCB on_reply;
    if (server_cb) {
        on_req = [server_cb](void *src, std::string &proc_id, const MsgRequestTopic &request) {
            std::string sreq(request.SerializeAsString());
            server_cb(proc_id.data(), proc_id.size(), sreq.data(), sreq.size(), src);
        };
    }
    if (sub_cb) {
        on_sub = [sub_cb](const std::string &proc_id, const MsgPublish &pub) {
            std::string s(pub.SerializeAsString());
            sub_cb(proc_id.data(), proc_id.size(), s.data(), s.size());
        };
    }
    if (client_cb) {
        on_reply = [client_cb](const BHMsgHead &head, const MsgRequestTopicReply &rep) {
            std::string s(rep.SerializeAsString());
            client_cb(head.proc_id().data(), head.proc_id().size(),
                      head.msg_id().data(), head.msg_id().size(),
                      s.data(), s.size());
        };
    }
 
    ProcNode().Start(on_req, on_sub, on_reply);
}
 
void BHFree(void *data, int size)
{
    free(data);
}
 
int BHCleanup()
{
    ProcNodePtr().reset();
    return 0;
}
 
int BHGetLastError(void **msg, int *msg_len)
{
    int ec = 0;
    if (msg && msg_len) {
        std::string err_msg;
        GetLastError(ec, err_msg);
        TmpPtr p(err_msg);
        p.ReleaseTo(msg, msg_len);
    }
    return ec;
}
 
#undef BH_SOCKET_MEMF_CALL