node_imsresponsecmp.cpp
7.47 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
/*
* 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 <node_imsresponsecmp.h>
#include <json/json.h>
NodeRequestResponseCompare::NodeRequestResponseCompare(const uint32_t id, const string& name,
const string& type, const string& desc, const key_map_t& keymap)
: NodeResource(id, name, type, desc, keymap) {
}
NodeRequestResponseCompare::~NodeRequestResponseCompare() {
}
NodeBase* NodeRequestResponseCompare::run(base_script_t* param) {
if (NULL == param) {
IVR_WARN("base_script_t pointer should not be null");
return NULL;
}
IVR_TRACE("%s", enter(param->name_var_map).c_str());
const char* exit = EXIT_FAIL;
string response;
const map<string, variable_t>& vars = param->name_var_map;
if (!parse_expression(_response, vars, response)) {
IVR_WARN("check ims reponse failed, parse json error!");
return _find_exit_node(param, exit);
}
if (_agent.empty()) {
IVR_WARN("check output param failed, (agent) is empty!");
return _find_exit_node(param, exit);
}
ims_routerequest_id_t requestid = 0;
int32_t reason = 0;
string agent;
map<string, variable_t>::iterator it = param->name_var_map.find(_agent);
if (it == param->name_var_map.end()) {
IVR_WARN("δÕÒµ½ÐèÒª¸³ÖµµÄ±äÁ¿ %s", _agent.c_str());
} else {
// try to parse json string
if (_get_values(response, requestid, reason, agent)) {
if (ims_routeevent_reason_t::RouteReasonSuccess == reason) {
// acd got agent
IVR_TRACE("get agent success, agent %s!", agent.c_str());
if (it->second.type == STRING && it->second.pvalue != NULL) {
// set output param
*(string*)it->second.pvalue = agent;
exit = EXIT_SUCC;
} else {
IVR_WARN("output param is not STRING type!");
}
} else {
IVR_WARN("get agent failed!");
}
} else {
IVR_WARN("failed parse json string!");
}
}
return _find_exit_node(param, exit);
}
bool NodeRequestResponseCompare::_get_values(const string& response,
ims_routerequest_id_t& requestid, int32_t& reason, string& agent) {
IVR_TRACE("parse json string(%s)", response.c_str());
json_object* obj = json_tokener_parse(response.c_str());
if (NULL == obj || is_error(obj)) {
IVR_WARN("check ims reponse failed, parse json error!");
return false;
}
{
// get request id
json_object* obj_requestid = json_object_object_get(obj, "requestID");
if (NULL == obj_requestid || is_error(obj_requestid)) {
IVR_WARN("check ims reponse failed, get requestid error!");
json_object_put(obj);
return false;
}
const char* requestid_result = json_object_get_string(obj_requestid);
if (NULL == requestid_result) {
IVR_WARN("check ims reponse failed, get requestid string error!");
//json_object_put(obj_requestid);
json_object_put(obj);
return false;
}
requestid = atoll(requestid_result);
//json_object_put(obj_requestid);
}
{
// get response result
json_object* obj_resresult = json_object_object_get(obj, "RequestResult");
if (NULL == obj_resresult || is_error(obj_resresult)) {
IVR_WARN("check ims reponse failed, get response result error!");
json_object_put(obj);
return false;
}
const char* response_result = json_object_get_string(obj_resresult);
if (NULL == response_result) {
IVR_WARN("check ims reponse failed, get response result string error!");
//json_object_put(obj_resresult);
json_object_put(obj);
return false;
} else if (0 == strcasecmp(response_result, "RouteReasonSuccess")) {
IVR_TRACE("route reason success!");
reason = ims_routeevent_reason_t::RouteReasonSuccess;
} else if (0 == strcasecmp(response_result, "RouteReasonBusy")) {
IVR_TRACE("route reason busy!");
reason = ims_routeevent_reason_t::RouteReasonBusy;
} else if (0 == strcasecmp(response_result, "RouteReasonPrivateBusy")) {
IVR_TRACE("route reason private busy!");
reason = ims_routeevent_reason_t::RouteReasonPrivateBusy;
} else if (0 == strcasecmp(response_result, "RouteReasonError")) {
IVR_TRACE("route reason error!");
reason = ims_routeevent_reason_t::RouteReasonError;
} else if (0 == strcasecmp(response_result, "RouteReasonOther")) {
IVR_TRACE("route reason other!");
reason = ims_routeevent_reason_t::RouteReasonOther;
} else {
IVR_WARN("unknown reason %s!", response_result);
//json_object_put(obj_resresult);
json_object_put(obj);
return false;
}
//json_object_put(obj_resresult);
}
{
// get agent
json_object* obj_agent = json_object_object_get(obj, "Agent");
if (NULL == obj_agent || is_error(obj_agent)) {
IVR_WARN("check ims reponse failed, get agent error!");
json_object_put(obj);
return false;
}
const char* agent_result = json_object_get_string(obj_agent);
if (NULL == agent_result) {
IVR_WARN("check ims reponse failed, get requestid string error!");
//json_object_put(obj_agent);
json_object_put(obj);
return false;
}
agent = agent_result;
//json_object_put(obj_agent);
}
json_object_put(obj);
return true;
}
string NodeRequestResponseCompare::enter(const map<string, variable_t>& vars) const {
string_build strb;
strb + NodeBase::enter(vars) +
"response: " + _response;
return strb.str();
}
string NodeRequestResponseCompare::leave(const map<string, variable_t>& vars) const {
string_build strb;
strb + NodeBase::leave(vars) +
"agent: " + _agent;
return strb.str();
}
NodeBase* NodeRequestResponseCompare::_find_exit_node(base_script_t* param, const char* exit) {
NodeBase* exit_node_ptr = NULL;
std::map<std::string, NodeBase*>::iterator citr;
citr = _exit_node_map.find(exit);
if (citr != _exit_node_map.end()) {
exit_node_ptr = citr->second;
IVR_TRACE("%s exit from %s(%s)",
leave(param->name_var_map).c_str(), exit, param->callid.c_str());
} else {
IVR_WARN("Can not find exit %s in _exit_node_map", exit);
}
return exit_node_ptr;
}
bool NodeRequestResponseCompare::load_other() {
return NodeBase::load_other() &&
valid_str(_key_map, PARAM_AGENT, _agent) &&
valid_str(_key_map, PARAM_RESPONSE, _response);
}
const char* NodeRequestResponseCompare::PARAM_RESPONSE = "response";
const char* NodeRequestResponseCompare::PARAM_AGENT = "agent";