Changeset 624:a5682d003ed9 in freeDiameter
- Timestamp:
- Dec 9, 2010, 5:14:24 PM (13 years ago)
- Branch:
- default
- Phase:
- public
- Files:
-
- 1 added
- 3 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
doc/dbg_interactive.py.sample
r623 r624 30 30 31 31 # Print messages to freeDiameter's debug facility 32 fd_log_debug("3 + 4 = %d\n" , 7)33 # Hum... Currently I get "3 + 4 = 0" output... need some fix...32 fd_log_debug("3 + 4 = %d\n" % (7)) 33 # See SWIG documentation about varargs functions for more information 34 34 35 35 … … 42 42 # foo_a_get(s) --> returns s->a value 43 43 # delete_foo(s) --> free(s) 44 # 45 # In addition, thanks to proxy (aka shadow) class, we can also do: 46 # s = foo() 47 # s.a = 2 48 # s.a 49 # del s 44 50 45 51 … … 63 69 peer_info_config_pic_flags_pro4_set(mypicflag, 1) # 1 for TCP 64 70 fd_peer_add(mypeer, "python", None, None) 71 delete_peer_info(mypeer) 65 72 66 73 … … 78 85 # Dictionary 79 86 gdict = fd_config_cnf_dict_get(cvar.fd_g_config) 87 80 88 id = new_int_ptr() 81 89 int_ptr_assign(id, 3) 82 res = new_dict_object_ptr() 83 err = fd_dict_search ( gdict, DICT_APPLICATION, APPLICATION_BY_ID, id, res, -1 ) 84 obj = dict_object_ptr_value(res) 90 pobj = new_dict_object_pptr() 91 fd_dict_search ( gdict, DICT_APPLICATION, APPLICATION_BY_ID, id, pobj, -1 ) 92 delete_int_ptr(id) 93 obj = dict_object_pptr_value(pobj) 94 delete_dict_object_pptr(pobj) 85 95 t = new_dict_object_type_ptr() 86 err =fd_dict_gettype(obj, t)96 fd_dict_gettype(obj, t) 87 97 dict_object_type_ptr_dump(t) 88 v = new_dict_application_data() 89 err = fd_dict_getval(obj, v) 90 dict_application_data_application_name_get(v) 98 delete_dict_object_type_ptr(t) 99 objdata = new_dict_application_data() 100 fd_dict_getval(obj, objdata) 101 dict_application_data_application_name_get(objdata) 102 delete_dict_application_data(objdata) 103 104 vd = new_dict_vendor_data() 105 dict_vendor_data_vendor_id_set(vd, 123) 106 dict_vendor_data_vendor_name_set(vd, "my test vendor") 107 pobj = new_dict_object_pptr() 108 fd_dict_new ( gdict, DICT_VENDOR, vd, None, pobj) 109 delete_dict_vendor_data(vd) 110 obj = dict_object_pptr_value(pobj) 111 delete_dict_object_pptr(pobj) 112 fd_dict_dump_object(obj) 91 113 92 114 115 # Sessions 116 pmyhdl = new_session_handler_pptr() 117 fd_sess_handler_create_internal(pmyhdl, None) 118 ### Have to work on this one, a cleanup handler is actually required. 119 ### How to define the handler in python ? 120 myhdl = session_handler_pptr_value(pmyhdl) 121 delete_session_handler_pptr(pmyhdl) 93 122 123 psess = new_session_pptr() 124 fd_sess_new (psess, fd_config_cnf_diamid_get(cvar.fd_g_config), "dbg_interactive", 0) 125 sess = session_pptr_value(psess) 126 fd_sess_dump(0, sess) 127 fd_sess_destroy(psess) 128 delete_session_pptr(psess) 129 130 131 # Routing data 132 prtd = new_rt_data_pptr() 133 fd_rtd_init(prtd) 134 fd_rtd_candidate_add(rt_data_pptr_value(prtd), "p1.testbed.aaa", "testbed.aaa") 135 fd_rtd_candidate_add(rt_data_pptr_value(prtd), "p2.testbed.aaa", "testbed.aaa") 136 fd_rtd_candidate_add(rt_data_pptr_value(prtd), "p3.testbed.aaa", "testbed.aaa") 137 fd_rtd_candidate_del(rt_data_pptr_value(prtd), "p2.testbed.aaa", 0) 138 pcands = new_fd_list_pptr() 139 fd_rtd_candidate_extract(rt_data_pptr_value(prtd), pcands, 0) 140 li = fd_list_pptr_value(pcands) 141 li = fd_list_next_get(li) 142 c = fd_list_to_rtd_candidate(li) 143 rtd_candidate_diamid_get(c) 144 li = fd_list_next_get(li) 145 c = fd_list_to_rtd_candidate(li) 146 rtd_candidate_diamid_get(c) 147 148 149 # Messages 150 gdict = fd_config_cnf_dict_get(cvar.fd_g_config) 151 pobj = new_dict_object_pptr() 152 fd_dict_search ( gdict, DICT_COMMAND, CMD_BY_NAME, char_to_void("Capabilities-Exchange-Request"), pobj, -1 ) 153 cerdict = dict_object_pptr_value(pobj) 154 fd_dict_search ( gdict, DICT_AVP, AVP_BY_NAME, char_to_void("Origin-Host"), pobj, -1 ) 155 ohdict = dict_object_pptr_value(pobj) 156 delete_dict_object_pptr(pobj) 157 158 pmsg = new_msg_pptr() 159 fd_msg_new(cerdict, MSGFL_ALLOC_ETEID, pmsg) 160 msg = msg_pptr_value(pmsg); 161 pavp = new_avp_pptr() 162 fd_msg_avp_new(ohdict, 0, pavp) 163 avp = avp_pptr_value(pavp); 164 fd_msg_avp_add(msg, MSG_BRW_FIRST_CHILD, avp) 165 fd_msg_dump_walk(0, msg) 166 167 pahdr = new_avp_hdr_pptr() 168 fd_msg_avp_hdr(avp, pahdr) 169 ahdr = avp_hdr_pptr_value(pahdr) 170 delete_avp_hdr_pptr(pahdr) 171 avp_hdr_avp_code_get(ahdr) 172 os = new_avp_value_os() 173 avp_value_os_fromstr(os, fd_config_cnf_diamid_get(cvar.fd_g_config)) 174 val = new_avp_value() 175 avp_value_os_set(val, os) 176 delete_avp_value_os(os) 177 fd_msg_avp_setvalue(avp, val) 178 delete_avp_value(val) 179 180 r,buf = fd_msg_bufferize_py(msg) 181 fd_msg_free(msg) 182 delete_avp_pptr(pavp) 183 -
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.