-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigServer.cpp
More file actions
170 lines (150 loc) · 3.97 KB
/
Copy pathConfigServer.cpp
File metadata and controls
170 lines (150 loc) · 3.97 KB
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
#include "ConfigServer.h"
#include "ConfigServantImp.h"
#include "LogComm.h"
//
using namespace std;
//
ConfigServer g_app;
/////////////////////////////////////////////////////////////////
void ConfigServer::initialize()
{
//initialize application here:
//...
///注册服务接口
addServant<ConfigServantImp>(ServerConfig::Application + "." + ServerConfig::ServerName + ".ConfigServantObj");
//初始化外部接口对象
initOuterFactory();
//初始化DB操作对象
initDBOperator();
// 注册动态加载命令
TARS_ADD_ADMIN_CMD_NORMAL("reload", ConfigServer::reloadSvrConfig);
//加载DB配置
TARS_ADD_ADMIN_CMD_NORMAL("reloaddb", ConfigServer::reloadDBConfig);
}
/////////////////////////////////////////////////////////////////
void ConfigServer::destroyApp()
{
//destroy application here:
//...
}
/*
* 配置变更,重新加载配置
*/
bool ConfigServer::reloadSvrConfig(const string &command, const string ¶ms, string &result)
{
try
{
//加载配置
getOuterFactoryPtr()->readAllConfig();
result = "reload server config success.";
LOG_DEBUG << "reloadSvrConfig: " << result << endl;
return true;
}
catch (TC_Exception const &e)
{
result = string("catch tc exception: ") + e.what();
}
catch (std::exception const &e)
{
result = string("catch std exception: ") + e.what();
}
catch (...)
{
result = "catch unknown exception.";
}
result += "\n fail, please check it.";
LOG_DEBUG << "reloadSvrConfig: " << result << endl;
return true;
}
/**
* 加载DB配置数据
*/
bool ConfigServer::reloadDBConfig(const string &command, const string ¶ms, string &result)
{
try
{
//加载DB数据
loadDBConfig();
result = "reload db config success.";
LOG_DEBUG << "reloadDBConfig: " << result << endl;
return true;
}
catch (TC_Exception const &e)
{
result = string("catch tc exception: ") + e.what();
}
catch (std::exception const &e)
{
result = string("catch std exception: ") + e.what();
}
catch (...)
{
result = "catch unknown exception.";
}
result += "\n fail, please check it.";
LOG_DEBUG << "reloadDBConfig: " << result << endl;
return true;
}
/**
* 初始化外部接口对象
**/
int ConfigServer::initOuterFactory()
{
_pOuter = new OuterFactoryImp();
return 0;
}
/**
* 初始化DB操作对象
*/
void ConfigServer::initDBOperator()
{
const DBConf &dbConf = getOuterFactoryPtr()->getDBConfig();
int iRet = DBOperatorSingleton::getInstance()->init(dbConf.Host, dbConf.user, dbConf.password, dbConf.dbname, dbConf.charset, dbConf.port);
if (iRet != 0)
{
ROLLLOG_ERROR << "Init DBOperator failed, exit server." << endl;
//terminate();
return;
}
//加载数据
iRet = DBOperatorSingleton::getInstance()->loadConfig();
if (iRet != 0)
{
ROLLLOG_ERROR << "load config failed, exit server." << endl;
//terminate();
return;
}
}
/**
* 加载DB数据
*/
void ConfigServer::loadDBConfig()
{
//加载数据
int iRet = DBOperatorSingleton::getInstance()->loadConfig();
if(iRet != 0)
{
ROLLLOG_ERROR << "load config failed, exit server." << endl;
//terminate();
return;
}
}
/////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
try
{
g_app.main(argc, argv);
g_app.waitForShutdown();
}
catch (std::exception &e)
{
cerr << "std::exception : " << e.what() << std::endl;
}
catch (...)
{
cerr << "unknown exception." << std::endl;
}
return -1;
}
/////////////////////////////////////////////////////////////////