Navigation



Ignore:
Timestamp:
Dec 17, 2010, 6:41:19 PM (13 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Improved usability of dbg_interactive

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/dbg_interactive/messages.i

    r637 r638  
    3838/****** MESSAGES *********/
    3939
     40%{
     41struct anscb_py_layer {
     42        PyObject * cb;
     43        PyObject * data;
     44};
     45
     46/* If a python callback was provided, it is received in cbdata */
     47static void anscb_python(void *cbdata, struct msg ** msg) {
     48        /* The python callback is received in cbdata */
     49        PyObject * result, *PyMsg;
     50        struct anscb_py_layer * l = cbdata;
     51       
     52        if (!l) {
     53                fd_log_debug("Internal error! Python callback disappeared...\n");
     54                return;
     55        }
     56       
     57        SWIG_PYTHON_THREAD_BEGIN_BLOCK;
     58       
     59        if (!msg || !*msg) {
     60                PyMsg = Py_None;
     61        } else {
     62                PyMsg = SWIG_NewPointerObj((void *)*msg,     SWIGTYPE_p_msg,     0 );
     63        }
     64       
     65        result = PyEval_CallFunction(l->cb, "(OO)", PyMsg, l->data);
     66        Py_XDECREF(l->cb);
     67        Py_XDECREF(l->data);
     68        free(l);
     69       
     70        /* The callback is supposed to return a message or NULL */
     71        if (!SWIG_IsOK(SWIG_ConvertPtr(result, (void *)msg, SWIGTYPE_p_msg, SWIG_POINTER_DISOWN))) {
     72                fd_log_debug("Error: Cannot convert the return value to message.\n");
     73                *msg = NULL;
     74        }
     75       
     76        Py_XDECREF(result);
     77       
     78        SWIG_PYTHON_THREAD_END_BLOCK;
     79       
     80}
     81%}
     82
    4083struct msg {
    4184};
     
    75118                }
    76119        }
     120       
     121        /* SEND THE MESSAGE */
     122        %delobject send; /* when this has been called, the msg must not be freed anymore */
     123        void send(PyObject * PyCb = NULL, PyObject * data = NULL) {
     124                int ret;
     125                struct msg * m = $self;
     126                struct anscb_py_layer * l = NULL;
     127               
     128                if (PyCb) {
     129                        l = malloc(sizeof(struct anscb_py_layer));
     130                        if (!l) {
     131                                DI_ERROR_MALLOC;
     132                                return;
     133                        }
     134
     135                        Py_XINCREF(PyCb);
     136                        Py_XINCREF(data);
     137                        l->cb = PyCb;
     138                        l->data = data;
     139                }
     140               
     141                ret = fd_msg_send(&m, PyCb ? anscb_python : NULL, l);
     142                if (ret != 0) {
     143                        DI_ERROR(ret, NULL, NULL);
     144                }
     145        }
     146       
     147        /* Create an answer */
    77148        %delobject create_answer; /* when this has been called, the original msg should not be freed anymore */
    78149        struct msg * create_answer(struct dictionary * dict = NULL, int flags = 0) {
     
    313384                }
    314385        }
     386       
     387        /* Set the result code */
     388        void rescode_set(char * rescode = "DIAMETER_SUCCESS", char * errormsg = NULL, struct avp * optavp = NULL, int type_id = 0) {
     389                int ret = fd_msg_rescode_set( $self, rescode, errormsg, optavp, type_id );
     390                if (ret) {
     391                        DI_ERROR(ret, NULL, NULL);
     392                }
     393        }
     394       
     395        /* Add the origin */
     396        void add_origin(int osi = 0) {
     397                int ret = fd_msg_add_origin( $self, osi );
     398                if (ret) {
     399                        DI_ERROR(ret, NULL, NULL);
     400                }
     401        }
     402       
    315403}
    316404
Note: See TracChangeset for help on using the changeset viewer.