Browse Source

topoh: execute msg-outgoing and msg-sending event routes

- similar to topos module
- new event_mode modparam to control kemi callbacks execution
Daniel-Constantin Mierla 5 năm trước cách đây
mục cha
commit
21e04744a1
1 tập tin đã thay đổi với 34 bổ sung11 xóa
  1. 34 11
      src/modules/topoh/topoh_mod.c

+ 34 - 11
src/modules/topoh/topoh_mod.c

@@ -84,14 +84,20 @@ sanity_api_t scb;
 
 int th_msg_received(sr_event_param_t *evp);
 int th_msg_sent(sr_event_param_t *evp);
-int th_execute_event_route(sip_msg_t *msg, sr_event_param_t *evp);
+int th_execute_event_route(sip_msg_t *msg, sr_event_param_t *evp,
+		int evtype, int evidx, str *evname);
 
 /** module functions */
 static int mod_init(void);
 
+#define TH_EVENTRT_OUTGOING 1
+#define TH_EVENTRT_SENDING  2
+static int _th_eventrt_mode = TH_EVENTRT_OUTGOING | TH_EVENTRT_SENDING;
 static int _th_eventrt_outgoing = -1;
 static str _th_eventrt_callback = STR_NULL;
-static str _th_eventrt_name = str_init("topoh:msg-outgoing");
+static str _th_eventrt_outgoing_name = str_init("topoh:msg-outgoing");
+static int _th_eventrt_sending = -1;
+static str _th_eventrt_sending_name = str_init("topoh:msg-sending");
 
 static param_export_t params[]={
 	{"mask_key",		PARAM_STR, &_th_key},
@@ -105,6 +111,7 @@ static param_export_t params[]={
 	{"sanity_checks",	PARAM_INT, &th_sanity_checks},
 	{"uri_prefix_checks",	PARAM_INT, &th_uri_prefix_checks},
 	{"event_callback",	PARAM_STR, &_th_eventrt_callback},
+	{"event_mode",		PARAM_INT, &_th_eventrt_mode},
 	{0,0,0}
 };
 
@@ -136,11 +143,16 @@ static int mod_init(void)
 	sip_uri_t puri;
 	char buri[MAX_URI_SIZE];
 
-	_th_eventrt_outgoing = route_lookup(&event_rt, _th_eventrt_name.s);
+	_th_eventrt_outgoing = route_lookup(&event_rt, _th_eventrt_outgoing_name.s);
 	if(_th_eventrt_outgoing<0
 			|| event_rt.rlist[_th_eventrt_outgoing]==NULL) {
 		_th_eventrt_outgoing = -1;
 	}
+	_th_eventrt_sending = route_lookup(&event_rt, _th_eventrt_sending_name.s);
+	if(_th_eventrt_sending<0
+			|| event_rt.rlist[_th_eventrt_sending]==NULL) {
+		_th_eventrt_sending = -1;
+	}
 
 	if(faked_msg_init()<0) {
 		LM_ERR("failed to init fmsg\n");
@@ -421,6 +433,11 @@ int th_msg_sent(sr_event_param_t *evp)
 
 	obuf = (str*)evp->data;
 
+	if(th_execute_event_route(NULL, evp, TH_EVENTRT_OUTGOING,
+				_th_eventrt_outgoing, &_th_eventrt_outgoing_name)==1) {
+		return 0;
+	}
+
 	memset(&msg, 0, sizeof(sip_msg_t));
 	msg.buf = obuf->s;
 	msg.len = obuf->len;
@@ -439,7 +456,8 @@ int th_msg_sent(sr_event_param_t *evp)
 		th_del_cookie(&msg);
 	}
 
-	if(th_execute_event_route(&msg, evp)==1) {
+	if(th_execute_event_route(&msg, evp, TH_EVENTRT_SENDING,
+				_th_eventrt_sending, &_th_eventrt_sending_name)==1) {
 		goto done;
 	}
 
@@ -523,7 +541,8 @@ done:
 /**
  *
  */
-int th_execute_event_route(sip_msg_t *msg, sr_event_param_t *evp)
+int th_execute_event_route(sip_msg_t *msg, sr_event_param_t *evp,
+		int evtype, int evidx, str *evname)
 {
 	struct sip_msg *fmsg;
 	struct run_act_ctx ctx;
@@ -531,7 +550,11 @@ int th_execute_event_route(sip_msg_t *msg, sr_event_param_t *evp)
 	sr_kemi_eng_t *keng = NULL;
 	struct onsend_info onsnd_info = {0};
 
-	if(_th_eventrt_outgoing<0) {
+	if(!(_th_eventrt_mode & evtype)) {
+		return 0;
+	}
+
+	if(evidx<0) {
 		if(_th_eventrt_callback.s!=NULL || _th_eventrt_callback.len>0) {
 			keng = sr_kemi_eng_get();
 			if(keng==NULL) {
@@ -542,7 +565,7 @@ int th_execute_event_route(sip_msg_t *msg, sr_event_param_t *evp)
 		}
 	}
 
-	if(_th_eventrt_outgoing<0 && keng==NULL) {
+	if(evidx<0 && keng==NULL) {
 		return 0;
 	}
 
@@ -566,12 +589,12 @@ int th_execute_event_route(sip_msg_t *msg, sr_event_param_t *evp)
 	rtb = get_route_type();
 	set_route_type(REQUEST_ROUTE);
 	init_run_actions_ctx(&ctx);
-	if(_th_eventrt_outgoing>=0) {
-		run_top_route(event_rt.rlist[_th_eventrt_outgoing], fmsg, &ctx);
+	if(evidx>=0) {
+		run_top_route(event_rt.rlist[evidx], (msg)?msg:fmsg, &ctx);
 	} else {
 		if(keng!=NULL) {
-			if(sr_kemi_ctx_route(keng, &ctx, fmsg, EVENT_ROUTE,
-						&_th_eventrt_callback, &_th_eventrt_name)<0) {
+			if(sr_kemi_ctx_route(keng, &ctx, (msg)?msg:fmsg, EVENT_ROUTE,
+						&_th_eventrt_callback, evname)<0) {
 				LM_ERR("error running event route kemi callback\n");
 				p_onsend=NULL;
 				return -1;