-
Notifications
You must be signed in to change notification settings - Fork 2
/
gent_app_mgr.cpp
executable file
·182 lines (164 loc) · 3.47 KB
/
gent_app_mgr.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
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
171
172
173
174
175
176
177
178
179
180
181
182
/*
* gent_app_mgr.h
*
* Created on: 2012-7-7
* Author: wyt
*/
#include "gent_app_mgr.h"
#include "gentle.h"
#include "prefine.h"
GentAppMgr *GentAppMgr::intance_ = NULL;
CommLock GentAppMgr::lock;
GentAppMgr *GentAppMgr::Instance() {
AutoLock loc(&lock);
if(intance_ == NULL) {
intance_ = new GentAppMgr();
}
return intance_;
}
void GentAppMgr::UnInstance() {
if(intance_) delete intance_;
}
GentAppMgr::GentAppMgr():def_num(10)
{
total_conn = 0;
}
GentAppMgr::~GentAppMgr()
{
}
int GentAppMgr::Register(int cmd, GentBasic *app)
{
if(app_mgr_.count(cmd)) return 1;
if(app_mgr_[cmd].size()>=def_num) return 1;
vector<GentBasic *> module_vec;
for(unsigned int i=0;i<def_num; i++) {
GentBasic *p = app->CloneProccess();
module_vec.push_back(p);
}
app_mgr_[cmd] = module_vec;
delete app;
return 0;
}
int GentAppMgr::GetModule(int cmd, GentBasic *&app)
{
APP_MODULE::iterator iter = app_mgr_.find(cmd);
if(iter == app_mgr_.end()){
//写日志
cout << "GentAppMgr::GetModule: no find " << cmd << endl;
return 1;
}
size_t len = (*iter).second.size();
if(len<=0){
cout << "GentAppMgr::GetModule: len " << len << endl;
return 1;
}
app = (*iter).second[len-1];
(*iter).second.pop_back();
return 0;
}
int GentAppMgr::SetModule(int cmd, GentBasic *&app)
{
APP_MODULE::iterator iter = app_mgr_.find(cmd);
if(iter == app_mgr_.end()){
//写日志
return 1;
}
(*iter).second.push_back(app);
return 0;
}
void GentAppMgr::SetPlugin(GentCommand *command)
{
plus = command;
}
bool GentAppMgr::Init()
{
string msg;
if(plus->Init(msg)) {
return false;
}
return true;
}
/*
GentConnect *GentAppMgr::GetConnect(int sfd)
{
size_t len = free_conn_mgr.size();
AutoLock lock(&conn_lock);
GentConnect *c;
if(len > 0) {
c= free_conn_mgr[len-1];
c->Init(sfd);
free_conn_mgr.pop_back();
}else{
c = new GentConnect(sfd);
c->Init(sfd);
total_conn++;
}
LOG(GentLog::BUG,"add fd:%d to conn_mgr", sfd);
conn_mgr[sfd] = c;
return c;
}
*/
GentConnect *GentAppMgr::GetConnect(int sfd)
{
AutoLock lock(&conn_lock);
size_t len = free_conn_mgr.size();
GentConnect *c;
if(len > 0) {
c= free_conn_mgr.back();
c->Init(sfd);
free_conn_mgr.pop_back();
}else{
c = new GentConnect(sfd);
c->Init(sfd);
total_conn++;
}
LOG(GentLog::BUG,"add fd:%d to conn_mgr", sfd);
conn_mgr[sfd] = c;
return c;
}
GentCommand *GentAppMgr::GetCommand(GentConnect *connect,int id)
{
GentCommand *p = plus->Clone(connect);
plus_mgr[id] = p;
return p;
}
void GentAppMgr::Destroy(int id)
{
PLUGIN::iterator iter = plus_mgr.find(id);
if(iter != plus_mgr.end()){
if(plus_mgr[id])
delete plus_mgr[id];
}
}
void GentAppMgr::RetConnect(GentConnect *c)
{
AutoLock lock(&conn_lock);
if(!c) return;
CONNPOOL::iterator it = conn_mgr.find(c->fd);
if(it == conn_mgr.end()) {
LOG(GentLog::FATAL,"link fd:%d not find", c->fd);
exit(1);
}
c->fd = -1;
free_conn_mgr.push_front(c);
conn_mgr.erase(it);
//check link num
size_t linkNum = GetConnCount()*20;
if(linkNum < free_conn_mgr.size()) {
size_t num = free_conn_mgr.size() - linkNum;
for(size_t i=1; i<=num; i++) {
GentConnect *conn = free_conn_mgr.front();
delete conn;
total_conn--;
free_conn_mgr.pop_front();
}
}
}
size_t GentAppMgr::GetConnCount()
{
return total_conn - free_conn_mgr.size();
}
size_t GentAppMgr::GetTotalConnCount()
{
return total_conn;
}