Navigation



Ignore:
Timestamp:
Dec 14, 2010, 5:34:46 PM (13 years ago)
Author:
Sebastien Decugis <sdecugis@nict.go.jp>
Branch:
default
Phase:
public
Message:

Continued work on python interface

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/dbg_interactive/dbg_interactive.i

    r625 r635  
    5151%include <typemaps.i>
    5252
    53 /* Some functions are not available through the wrapper */
     53/* Some functions are not available through the wrapper, or accessed differently */
    5454%ignore fd_lib_init;
    5555%ignore fd_lib_fini;
     56%ignore fd_dict_init;
     57%ignore fd_dict_fini;
     58%ignore fd_sess_handler_create_internal;
     59%ignore fd_sess_handler_destroy;
     60%ignore fd_sess_new;
     61%ignore fd_sess_getsid;
     62%ignore fd_sess_destroy;
     63%ignore fd_sess_reclaim;
     64%ignore fd_sess_state_store_internal;
     65%ignore fd_sess_state_retrieve_internal;
     66
    5667
    5768/* Inline functions seems to give problems to SWIG -- just remove the inline definition */
     
    8091}
    8192%apply SWIGTYPE ** OUTPUT { struct dict_object ** ref };
     93%apply SWIGTYPE ** OUTPUT { struct dict_object ** obj };
    8294%apply SWIGTYPE ** OUTPUT { struct dict_object ** result };
    83 
    84 
     95%apply SWIGTYPE ** OUTPUT { struct dictionary  ** dict }; /* this is for fd_dict_getdict, not fd_dict_init (use constructor) */
     96%apply int * OUTPUT { enum dict_object_type * type };
     97%apply (char *STRING, size_t LENGTH) { (char * sid, size_t len) };
     98%apply SWIGTYPE ** OUTPUT { struct session ** session };
     99%apply int * OUTPUT { int * new };
    85100
    86101/*********************************************************
     
    211226%}
    212227
     228/* The following wrapper leaks memory each time an union avp_value is assigned an octet string.
     229 TODO: fix this leak by better understanding SWIG...
     230   -- the alternative is to uncomment the "free" statements bellow, but then it is easy to
     231   create a segmentation fault by assigning first an integer, then an octetstring.
     232 */
     233%extend avp_value {
     234        /* The following hack in the proxy file allows assigning the octet string directly like this:
     235        avp_value.os = "blabla"
     236        */
     237        %pythoncode
     238        {
     239    __swig_setmethods__["os"] = _fDpy.avp_value_os_set
     240    if _newclass:os = _swig_property(_fDpy.avp_value_os_get, _fDpy.avp_value_os_set)
     241        }
     242        void os_set(char *STRING, size_t LENGTH) {
     243                /* free($self->os.data);  -- do not free, in case the previous value was not an OS */
     244                $self->os.data = malloc(LENGTH);
     245                if (!$self->os.data) {
     246                        fd_log_debug("Out of memory!\n");
     247                        PyErr_SetString(PyExc_MemoryError,"Not enough memory");
     248                        return;
     249                }
     250                memcpy($self->os.data, STRING, LENGTH);
     251                $self->os.len = LENGTH;
     252        }
     253        void os_set(avp_value_os * os) {
     254                /* free($self->os.data);  -- do not free, in case the previous value was not an OS */
     255                $self->os.data = malloc(os->len);
     256                if (!$self->os.data) {
     257                        fd_log_debug("Out of memory!\n");
     258                        PyErr_SetString(PyExc_MemoryError,"Not enough memory");
     259                        return;
     260                }
     261                memcpy($self->os.data, os->data, os->len);
     262                $self->os.len = os->len;
     263        }
     264};
    213265
    214266%extend avp_value_os {
    215         ~avp_value_os() {
    216                 if (self)
    217                         free(self->data);
    218         }
    219267        void dump() {
    220268                if ($self) {
     
    231279}
    232280
    233 %extend avp_value {
    234         void os_set(char *STRING, size_t LENGTH) {
    235                 free($self->os.data);
    236                 $self->os.data = malloc(LENGTH);
    237                 if (!$self->os.data) {
    238                         fd_log_debug("Out of memory!\n");
    239                         PyErr_SetString(PyExc_MemoryError,"Not enough memory");
     281/****** SESSIONS *********/
     282
     283%{
     284/* At the moment, only 1 callback is supported... */
     285static PyObject * py_cleanup_cb = NULL;
     286static void call_the_python_cleanup_callback(session_state * state, char * sid) {
     287        PyObject *result;
     288        if (!py_cleanup_cb)
     289                return;
     290       
     291        /* Call the function */
     292        result = PyEval_CallFunction(py_cleanup_cb, "(Os)", state, sid);
     293       
     294        Py_XDECREF(result);
     295        return;
     296}
     297%}
     298
     299struct session_handler {
     300};
     301
     302%extend session_handler {
     303        session_handler() {
     304                fd_log_debug("Error: a cleanup callback parameter is required.\n");
     305                PyErr_SetString(PyExc_SyntaxError,"Error: a cleanup callback parameter is required.\n");
     306                return NULL;
     307        }
     308        session_handler(PyObject * PyCleanupCb) {
     309                struct session_handler * hdl = NULL;
     310                int ret;
     311                if (py_cleanup_cb) {
     312                        fd_log_debug("dbg_interactive supports only 1 session handler in python at the moment\n");
     313                        PyErr_SetString(PyExc_SyntaxError,"dbg_interactive supports only 1 session handler in python at the moment\n");
     314                        return NULL;
     315                }
     316                if (!PyCallable_Check(PyCleanupCb)) {
     317                        PyErr_SetString(PyExc_TypeError, "Need a callable object!");
     318                        return NULL;
     319                }
     320                py_cleanup_cb = PyCleanupCb;
     321                Py_XINCREF(py_cleanup_cb);
     322               
     323                ret = fd_sess_handler_create_internal ( &hdl, call_the_python_cleanup_callback );
     324                if (ret != 0) {
     325                        fd_log_debug("Error: %s\n", strerror(ret));
     326                        PyErr_SetString(PyExc_MemoryError,"Not enough memory");
     327                        return NULL;
     328                }
     329                return hdl;
     330        }
     331        ~session_handler() {
     332                if (self) {
     333                        struct session_handler * hdl = self;
     334                        int ret = fd_sess_handler_destroy(&hdl);
     335                        if (ret != 0) {
     336                                fd_log_debug("Error: %s\n", strerror(ret));
     337                        }
    240338                        return;
    241339                }
    242                 memcpy($self->os.data, STRING, LENGTH);
    243                 $self->os.len = LENGTH;
    244         }
    245         void os_set(avp_value_os * os) {
    246                 free($self->os.data);
    247                 $self->os.data = malloc(os->len);
    248                 if (!$self->os.data) {
    249                         fd_log_debug("Out of memory!\n");
    250                         PyErr_SetString(PyExc_MemoryError,"Not enough memory");
     340        }
     341        void dump() {
     342                if ($self) {
     343                        fd_sess_dump_hdl(0, $self);
     344                }
     345        }
     346}
     347
     348struct session {
     349};
     350
     351%extend session {
     352        /* The first two versions create a new session string. The third one allow to use an existing string. */
     353        session() {
     354                int ret;
     355                struct session * s = NULL;
     356                ret = fd_sess_new(&s, fd_g_config->cnf_diamid, "dbg_interactive", sizeof("dbg_interactive"));
     357                if (ret != 0) {
     358                        fd_log_debug("Error: %s\n", strerror(ret));
     359                        PyErr_SetString(PyExc_MemoryError,"Not enough memory");
     360                        return NULL;
     361                }
     362                return s;
     363        }
     364        session(char * diamid, char * STRING, size_t LENGTH) {
     365                int ret;
     366                struct session * s = NULL;
     367                ret = fd_sess_new(&s, diamid, STRING, LENGTH);
     368                if (ret != 0) {
     369                        fd_log_debug("Error: %s\n", strerror(ret));
     370                        PyErr_SetString(PyExc_MemoryError,"Not enough memory");
     371                        return NULL;
     372                }
     373                return s;
     374        }
     375        session(char * STRING, size_t LENGTH) {
     376                int ret, n;
     377                struct session * s = NULL;
     378                ret = fd_sess_fromsid(STRING, LENGTH, &s, &n);
     379                if (ret != 0) {
     380                        fd_log_debug("Error: %s\n", strerror(ret));
     381                        PyErr_SetString(PyExc_MemoryError,"Not enough memory");
     382                        return NULL;
     383                }
     384                /* When defining n as OUTPUT parameter, we get something strange... Use fd_sess_fromsid if you need it */
     385                if (n) {
     386                        fd_log_debug("A new session has been created\n");
     387                } else {
     388                        fd_log_debug("A session with same id already existed\n");
     389                }
     390                return s;
     391        }
     392        ~session() {
     393                if (self) {
     394                        struct session * s = self;
     395                        int ret = fd_sess_reclaim(&s);
     396                        if (ret != 0) {
     397                                fd_log_debug("Error: %s\n", strerror(ret));
     398                        }
    251399                        return;
    252400                }
    253                 memcpy($self->os.data, os->data, os->len);
    254                 $self->os.len = os->len;
    255         }
    256 };
     401        }
     402        char * getsid() {
     403                int ret;
     404                char * sid = NULL;
     405                if (!$self)
     406                        return NULL;
     407                ret = fd_sess_getsid( $self, &sid);
     408                if (ret != 0) {
     409                        fd_log_debug("Error: %s\n", strerror(ret));
     410                        PyErr_SetString(PyExc_MemoryError,"Problem...");
     411                        return NULL;
     412                }
     413                return sid;
     414        }
     415        void settimeout(long seconds) {
     416                struct timespec timeout;
     417                int ret;
     418                clock_gettime(CLOCK_REALTIME, &timeout);
     419                timeout.tv_sec += seconds;
     420                ret = fd_sess_settimeout( $self, &timeout );
     421                if (ret != 0) {
     422                        fd_log_debug("Error: %s\n", strerror(ret));
     423                        PyErr_SetString(PyExc_MemoryError,"Problem...");
     424                }
     425        }
     426        void dump() {
     427                if ($self) {
     428                        fd_sess_dump(0, $self);
     429                }
     430        }
     431        void store(struct session_handler * handler, PyObject * state) {
     432                int ret;
     433                void * store = state;
     434                Py_INCREF(state);
     435                ret = fd_sess_state_store_internal(handler, $self, (void *) &store);
     436                if (ret != 0) {
     437                        fd_log_debug("Error: %s\n", strerror(ret));
     438                        PyErr_SetString(PyExc_MemoryError,"Problem...");
     439                }
     440        }
     441        PyObject *  retrieve(struct session_handler * handler) {
     442                int ret;
     443                PyObject * state = NULL;
     444                ret = fd_sess_state_retrieve_internal(handler, $self, (void *) &state);
     445                if (ret != 0) {
     446                        fd_log_debug("Error: %s\n", strerror(ret));
     447                        PyErr_SetString(PyExc_MemoryError,"Problem...");
     448                }
     449                if (state == NULL) {
     450                        return Py_None;
     451                }
     452                Py_DECREF(state);
     453                return state;
     454        }
     455}       
     456
     457
     458
     459/****** MESSAGES *********/
     460
    257461
    258462%cstring_output_allocate_size(char ** swig_buffer, size_t * swig_len, free(*$1))
Note: See TracChangeset for help on using the changeset viewer.