Changeset 624:a5682d003ed9 in freeDiameter for extensions/dbg_interactive
- Timestamp:
- Dec 9, 2010, 5:14:24 PM (13 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- extensions/dbg_interactive
- Files:
-
- 1 added
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
extensions/dbg_interactive/CMakeLists.txt
r618 r624 11 11 FIND_PACKAGE(SWIG REQUIRED) 12 12 INCLUDE(${SWIG_USE_FILE}) 13 SET(CMAKE_SWIG_FLAGS "")13 SET(CMAKE_SWIG_FLAGS -castmode -threads) 14 14 15 15 # Add the dependencies for re-swig-ing the file 16 SET(SWIG_MODULE_ diwrap_EXTRA_DEPS16 SET(SWIG_MODULE_fDpy_EXTRA_DEPS 17 17 ${CMAKE_BINARY_DIR}/include/freeDiameter/freeDiameter-host.h 18 18 ${CMAKE_SOURCE_DIR}/include/freeDiameter/libfreeDiameter.h 19 19 ${CMAKE_SOURCE_DIR}/include/freeDiameter/freeDiameter.h) 20 SET_SOURCE_FILES_PROPERTIES(dbg_interactive.i PROPERTIES SWIG_MODULE_NAME fDpy) 20 21 21 # We create the module even if we don't use it, so that intermediate values are defined (OK, this is stupid...) 22 SWIG_ADD_MODULE(diwrap python diwrap.i) 22 # The following code is inspired from SWIG_ADD_MODULE, but we do only what we need 23 SWIG_MODULE_INITIALIZE(fDpy python) 24 SWIG_ADD_SOURCE_TO_MODULE(fDpy swig_generated_sources "dbg_interactive.i") 25 26 # In order to avoid shipping the python file that contains the shadow class definitions, 27 # we transform this file in a C-style string and compile it within our software. 28 # We use for this purpose xxd tool provided with vim package. 29 FIND_PROGRAM(XXD_EXECUTABLE xxd) 30 # To avoid the dependency, simply compile it if not provided 31 if (NOT XXD_EXECUTABLE) 32 SET_SOURCE_FILES_PROPERTIES(helper/xxd.c PROPERTIES COMPILE_DEFINITIONS UNIX) 33 ADD_EXECUTABLE(xxd helper/xxd.c) 34 SET(XXD_EXECUTABLE xxd) 35 endif (NOT XXD_EXECUTABLE) 36 # And now the magic command 37 ADD_CUSTOM_COMMAND( 38 OUTPUT "fDpy-inc.c" 39 COMMAND "${XXD_EXECUTABLE}" 40 ARGS "-i" 41 "fDpy.py" 42 "fDpy-inc.c" 43 MAIN_DEPENDENCY "${swig_extra_generated_files}" 44 COMMENT "Converting swig proxy class to C string") 45 46 # Ensure that the generated source files are removed 47 GET_DIRECTORY_PROPERTY(swig_extra_clean_files ADDITIONAL_MAKE_CLEAN_FILES) 48 SET_DIRECTORY_PROPERTIES(PROPERTIES 49 ADDITIONAL_MAKE_CLEAN_FILES "${swig_extra_clean_files};${swig_generated_sources};fDpy-inc.c") 50 23 51 24 52 ##### 25 53 # Extension that embeds the python interpreter 26 FD_ADD_EXTENSION(dbg_interactive dbg_interactive.c ${swig_generated_sources} ${swig_other_sources})54 FD_ADD_EXTENSION(dbg_interactive dbg_interactive.c ${swig_generated_sources} fDpy-inc.c) 27 55 TARGET_LINK_LIBRARIES(dbg_interactive ${PYTHON_LIBRARIES}) 28 56 -
extensions/dbg_interactive/dbg_interactive.c
r621 r624 38 38 39 39 /* wrapper generated by SWIG */ 40 extern void init_diwrap(void); 40 #if PY_VERSION_HEX >= 0x03000000 41 extern void PyInit__fDpy(void); 42 #define WRAPPER_INIT PyInit__fDpy() 43 #else /* PY_VERSION_HEX */ 44 extern void init_fDpy(void); 45 #define WRAPPER_INIT init_fDpy() 46 #endif /* PY_VERSION_HEX */ 47 48 /* The string created in the shadow proxy C string file */ 49 extern unsigned char fDpy_py[]; 50 extern unsigned int fDpy_py_len; 41 51 42 52 /* Run an interactive interpreter in a separate thread */ … … 63 73 static int di_main(char * conffile) 64 74 { 75 char * shadow_hlp = NULL; 76 int mustfree = 0; 65 77 TRACE_ENTRY("%p", conffile); 66 78 67 79 Py_Initialize(); 68 80 69 #if PY_VERSION_HEX >= 0x03000000 70 PyInit__diwrap(); 71 #else /* PY_VERSION_HEX */ 72 init_diwrap(); 73 #endif /* PY_VERSION_HEX */ 81 WRAPPER_INIT; 74 82 75 /* In future version, it might be better to use the diwrap.py file generated by SWIG here to get the proxy classes definitions */ 76 PyRun_SimpleString("from _diwrap import *\n"); 83 /* Small hack to avoid duplicating the string, we replace the last char by a \0. 84 It works if the python file is terminated with several \n */ 85 if ( (fDpy_py[fDpy_py_len - 2] == '\n') 86 && (fDpy_py[fDpy_py_len - 1] == '\n')) { 87 fDpy_py[fDpy_py_len - 1] = '\0'; 88 shadow_hlp = (char *)&fDpy_py[0]; 89 } else { 90 CHECK_MALLOC(shadow_hlp = malloc(fDpy_py_len + 1)); 91 memcpy(shadow_hlp, fDpy_py, fDpy_py_len); 92 shadow_hlp[fDpy_py_len] = '\0'; 93 mustfree=1; 94 } 95 96 PyRun_SimpleString("__file__ = \"\"\n"); 97 PyRun_SimpleString(shadow_hlp); 98 99 if (mustfree) 100 free(shadow_hlp); 101 102 77 103 if (TRACE_BOOL(INFO)) { 78 104 PyRun_SimpleString("print \"[dbg_interactive] \",FD_PROJECT_NAME,FD_PROJECT_VERSION_MAJOR,FD_PROJECT_VERSION_MINOR,FD_PROJECT_VERSION_REV\n"); -
extensions/dbg_interactive/dbg_interactive.i
r623 r624 1 1 /* This interface file is processed by SWIG to create a python wrapper interface to freeDiameter framework. */ 2 %module diwrap2 %module fDpy 3 3 %begin %{ 4 4 /********************************************************************************************************* … … 38 38 %} 39 39 40 41 %init %{42 /* TODO: How to load the proxy classes here? */43 %}44 45 40 %{ 46 /* Define types etc.*/41 /* This text is included in the generated wrapper verbatim */ 47 42 #define SWIG 48 43 #include <freeDiameter/extension.h> … … 53 48 %include <stdint.i> 54 49 %include <cpointer.i> 50 %include <cdata.i> 51 %include <cstring.i> 55 52 %include <typemaps.i> 56 53 … … 80 77 */ 81 78 82 83 79 /* Retrieve the compile-time definitions of freeDiameter */ 84 80 %include "freeDiameter/freeDiameter-host.h" … … 88 84 89 85 /* Some pointer types that are useful */ 90 %pointer_class(int, int_ptr); 91 %pointer_class(enum dict_object_type, dict_object_type_ptr); 92 %pointer_functions(struct dict_object *, dict_object_ptr); 93 %pointer_functions(struct session *, session_ptr); 86 %pointer_functions(int, int_ptr); 87 %pointer_functions(uint8_t *, uint8_t_pptr); 88 %pointer_cast(char *, void *, char_to_void); 94 89 90 %pointer_functions(struct fd_list *, fd_list_pptr); 95 91 92 %pointer_functions(enum dict_object_type, dict_object_type_ptr); 93 %pointer_functions(struct dict_object *, dict_object_pptr); 94 95 %pointer_functions(struct session *, session_pptr); 96 %pointer_functions(struct session_handler *, session_handler_pptr); 97 %pointer_functions(session_state *, session_state_pptr); 98 99 %pointer_functions(struct rt_data *, rt_data_pptr); 100 %pointer_cast(struct fd_list *, struct rtd_candidate *, fd_list_to_rtd_candidate); 101 102 %pointer_functions(struct msg *, msg_pptr); 103 %pointer_functions(struct msg_hdr *, msg_hdr_pptr); 104 %pointer_functions(struct avp *, avp_pptr); 105 %pointer_functions(struct avp_hdr *, avp_hdr_pptr); 96 106 97 107 … … 103 113 if (!li) { 104 114 fd_log_debug("Out of memory!\n"); 115 PyErr_SetString(PyExc_MemoryError,"Not enough memory"); 105 116 return NULL; 106 117 } … … 121 132 }; 122 133 123 %extend dict_object_type_ptr { 124 void dump() { 125 %#define CASE_STR(x) case x: fd_log_debug(#x "\n"); break; 126 switch (*$self) { 127 CASE_STR(DICT_VENDOR) 128 CASE_STR(DICT_APPLICATION) 129 CASE_STR(DICT_TYPE) 130 CASE_STR(DICT_ENUMVAL) 131 CASE_STR(DICT_AVP) 132 CASE_STR(DICT_COMMAND) 133 CASE_STR(DICT_RULE) 134 default: fd_log_debug("Invalid value (%d)", *$self); break; 135 } 134 %inline %{ 135 void dict_object_type_ptr_dump(enum dict_object_type * t) { 136 #define CASE_STR(x) case x: fd_log_debug(#x "\n"); break; 137 switch (*t) { 138 CASE_STR(DICT_VENDOR) 139 CASE_STR(DICT_APPLICATION) 140 CASE_STR(DICT_TYPE) 141 CASE_STR(DICT_ENUMVAL) 142 CASE_STR(DICT_AVP) 143 CASE_STR(DICT_COMMAND) 144 CASE_STR(DICT_RULE) 145 default: 146 fd_log_debug("Invalid value (%d)", *t); 147 PyErr_SetString(PyExc_IndexError,"Invalid dictionary type object"); 136 148 } 137 149 } 150 %} 138 151 139 %inline %{ 140 void session_ptr_showsid(struct session * s) { 141 char * sid; 142 int ret = fd_sess_getsid ( s, &sid ); 143 if (ret != 0) { 144 fd_log_debug("Error %d\n", ret); 145 /* throw an exception in SWIG? */ 146 return; 152 %extend avp_value_os { 153 void fromstr(char * string) { 154 if ($self->data) free($self->data); 155 $self->data = (uint8_t *)strdup(string); 156 if (!$self->data) { 157 fd_log_debug("Out of memory!\n"); 158 PyErr_SetString(PyExc_MemoryError,"Not enough memory"); 159 $self->len = 0; 160 return; 161 } 162 $self->len = strlen(string); 147 163 } 148 fd_log_debug("%s\n", sid); 164 %newobject as_str; 165 char * tostr() { 166 char * r; 167 if ($self->len == 0) 168 return NULL; 169 r = malloc($self->len + 1); 170 if (r) { 171 strncpy(r, (char *)$self->data, $self->len + 1); 172 } else { 173 fd_log_debug("Out of memory!\n"); 174 PyErr_SetString(PyExc_MemoryError,"Not enough memory"); 175 } 176 return r; 177 } 178 }; 179 180 %extend avp_value { 181 void os_set(void * os) { 182 memcpy(&$self->os, os, sizeof($self->os)); 183 } 184 }; 185 186 %cstring_output_allocate_size(char ** swig_buffer, size_t * swig_len, free(*$1)) 187 %inline %{ 188 int fd_msg_bufferize_py ( struct msg * msg, char ** swig_buffer, size_t * swig_len ) { 189 return fd_msg_bufferize(msg, (void *)swig_buffer, swig_len); 149 190 } 150 %} 191 %}; 192
Note: See TracChangeset
for help on using the changeset viewer.