4
* Copyright (C) 2012 Carlos Ruiz Díaz (caruizdiaz.com),
5
* ConexionGroup (www.conexiongroup.com)
7
* This file is part of Kamailio, a free SIP server.
9
* Kamailio is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version
14
* Kamailio is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
#include "../../locking.h"
28
#include "../../lock_ops.h"
29
#include "../../rpc.h"
30
#include "../../rpc_lookup.h"
32
#include "cnxcc_mod.h"
36
void rpc_kill_call(rpc_t* rpc, void* ctx)
42
if (!rpc->scan(ctx, "S", &callid))
44
LM_ERR("%s: error reading RPC param\n", __FUNCTION__);
48
if (try_get_call_entry(&callid, &call, &hts) != 0)
50
LM_ERR("%s: call [%.*s] not found\n", __FUNCTION__, callid.len, callid.s);
51
rpc->fault(ctx, 404, "CallID Not Found");
57
LM_ERR("%s: call [%.*s] is in null state\n", __FUNCTION__, callid.len, callid.s);
58
rpc->fault(ctx, 500, "Call is NULL");
62
LM_ALERT("Killing call [%.*s] via XMLRPC request\n", callid.len, callid.s);
64
lock_get(&call->lock);
68
lock_release(&call->lock);
71
void rpc_check_client_stats(rpc_t* rpc, void* ctx)
77
credit_data_t *credit_data;
79
if (!rpc->scan(ctx, "S", &client_id))
81
LM_ERR("%s: error reading RPC param\n", __FUNCTION__);
85
if (try_get_credit_data_entry(&client_id, &credit_data) != 0)
87
LM_ERR("%s: client [%.*s] not found\n", __FUNCTION__, client_id.len, client_id.s);
88
rpc->fault(ctx, 404, "Not Found");
92
if (credit_data == NULL)
94
LM_ERR("%s: credit data for client [%.*s] is NULL\n", __FUNCTION__, client_id.len, client_id.s);
95
rpc->fault(ctx, 500, "Internal Server Error");
99
lock_get(&credit_data->lock);
101
if (credit_data->number_of_calls <= 0)
103
lock_release(&credit_data->lock);
104
LM_INFO("No calls for current client\n");
109
rows.s = pkg_malloc(10);
114
clist_foreach_safe(credit_data->call_list, call, tmp, next)
118
memset(row_buffer, 0, sizeof(row_buffer));
120
if (credit_data->type == CREDIT_MONEY)
121
snprintf(row_buffer, sizeof(row_buffer), "id:%d,confirmed:%s,local_consumed_amount:%f,global_consumed_amount:%f,local_max_amount:%f,global_max_amount:%f,call_id:%.*s,start_timestamp:%d"
122
",inip:%d,finp:%d,cps:%f;",
124
call->confirmed ? "yes" : "no",
125
call->consumed_amount,
126
credit_data->consumed_amount,
128
credit_data->max_amount,
129
call->sip_data.callid.len, call->sip_data.callid.s,
130
call->start_timestamp,
131
call->money_based.initial_pulse,
132
call->money_based.final_pulse,
133
call->money_based.cost_per_second
136
snprintf(row_buffer, sizeof(row_buffer), "id:%d,confirmed:%s,local_consumed_amount:%d,global_consumed_amount:%d,local_max_amount:%d,global_max_amount:%d,call_id:%.*s,start_timestamp:%d;",
138
call->confirmed ? "yes" : "no",
139
(int) call->consumed_amount,
140
(int) credit_data->consumed_amount,
141
(int) call->max_amount,
142
(int) credit_data->max_amount,
143
call->sip_data.callid.len, call->sip_data.callid.s,
144
call->start_timestamp);
146
row_len = strlen(row_buffer);
147
rows.s = pkg_realloc(rows.s, rows.len + row_len);
151
lock_release(&credit_data->lock);
155
memcpy(rows.s + rows.len, row_buffer, row_len);
161
lock_release(&credit_data->lock);
163
if (rpc->add(ctx, "S", &rows) < 0)
165
LM_ERR("%s: error creating RPC struct\n", __FUNCTION__);
174
LM_ERR("No more pkg memory");
175
rpc->fault(ctx, 500, "No more memory\n");
178
static int iterate_over_table(hash_tables_t *hts, str *result, credit_type_t type)
180
struct str_hash_entry *h_entry, *tmp;
181
char row_buffer[512];
184
lock_get(&hts->lock);
186
if (hts->credit_data_by_client->table)
187
for(index = 0; index < hts->credit_data_by_client->size; index++)
188
clist_foreach_safe(&hts->credit_data_by_client->table[index], h_entry, tmp, next)
190
credit_data_t *credit_data = (credit_data_t *) h_entry->u.p;
191
lock_get(&credit_data->lock);
195
memset(row_buffer, 0, sizeof(row_buffer));
197
if (type == CREDIT_TIME)
199
snprintf(row_buffer, sizeof(row_buffer), "client_id:%.*s,"
200
"number_of_calls:%d,"
201
"concurrent_calls:%d,"
204
"consumed_amount:%d;",
205
credit_data->call_list->client_id.len, credit_data->call_list->client_id.s,
206
credit_data->number_of_calls,
207
credit_data->concurrent_calls,
209
(int) credit_data->max_amount,
210
(int) credit_data->consumed_amount);
212
else if (type == CREDIT_MONEY)
214
snprintf(row_buffer, sizeof(row_buffer), "client_id:%.*s,"
215
"number_of_calls:%d,"
216
"concurrent_calls:%d,"
219
"consumed_amount:%f;",
220
credit_data->call_list->client_id.len, credit_data->call_list->client_id.s,
221
credit_data->number_of_calls,
222
credit_data->concurrent_calls,
224
credit_data->max_amount,
225
credit_data->consumed_amount);
229
LM_ERR("Unknown credit type: %d", type);
233
lock_release(&credit_data->lock);
235
row_len = strlen(row_buffer);
236
result->s = pkg_realloc(result->s, result->len + row_len);
238
if (result->s == NULL)
240
lock_release(&hts->lock);
244
memcpy(result->s + result->len, row_buffer, row_len);
245
result->len += row_len;
249
lock_release(&hts->lock);
254
LM_ERR("No more pkg memory");
258
void rpc_active_clients(rpc_t* rpc, void* ctx)
262
rows.s = pkg_malloc(10);
269
iterate_over_table(&_data.time, &rows, CREDIT_TIME);
270
iterate_over_table(&_data.money, &rows, CREDIT_MONEY);
272
if (!rpc->add(ctx, "S", &rows) < 0)
274
LM_ERR("%s: error creating RPC struct\n", __FUNCTION__);
283
LM_ERR("No more pkg memory");
284
rpc->fault(ctx, 500, "No more memory\n");