-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.cpp
97 lines (78 loc) · 2.09 KB
/
config.cpp
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
/**
***All Rights Reserved
*
*author frankiezhu
*date 2013-03-22
*/
#include "log.h"
#include "config.h"
#include "tinyxml.h"
const static int DEFAULT_WORK_NUM = 4;
Config::Config()
{
}
Config::~Config()
{
}
int Config::read_config(const char *file_name)
{
TiXmlDocument doc(file_name);
bool loadOkay = doc.LoadFile();
if (!loadOkay)
{
logerr( "Could not load file:%s. Error='%s'. Exiting.\n", file_name, doc.ErrorDesc());
return -1;
}
TiXmlElement *root_element = doc.RootElement();
TiXmlElement *child_element = root_element->FirstChildElement("port");
if (!child_element)
{
logerr("read port not found!\n");
return -1;
}
m_port = atoi(child_element->GetText());
logdbg("m_port:%d\n", m_port);
child_element = root_element->FirstChildElement("worker_num");
if (!child_element)
{
logerr("read port not found!\n");
return -1;
}
m_worker_num = atoi(child_element->GetText());
if (m_worker_num > DEFAULT_WORK_NUM || m_worker_num < 1)
{
logerr("Invalid worker num:%d, set to:%d\n", m_worker_num, DEFAULT_WORK_NUM);
m_worker_num = DEFAULT_WORK_NUM;
}
logdbg("worker:%d\n", m_worker_num);
child_element = root_element->FirstChildElement("keep_alive");
if (!child_element)
{
logerr("read keep_alive not found!\n");
return -1;
}
m_keep_alive = atoi(child_element->GetText());
logdbg("keep-alive:%d\n", m_keep_alive);
child_element = root_element->FirstChildElement("asure_count");
if (!child_element)
{
logerr("read asure_count not found!\n");
return -1;
}
m_asure_count = atoi(child_element->GetText());
logdbg("asure_count:%d\n", m_asure_count);
child_element = root_element->FirstChildElement("lifetime");
if (!child_element)
{
logerr("read lifetime not found!\n");
return -1;
}
m_lifetime = atoi(child_element->GetText());
logdbg("lifetime:%d\n", m_lifetime);
return 0;
}
Config *glb_config()
{
static Config s_instance;
return &s_instance;
}