inbound_chan_thread.cpp
9.35 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* CC/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "lock.h"
#include "tools.h"
#include "inbound_chan_thread.h"
#include "cpu_manager.h"
#include "calldata_manager.h"
#include "node_base.h"
#include "ivr_timer.h"
#include "ivr_instance_manager.h"
#include "flow_manager.h"
#include "fs_mgr.h"
#include "fs_opr.h"
#include "ivr_data_collection.h"
#include <ims/ims_mgr.h>
#if IVR_ENABLE_DEBUGER
#include "ivrdebuger.h"
#endif
using ivr::IvrCallDataCollection;
using ivr::IvrInboundCall;
extern inbound_conf_t g_inbound_conf;
//extern map<string, flow_t*> g_flow_name_info_map;
extern FlowManager g_flowmanager;
InboundChanThread::~InboundChanThread(void) {
}
bool InboundChanThread::check_capability() {
bool result = false;
ostringstream ostm;
{
rw_lock_t lock(g_inbound_chan_rwlock, true);
if (lock.locked()) {
if (g_inbound_chan_num < g_inbound_conf.max_inbound_chan_num) {
++g_inbound_chan_num;
result = true;
} else {
ostm << "[IB]achieve max capability,reject service(" << g_inbound_chan_num << ")";
}
} else {
ostm << "lock inbound rwlock failed";
}
}
if (!result) {
IVR_WARN("%s", ostm.str().c_str());
}
return result;
}
void InboundChanThread::heartbeat(esl_handle_t& handle) {
uint32_t inbound_chan_num_cp = 0;
uint32_t cpu = CpuManager::get_instance()->get_cpu_occupy();
std::string err;
char resp[20] = {0};
bzero(resp, 20);
{
rw_lock_t lock(g_inbound_chan_rwlock);
if (lock.locked()) {
inbound_chan_num_cp = g_inbound_chan_num;
} else {
err = "lock inbound num failed";
}
}
if (err.length()) {
IVR_WARN("%s", err.c_str());
}
snprintf(resp, 20, "cpu=%02u,chan=%05u", cpu, inbound_chan_num_cp);
esl_send(&handle, resp);
IVR_TRACE("ivr-loadbalance-resp = %s", resp);
}
uint32_t InboundChanThread::execute(void* taskparam) {
string callid, ani, dnis;
NodeBase* node = NULL;
inbound_script_t script;
std::string err;
channel_info_t cinfo;
// clear flow ptr
script.flow = NULL;
uint32_t ret = 0;
std::string curr;
dnis = ((inbound_script_t*)_param)->dnis;
ostringstream ost;
fs_opr_t* opr = NULL;
IVR_DEBUG("inbound_execute event queue pointer %lu, queue size: %lu",
(uint64_t)(((session_dynamic_resource_t*)taskparam)->event_queue),
((session_dynamic_resource_t*)taskparam)->event_queue->size());
ivr_session_id_t ivr_s_id;
session_dynamic_resource_t* resource = NULL;
IvrInstanceManager* mgr = NULL;
if (!check_capability()) {
ret = 1;
goto END;
}
//inbound_script 对象 作为流程执行run函数的参数param
{
rw_lock_t lock(g_inbound_conf_rwlock, false);
if (!lock.locked()) {
IVR_WARN("failed lock!");
goto CHAN_END;
}
if (g_inbound_conf.dnis_script_map.find(dnis) == g_inbound_conf.dnis_script_map.end()) {
if (!g_inbound_conf.has_default) {
IVR_WARN("没有找到可以匹配的流程(dnis=%s),且默认流程未配置或未正确加载", dnis.c_str());
ret = 1;
goto CHAN_END;
} else {
IVR_TRACE("没有找到可以匹配的流程(dnis=%s),启动默认流程", dnis.c_str());
script = g_inbound_conf.default_script;
if (SUCCESS != FlowManager::attach_flow(g_inbound_conf.default_script.flow, &script.flow)) {
ret = 1;
goto CHAN_END;
}
}
} else {
IVR_TRACE("找到匹配的流程(dnis=%s)", dnis.c_str());
script = g_inbound_conf.dnis_script_map[dnis];
if (SUCCESS != FlowManager::attach_flow(g_inbound_conf.dnis_script_map[dnis].flow, &script.flow)) {
ret = 1;
goto CHAN_END;
}
}
}
// init ims information
script.imsno = (uint32_t) - 1;
script.imssid = 0;
script.requestId = 0;
script.type = ((inbound_script_t*)_param)->type;
script.fid = ((inbound_script_t*)_param)->fid;
script.ani = ((inbound_script_t*)_param)->ani;
script.dnis = ((inbound_script_t*)_param)->dnis;
script.callid = ((inbound_script_t*)_param)->callid;
script.callsource = ((inbound_script_t*)_param)->callsource;
script.channel_name = ((inbound_script_t*)_param)->channel_name;
script.channel_id = ((inbound_script_t*)_param)->channel_id;
script.trunck = ((inbound_script_t*)_param)->trunck;
ivr_tools_t::build_vars(script.flow->name_var_map, script.name_var_map);
// don't add channel information here
// if answer the call, will add information
//创建实例
resource = (session_dynamic_resource_t*)taskparam;
mgr = IvrInstanceManager::get_instance();
if (IVR_SUCCESS != mgr->create_ivr_instance(script.type, script.fid, &ivr_s_id, *resource,
script.callid.c_str())) {
IVR_WARN("创建实例失败(dnis=%s)", script.dnis.c_str());
goto CHAN_END;
}
//添加uuid
if (IVR_SUCCESS != mgr->add_uuid(ivr_s_id, script.callid)) {
IVR_WARN("创建实例失败(dnis=%s)", script.dnis.c_str());
mgr->destroy_ivr_instance(ivr_s_id);
goto CHAN_END;
}
//注册uuid
if (fs_mgr_t::instance()->fetch_opr(script.fid, &opr) == IVR_SUCCESS) {
if (NULL == opr || IVR_SUCCESS != opr->register_session(script.callid.c_str(), ivr_s_id)) {
IVR_WARN("添加uuid失败(dnis=%s)", script.dnis.c_str());
mgr->destroy_ivr_instance(ivr_s_id);
fs_mgr_t::instance()->free_opr(&opr);
goto CHAN_END;
}
fs_mgr_t::instance()->free_opr(&opr);
} else {
IVR_WARN("注册实例失败(dnis=%s)", script.dnis.c_str());
mgr->destroy_ivr_instance(ivr_s_id);
goto CHAN_END;
}
// update channel info
cinfo.callsource = _param->callsource;
cinfo.channel_id = _param->channel_id;
cinfo.channel_name = _param->channel_name;
IvrInstanceManager::get_instance()->add_channel_info(script.callid, cinfo);
//设置script内容
script.session_id = ivr_s_id;
script.timer = resource->timer;
curr = ivr_tools_t::get_current_second();
*(string*)script.name_var_map[SYS_VAR[sys_var_t::ANI]].pvalue = script.ani;
*(string*)script.name_var_map[SYS_VAR[sys_var_t::DNIS]].pvalue = script.dnis;
*(string*)script.name_var_map[SYS_VAR[sys_var_t::CALLID]].pvalue = script.callid;
*(string*)script.name_var_map[SYS_VAR[sys_var_t::TRUNCK]].pvalue = script.trunck;
*(string*)script.name_var_map[SYS_VAR[sys_var_t::RECORD_FILE]].pvalue = "";
*(string*)script.name_var_map[SYS_VAR[sys_var_t::HANGUP_CAUSE]].pvalue = "user_hangup";
*(string*)script.name_var_map[SYS_VAR[sys_var_t::CALL_BEGIN_TIME]].pvalue = curr;
*(string*)script.name_var_map[SYS_VAR[sys_var_t::CALL_ANSWER_TIME]].pvalue = curr;
*(string*)script.name_var_map[SYS_VAR[sys_var_t::AGENTID]].pvalue = "";
//运行流程
node = script.flow->id_node_map[script.flow->begin_id];
ost << "\n================";
ost << "ANI:\t" << script.ani << endl;
ost << "DNIS:\t" << script.dnis << endl;
ost << "CALLID:\t" << script.callid << endl;
ost << "TRUNCK:\t" << script.trunck << endl;
ost << "FID:\t" << script.fid << endl;
ost << "SESSIONID:\t" << script.session_id << endl;
ost << "================";
IVR_DEBUG("%s", ost.str().c_str());
// add to data collection
IvrCallDataCollection::instance().new_accept_call(script.ani, script.dnis, script.session_id, script.callid);
#if IVR_ENABLE_DEBUGER
IvrDebuger::get_instance()->report(this, "startup", script, node);
#endif
do {
#if IVR_ENABLE_DEBUGER
if (IvrDebuger::get_instance()->wait(this) == 1) {
break;
}
#endif
node = node->run(&script);
#if IVR_ENABLE_DEBUGER
IvrDebuger::get_instance()->report(this, "runnode", script, node);
#endif
} while (node && !g_stopped);
if (!node) {
CallDataManager* calldata_manager = CallDataManager::get_instance();
calldata_manager->push_calldata(script.bill_info().c_str());
}
//set state
IvrCallDataCollection::instance().set_state(script.session_id, IvrInboundCall::EXITFLOW, "");
ivr_tools_t::destroy_vars(script.name_var_map);
mgr->destroy_ivr_instance(ivr_s_id);
ims_mgr_t::get_instance()->unbind_session_id(ivr_s_id);
if (resource->timer != NULL) {
// clear all timer
resource->timer->clear_all_timer();
}
CHAN_END:
FlowManager::detach_flow(&script.flow);
{
rw_lock_t lock(g_inbound_chan_rwlock, true);
if (lock.locked()) {
--g_inbound_chan_num;
} else {
err = "lock inbound rwlock failed";
}
}
if (err.length()) {
IVR_WARN("%s", err.c_str());
}
END:
return ret;
}