-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConfigMgr.cpp
129 lines (99 loc) · 2.27 KB
/
ConfigMgr.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
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
/*************************************************************************
> File Name: ConfigMgr.cpp
> Author: libo
> Created Time: Thu 12 Jul 2018 02:58:56 PM CST
************************************************************************/
#include "ConfigMgr.h"
#include "Config.h"
#include <boost/thread.hpp>
int ConfigMgr::getServant(vector<string>&avServant)
{
TC_ThreadRLock Rlock(cServantLock);
for(auto e:cMapServant)
avServant.push_back(e.first);
return 0;
}
void ConfigMgr::setServant(const string&asServant)
{
TC_ThreadWLock Wlock(cServantLock);
cMapServant[asServant] = ""; //预留
cSignal.notify();
}
void ConfigMgr::init()
{
boost::thread update(&ConfigMgr::updateThread, ConfigMgr::getInstance());
getServant();
}
void ConfigMgr::updateThread()
{
LOG_DEBUG<<"updateThread starting..."<<endl;
while(true)
{
TC_Mysql lMysql;
lMysql.init(ConfigInit::getInstance()->getDBConf());
{
TC_ThreadRLock Rlock(cServantLock);
for(auto e: cMapServant)
{
insertServant(e.first);
}
}
cSignal.wait();
}
}
int ConfigMgr::insertServant(const string&asServant)
{
TC_Mysql lMysql;
lMysql.init(ConfigInit::getInstance()->getDBConf());
char lsSql[1024] = "";
snprintf(lsSql, sizeof(lsSql), "insert into servant (name) values('%s')", asServant.c_str());
LOG_DEBUG<<lsSql<<endl;
try
{
lMysql.execute(lsSql);
}
catch(exception&e)
{
LOG_ERROR<<e.what()<<endl;
return -1;
}
catch(...)
{
LOG_ERROR<<"unknow exception..."<<endl;
return -2;
}
return 0;
}
taf::Int32 ConfigMgr::getServant()
{
char lsTemp[10240] ="";
try
{
TC_Mysql lMysql;
lMysql.init(ConfigInit::getInstance()->getDBConf());
LOG_DEBUG<<"init db"<<endl;
TC_Mysql::MysqlData loData;
snprintf(lsTemp, sizeof(lsTemp), "select name from servant");
LOG_DEBUG<<"sql:"<<lsTemp<<endl;
loData = lMysql.queryRecord(lsTemp);
LOG_DEBUG<<"result size:"<<loData.size()<<endl;
for(size_t i = 0; i < loData.size(); i++ )
{
LOG_DEBUG<<loData[i]["name"]<<endl;
TC_ThreadWLock Wlock(cServantLock);
cMapServant[ loData[i]["name"]] = ""; //预留
}
return 0;
}
catch(exception&e)
{
LOG_ERROR<<e.what()<<endl;
return -1;
}
catch(...)
{
LOG_ERROR<<"unkonw exception"<<endl;
return -2;
}
return 1;
}