comparison extensions/dbg_interactive/dispatch.i @ 639:95a784729cac

Added new opaque pointer to fd_sess_handler_create and fd_disp_register for usability. Bumped API version number.
author Sebastien Decugis <sdecugis@nict.go.jp>
date Mon, 20 Dec 2010 13:07:06 +0900
parents 9448cba86673
children 4a9f08d6b6ba
comparison
equal deleted inserted replaced
638:9448cba86673 639:95a784729cac
37 37
38 /****** DISPATCH *********/ 38 /****** DISPATCH *********/
39 39
40 40
41 %{ 41 %{
42 /* store the python callback function here */
43 static PyObject * py_dispatch_cb = NULL;
44 static int py_dispatch_cb_n = 0;
45 /* call it (will be called from a different thread than the interpreter, when message arrives) */ 42 /* call it (will be called from a different thread than the interpreter, when message arrives) */
46 static int call_the_python_dispatch_callback(struct msg **msg, struct avp *avp, struct session *session, enum disp_action *action) { 43 static int call_the_python_dispatch_callback(struct msg **msg, struct avp *avp, struct session *session, void * pycb, enum disp_action *action) {
47 PyObject *PyMsg, *PyAvp, *PySess; 44 PyObject *PyMsg, *PyAvp, *PySess;
48 PyObject *result = NULL; 45 PyObject *cb, *result = NULL;
49 int ret = 0; 46 int ret = 0;
50 47
51 if (!py_dispatch_cb) 48 if (!pycb) {
49 fd_log_debug("Internal error: missing the callback!\n");
52 return ENOTSUP; 50 return ENOTSUP;
51 }
52 cb = pycb;
53 53
54 SWIG_PYTHON_THREAD_BEGIN_BLOCK; 54 SWIG_PYTHON_THREAD_BEGIN_BLOCK;
55 /* Convert the arguments */ 55 /* Convert the arguments */
56 PyMsg = SWIG_NewPointerObj((void *)*msg, SWIGTYPE_p_msg, 0 ); 56 PyMsg = SWIG_NewPointerObj((void *)*msg, SWIGTYPE_p_msg, 0 );
57 PyAvp = SWIG_NewPointerObj((void *) avp, SWIGTYPE_p_avp, 0 ); 57 PyAvp = SWIG_NewPointerObj((void *) avp, SWIGTYPE_p_avp, 0 );
58 PySess = SWIG_NewPointerObj((void *) session, SWIGTYPE_p_session, 0 ); 58 PySess = SWIG_NewPointerObj((void *) session, SWIGTYPE_p_session, 0 );
59 59
60 /* Call the function */ 60 /* Call the function */
61 result = PyEval_CallFunction(py_dispatch_cb, "(OOO)", PyMsg, PyAvp, PySess); 61 result = PyEval_CallFunction(cb, "(OOO)", PyMsg, PyAvp, PySess);
62 62
63 /* The result is supposedly composed of: [ ret, *msg, *action ] */ 63 /* The result is supposedly composed of: [ ret, *msg, *action ] */
64 if ((result == NULL) || (!PyList_Check(result)) || (PyList_Size(result) != 3)) { 64 if ((result == NULL) || (!PyList_Check(result)) || (PyList_Size(result) != 3)) {
65 fd_log_debug("Error: The Python callback did not return [ ret, msg, action ].\n"); 65 fd_log_debug("Error: The Python callback did not return [ ret, msg, action ].\n");
66 ret = EINVAL; 66 ret = EINVAL;
105 %nodefaultctor disp_hdl; 105 %nodefaultctor disp_hdl;
106 %extend disp_hdl { 106 %extend disp_hdl {
107 disp_hdl(PyObject * PyCb, enum disp_how how, struct disp_when * when) { 107 disp_hdl(PyObject * PyCb, enum disp_how how, struct disp_when * when) {
108 struct disp_hdl * hdl = NULL; 108 struct disp_hdl * hdl = NULL;
109 int ret; 109 int ret;
110 if (py_dispatch_cb && (py_dispatch_cb != PyCb)) {
111 DI_ERROR(EINVAL, PyExc_SyntaxError, "Only one dispatch callback is supported at the moment in this extension\n.");
112 return NULL;
113 }
114 py_dispatch_cb = PyCb;
115 py_dispatch_cb_n += 1;
116 Py_XINCREF(py_dispatch_cb);
117 110
118 ret = fd_disp_register ( call_the_python_dispatch_callback, how, when, &hdl ); 111 Py_XINCREF(PyCb);
112
113 ret = fd_disp_register ( call_the_python_dispatch_callback, how, when, PyCb, &hdl );
119 if (ret != 0) { 114 if (ret != 0) {
120 DI_ERROR(ret, NULL, NULL); 115 DI_ERROR(ret, NULL, NULL);
121 return NULL; 116 return NULL;
122 } 117 }
123 return hdl; 118 return hdl;
124 } 119 }
125 ~disp_hdl() { 120 ~disp_hdl() {
126 struct disp_hdl * hdl = self; 121 struct disp_hdl * hdl = self;
127 int ret = fd_disp_unregister(&hdl); 122 PyObject * cb = NULL;
123 int ret = fd_disp_unregister(&hdl, (void *)&cb);
128 if (ret != 0) { 124 if (ret != 0) {
129 DI_ERROR(ret, NULL, NULL); 125 DI_ERROR(ret, NULL, NULL);
130 } 126 }
131 /* Now free the callback */ 127 Py_XDECREF(cb);
132 Py_XDECREF(py_dispatch_cb);
133 py_dispatch_cb_n -= 1;
134 if (!py_dispatch_cb_n)
135 py_dispatch_cb = NULL;
136 return; 128 return;
137 } 129 }
138 } 130 }
139 131
140 132
"Welcome to our mercurial repository"