Parcourir la source

app_python: exported special kemi function to execute a module function

- KSR.x.modf(self, msg, fname, params...)
Daniel-Constantin Mierla il y a 9 ans
Parent
commit
9aac91c935

+ 18 - 3
modules/app_python/apy_kemi.c

@@ -31,6 +31,7 @@
 #include "../../pvar.h"
 #include "../../mem/pkg.h"
 
+#include "msgobj_struct.h"
 #include "python_exec.h"
 #include "apy_kemi_export.h"
 #include "apy_kemi.h"
@@ -547,7 +548,7 @@ PyMethodDef *_sr_KSRMethods = NULL;
 /**
  *
  */
-static int sr_apy_kemi_f_dbg(sip_msg_t *msg, str *txt)
+static int sr_apy_kemi_f_ktest(sip_msg_t *msg, str *txt)
 {
 	if(txt!=NULL && txt->s!=NULL)
 		LM_DBG("%.*s", txt->len, txt->s);
@@ -559,7 +560,7 @@ static int sr_apy_kemi_f_dbg(sip_msg_t *msg, str *txt)
  */
 static sr_kemi_t _sr_apy_kemi_test[] = {
 	{ str_init(""), str_init("ktest"),
-		SR_KEMIP_NONE, sr_apy_kemi_f_dbg,
+		SR_KEMIP_NONE, sr_apy_kemi_f_ktest,
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
 	},
@@ -832,7 +833,6 @@ static PyObject *sr_apy_kemi_f_pv_is_null(PyObject *self, PyObject *args)
 		return sr_apy_kemi_return_false();
 	}
 
-
 	pvn.len = strlen(pvn.s);
 	LM_DBG("pv is null test: %s\n", pvn.s);
 	pl = pv_locate_name(&pvn);
@@ -872,6 +872,14 @@ static PyMethodDef _sr_apy_kemi_pv_Methods[] = {
 	{NULL, 		NULL, 			0, 		NULL}
 };
 
+/**
+ *
+ */
+static PyMethodDef _sr_apy_kemi_x_Methods[] = {
+	{"modf", (PyCFunction)msg_call_function, METH_VARARGS,
+		"Invoke function exported by the other module."},
+	{NULL, NULL, 0, NULL} /* sentinel */
+};
 
 /**
  *
@@ -950,6 +958,13 @@ int sr_apy_init_ksr(void)
 			"pv", _sr_apy_ksr_modules_list[m]);
 	Py_INCREF(_sr_apy_ksr_modules_list[m]);
 	m++;
+	/* special sub-modules - x.modf() can have variable number of params */
+	_sr_apy_ksr_modules_list[m] = Py_InitModule("KSR.x",
+			_sr_apy_kemi_x_Methods);
+	PyDict_SetItemString(_sr_apy_ksr_module_dict,
+			"x", _sr_apy_ksr_modules_list[m]);
+	Py_INCREF(_sr_apy_ksr_modules_list[m]);
+	m++;
 
 	if(emods_size>1) {
 		for(k=1; k<emods_size; k++) {

+ 36 - 0
modules/app_python/msgobj_struct.h

@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.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 _MSGOBJ_STRUCT_H
+#define  _MSGOBJ_STRUCT_H
+
+#include "../../parser/msg_parser.h"
+
+#include <Python.h>
+
+typedef struct {
+	PyObject_HEAD
+	struct sip_msg *msg;
+} msgobject;
+
+PyObject *msg_call_function(msgobject *, PyObject *);
+
+#endif

+ 12 - 11
modules/app_python/python_msgobj.c

@@ -27,13 +27,9 @@
 #include "../../dset.h"
 #include "../../parser/msg_parser.h"
 
+#include "msgobj_struct.h"
 #include "structmember.h"
 
-typedef struct {
-	PyObject_HEAD
-	struct sip_msg *msg;
-} msgobject;
-
 static PyTypeObject MSGtype;
 
 #define is_msgobject(v)         ((v)->ob_type == &MSGtype)
@@ -165,7 +161,7 @@ static PyObject *msg_getHeader(msgobject *self, PyObject *args)
 	return PyString_FromStringAndSize(hbody->s, hbody->len);
 }
 
-static PyObject *msg_call_function(msgobject *self, PyObject *args)
+PyObject *msg_call_function(msgobject *self, PyObject *args)
 {
 	int i, rval;
 	char *fname, *arg1, *arg2;
@@ -263,11 +259,16 @@ PyDoc_STRVAR(copy_doc,
 		Return a copy (``clone'') of the msg object.");
 
 static PyMethodDef msg_methods[] = {
-	{"copy",          (PyCFunction)msg_copy,          METH_NOARGS,		copy_doc},
-	{"rewrite_ruri",  (PyCFunction)msg_rewrite_ruri,  METH_VARARGS,		"Rewrite Request-URI."},
-	{"set_dst_uri",   (PyCFunction)msg_set_dst_uri,   METH_VARARGS,		"Set destination URI."},
-	{"getHeader",     (PyCFunction)msg_getHeader,     METH_VARARGS,		"Get SIP header field by name."},
-	{"call_function", (PyCFunction)msg_call_function, METH_VARARGS,		"Invoke function exported by the other module."},
+	{"copy",          (PyCFunction)msg_copy,          METH_NOARGS,
+		copy_doc},
+	{"rewrite_ruri",  (PyCFunction)msg_rewrite_ruri,  METH_VARARGS,
+		"Rewrite Request-URI."},
+	{"set_dst_uri",   (PyCFunction)msg_set_dst_uri,   METH_VARARGS,
+		"Set destination URI."},
+	{"getHeader",     (PyCFunction)msg_getHeader,     METH_VARARGS,
+		"Get SIP header field by name."},
+	{"call_function", (PyCFunction)msg_call_function, METH_VARARGS,
+		"Invoke function exported by the other module."},
 	{NULL, NULL, 0, NULL} /* sentinel */
 };
 

+ 1 - 1
modules/app_python/python_msgobj.h

@@ -1,4 +1,4 @@
-/* 
+/*
  * Copyright (C) 2009 Sippy Software, Inc., http://www.sippysoft.com
  *
  * This file is part of Kamailio, a free SIP server.