@@ -59,6 +59,8 @@ namespace mod_event_kafka {
59
59
10 , NULL , " buffer-size" , " queue.buffering.max.messages" ),
60
60
SWITCH_CONFIG_ITEM (" compression" , SWITCH_CONFIG_STRING, CONFIG_RELOADABLE, &globals.compression ,
61
61
" snappy" , NULL , " snappy / lz4 " , " Compression" ),
62
+ SWITCH_CONFIG_ITEM (" event-filter" , SWITCH_CONFIG_STRING, CONFIG_RELOADABLE, &globals.event_filter ,
63
+ " " , NULL , " comma separated value of event names" , " Event Filter" ),
62
64
SWITCH_CONFIG_ITEM_END ()
63
65
};
64
66
@@ -252,14 +254,49 @@ namespace mod_event_kafka {
252
254
253
255
KafkaModule (switch_loadable_module_interface_t **module_interface, switch_memory_pool_t *pool): _publisher() {
254
256
255
- // Subscribe to all switch events of any subclass
256
- // Store a pointer to ourself in the user data
257
- if (switch_event_bind_removable (modname, SWITCH_EVENT_ALL, SWITCH_EVENT_SUBCLASS_ANY, event_handler,
258
- static_cast <void *>(&_publisher), &_node)
259
- != SWITCH_STATUS_SUCCESS) {
260
- throw std::runtime_error (" Couldn't bind to switch events." );
257
+ char *event_filter_name[SWITCH_EVENT_ALL];
258
+ char *switch_event_custom = (char *) std::string (" SWITCH_EVENT_CUSTOM::" ).c_str ();
259
+ profile.event_subscriptions = switch_separate_string (globals.event_filter , ' ,' , event_filter_name, (sizeof (event_filter_name) / sizeof (event_filter_name[0 ])));
260
+ switch_log_printf (SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " Found %d subscriptions\n " , profile.event_subscriptions );
261
+ for (int i = 0 ; i < profile.event_subscriptions ; i++) {
262
+ if (switch_name_event (event_filter_name[i], &(profile.event_ids [i])) != SWITCH_STATUS_SUCCESS && !switch_strstr (event_filter_name[i],switch_event_custom) ) {
263
+ switch_log_printf (SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, " The switch event %s was not recognised.\n " , event_filter_name[i]);
264
+ } else {
265
+ // switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Found subscription for %s event.\n", argv[arg]);
266
+ }
267
+ }
268
+
269
+ if (profile.event_subscriptions > 0 ) {
270
+ /* Subscribe events */
271
+ for (int i = 0 ; i < profile.event_subscriptions ; i++) {
272
+ if ( switch_strstr (event_filter_name[i], switch_event_custom)) {
273
+ if (switch_event_bind_removable (modname, SWITCH_EVENT_CUSTOM, event_filter_name[i] + strlen (" SWITCH_EVENT_CUSTOM::" ),
274
+ event_handler, static_cast <void *>(&_publisher),&(profile.event_nodes [i])) != SWITCH_STATUS_SUCCESS) {
275
+ switch_log_printf (SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Cannot bind to event handler %d!\n " ,(int )profile.event_ids [i]);
276
+ throw std::invalid_argument (" Failed to bind event handler for " + std::string (event_filter_name[i]));
277
+ }
278
+ } else {
279
+ if (switch_event_bind_removable (modname, profile.event_ids [i], SWITCH_EVENT_SUBCLASS_ANY,
280
+ event_handler, static_cast <void *>(&_publisher), &(profile.event_nodes [i])) != SWITCH_STATUS_SUCCESS) {
281
+ switch_log_printf (SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, " Cannot bind to event handler %d!\n " ,(int )profile.event_ids [i]);
282
+ throw std::invalid_argument ( " Failed to bind event handler for " + std::string (event_filter_name[i]));
283
+ }
284
+ }
285
+ switch_log_printf (SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, " Subscribed to %s event.\n " , event_filter_name[i]);
286
+ }
287
+
288
+ } else {
289
+ // Subscribe to all switch events of any subclass
290
+ // Store a pointer to ourself in the user data
291
+ if (switch_event_bind_removable (modname, SWITCH_EVENT_ALL, SWITCH_EVENT_SUBCLASS_ANY, event_handler,
292
+ static_cast <void *>(&_publisher), &_node)
293
+ != SWITCH_STATUS_SUCCESS) {
294
+ throw std::runtime_error (" Couldn't bind to switch events." );
295
+ }
296
+ switch_log_printf (SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " Subscribed to ALL events\n " );
297
+
261
298
}
262
- switch_log_printf (SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " Subscribed to events \n " );
299
+
263
300
264
301
// Create our module interface registration
265
302
*module_interface = switch_loadable_module_create_module_interface (pool, modname);
@@ -276,7 +313,13 @@ namespace mod_event_kafka {
276
313
277
314
~KafkaModule () {
278
315
// Unsubscribe from the switch events
279
- switch_event_unbind (&_node);
316
+ if (profile.event_subscriptions > 0 ) {
317
+ for (int i = 0 ; i < profile.event_subscriptions ; i++) {
318
+ switch_event_unbind (&(profile.event_nodes [i]));
319
+ }
320
+ } else {
321
+ switch_event_unbind (&_node);
322
+ }
280
323
switch_log_printf (SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, " Module shut down\n " );
281
324
}
282
325
0 commit comments