Bladeren bron

app_python: register the module as a kemi config interpreter

Daniel-Constantin Mierla 9 jaren geleden
bovenliggende
commit
87aaba7d07

+ 24 - 0
modules/app_python/app_python_mod.c

@@ -24,6 +24,7 @@
 
 #include "../../str.h"
 #include "../../sr_module.h"
+#include "../../kemi.h"
 
 #include "python_exec.h"
 #include "python_iface.h"
@@ -36,6 +37,8 @@
 #include "mod_Ranks.h"
 #include "mod_Logger.h"
 
+#include "apy_kemi.h"
+
 MODULE_VERSION
 
 
@@ -86,6 +89,21 @@ struct module_exports exports = {
 	child_init                      /* per-child init function */
 };
 
+/**
+ *
+ */
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
+{
+	str ename = str_init("python");
+
+	sr_kemi_eng_register(&ename, sr_kemi_config_engine_python);
+
+	return 0;
+}
+
+/**
+ *
+ */
 static int mod_init(void)
 {
 	char *dname_src, *bname_src;
@@ -259,6 +277,9 @@ static int mod_init(void)
 	return 0;
 }
 
+/**
+ *
+ */
 static int child_init(int rank)
 {
 	PyObject *pFunc, *pArgs, *pValue, *pResult;
@@ -370,6 +391,9 @@ static int child_init(int rank)
 	return rval;
 }
 
+/**
+ *
+ */
 static void mod_destroy(void)
 {
 	if (dname)

+ 84 - 0
modules/app_python/apy_kemi.c

@@ -0,0 +1,84 @@
+/**
+ * Copyright (C) 2016 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include "../../dprint.h"
+#include "../../route.h"
+
+#include "python_exec.h"
+#include "apy_kemi.h"
+
+/**
+ *
+ */
+int sr_kemi_config_engine_python(sip_msg_t *msg, int rtype, str *rname)
+{
+	int ret;
+
+	ret = -1;
+	if(rtype==REQUEST_ROUTE) {
+		ret = apy_exec(msg, "ksr_request_route", NULL, 1);
+	} else if(rtype==CORE_ONREPLY_ROUTE) {
+		ret = apy_exec(msg, "ksr_reply_route", NULL, 0);
+	} else if(rtype==BRANCH_ROUTE) {
+		if(rname!=NULL && rname->s!=NULL) {
+			ret = apy_exec(msg, rname->s, NULL, 0);
+		}
+	} else if(rtype==FAILURE_ROUTE) {
+		if(rname!=NULL && rname->s!=NULL) {
+			ret = apy_exec(msg, rname->s, NULL, 0);
+		}
+	} else if(rtype==BRANCH_FAILURE_ROUTE) {
+		if(rname!=NULL && rname->s!=NULL) {
+			ret = apy_exec(msg, rname->s, NULL, 0);
+		}
+	} else if(rtype==TM_ONREPLY_ROUTE) {
+		if(rname!=NULL && rname->s!=NULL) {
+			ret = apy_exec(msg, rname->s, NULL, 0);
+		}
+	} else if(rtype==ONSEND_ROUTE) {
+		ret = apy_exec(msg, "ksr_onsend_route", NULL, 0);
+	} else if(rtype==EVENT_ROUTE) {
+		if(rname!=NULL && rname->s!=NULL) {
+			ret = apy_exec(msg, rname->s, NULL, 0);
+		}
+	} else {
+		if(rname!=NULL) {
+			LM_ERR("route type %d with name [%.*s] not implemented\n",
+				rtype, rname->len, rname->s);
+		} else {
+			LM_ERR("route type %d with no name not implemented\n",
+				rtype);
+		}
+	}
+
+	if(rname!=NULL) {
+		LM_DBG("execution of route type %d with name [%.*s] returned %d\n",
+				rtype, rname->len, rname->s, ret);
+	} else {
+		LM_DBG("execution of route type %d with no name returned %d\n",
+			rtype, ret);
+	}
+
+	return 1;
+}

+ 29 - 0
modules/app_python/apy_kemi.h

@@ -0,0 +1,29 @@
+/**
+ * Copyright (C) 2016 Daniel-Constantin Mierla (asipto.com)
+ *
+ * This file is part of Kamailio, a free SIP server.
+ *
+ * Kamailio is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version
+ *
+ * Kamailio is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+#ifndef __APY_KEMI_H__
+#define __APY_KEMI_H__
+
+#include "../../parser/msg_parser.h"
+
+int sr_kemi_config_engine_python(sip_msg_t *msg, int rtype, str *rname);
+
+#endif

+ 4 - 4
modules/app_python/python_exec.c

@@ -47,7 +47,7 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
 
 	pFunc = PyObject_GetAttrString(handler_obj, fname);
 	if (pFunc == NULL || !PyCallable_Check(pFunc)) {
-		if(emode==0) {
+		if(emode==1) {
 			LM_ERR("%s not found or is not callable\n", fname);
 		} else {
 			LM_DBG("%s not found or is not callable\n", fname);
@@ -55,7 +55,7 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
 		Py_XDECREF(pFunc);
 		PyThreadState_Swap(NULL);
 		PyEval_ReleaseLock();
-		if(emode==0) {
+		if(emode==1) {
 			return -1;
 		} else {
 			return 1;
@@ -130,7 +130,7 @@ int apy_exec(sip_msg_t *_msg, char *fname, char *fparam, int emode)
  */
 int python_exec1(sip_msg_t *_msg, char *method_name, char *foobar)
 {
-	return apy_exec(_msg, method_name, NULL, 0);
+	return apy_exec(_msg, method_name, NULL, 1);
 }
 
 /**
@@ -138,5 +138,5 @@ int python_exec1(sip_msg_t *_msg, char *method_name, char *foobar)
  */
 int python_exec2(sip_msg_t *_msg, char *method_name, char *mystr)
 {
-	return apy_exec(_msg, method_name, mystr, 0);
+	return apy_exec(_msg, method_name, mystr, 1);
 }