From 87aaba7d07650b46328856b6c0b02a5c5b7f87e9 Mon Sep 17 00:00:00 2001 From: Daniel-Constantin Mierla Date: Mon, 18 Apr 2016 15:05:22 +0200 Subject: [PATCH] app_python: register the module as a kemi config interpreter --- modules/app_python/app_python_mod.c | 24 +++++++++ modules/app_python/apy_kemi.c | 84 +++++++++++++++++++++++++++++ modules/app_python/apy_kemi.h | 29 ++++++++++ modules/app_python/python_exec.c | 8 +-- 4 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 modules/app_python/apy_kemi.c create mode 100644 modules/app_python/apy_kemi.h diff --git a/modules/app_python/app_python_mod.c b/modules/app_python/app_python_mod.c index 1708215aa8b..120759e1a02 100644 --- a/modules/app_python/app_python_mod.c +++ b/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) diff --git a/modules/app_python/apy_kemi.c b/modules/app_python/apy_kemi.c new file mode 100644 index 00000000000..2928f9ccd3b --- /dev/null +++ b/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 +#include +#include + +#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; +} diff --git a/modules/app_python/apy_kemi.h b/modules/app_python/apy_kemi.h new file mode 100644 index 00000000000..1fb7c5d244c --- /dev/null +++ b/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 diff --git a/modules/app_python/python_exec.c b/modules/app_python/python_exec.c index 72e6bef4521..75d661c37c9 100644 --- a/modules/app_python/python_exec.c +++ b/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); }