lichao
2021-04-21 ad4f3dcedab29a690c5eedbb08ba1b393917db0b
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
/*
 * =====================================================================================
 *
 *       Filename:  center_main.cc
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2021年04月13日 16时16分26秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Li Chao (), lichao@aiotlink.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include "app_arg.h"
#include "box.h"
#include "center.h"
#include "defs.h"
#include "signalhandle.h"
#include <boost/interprocess/sync/named_mutex.hpp>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
using namespace bhome_shm;
 
namespace
{
const std::string kCenterRunningFlag = "bh_center_single_flag_0";
 
class InstanceFlag
{
 
public:
    InstanceFlag(SharedMemory &shm, const std::string &name) :
        shm_(shm), name_(name), run_(false) {}
    ~InstanceFlag() { Stop(); }
 
    bool TryStartAsFirstInstance()
    {
        if (run_) {
            return true;
        }
 
        auto mtx(shm_.find_or_construct<Mutex>((name_ + "_mutex_0").c_str())());
        auto time_stamp(shm_.find_or_construct<int64_t>((name_ + "_timestamp_0").c_str())(0));
 
        if (mtx && time_stamp) {
            Guard lock(*mtx);
            auto now = NowSec();
            // printf("old: %ld, now: %ld\n", *time_stamp, now);
            if (now > *time_stamp + 10) {
                *time_stamp = now;
                auto UpdateTime = [this, time_stamp]() {
                    while (run_) {
                        std::this_thread::sleep_for(1s);
                        *time_stamp = NowSec();
                    }
                };
                run_.store(true);
                std::thread(UpdateTime).swap(worker_);
                return true;
            }
        }
        return false;
    }
 
private:
    void Stop()
    {
        run_.store(false);
        if (worker_.joinable()) {
            worker_.join();
        }
    }
 
    std::thread worker_;
    SharedMemory &shm_;
    std::string name_;
    std::atomic<bool> run_;
};
 
} // namespace
int center_main(int argc, const char *argv[])
{
    auto &shm = BHomeShm();
 
    AppArg args(argc, argv);
    if (args.Has("remove")) {
        shm.Remove();
        return 0;
    }
 
    InstanceFlag inst(shm, kCenterRunningFlag);
    if (!inst.TryStartAsFirstInstance()) {
        printf("another instance is running, exit.\n");
        return 0;
    }
 
    if (args.Has("daemon") || args.Has("d")) {
        int r = daemon(0, 0); // TODO center control msg to close itself.
    }
 
    BHCenter center(shm);
    center.Start();
 
    printf("center started ...\n");
    WaitForSignals({SIGINT, SIGTERM});
    return 0;
}
 
namespace
{
static bool install = BoxInstall("bhshmq_center", center_main, "bhome center program.");
}