refactor sonar, remove global vars so it can run multiple sonar at a time
This commit is contained in:
parent
17bd89c2a5
commit
33acb29a5b
|
@ -58,27 +58,38 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_sonar_load);
|
|||
*/
|
||||
SWITCH_MODULE_DEFINITION(mod_sonar, mod_sonar_load, mod_sonar_shutdown, NULL);
|
||||
|
||||
switch_time_t start, end, diff;
|
||||
int samples[1024];
|
||||
int received;
|
||||
int sum, min, max = 0;
|
||||
|
||||
struct sonar_ping_helper_s {
|
||||
switch_time_t start, end, diff;
|
||||
int samples[1024];
|
||||
int received;
|
||||
int sum, min, max;
|
||||
};
|
||||
|
||||
typedef struct sonar_ping_helper_s sonar_ping_helper_t;
|
||||
|
||||
switch_bool_t sonar_ping_callback(switch_core_session_t *session, const char *app, const char *app_data){
|
||||
|
||||
if ( end ) {
|
||||
switch_channel_t *channel = switch_core_session_get_channel(session);
|
||||
sonar_ping_helper_t *ph = switch_channel_get_private(channel, "__sonar_ping__");
|
||||
int diff;
|
||||
|
||||
if (!ph) return SWITCH_TRUE;
|
||||
|
||||
if ( ph->end ) {
|
||||
return SWITCH_TRUE;
|
||||
}
|
||||
end = switch_time_now();
|
||||
diff = end - start;
|
||||
|
||||
start = 0;
|
||||
ph->end = switch_time_now();
|
||||
diff = ph->end - ph->start;
|
||||
|
||||
ph->start = 0;
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Sonar ping took %ld milliseconds\n", (long)diff / 1000);
|
||||
|
||||
diff /= 1000;
|
||||
sum += diff;
|
||||
max = MAX(max, diff);
|
||||
min = MIN(min, diff);
|
||||
samples[received++] = diff;
|
||||
ph->sum += diff;
|
||||
ph->max = MAX(ph->max, diff);
|
||||
ph->min = MIN(ph->min, diff);
|
||||
ph->samples[ph->received++] = diff;
|
||||
|
||||
return SWITCH_TRUE;
|
||||
}
|
||||
|
@ -92,7 +103,9 @@ SWITCH_STANDARD_APP(sonar_app)
|
|||
int lost = 0;
|
||||
int x;
|
||||
int avg = 0, mdev = 0;
|
||||
int sum2;
|
||||
switch_event_t *event;
|
||||
sonar_ping_helper_t ph = { 0 };
|
||||
|
||||
if (zstr(arg)) {
|
||||
loops = 5;
|
||||
|
@ -108,7 +121,8 @@ SWITCH_STANDARD_APP(sonar_app)
|
|||
|
||||
switch_channel_answer(channel);
|
||||
switch_ivr_sleep(session, 1000, SWITCH_FALSE, NULL);
|
||||
|
||||
switch_channel_set_private(channel, "__sonar_ping__", &ph);
|
||||
|
||||
switch_ivr_tone_detect_session(session,
|
||||
"ping", "1004",
|
||||
"r", 0,
|
||||
|
@ -116,13 +130,13 @@ SWITCH_STANDARD_APP(sonar_app)
|
|||
|
||||
switch_ivr_sleep(session, 1000, SWITCH_FALSE, NULL);
|
||||
|
||||
received = 0; sum = 0; max = 0; min = 999999;
|
||||
ph.min = 999999;
|
||||
for( x = 0; x < loops; x++ ) {
|
||||
end = 0;
|
||||
start = switch_time_now();
|
||||
ph.end = 0;
|
||||
ph.start = switch_time_now();
|
||||
switch_ivr_gentones(session, tone, 1, NULL);
|
||||
switch_ivr_sleep(session, 2000, SWITCH_FALSE, NULL);
|
||||
if ( start ) {
|
||||
if ( ph.start ) {
|
||||
lost++;
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Lost sonar ping\n");
|
||||
}
|
||||
|
@ -136,32 +150,32 @@ SWITCH_STANDARD_APP(sonar_app)
|
|||
return;
|
||||
}
|
||||
|
||||
if (received + lost != loops) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Race happend %d + %d != %d\n", received, lost, loops);
|
||||
if (ph.received + lost != loops) {
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Race happend %d + %d != %d\n", ph.received, lost, loops);
|
||||
}
|
||||
|
||||
if (received > 0) avg = sum / received;
|
||||
if (ph.received > 0) avg = ph.sum / ph.received;
|
||||
|
||||
sum = 0;
|
||||
for(x = 0; x < received; x++) {
|
||||
sum += (samples[x] - avg) * (samples[x] - avg);
|
||||
sum2 = 0;
|
||||
for(x = 0; x < ph.received; x++) {
|
||||
sum2 += (ph.samples[x] - avg) * (ph.samples[x] - avg);
|
||||
}
|
||||
|
||||
if (received > 1) {
|
||||
mdev = sqrt(sum / (received - 1));
|
||||
if (ph.received > 1) {
|
||||
mdev = sqrt(sum2 / (ph.received - 1));
|
||||
}
|
||||
|
||||
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO,
|
||||
"Sonar Ping (in ms): min:%d max:%d avg:%d mdev:%d sent:%d recv: %d lost:%d lost/send:%2.2f%%\n",
|
||||
min, max, avg, mdev, loops, received, lost, lost * 1.0 / loops);
|
||||
ph.min, ph.max, avg, mdev, loops, ph.received, lost, lost * 1.0 / loops);
|
||||
|
||||
if (switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, "sonar::ping") == SWITCH_STATUS_SUCCESS) {
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_min", "%d", min);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_max", "%d", max);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_min", "%d", ph.min);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_max", "%d", ph.max);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_avg", "%d", avg);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_mdev", "%d", mdev);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_sent", "%d", loops);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_recv", "%d", received);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_recv", "%d", ph.received);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "ping_lost", "%d", lost);
|
||||
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "lost_rate", "%2.2f%%", lost * 1.0 / loops);
|
||||
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "destination_number",
|
||||
|
|
Loading…
Reference in New Issue