Changeset 640:237cf6339546 in freeDiameter for extensions/dbg_interactive/peers.i
- Timestamp:
- Dec 20, 2010, 7:36:40 PM (13 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/dbg_interactive/peers.i
r638 r640 67 67 68 68 %extend peer_info { 69 peer_info () { 70 struct peer_info *np = (struct peer_info *)calloc(1, sizeof(struct peer_info)); 71 if (!np) { 72 DI_ERROR_MALLOC; 73 return NULL; 74 } 75 76 fd_list_init(&np->pi_endpoints, NULL); 77 78 return np; 79 } 80 69 81 /* Wrapper around fd_peer_add to allow calling the python callback */ 82 %delobject add; 70 83 void add(PyObject * PyCb=NULL) { 71 84 int ret; … … 81 94 } 82 95 } 83 84 /* Add an endpoint */ 85 void add_endpoint(char * endpoint) { 86 fd_log_debug("What is the best way in python to pass an endpoint? (ip + port)"); 87 } 88 89 } 96 } 97 98 %inline %{ 99 static struct peer_hdr * peer_search(char *diamid) { 100 struct peer_hdr *r = NULL; 101 int ret = fd_peer_getbyid( diamid, &r ); 102 if (ret) { 103 DI_ERROR(ret, NULL, NULL); 104 return NULL; 105 } 106 return r; 107 } 108 %} 109 110 %{ 111 static PyObject * validate_cb_py = NULL; 112 static PyObject * validate_cb2_py = NULL; 113 114 /* C wrapper that calls validate_cb2_py */ 115 int call_the_python_validate_callback2(struct peer_info * info) { 116 PyObject *PyInfo; 117 PyObject *result = NULL; 118 int ret = 0; 119 120 if (!validate_cb2_py) { 121 fd_log_debug("Internal error: missing the callback2!\n"); 122 return ENOTSUP; 123 } 124 125 SWIG_PYTHON_THREAD_BEGIN_BLOCK; 126 /* Convert the arguments */ 127 PyInfo = SWIG_NewPointerObj((void *)info, SWIGTYPE_p_peer_info, 0 ); 128 129 /* Call the function */ 130 result = PyEval_CallFunction(validate_cb2_py, "(O)", PyInfo); 131 132 /* The result is an integer */ 133 if ((result == NULL) || !SWIG_IsOK(SWIG_AsVal_int(result, &ret))) { 134 fd_log_debug("Error: The Python callback did not return an integer.\n"); 135 ret = EINVAL; 136 goto out; 137 } 138 139 out: 140 Py_XDECREF(result); 141 SWIG_PYTHON_THREAD_END_BLOCK; 142 return ret; 143 } 144 145 /* C wrapper that calls validate_cb_py */ 146 int call_the_python_validate_callback(struct peer_info * info, int * auth, int (**cb2)(struct peer_info *)) { 147 PyObject *PyInfo; 148 PyObject *result = NULL; 149 int ret = 0; 150 151 if (!validate_cb_py) { 152 fd_log_debug("Internal error: missing the callback!\n"); 153 return ENOTSUP; 154 } 155 156 SWIG_PYTHON_THREAD_BEGIN_BLOCK; 157 /* Convert the arguments */ 158 PyInfo = SWIG_NewPointerObj((void *)info, SWIGTYPE_p_peer_info, 0 ); 159 160 /* Call the function */ 161 result = PyEval_CallFunction(validate_cb_py, "(O)", PyInfo); 162 163 /* The result is supposedly -1, 1, or a cb2 */ 164 if (result == NULL) { 165 fd_log_debug("Error: The Python callback did not return a value.\n"); 166 ret = EINVAL; 167 goto out; 168 } 169 170 if (PyCallable_Check(result)) { 171 if (cb2) { 172 if (validate_cb2_py && (validate_cb2_py != result)) { 173 fd_log_debug("Only 1 register callback2 is supported currently\n"); 174 ret = ENOTSUP; 175 goto out; 176 } 177 validate_cb2_py = result; 178 *cb2 = call_the_python_validate_callback2; 179 *auth = 1; 180 goto out_nodec; 181 } else { 182 *auth = 1; 183 goto out; /* ignore the callback since it won't be used */ 184 } 185 } else { /* In this case, the return value must be -1, 0, or 1 */ 186 if (!SWIG_IsOK(SWIG_AsVal_int(result, auth))) { 187 fd_log_debug("Error: Cannot convert the return value to integer.\n"); 188 ret = EINVAL; 189 goto out; 190 } 191 } 192 193 out: 194 Py_XDECREF(result); 195 out_nodec: 196 SWIG_PYTHON_THREAD_END_BLOCK; 197 TRACE_DEBUG(FULL, "ret=%d, *auth=%d, cb2=%p, *cb2=%p", ret, *auth, cb2, cb2 ? *cb2 : NULL); 198 return ret; 199 } 200 201 %} 202 203 %inline %{ 204 static void peer_validate_register(PyObject * PyCb) { 205 int ret ; 206 207 if (!PyCb) { 208 DI_ERROR(EINVAL, NULL, "The callback must be provided"); 209 return; 210 } 211 212 if (validate_cb_py) { 213 if (PyCb != validate_cb_py) { 214 DI_ERROR(ENOTSUP, PyExc_RuntimeError, "Only 1 register callback is supported currently"); 215 return; 216 } 217 } else { 218 validate_cb_py = PyCb; 219 Py_XINCREF(PyCb); 220 } 221 222 ret = fd_peer_validate_register ( call_the_python_validate_callback ); 223 if (ret) { 224 DI_ERROR(ret, NULL, NULL); 225 } 226 } 227 %}
Note: See TracChangeset
for help on using the changeset viewer.