Mercurial > hg > freeDiameter
changeset 609:2d15fd8ef5ba
Very very early python interactive extension, using SWIG
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Wed, 01 Dec 2010 17:52:28 +0900 |
parents | ee1dd2a6fd7e |
children | a137913d9f88 |
files | extensions/CMakeLists.txt extensions/dbg_interactive/CMakeLists.txt extensions/dbg_interactive/dbg_interactive.c extensions/dbg_interactive/diwrap.i include/freeDiameter/libfreeDiameter.h |
diffstat | 5 files changed, 168 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/extensions/CMakeLists.txt Fri Nov 26 11:04:29 2010 +0900 +++ b/extensions/CMakeLists.txt Wed Dec 01 17:52:28 2010 +0900 @@ -85,6 +85,7 @@ FD_EXTENSION_SUBDIR(dbg_rt "Routing extension for debugging the routing module" OFF) FD_EXTENSION_SUBDIR(test_app "Testing application to send dummy message to another peer, like a Diameter 'ping'" OFF) FD_EXTENSION_SUBDIR(test_sip "Testing application to simulate Diameter-SIP client (RFC4740)" OFF) +FD_EXTENSION_SUBDIR(dbg_interactive "Python-interpreter based module" OFF) # The following extension have very little use except for specific tests, so we disable them except in Debug configurations.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extensions/dbg_interactive/CMakeLists.txt Wed Dec 01 17:52:28 2010 +0900 @@ -0,0 +1,22 @@ +# The rt_debug extension +PROJECT("Interactive debug facility based on SWIG" C) + + +# This module is using Python +FIND_PACKAGE(PythonLibs) +INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH}) + +##### +# Wrapper to fD in python +FIND_PACKAGE(SWIG REQUIRED) +INCLUDE(${SWIG_USE_FILE}) +SET(CMAKE_SWIG_FLAGS "") + +# We create the module even if we don't use it, so that intermediate values are defined +SWIG_ADD_MODULE(diwrap python diwrap.i) + +##### +# Extension that embeds the python interpreter +FD_ADD_EXTENSION(dbg_interactive dbg_interactive.c ${swig_generated_sources} ${swig_other_sources}) +TARGET_LINK_LIBRARIES(dbg_interactive ${PYTHON_LIBRARIES}) +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extensions/dbg_interactive/dbg_interactive.c Wed Dec 01 17:52:28 2010 +0900 @@ -0,0 +1,97 @@ +/********************************************************************************************************* +* Software License Agreement (BSD License) * +* Author: Sebastien Decugis <sdecugis@nict.go.jp> * +* * +* Copyright (c) 2010, WIDE Project and NICT * +* All rights reserved. * +* * +* Redistribution and use of this software in source and binary forms, with or without modification, are * +* permitted provided that the following conditions are met: * +* * +* * Redistributions of source code must retain the above * +* copyright notice, this list of conditions and the * +* following disclaimer. * +* * +* * Redistributions in binary form must reproduce the above * +* copyright notice, this list of conditions and the * +* following disclaimer in the documentation and/or other * +* materials provided with the distribution. * +* * +* * Neither the name of the WIDE Project or NICT nor the * +* names of its contributors may be used to endorse or * +* promote products derived from this software without * +* specific prior written permission of WIDE Project and * +* NICT. * +* * +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * +* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * +* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * +* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * +*********************************************************************************************************/ + +#include <Python.h> +#include <freeDiameter/extension.h> + +/* wrapper generated by SWIG */ +extern void init_diwrap(void); + +/* Run an interactive interpreter in a separate thread */ +static pthread_t pyinterp = (pthread_t)NULL; +static void * myinterp (void * arg) +{ + char * dum[2] = { arg, NULL }; + TRACE_ENTRY("%p", arg); + + sleep(1); + fd_log_debug("\nStarting python interpreter [experimental].\n"); + fd_log_debug("Example syntax:\n>>> print fd_config_cnf_diamid_get(cvar.fd_g_config)\n\n"); + Py_Main(arg ? 1 : 0, dum); + + /* Upon exit, issue the order of terminating to fD */ + CHECK_FCT_DO(fd_event_send(fd_g_config->cnf_main_ev, FDEV_TERMINATE, 0, NULL), ); + + return NULL; +} + +/* Register the callbacks to the daemon */ +static int di_main(char * conffile) +{ + TRACE_ENTRY("%p", conffile); + + Py_Initialize(); + init_diwrap(); + + PyRun_SimpleString("from _diwrap import *\n"); + if (TRACE_BOOL(INFO)) { + PyRun_SimpleString("print \"[dbg_interactive] fD \",FD_PROJECT_VERSION_MAJOR,\".\",FD_PROJECT_VERSION_MINOR,\".\",FD_PROJECT_VERSION_REV\n"); + } + + /* + PyRun_SimpleString("config = cvar.fd_g_config\n"); + PyRun_SimpleString("dict = fd_config_cnf_dict_get(config)\n"); + PyRun_SimpleString("fd_dict_dump(dict)\n"); + */ + CHECK_POSIX( pthread_create(&pyinterp, NULL, myinterp, conffile) ); + + return 0; +} + +/* Terminate the extension */ +void fd_ext_fini(void) +{ + TRACE_ENTRY(); + + CHECK_FCT_DO(fd_thr_term(&pyinterp), ); + + /* Cleanup the python interpreter */ + Py_Finalize(); + + return ; +} + +/* Define the entry point function */ +EXTENSION_ENTRY("dbg_interactive", di_main);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/extensions/dbg_interactive/diwrap.i Wed Dec 01 17:52:28 2010 +0900 @@ -0,0 +1,46 @@ +/********************************************************************************************************* +* Software License Agreement (BSD License) * +* Author: Sebastien Decugis <sdecugis@nict.go.jp> * +* * +* Copyright (c) 2010, WIDE Project and NICT * +* All rights reserved. * +* * +* Redistribution and use of this software in source and binary forms, with or without modification, are * +* permitted provided that the following conditions are met: * +* * +* * Redistributions of source code must retain the above * +* copyright notice, this list of conditions and the * +* following disclaimer. * +* * +* * Redistributions in binary form must reproduce the above * +* copyright notice, this list of conditions and the * +* following disclaimer in the documentation and/or other * +* materials provided with the distribution. * +* * +* * Neither the name of the WIDE Project or NICT nor the * +* names of its contributors may be used to endorse or * +* promote products derived from this software without * +* specific prior written permission of WIDE Project and * +* NICT. * +* * +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * +* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * +* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * +* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * +*********************************************************************************************************/ + +%module diwrap +%{ +/* include the header in the wrapper code */ +#include <freeDiameter/extension.h> +%} + +/* Parse the header to generate the wrappers */ +%include "freeDiameter/freeDiameter-host.h" +%include "freeDiameter/libfreeDiameter.h" +%include "freeDiameter/freeDiameter.h" +
--- a/include/freeDiameter/libfreeDiameter.h Fri Nov 26 11:04:29 2010 +0900 +++ b/include/freeDiameter/libfreeDiameter.h Wed Dec 01 17:52:28 2010 +0900 @@ -569,6 +569,7 @@ /*============================================================*/ /* THREADS */ /*============================================================*/ +#ifndef SWIG /* Terminate a thread */ static __inline__ int fd_thr_term(pthread_t * th) @@ -632,6 +633,7 @@ } } +#endif /* SWIG */ /*============================================================*/ /* SIGNALS */