/
mod_erlang_event.c
2221 lines (1802 loc) · 70.4 KB
/
mod_erlang_event.c
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2014, Anthony Minessale II <anthm@freeswitch.org>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <anthm@freeswitch.org>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <anthm@freeswitch.org>
* Andrew Thompson <andrew@hijacked.us>
* Rob Charlton <rob.charlton@savageminds.com>
* Tamas Cseke <tamas.cseke@virtual-call-center.eu>
* Seven Du <dujinfang@gmail.com>
*
*
* mod_erlang_event.c -- Erlang Event Handler derived from mod_event_socket
*
*/
#include <switch.h>
#include <ei.h>
#define DEFINE_GLOBALS
#include "mod_erlang_event.h"
SWITCH_MODULE_LOAD_FUNCTION(mod_erlang_event_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_erlang_event_shutdown);
SWITCH_MODULE_RUNTIME_FUNCTION(mod_erlang_event_runtime);
SWITCH_MODULE_DEFINITION(mod_erlang_event, mod_erlang_event_load, mod_erlang_event_shutdown, mod_erlang_event_runtime);
static switch_memory_pool_t *module_pool = NULL;
static void remove_listener(listener_t *listener);
static void destroy_listener(listener_t *listener);
static switch_status_t state_handler(switch_core_session_t *session);
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_pref_ip, prefs.ip);
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_pref_cookie, prefs.cookie);
SWITCH_DECLARE_GLOBAL_STRING_FUNC(set_pref_nodename, prefs.nodename);
static void *SWITCH_THREAD_FUNC listener_run(switch_thread_t *thread, void *obj);
static void launch_listener_thread(listener_t *listener);
session_elem_t *find_session_elem_by_uuid(listener_t *listener, const char *uuid);
static switch_status_t socket_logger(const switch_log_node_t *node, switch_log_level_t level)
{
listener_t *l;
switch_thread_rwlock_rdlock(mod_erlang_event_globals.listener_rwlock);
for (l = listen_list.listeners; l; l = l->next) {
if (switch_test_flag(l, LFLAG_LOG) && l->level >= node->level) {
switch_log_node_t *dnode = switch_log_node_dup(node);
if (switch_queue_trypush(l->log_queue, dnode) == SWITCH_STATUS_SUCCESS) {
if (l->lost_logs) {
int ll = l->lost_logs;
switch_event_t *event;
l->lost_logs = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Lost %d log lines!\n", ll);
if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "info", "lost %d log lines", ll);
switch_event_fire(&event);
}
}
} else {
switch_log_node_free(&dnode);
l->lost_logs++;
}
}
}
switch_thread_rwlock_unlock(mod_erlang_event_globals.listener_rwlock);
return SWITCH_STATUS_SUCCESS;
}
static void remove_binding(listener_t *listener, erlang_pid * pid)
{
struct erlang_binding *ptr, *lst = NULL;
switch_thread_rwlock_wrlock(mod_erlang_event_globals.bindings_rwlock);
switch_xml_set_binding_sections(bindings.search_binding, SWITCH_XML_SECTION_MAX);
for (ptr = bindings.head; ptr; lst = ptr, ptr = ptr->next) {
if ((listener && ptr->listener == listener) || (pid && (ptr->process.type == ERLANG_PID) && !ei_compare_pids(&ptr->process.pid, pid))) {
if (bindings.head == ptr) {
if (ptr->next) {
bindings.head = ptr->next;
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Removed all (only?) binding\n");
bindings.head = NULL;
break;
}
} else {
if (ptr->next) {
lst->next = ptr->next;
} else {
lst->next = NULL;
}
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Removed binding\n");
} else {
switch_xml_set_binding_sections(bindings.search_binding, switch_xml_get_binding_sections(bindings.search_binding) | ptr->section);
}
}
switch_thread_rwlock_unlock(mod_erlang_event_globals.bindings_rwlock);
}
static void send_event_to_attached_sessions(listener_t *listener, switch_event_t *event)
{
char *uuid = switch_event_get_header(event, "unique-id");
switch_event_t *clone = NULL;
session_elem_t *s = NULL;
if (!uuid) {
return;
}
if ((s = (session_elem_t*)find_session_elem_by_uuid(listener, uuid))) {
int send = 0;
switch_thread_rwlock_rdlock(s->event_rwlock);
if (s->event_list[SWITCH_EVENT_ALL]) {
send = 1;
} else if ((s->event_list[event->event_id])) {
if (event->event_id != SWITCH_EVENT_CUSTOM || !event->subclass_name || (switch_core_hash_find(s->event_hash, event->subclass_name))) {
send = 1;
}
}
switch_thread_rwlock_unlock(s->event_rwlock);
if (send) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_DEBUG, "Sending event %s to attached session %s\n",
switch_event_name(event->event_id), s->uuid_str);
if (switch_event_dup(&clone, event) == SWITCH_STATUS_SUCCESS) {
/* add the event to the queue for this session */
if (switch_queue_trypush(s->event_queue, clone) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_ERROR, "Lost event!\n");
switch_event_destroy(&clone);
}
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_ERROR, "Memory Error!\n");
}
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(s->uuid_str), SWITCH_LOG_DEBUG, "Ignoring event %s for attached session %s\n",
switch_event_name(event->event_id), s->uuid_str);
}
switch_thread_rwlock_unlock(s->rwlock);
}
}
static void event_handler(switch_event_t *event)
{
switch_event_t *clone = NULL;
listener_t *l, *lp;
switch_assert(event != NULL);
if (!listen_list.ready) {
return;
}
switch_thread_rwlock_rdlock(mod_erlang_event_globals.listener_rwlock);
lp = listen_list.listeners;
while (lp) {
uint8_t send = 0;
l = lp;
lp = lp->next;
/* test all of the sessions attached to this listener in case
one of them should receive the event as well
*/
send_event_to_attached_sessions(l, event);
if (!switch_test_flag(l, LFLAG_EVENTS)) {
continue;
}
switch_thread_rwlock_rdlock(l->event_rwlock);
if (l->event_list[SWITCH_EVENT_ALL]) {
send = 1;
} else if ((l->event_list[event->event_id])) {
if (event->event_id != SWITCH_EVENT_CUSTOM || !event->subclass_name || (switch_core_hash_find(l->event_hash, event->subclass_name))) {
send = 1;
}
}
if (send) {
switch_mutex_lock(l->filter_mutex);
if (l->filters && l->filters->headers) {
switch_event_header_t *hp;
const char *hval;
for (hp = l->filters->headers; hp; hp = hp->next) {
if ((hval = switch_event_get_header(event, hp->name))) {
const char *comp_to = hp->value;
int pos = 1, cmp = 0;
while (comp_to && *comp_to) {
if (*comp_to == '+') {
pos = 1;
} else if (*comp_to == '-') {
pos = 0;
} else if (*comp_to != ' ') {
break;
}
comp_to++;
}
if (!(comp_to && *comp_to)) {
if (pos) {
send = 1;
continue;
} else {
send = 0;
break;
}
}
if (*hp->value == '/') {
switch_regex_t *re = NULL;
int ovector[30];
cmp = !!switch_regex_perform(hval, comp_to, &re, ovector, sizeof(ovector) / sizeof(ovector[0]));
switch_regex_safe_free(re);
} else {
cmp = !strcasecmp(hval, comp_to);
}
if (cmp) {
if (pos) {
send = 1;
} else {
send = 0;
break;
}
} else {
if (pos) {
send = 0;
break;
} else {
send = 1;
}
}
}
}
}
switch_mutex_unlock(l->filter_mutex);
}
switch_thread_rwlock_unlock(l->event_rwlock);
if (send) {
if (switch_event_dup(&clone, event) == SWITCH_STATUS_SUCCESS) {
if (switch_queue_trypush(l->event_queue, clone) == SWITCH_STATUS_SUCCESS) {
if (l->lost_events) {
int le = l->lost_events;
l->lost_events = 0;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Lost %d events!\n", le);
clone = NULL;
if (switch_event_create(&clone, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(clone, SWITCH_STACK_BOTTOM, "info", "lost %d events", le);
switch_event_fire(&clone);
}
}
} else {
l->lost_events++;
switch_event_destroy(&clone);
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Memory Error!\n");
}
}
}
switch_thread_rwlock_unlock(mod_erlang_event_globals.listener_rwlock);
}
static void close_socket(switch_socket_t ** sock)
{
switch_mutex_lock(listen_list.sock_mutex);
if (*sock) {
switch_socket_shutdown(*sock, SWITCH_SHUTDOWN_READWRITE);
switch_socket_close(*sock);
*sock = NULL;
}
switch_mutex_unlock(listen_list.sock_mutex);
}
static void add_listener(listener_t *listener)
{
/* add me to the listeners so I get events */
switch_thread_rwlock_wrlock(mod_erlang_event_globals.listener_rwlock);
listener->next = listen_list.listeners;
listen_list.listeners = listener;
switch_thread_rwlock_unlock(mod_erlang_event_globals.listener_rwlock);
}
static void remove_listener(listener_t *listener)
{
listener_t *l, *last = NULL;
switch_thread_rwlock_wrlock(mod_erlang_event_globals.listener_rwlock);
for (l = listen_list.listeners; l; l = l->next) {
if (l == listener) {
if (last) {
last->next = l->next;
} else {
listen_list.listeners = l->next;
}
}
last = l;
}
switch_thread_rwlock_unlock(mod_erlang_event_globals.listener_rwlock);
}
/* Search for a listener already talking to the specified node and lock for reading*/
static listener_t *find_listener(char *nodename)
{
listener_t *l = NULL;
switch_thread_rwlock_rdlock(mod_erlang_event_globals.listener_rwlock);
for (l = listen_list.listeners; l; l = l->next) {
if (!strncmp(nodename, l->peer_nodename, MAXNODELEN)) {
switch_thread_rwlock_rdlock(l->rwlock);
break;
}
}
switch_thread_rwlock_unlock(mod_erlang_event_globals.listener_rwlock);
return l;
}
static void add_session_elem_to_listener(listener_t *listener, session_elem_t *session_element)
{
switch_thread_rwlock_wrlock(listener->session_rwlock);
switch_core_hash_insert(listener->sessions, session_element->uuid_str, (void*) session_element);
switch_thread_rwlock_unlock(listener->session_rwlock);
}
static void remove_session_elem_from_listener(listener_t *listener, session_elem_t *session_element)
{
switch_thread_rwlock_wrlock(listener->session_rwlock);
switch_core_hash_delete(listener->sessions, session_element->uuid_str);
switch_thread_rwlock_unlock(listener->session_rwlock);
}
static void destroy_session_elem(session_elem_t *session_element)
{
switch_core_session_t *session;
void *pop;
/* wait for readers */
switch_thread_rwlock_wrlock(session_element->rwlock);
switch_thread_rwlock_unlock(session_element->rwlock);
if ((session = switch_core_session_force_locate(session_element->uuid_str))) {
switch_channel_t *channel = switch_core_session_get_channel(session);
switch_channel_set_private(channel, "_erlang_session_", NULL);
switch_channel_clear_flag(channel, CF_CONTROLLED);
switch_core_session_soft_unlock(session);
switch_core_session_rwunlock(session);
}
while (switch_queue_trypop(session_element->event_queue, &pop) == SWITCH_STATUS_SUCCESS) {
switch_event_t *event = (switch_event_t *) pop;
switch_event_destroy(&event);
}
switch_core_hash_destroy(&session_element->event_hash);
switch_core_destroy_memory_pool(&session_element->pool);
}
session_elem_t *find_session_elem_by_uuid(listener_t *listener, const char *uuid)
{
session_elem_t *session = NULL;
switch_thread_rwlock_rdlock(listener->session_rwlock);
if ((session = (session_elem_t*)switch_core_hash_find(listener->sessions, uuid))) {
switch_thread_rwlock_rdlock(session->rwlock);
}
switch_thread_rwlock_unlock(listener->session_rwlock);
return session;
}
session_elem_t *find_session_elem_by_pid(listener_t *listener, erlang_pid *pid)
{
switch_hash_index_t *iter = NULL;
const void *key = NULL;
void *val = NULL;
session_elem_t *session = NULL;
switch_thread_rwlock_rdlock(listener->session_rwlock);
for (iter = switch_core_hash_first(listener->sessions); iter; iter = switch_core_hash_next(&iter)) {
switch_core_hash_this(iter, &key, NULL, &val);
if (((session_elem_t*)val)->process.type == ERLANG_PID && !ei_compare_pids(pid, &((session_elem_t*)val)->process.pid)) {
session = (session_elem_t*)val;
switch_thread_rwlock_rdlock(session->rwlock);
break;
}
}
switch_safe_free(iter);
switch_thread_rwlock_unlock(listener->session_rwlock);
return session;
}
static fetch_reply_t *new_fetch_reply(const char *uuid_str)
{
fetch_reply_t *reply = NULL;
switch_memory_pool_t *pool = NULL;
if (switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "OH OH no pool\n");
abort();
}
switch_assert(pool != NULL);
reply = switch_core_alloc(pool, sizeof(*reply));
switch_assert(reply != NULL);
memset(reply, 0, sizeof(*reply));
reply->uuid_str = switch_core_strdup(pool, uuid_str);
reply->pool = pool;
switch_thread_cond_create(&reply->ready_or_found, pool);
switch_mutex_init(&reply->mutex, SWITCH_MUTEX_UNNESTED, pool);
reply->state = reply_not_ready;
reply->reply = NULL;
switch_mutex_lock(reply->mutex);
switch_core_hash_insert_locked(mod_erlang_event_globals.fetch_reply_hash, uuid_str, reply, mod_erlang_event_globals.fetch_reply_mutex);
reply->state = reply_waiting;
return reply;
}
static void destroy_fetch_reply(fetch_reply_t *reply)
{
switch_core_hash_delete_locked(mod_erlang_event_globals.fetch_reply_hash, reply->uuid_str, mod_erlang_event_globals.fetch_reply_mutex);
/* lock so nothing can have it while we delete it */
switch_mutex_lock(reply->mutex);
switch_mutex_unlock(reply->mutex);
switch_mutex_destroy(reply->mutex);
switch_thread_cond_destroy(reply->ready_or_found);
switch_safe_free(reply->reply);
switch_core_destroy_memory_pool(&(reply->pool));
}
fetch_reply_t *find_fetch_reply(const char *uuid)
{
fetch_reply_t *reply = NULL;
switch_mutex_lock(mod_erlang_event_globals.fetch_reply_mutex);
if ((reply = switch_core_hash_find(mod_erlang_event_globals.fetch_reply_hash, uuid))) {
if (switch_mutex_lock(reply->mutex) != SWITCH_STATUS_SUCCESS) {
reply = NULL;
}
}
switch_mutex_unlock(mod_erlang_event_globals.fetch_reply_mutex);
return reply;
}
static switch_xml_t erlang_fetch(const char *sectionstr, const char *tag_name, const char *key_name, const char *key_value,
switch_event_t *params, void *user_data)
{
char uuid_str[SWITCH_UUID_FORMATTED_LENGTH + 1];
int type, size;
fetch_reply_t *p = NULL;
switch_time_t now = 0;
char *xmlstr;
struct erlang_binding *ptr;
switch_uuid_t uuid;
switch_xml_section_t section;
switch_xml_t xml = NULL;
ei_x_buff *rep;
ei_x_buff buf;
ei_x_new_with_version(&buf);
switch_uuid_get(&uuid);
switch_uuid_format(uuid_str, &uuid);
ei_x_encode_tuple_header(&buf, 7);
ei_x_encode_atom(&buf, "fetch");
ei_x_encode_atom(&buf, sectionstr);
_ei_x_encode_string(&buf, tag_name ? tag_name : "undefined");
_ei_x_encode_string(&buf, key_name ? key_name : "undefined");
_ei_x_encode_string(&buf, key_value ? key_value : "undefined");
_ei_x_encode_string(&buf, uuid_str);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "looking for bindings\n");
section = switch_xml_parse_section_string((char *) sectionstr);
switch_thread_rwlock_rdlock(mod_erlang_event_globals.bindings_rwlock);
/* Keep the listener from getting pulled out from under us */
switch_thread_rwlock_rdlock(mod_erlang_event_globals.listener_rwlock);
for (ptr = bindings.head; ptr; ptr = ptr->next) {
/* If we got listener_rwlock while a listner thread was dying after removing the listener
from listener_list but before locking for the bindings removal (now pending our lock) check
if it already closed the socket. Our listener pointer should still be good (pointed at an orphan
listener) until it is removed from the binding...*/
if (!ptr->listener) {
continue;
}
if (ptr->section != section) {
continue;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "binding for %s in section %s with key %s and value %s requested from node %s\n", tag_name, sectionstr, key_name, key_value, ptr->process.pid.node);
if (params) {
ei_encode_switch_event_headers(&buf, params);
} else {
ei_x_encode_empty_list(&buf);
}
if (!p) {
p = new_fetch_reply(uuid_str);
now = switch_micro_time_now();
}
/* We don't need to lock here because everybody is waiting
on our condition before the action starts. */
switch_mutex_lock(ptr->listener->sock_mutex);
if (ptr->listener->sockdes) {
ei_sendto(ptr->listener->ec, ptr->listener->sockdes, &ptr->process, &buf);
}
switch_mutex_unlock(ptr->listener->sock_mutex);
}
switch_thread_rwlock_unlock(mod_erlang_event_globals.bindings_rwlock);
switch_thread_rwlock_unlock(mod_erlang_event_globals.listener_rwlock);
ei_x_free(&buf);
if (!p) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "no binding for %s\n", sectionstr);
goto cleanup;
}
/* Wait five seconds for a reply. */
switch_thread_cond_timedwait(p->ready_or_found, p->mutex, 5000000);
if (!p->reply) {
p->state = reply_timeout;
switch_mutex_unlock(p->mutex);
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "Timed out after %d milliseconds when waiting for XML fetch response for %s\n", (int) (switch_micro_time_now() - now) / 1000, uuid_str);
goto cleanup;
}
rep = p->reply;
switch_mutex_unlock(p->mutex);
ei_get_type(rep->buff, &rep->index, &type, &size);
if (type == ERL_NIL_EXT) {
// empty string returned
goto cleanup;
}
if (type != ERL_STRING_EXT && type != ERL_BINARY_EXT) { /* XXX no unicode or character codes > 255 */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "XML fetch response contained non ASCII characters? (was type %d of size %d)\n", type,
size);
goto cleanup;
}
if (!(xmlstr = malloc(size + 1))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Memory Error\n");
goto cleanup;
}
ei_decode_string_or_binary(rep->buff, &rep->index, size, xmlstr);
if (mod_erlang_event_globals.debug) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "got data %s after %d milliseconds from %s for %s!\n", xmlstr, (int) (switch_micro_time_now() - now) / 1000, p->winner, uuid_str);
}
if (zstr(xmlstr)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "No Result\n");
} else if (!(xml = switch_xml_parse_str_dynamic(xmlstr, SWITCH_FALSE))) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error Parsing XML Result!\n");
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "XML parsed OK!\n");
}
/* cleanup */
cleanup:
if (p) {
destroy_fetch_reply(p);
}
return xml;
}
static switch_status_t notify_new_session(listener_t *listener, session_elem_t *session_element)
{
int result;
switch_core_session_t *session;
switch_event_t *call_event = NULL;
switch_channel_t *channel = NULL;
ei_x_buff lbuf;
/* Send a message to the associated registered process to let it know there is a call.
Message is a tuple of the form {call, <call-event>}
*/
if (switch_event_create(&call_event, SWITCH_EVENT_CHANNEL_DATA) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(session_element->uuid_str), SWITCH_LOG_CRIT, "Memory Error!\n");
return SWITCH_STATUS_MEMERR;
}
if (!(session = switch_core_session_locate(session_element->uuid_str))) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(session_element->uuid_str), SWITCH_LOG_WARNING, "Can't locate session %s\n", session_element->uuid_str);
switch_event_destroy(&call_event);
return SWITCH_STATUS_FALSE;
}
channel = switch_core_session_get_channel(session);
switch_caller_profile_event_set_data(switch_channel_get_caller_profile(channel), "Channel", call_event);
switch_channel_event_set_data(channel, call_event);
switch_core_session_rwunlock(session);
/* TODO reply? sure? */
switch_event_add_header_string(call_event, SWITCH_STACK_BOTTOM, "Content-Type", "command/reply");
switch_event_add_header_string(call_event, SWITCH_STACK_BOTTOM, "Reply-Text", "+OK\n");
ei_x_new_with_version(&lbuf);
ei_x_encode_tuple_header(&lbuf, 2);
ei_x_encode_atom(&lbuf, "call");
ei_encode_switch_event(&lbuf, call_event);
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(session_element->uuid_str), SWITCH_LOG_DEBUG, "Sending initial call event for %s\n",
session_element->uuid_str);
switch_mutex_lock(listener->sock_mutex);
result = ei_sendto(listener->ec, listener->sockdes, &session_element->process, &lbuf);
switch_mutex_unlock(listener->sock_mutex);
if (result) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(session_element->uuid_str), SWITCH_LOG_ERROR, "Failed to send call event for %s\n",
session_element->uuid_str);
}
switch_event_destroy(&call_event);
ei_x_free(&lbuf);
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t check_attached_sessions(listener_t *listener, int *msgs_sent)
{
session_elem_t *sp;
switch_status_t status = SWITCH_STATUS_SUCCESS;
void *pop;
const void *key;
void * value;
switch_hash_index_t *iter;
/* event used to track sessions to remove */
switch_event_t *event = NULL;
switch_event_header_t *header = NULL;
switch_event_create_subclass(&event, SWITCH_EVENT_CLONE, NULL);
switch_assert(event);
/* check up on all the attached sessions -
if they have not yet sent an initial call event to the associated erlang process then do so
if they have pending events in their queues then send them
if the session has finished then clean it up
*/
/* TODO try to minimize critical section */
switch_thread_rwlock_rdlock(listener->session_rwlock);
for (iter = switch_core_hash_first(listener->sessions); iter; iter = switch_core_hash_next(&iter)) {
switch_core_hash_this(iter, &key, NULL, &value);
sp = (session_elem_t*)value;
if (switch_test_flag(sp, LFLAG_WAITING_FOR_PID)) {
continue;
}
if (!switch_test_flag(sp, LFLAG_OUTBOUND_INIT)) {
status = notify_new_session(listener, sp);
if (status != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(sp->uuid_str), SWITCH_LOG_DEBUG, "Notifying new session failed\n");
/* mark this session for removal */
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "delete", (const char *) key);
continue;
}
switch_set_flag_locked(sp, LFLAG_OUTBOUND_INIT);
}
if (switch_test_flag(sp, LFLAG_SESSION_COMPLETE)) {
ei_x_buff ebuf;
/* flush the event queue */
while (switch_queue_trypop(sp->event_queue, &pop) == SWITCH_STATUS_SUCCESS) {
switch_event_t *pevent = (switch_event_t *) pop;
/*switch_log_printf(SWITCH_CHANNEL_UUID_LOG(sp->uuid_str), SWITCH_LOG_DEBUG, "flushed event %s for %s\n", switch_event_name(pevent->event_id), sp->uuid_str); */
/* events from attached sessions are wrapped in a {call_event,<EVT>} tuple
to distinguish them from normal events (if they are sent to the same process)
*/
ei_x_new_with_version(&ebuf);
ei_x_encode_tuple_header(&ebuf, 2);
ei_x_encode_atom(&ebuf, "call_event");
ei_encode_switch_event(&ebuf, pevent);
switch_mutex_lock(listener->sock_mutex);
ei_sendto(listener->ec, listener->sockdes, &sp->process, &ebuf);
switch_mutex_unlock(listener->sock_mutex);
(*msgs_sent)++;
ei_x_free(&ebuf);
switch_event_destroy(&pevent);
}
/* this session can be removed */
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(sp->uuid_str), SWITCH_LOG_DEBUG, "Destroy event for attached session for %s in state %s\n",
sp->uuid_str, switch_channel_state_name(sp->channel_state));
ei_x_new_with_version(&ebuf);
ei_x_encode_tuple_header(&ebuf, 2);
ei_x_encode_atom(&ebuf, "call_hangup");
_ei_x_encode_string(&ebuf, sp->uuid_str);
switch_mutex_lock(listener->sock_mutex);
ei_sendto(listener->ec, listener->sockdes, &sp->process, &ebuf);
(*msgs_sent)++;
switch_mutex_unlock(listener->sock_mutex);
ei_x_free(&ebuf);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "delete", (const char *) key);
continue;
} else if (switch_queue_trypop(sp->event_queue, &pop) == SWITCH_STATUS_SUCCESS) {
/* check event queue for this session */
switch_event_t *pevent = (switch_event_t *) pop;
/*switch_log_printf(SWITCH_CHANNEL_UUID_LOG(sp->uuid_str), SWITCH_LOG_DEBUG, "popped event %s for %s\n", switch_event_name(pevent->event_id), sp->uuid_str); */
/* events from attached sessions are wrapped in a {call_event,<EVT>} tuple
to distinguish them from normal events (if they are sent to the same process)
*/
ei_x_buff ebuf;
ei_x_new_with_version(&ebuf);
ei_x_encode_tuple_header(&ebuf, 2);
ei_x_encode_atom(&ebuf, "call_event");
ei_encode_switch_event(&ebuf, pevent);
switch_mutex_lock(listener->sock_mutex);
ei_sendto(listener->ec, listener->sockdes, &sp->process, &ebuf);
switch_mutex_unlock(listener->sock_mutex);
(*msgs_sent)++;
ei_x_free(&ebuf);
switch_event_destroy(&pevent);
}
}
switch_thread_rwlock_unlock(listener->session_rwlock);
/* do the deferred remove */
for (header = event->headers; header; header = header->next) {
if ((sp = (session_elem_t*)find_session_elem_by_uuid(listener, header->value))) {
remove_session_elem_from_listener(listener, sp);
switch_thread_rwlock_unlock(sp->rwlock);
destroy_session_elem(sp);
}
}
/* remove the temporary event */
switch_event_destroy(&event);
if (prefs.done) {
return SWITCH_STATUS_FALSE; /* we're shutting down */
} else {
return SWITCH_STATUS_SUCCESS;
}
}
static int check_log_queue(listener_t *listener)
{
void *pop;
int msgs_sent = 0;
/* send out any pending crap in the log queue */
if (switch_test_flag(listener, LFLAG_LOG)) {
while (msgs_sent < prefs.max_log_bulk && switch_queue_trypop(listener->log_queue, &pop) == SWITCH_STATUS_SUCCESS) {
switch_log_node_t *dnode = (switch_log_node_t *) pop;
if (dnode->data) {
ei_x_buff lbuf;
ei_x_new_with_version(&lbuf);
ei_x_encode_tuple_header(&lbuf, 2);
ei_x_encode_atom(&lbuf, "log");
ei_x_encode_list_header(&lbuf, 7);
ei_x_encode_tuple_header(&lbuf, 2);
ei_x_encode_atom(&lbuf, "level");
ei_x_encode_char(&lbuf, (unsigned char) dnode->level);
ei_x_encode_tuple_header(&lbuf, 2);
ei_x_encode_atom(&lbuf, "text_channel");
ei_x_encode_char(&lbuf, (unsigned char) dnode->level);
ei_x_encode_tuple_header(&lbuf, 2);
ei_x_encode_atom(&lbuf, "file");
ei_x_encode_string(&lbuf, dnode->file);
ei_x_encode_tuple_header(&lbuf, 2);
ei_x_encode_atom(&lbuf, "func");
ei_x_encode_string(&lbuf, dnode->func);
ei_x_encode_tuple_header(&lbuf, 2);
ei_x_encode_atom(&lbuf, "line");
ei_x_encode_ulong(&lbuf, (unsigned long) dnode->line);
ei_x_encode_tuple_header(&lbuf, 2);
ei_x_encode_atom(&lbuf, "data");
ei_x_encode_string(&lbuf, dnode->data);
ei_x_encode_tuple_header(&lbuf, 2);
ei_x_encode_atom(&lbuf, "user_data");
ei_x_encode_string(&lbuf, switch_str_nil(dnode->userdata));
ei_x_encode_empty_list(&lbuf);
switch_mutex_lock(listener->sock_mutex);
ei_sendto(listener->ec, listener->sockdes, &listener->log_process, &lbuf);
switch_mutex_unlock(listener->sock_mutex);
msgs_sent ++;
ei_x_free(&lbuf);
switch_log_node_free(&dnode);
}
}
}
listener->total_logs += msgs_sent;
return msgs_sent;
}
static int check_event_queue(listener_t *listener)
{
void *pop;
int msgs_sent = 0;
/* send out any pending crap in the event queue */
if (switch_test_flag(listener, LFLAG_EVENTS)) {
while (msgs_sent < prefs.max_event_bulk && switch_queue_trypop(listener->event_queue, &pop) == SWITCH_STATUS_SUCCESS) {
switch_event_t *pevent = (switch_event_t *) pop;
ei_x_buff ebuf;
ei_x_new_with_version(&ebuf);
ei_encode_switch_event(&ebuf, pevent);
switch_mutex_lock(listener->sock_mutex);
ei_sendto(listener->ec, listener->sockdes, &listener->event_process, &ebuf);
switch_mutex_unlock(listener->sock_mutex);
msgs_sent++;
ei_x_free(&ebuf);
if (pevent->event_id == SWITCH_EVENT_CHANNEL_CREATE) {
listener->create++;
} else if (pevent->event_id == SWITCH_EVENT_CHANNEL_HANGUP_COMPLETE) {
listener->hangup++;
}
switch_event_destroy(&pevent);
}
}
listener->total_events += msgs_sent;
return msgs_sent;
}
static void handle_exit(listener_t *listener, erlang_pid * pid)
{
session_elem_t *s;
remove_binding(NULL, pid); /* TODO - why don't we pass the listener as the first argument? */
if ((s = find_session_elem_by_pid(listener, pid))) {
switch_set_flag_locked(s, LFLAG_SESSION_COMPLETE);
switch_thread_rwlock_unlock(s->rwlock);
}
if (listener->log_process.type == ERLANG_PID && !ei_compare_pids(&listener->log_process.pid, pid)) {
void *pop;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Log handler process for node %s exited\n", pid->node);
/*purge the log queue */
/* TODO don't we want to clear flag first? */
while (switch_queue_trypop(listener->log_queue, &pop) == SWITCH_STATUS_SUCCESS) {
switch_log_node_t *dnode = (switch_log_node_t *) pop;
switch_log_node_free(&dnode);
}
if (switch_test_flag(listener, LFLAG_LOG)) {
switch_clear_flag_locked(listener, LFLAG_LOG);
}
}
if (listener->event_process.type == ERLANG_PID && !ei_compare_pids(&listener->event_process.pid, pid)) {
void *pop;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Event handler process for node %s exited\n", pid->node);
/*purge the event queue */
/* TODO don't we want to clear flag first? */
while (switch_queue_trypop(listener->event_queue, &pop) == SWITCH_STATUS_SUCCESS) {
switch_event_t *pevent = (switch_event_t *) pop;
switch_event_destroy(&pevent);
}
if (switch_test_flag(listener, LFLAG_EVENTS)) {
uint8_t x = 0;
switch_clear_flag_locked(listener, LFLAG_EVENTS);
switch_thread_rwlock_wrlock(listener->event_rwlock);
for (x = 0; x <= SWITCH_EVENT_ALL; x++) {
listener->event_list[x] = 0;
}
switch_core_hash_delete_multi(listener->event_hash, NULL, NULL);
switch_thread_rwlock_unlock(listener->event_rwlock);
}
}
}
static void listener_main_loop(listener_t *listener)
{
int status = 1;
int msgs_sent = 0; /* how many messages we sent in a loop */
while ((status >= 0 || erl_errno == ETIMEDOUT || erl_errno == EAGAIN) && !prefs.done) {
erlang_msg msg;
ei_x_buff buf;
ei_x_buff rbuf;
ei_x_new(&buf);
ei_x_new_with_version(&rbuf);
msgs_sent = 0;
/* do we need the mutex when reading? */
/*switch_mutex_lock(listener->sock_mutex); */
status = ei_xreceive_msg_tmo(listener->sockdes, &msg, &buf, 1);
/*switch_mutex_unlock(listener->sock_mutex); */
switch (status) {
case ERL_TICK:
break;
case ERL_MSG:
switch (msg.msgtype) {
case ERL_SEND:
#ifdef EI_DEBUG
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "erl_send\n");
ei_x_print_msg(&buf, &msg.from, 0);