switch_hash_t *caller_orig_hash;
switch_hash_t *consumer_orig_hash;
switch_hash_t *bridge_hash;
+ switch_hash_t *use_hash;
+ switch_mutex_t *use_mutex;
switch_mutex_t *caller_orig_mutex;
switch_mutex_t *consumer_orig_mutex;
switch_mutex_t *bridge_mutex;
+static int fifo_dec_use_count(const char *outbound_id)
+{
+ int r = 0, *count;
+
+
+ switch_mutex_lock(globals.use_mutex);
+ if ((count = (int *) switch_core_hash_find(globals.use_hash, outbound_id))) {
+ if (*count > 0) {
+ r = --(*count);
+ }
+ }
+ switch_mutex_unlock(globals.use_mutex);
+
+ return r;
+}
+
+static int fifo_get_use_count(const char *outbound_id)
+{
+ int r = 0, *count;
+
+ switch_mutex_lock(globals.use_mutex);
+ if ((count = (int *) switch_core_hash_find(globals.use_hash, outbound_id))) {
+ r = *count;
+ }
+ switch_mutex_unlock(globals.use_mutex);
+
+ return r;
+}
+
+
+static int fifo_inc_use_count(const char *outbound_id)
+{
+ int r = 0, *count;
+
+ switch_mutex_lock(globals.use_mutex);
+ if (!(count = (int *) switch_core_hash_find(globals.use_hash, outbound_id))) {
+ count = switch_core_alloc(globals.pool, sizeof(int));
+ switch_core_hash_insert(globals.use_hash, outbound_id, count);
+ }
+
+ r = ++(*count);
+
+ switch_mutex_unlock(globals.use_mutex);
+
+ return r;
+}
+
+static void fifo_init_use_count(void)
+{
+ switch_mutex_lock(globals.use_mutex);
+ if (globals.use_hash) {
+ switch_core_hash_destroy(&globals.use_hash);
+ }
+ switch_core_hash_init(&globals.use_hash, globals.pool);
+ switch_mutex_unlock(globals.use_mutex);
+}
+
+
+
+
static int check_caller_outbound_call(const char *key)
{
int x = 0;
const char *epoch_start_a = NULL;
char *sql;
switch_event_t *event;
+ const char *outbound_id = NULL;
+ int use_count = 0;
switch_channel_clear_app_flag_key(FIFO_APP_KEY, consumer_channel, FIFO_APP_BRIDGE_TAG);
switch_channel_set_variable(consumer_channel, "fifo_bridged", NULL);
+ if ((outbound_id = switch_channel_get_variable(consumer_channel, "fifo_outbound_uuid"))) {
+ use_count = fifo_get_use_count(outbound_id);
+ }
+
ts = switch_micro_time_now();
switch_time_exp_lt(&tm, ts);
switch_strftime_nocheck(date, &retsize, sizeof(date), "%Y-%m-%d %T", &tm);
switch_channel_event_set_data(consumer_channel, event);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Name", MANUAL_QUEUE_NAME);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Action", "bridge-consumer-stop");
+ if (use_count) {
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Outbound-ID", outbound_id);
+ switch_event_add_header(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Use-Count", "%d", use_count);
+ }
switch_event_fire(&event);
}
switch_time_t ts;
switch_time_exp_t tm;
switch_size_t retsize;
- const char *ced_name, *ced_number, *cid_name, *cid_number;
+ const char *ced_name, *ced_number, *cid_name, *cid_number, *outbound_id;
if (switch_channel_test_app_flag_key(FIFO_APP_KEY, consumer_channel, FIFO_APP_BRIDGE_TAG)) {
goto end;
switch_channel_set_variable(consumer_channel, "fifo_bridged", "true");
switch_channel_set_variable(consumer_channel, "fifo_manual_bridge", "true");
switch_channel_set_variable(consumer_channel, "fifo_role", "consumer");
+ outbound_id = switch_channel_get_variable(consumer_channel, "fifo_outbound_uuid");
if (caller_channel) {
switch_channel_set_variable(caller_channel, "fifo_role", "caller");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Action", "bridge-consumer-start");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Caller-CID-Name", ced_name);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Caller-CID-Number", ced_number);
+ if (outbound_id) {
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Outbound-ID", outbound_id);
+ switch_event_add_header(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Use-Count", "%d", fifo_get_use_count(outbound_id));
+ }
switch_event_fire(&event);
}
static void dec_use_count(switch_core_session_t *session, switch_bool_t send_event)
{
char *sql;
- const char *outbound_id;
+ const char *outbound_id = NULL;
switch_event_t *event;
long now = (long) switch_epoch_time_now(NULL);
switch_channel_t *channel = switch_core_session_get_channel(session);
sql = switch_mprintf("update fifo_outbound set use_count=use_count-1, stop_time=%ld, next_avail=%ld + lag + 1 where use_count > 0 and uuid='%q'",
now, now, outbound_id);
fifo_execute_sql_queued(&sql, SWITCH_TRUE, SWITCH_TRUE);
+ fifo_dec_use_count(outbound_id);
}
if (send_event) {
switch_channel_event_set_data(channel, event);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Name", MANUAL_QUEUE_NAME);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Action", "channel-consumer-stop");
+ if (outbound_id) {
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Outbound-ID", outbound_id);
+ switch_event_add_header(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Use-Count", "%d", fifo_get_use_count(outbound_id));
+ }
switch_event_fire(&event);
}
}
sql = switch_mprintf("update fifo_outbound set stop_time=0,start_time=%ld,outbound_fail_count=0,use_count=use_count+1,%s=%s+1,%s=%s+1 where uuid='%q'",
(long) switch_epoch_time_now(NULL), col1, col1, col2, col2, data);
fifo_execute_sql_queued(&sql, SWITCH_TRUE, SWITCH_TRUE);
-
+ fifo_inc_use_count(data);
if (switch_channel_direction(channel) == SWITCH_CALL_DIRECTION_INBOUND) {
cid_name = switch_channel_get_variable(channel, "destination_number");
const char *url = NULL;
const char *caller_uuid = NULL;
const char *outbound_id = switch_channel_get_variable(channel, "fifo_outbound_uuid");
+
//const char *track_use_count = switch_channel_get_variable(channel, "fifo_track_use_count");
//int do_track = switch_true(track_use_count);
switch_process_import(session, other_channel, "fifo_caller_consumer_import", switch_channel_get_variable(channel, "fifo_import_prefix"));
switch_process_import(other_session, channel, "fifo_consumer_caller_import", switch_channel_get_variable(other_channel, "fifo_import_prefix"));
+
+ if (outbound_id) {
+ cancel_consumer_outbound_call(outbound_id, SWITCH_CAUSE_ORIGINATOR_CANCEL);
+ add_bridge_call(outbound_id);
+
+ sql = switch_mprintf("update fifo_outbound set stop_time=0,start_time=%ld,use_count=use_count+1,outbound_fail_count=0 where uuid='%s'",
+ switch_epoch_time_now(NULL), outbound_id);
+
+
+ fifo_execute_sql_queued(&sql, SWITCH_TRUE, SWITCH_TRUE);
+ fifo_inc_use_count(outbound_id);
+
+ }
+
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, FIFO_EVENT) == SWITCH_STATUS_SUCCESS) {
switch_channel_event_set_data(channel, event);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Name", argv[0]);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Action", "bridge-consumer-start");
+ if (outbound_id) {
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Outbound-ID", outbound_id);
+ switch_event_add_header(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Use-Count", "%d", fifo_get_use_count(outbound_id));
+ }
+
switch_event_fire(&event);
}
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, FIFO_EVENT) == SWITCH_STATUS_SUCCESS) {
switch_event_fire(&event);
}
- if (outbound_id) {
- cancel_consumer_outbound_call(outbound_id, SWITCH_CAUSE_ORIGINATOR_CANCEL);
- add_bridge_call(outbound_id);
-
- sql = switch_mprintf("update fifo_outbound set stop_time=0,start_time=%ld,use_count=use_count+1,outbound_fail_count=0 where uuid='%s'",
- switch_epoch_time_now(NULL), outbound_id);
-
-
- fifo_execute_sql_queued(&sql, SWITCH_TRUE, SWITCH_TRUE);
- }
-
+
add_bridge_call(switch_core_session_get_uuid(other_session));
add_bridge_call(switch_core_session_get_uuid(session));
fifo_execute_sql_queued(&sql, SWITCH_TRUE, SWITCH_TRUE);
del_bridge_call(outbound_id);
+ fifo_dec_use_count(outbound_id);
}
switch_channel_event_set_data(channel, event);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Name", argv[0]);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Action", "bridge-consumer-stop");
+ if (outbound_id) {
+ switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Outbound-ID", outbound_id);
+ switch_event_add_header(event, SWITCH_STACK_BOTTOM, "FIFO-Consumer-Use-Count", "%d", fifo_get_use_count(outbound_id));
+ }
switch_event_fire(&event);
}
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, FIFO_EVENT) == SWITCH_STATUS_SUCCESS) {
if (!reload) {
char *sql= "update fifo_outbound set start_time=0,stop_time=0,ring_count=0,use_count=0,outbound_call_count=0,outbound_fail_count=0 where static=0";
fifo_execute_sql_queued(&sql, SWITCH_FALSE, SWITCH_TRUE);
+ fifo_init_use_count();
}
if (reload) {
switch_core_hash_init(&globals.caller_orig_hash, globals.pool);
switch_core_hash_init(&globals.consumer_orig_hash, globals.pool);
switch_core_hash_init(&globals.bridge_hash, globals.pool);
+ switch_core_hash_init(&globals.use_hash, globals.pool);
switch_mutex_init(&globals.caller_orig_mutex, SWITCH_MUTEX_NESTED, globals.pool);
switch_mutex_init(&globals.consumer_orig_mutex, SWITCH_MUTEX_NESTED, globals.pool);
switch_mutex_init(&globals.bridge_mutex, SWITCH_MUTEX_NESTED, globals.pool);
switch_mutex_init(&globals.mutex, SWITCH_MUTEX_NESTED, globals.pool);
+ switch_mutex_init(&globals.use_mutex, SWITCH_MUTEX_NESTED, globals.pool);
switch_mutex_init(&globals.sql_mutex, SWITCH_MUTEX_NESTED, globals.pool);
globals.running = 1;