lichao
2021-06-02 993c556000a414011626770540678948f16eaa9e
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
/*
 * =====================================================================================
 *
 *       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 "log.h"
#include "signalhandle.h"
#include <boost/interprocess/sync/named_mutex.hpp>
#include <chrono>
#include <thread>
using namespace std::chrono_literals;
using namespace bhome_shm;
 
int center_main(int argc, const char *argv[])
{
    AppArg args(argc, argv);
    if (args.Has("remove")) {
        SharedMemory::Remove(BHomeShmName());
        return 0;
    }
 
    ns_log::AddLog(BHLogDir() + "bhshmq_center.log");
    auto lvl = args.Get("log", "info");
    if (strcasecmp(lvl.c_str(), "trace") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::trace); }
    if (strcasecmp(lvl.c_str(), "debug") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::debug); }
    if (strcasecmp(lvl.c_str(), "info") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::info); }
    if (strcasecmp(lvl.c_str(), "warning") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::warning); }
    if (strcasecmp(lvl.c_str(), "error") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::error); }
    if (strcasecmp(lvl.c_str(), "fatal") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::fatal); }
 
    if (!CenterInit()) {
        auto msg = "init memory failed, or center is already running.";
        LOG_FATAL() << msg;
        printf("%s\n", msg);
        exit(0);
    }
    auto &shm = BHomeShm();
    GlobalInit(shm);
 
    if (args.Has("daemon") || args.Has("d")) {
        int r = daemon(0, 0); // TODO center control msg to close itself.
    }
 
    BHCenter center(shm);
    center.Start();
 
    auto msg = "center (" + shm.name() + ") started ...";
    LOG_INFO() << msg;
    printf("%s\n", msg.c_str());
    WaitForSignals({SIGINT, SIGTERM});
    center.Stop();
    LOG_INFO() << "center stopped.";
    return 0;
}
 
namespace
{
static bool install = BoxInstall("bhshmq_center", center_main, "bhome center program.");
}