lichao
2021-04-09 4e5cb7960ce4e7e66d5190be67426aeca8b55c3d
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
/*
 * =====================================================================================
 *
 *       Filename:  util.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年03月30日 11时24分26秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), 
 *   Organization:  
 *
 * =====================================================================================
 */
 
#ifndef UTIL_W8A0OA5U
#define UTIL_W8A0OA5U
 
#include "bh_util.h"
#include "topic_node.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/noncopyable.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/timer/timer.hpp>
#include <chrono>
#include <functional>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <thread>
#include <vector>
 
using namespace boost::posix_time;
inline ptime Now() { return second_clock::universal_time(); };
 
using namespace std::chrono_literals;
 
typedef std::function<void(void)> FuncVV;
 
class ScopeCall : private boost::noncopyable
{
    FuncVV f_;
 
public:
    ScopeCall(FuncVV f) :
        f_(f) { f_(); }
    ~ScopeCall() { f_(); }
};
class ThreadManager
{
    std::vector<std::thread> threads_;
 
public:
    ~ThreadManager() { WaitAll(); }
    template <class T, class... P>
    void Launch(T t, P... p) { threads_.emplace_back(t, p...); }
    void WaitAll()
    {
        for (auto &t : threads_) {
            if (t.joinable()) {
                t.join();
            }
        }
    }
};
class ProcessManager
{
    std::vector<pid_t> procs_;
 
public:
    ~ProcessManager() { WaitAll(); }
    template <class T, class... P>
    void Launch(T t, P... p)
    {
        auto pid = fork();
        if (pid == 0) {
            // child
            t(p...);
            exit(0);
        } else if (pid != -1) { // Ok
            procs_.push_back(pid);
        }
    };
    void WaitAll()
    {
        for (auto &pid : procs_) {
            int status = 0;
            int options = WUNTRACED | WCONTINUED;
            waitpid(pid, &status, options);
        }
        procs_.clear();
    }
};
 
using namespace bhome_shm;
using namespace bhome_msg;
 
struct ShmRemover {
    std::string name_;
    ShmRemover(const std::string &name) :
        name_(name) { SharedMemory::Remove(name_); }
    ~ShmRemover() { SharedMemory::Remove(name_); }
};
 
class DemoNode : public TopicNode
{
    std::string id_;
 
public:
    DemoNode(const std::string &id, SharedMemory &shm) :
        TopicNode(shm), id_(id) { Init(); }
    void Init()
    {
        ProcInfo proc;
        proc.set_proc_id(id_);
        MsgCommonReply reply_body;
 
        if (!Register(proc, reply_body, 1000)) {
            printf("node %s register failed\n", id_.c_str());
        }
    }
};
 
#endif // end of include guard: UTIL_W8A0OA5U