From 867ea0c026779b226590fb9a2e63d4b80740fde1 Mon Sep 17 00:00:00 2001 From: Anthony Minessale Date: Wed, 16 Jul 2008 14:58:00 +0000 Subject: [PATCH] fix the evil snake (again) git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9051 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/mod/languages/mod_python/freeswitch.py | 9 + .../mod_python/freeswitch_python.cpp | 16 +- src/mod/languages/mod_python/mod_python.c | 153 +++++++------- .../languages/mod_python/mod_python_extra.c | 17 +- .../languages/mod_python/mod_python_extra.h | 6 +- .../languages/mod_python/mod_python_wrap.cpp | 189 ++++++++++++++++++ .../languages/mod_python/python_example.py | 61 ++++++ 7 files changed, 349 insertions(+), 102 deletions(-) create mode 100644 src/mod/languages/mod_python/python_example.py diff --git a/src/mod/languages/mod_python/freeswitch.py b/src/mod/languages/mod_python/freeswitch.py index 7a53a07ad2..74ca3ac0d8 100644 --- a/src/mod/languages/mod_python/freeswitch.py +++ b/src/mod/languages/mod_python/freeswitch.py @@ -246,6 +246,15 @@ class CoreSession(_object): __swig_setmethods__["hook_state"] = _freeswitch.CoreSession_hook_state_set __swig_getmethods__["hook_state"] = _freeswitch.CoreSession_hook_state_get if _newclass:hook_state = _swig_property(_freeswitch.CoreSession_hook_state_get, _freeswitch.CoreSession_hook_state_set) + __swig_setmethods__["uuid"] = _freeswitch.CoreSession_uuid_set + __swig_getmethods__["uuid"] = _freeswitch.CoreSession_uuid_get + if _newclass:uuid = _swig_property(_freeswitch.CoreSession_uuid_get, _freeswitch.CoreSession_uuid_set) + __swig_setmethods__["tts_name"] = _freeswitch.CoreSession_tts_name_set + __swig_getmethods__["tts_name"] = _freeswitch.CoreSession_tts_name_get + if _newclass:tts_name = _swig_property(_freeswitch.CoreSession_tts_name_get, _freeswitch.CoreSession_tts_name_set) + __swig_setmethods__["voice_name"] = _freeswitch.CoreSession_voice_name_set + __swig_getmethods__["voice_name"] = _freeswitch.CoreSession_voice_name_get + if _newclass:voice_name = _swig_property(_freeswitch.CoreSession_voice_name_get, _freeswitch.CoreSession_voice_name_set) def answer(*args): return _freeswitch.CoreSession_answer(*args) def preAnswer(*args): return _freeswitch.CoreSession_preAnswer(*args) def hangup(*args): return _freeswitch.CoreSession_hangup(*args) diff --git a/src/mod/languages/mod_python/freeswitch_python.cpp b/src/mod/languages/mod_python/freeswitch_python.cpp index ca722e225f..6b9685eeea 100644 --- a/src/mod/languages/mod_python/freeswitch_python.cpp +++ b/src/mod/languages/mod_python/freeswitch_python.cpp @@ -28,27 +28,27 @@ Session::~Session() if (session) { switch_core_event_hook_remove_state_change(session, python_hanguphook); } - Py_XDECREF(hangup_func); + Py_DECREF(hangup_func); hangup_func = NULL; } if (hangup_func_arg) { - Py_XDECREF(hangup_func_arg); + Py_DECREF(hangup_func_arg); hangup_func_arg = NULL; } if (cb_function) { - Py_XDECREF(cb_function); + Py_DECREF(cb_function); cb_function = NULL; } if (cb_arg) { - Py_XDECREF(cb_arg); + Py_DECREF(cb_arg); cb_arg = NULL; } if (Self) { - Py_XDECREF(Self); + Py_DECREF(Self); } } @@ -136,7 +136,7 @@ void Session::do_hangup_hook() } if (!Self) { - mod_python_conjure_session(NULL, session, NULL); + mod_python_conjure_session(NULL, session); } if (hangup_func_arg) { @@ -266,14 +266,14 @@ switch_status_t Session::run_dtmf_callback(void *input, switch_input_type_t ityp what = "dtmf"; } else if (itype == SWITCH_INPUT_TYPE_EVENT){ what = "event"; - io = mod_python_conjure_event(NULL, (switch_event_t *) input, NULL); + io = mod_python_conjure_event((switch_event_t *) input); } else { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "unsupported type!\n"); return SWITCH_STATUS_FALSE; } if (!Self) { - mod_python_conjure_session(NULL, session, NULL); + mod_python_conjure_session(NULL, session); } if (cb_arg) { diff --git a/src/mod/languages/mod_python/mod_python.c b/src/mod/languages/mod_python/mod_python.c index 180adb054f..de8b31cfd3 100644 --- a/src/mod/languages/mod_python/mod_python.c +++ b/src/mod/languages/mod_python/mod_python.c @@ -62,11 +62,11 @@ static struct { } globals; -static void eval_some_python(char *args, switch_core_session_t *session, switch_stream_handle_t *stream, switch_event_t *params, char **str) +static void eval_some_python(const char *funcname, char *args, switch_core_session_t *session, switch_stream_handle_t *stream, switch_event_t *params, char **str) { PyThreadState *tstate = NULL; char *dupargs = NULL; - char *argv[128] = { 0 }; + char *argv[2] = { 0 }; int argc; int lead = 0; char *script = NULL; @@ -74,7 +74,8 @@ static void eval_some_python(char *args, switch_core_session_t *session, switch_ PyObject *function = NULL; PyObject *arg = NULL; PyObject *result = NULL; - char *uuid = NULL; + switch_channel_t *channel = NULL; + char *p; if (str) { *str = NULL; @@ -93,11 +94,20 @@ static void eval_some_python(char *args, switch_core_session_t *session, switch_ goto done; } - script = argv[0]; + script = strdup(switch_str_nil(argv[0])); + lead = 1; - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Invoking py module: %s\n", script); + if ((p = strstr(script, "::"))) { + *p = '\0'; + p += 2; + if (p) { + funcname = p; + } + } + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Invoking py module: %s\n", script); + tstate = PyThreadState_New(mainThreadState->interp); if (!tstate) { switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error acquiring tstate\n"); @@ -105,12 +115,6 @@ static void eval_some_python(char *args, switch_core_session_t *session, switch_ } // swap in thread state PyEval_AcquireThread(tstate); - if (session) { - uuid = switch_core_session_get_uuid(session); - // record the fact that thread state is swapped in - switch_channel_t *channel = switch_core_session_get_channel(session); - switch_channel_set_private(channel, "SwapInThreadState", NULL); - } init_freeswitch(); // import the module @@ -130,50 +134,48 @@ static void eval_some_python(char *args, switch_core_session_t *session, switch_ goto done_swap_out; } - if (params) { - eve = mod_python_conjure_event(module, params, "params"); - } - - if (stream) { - stp = mod_python_conjure_stream(module, stream, "stream"); - if (stream->param_event) { - eve = mod_python_conjure_event(module, stream->param_event, "env"); - } - } - // get the handler function to be called - function = PyObject_GetAttrString(module, "handler"); + function = PyObject_GetAttrString(module, (char *)funcname); if (!function) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Module does not define handler(uuid)\n"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Module does not define %s\n", funcname); PyErr_Print(); PyErr_Clear(); goto done_swap_out; } - if (uuid) { - // build a tuple to pass the args, the uuid of session - arg = Py_BuildValue("(s)", uuid); - if (!arg) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error building args\n"); - PyErr_Print(); - PyErr_Clear(); - goto done_swap_out; - } - } else { - arg = PyTuple_New(1); - PyObject *nada = Py_BuildValue(""); - PyTuple_SetItem(arg, 0, nada); - } - if (session) { - sp = mod_python_conjure_session(module, session, "session"); + channel = switch_core_session_get_channel(session); + sp = mod_python_conjure_session(module, session); } + if (params) { + eve = mod_python_conjure_event(params); + } + + if (stream) { + stp = mod_python_conjure_stream(stream); + if (stream->param_event) { + eve = mod_python_conjure_event(stream->param_event); + } + } + + if (sp && eve && stp) { + arg = Py_BuildValue("(OOOs)", sp, stp, eve, switch_str_nil(argv[1])); + } else if (eve && stp) { + arg = Py_BuildValue("(sOOs)", "na", stp, eve, switch_str_nil(argv[1])); + } else if (eve) { + arg = Py_BuildValue("(Os)", eve, switch_str_nil(argv[1])); + } else if (sp) { + arg = Py_BuildValue("(Os)", sp, switch_str_nil(argv[1])); + } else { + arg = Py_BuildValue("(s)", switch_str_nil(argv[1])); + } + // invoke the handler switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Call python script \n"); result = PyEval_CallObjectWithKeywords(function, arg, (PyObject *) NULL); switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Finished calling python script \n"); - + // check the result and print out any errors if (result) { if (str) { @@ -183,44 +185,26 @@ static void eval_some_python(char *args, switch_core_session_t *session, switch_ switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error calling python script\n"); PyErr_Print(); PyErr_Clear(); + PyRun_SimpleString("python_makes_sense"); + PyGC_Collect(); + } + + done_swap_out: + + if (arg) { + Py_DECREF(arg); + } + + if (sp) { + Py_DECREF(sp); + } + + if (tstate) { + PyEval_ReleaseThread(tstate); } done: - done_swap_out: - - if (sp) { - Py_XDECREF(sp); - } - // swap out thread state - if (session) { - //switch_core_session_rwunlock(session); - // record the fact that thread state is swapped in - switch_channel_t *channel = switch_core_session_get_channel(session); - PyThreadState *swapin_tstate = (PyThreadState *) switch_channel_get_private(channel, "SwapInThreadState"); - // so lets assume nothing in the python script swapped any thread state in - // or out .. thread state will currently be swapped in, and the SwapInThreadState - // will be null - if (swapin_tstate == NULL) { - // clear out threadstate - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "clear threadstate \n"); - // we know we are swapped in because swapin_tstate is NULL, and therefore we have the GIL, so - // it is safe to call PyThreadState_Get. - PyThreadState *cur_tstate = PyThreadState_Get(); - PyThreadState_Clear(cur_tstate); - PyEval_ReleaseThread(cur_tstate); - PyThreadState_Delete(cur_tstate); - } else { - // thread state is already swapped out, so, nothing for us to do - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "according to chan priv data, already swapped out \n"); - } - } else { - // they ran python script from cmd line, behave a bit differently (untested) - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "No session: Threadstate mod_python.c swap-out! \n"); - PyEval_ReleaseThread(tstate); - } - - switch_safe_free(dupargs); @@ -239,7 +223,7 @@ static switch_xml_t python_fetch(const char *section, switch_assert(mycmd); - eval_some_python(mycmd, NULL, NULL, params, &str); + eval_some_python("xml_fetch", mycmd, NULL, NULL, params, &str); if (str) { if (switch_strlen_zero(str)) { @@ -290,7 +274,7 @@ static switch_status_t do_config(void) SWITCH_STANDARD_APP(python_function) { - eval_some_python((char *) data, session, NULL, NULL, NULL); + eval_some_python("handler", (char *) data, session, NULL, NULL, NULL); } @@ -304,7 +288,7 @@ static void *SWITCH_THREAD_FUNC py_thread_run(switch_thread_t *thread, void *obj switch_memory_pool_t *pool; struct switch_py_thread *pt = (struct switch_py_thread *) obj; - eval_some_python(pt->args, NULL, NULL, NULL, NULL); + eval_some_python("runtime", pt->args, NULL, NULL, NULL, NULL); pool = pt->pool; switch_core_destroy_memory_pool(&pool); @@ -315,7 +299,7 @@ static void *SWITCH_THREAD_FUNC py_thread_run(switch_thread_t *thread, void *obj SWITCH_STANDARD_API(api_python) { - eval_some_python((char *) cmd, session, stream, NULL, NULL); + eval_some_python("fsapi", (char *) cmd, session, stream, NULL, NULL); return SWITCH_STATUS_SUCCESS; } @@ -354,6 +338,15 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_python_load) { switch_api_interface_t *api_interface; switch_application_interface_t *app_interface; + char *pp = getenv("PYTHONPATH"); + + if (pp) { + char *path = switch_mprintf("%s:%s", pp, SWITCH_GLOBAL_dirs.script_dir); + setenv("PYTHONPATH", path, 1); + free(path); + } else { + setenv("PYTHONPATH", SWITCH_GLOBAL_dirs.script_dir, 1); + } switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, "Python Framework Loading...\n"); @@ -364,7 +357,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_python_load) // initialize python system Py_Initialize(); - + // create GIL and a threadstate PyEval_InitThreads(); diff --git a/src/mod/languages/mod_python/mod_python_extra.c b/src/mod/languages/mod_python/mod_python_extra.c index bb84438b61..886eb902e9 100644 --- a/src/mod/languages/mod_python/mod_python_extra.c +++ b/src/mod/languages/mod_python/mod_python_extra.c @@ -1,34 +1,27 @@ SWITCH_BEGIN_EXTERN_C -PyObject *mod_python_conjure_event(PyObject *module, switch_event_t *event, const char *name) +PyObject *mod_python_conjure_event(switch_event_t *event) { PyObject *obj; Event *result = new Event(event, 0); obj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Event, SWIG_POINTER_OWN ); - if (module && name) { - PyDict_SetItem(PyModule_GetDict(module), Py_BuildValue("s", name), obj); - } return obj; } -PyObject *mod_python_conjure_stream(PyObject *module, switch_stream_handle_t *stream, const char *name) +PyObject *mod_python_conjure_stream(switch_stream_handle_t *stream) { PyObject *obj; Stream *result = new Stream(stream); obj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Stream, SWIG_POINTER_OWN ); - if (module && name) { - PyDict_SetItem(PyModule_GetDict(module), Py_BuildValue("s", name), obj); - } - return obj; } -PyObject *mod_python_conjure_session(PyObject *module, switch_core_session_t *session, const char *name) +PyObject *mod_python_conjure_session(PyObject *module, switch_core_session_t *session) { PyObject *obj; @@ -38,11 +31,13 @@ PyObject *mod_python_conjure_session(PyObject *module, switch_core_session_t *se result->setPython(module); result->setSelf(obj); +#if 0 if (module && name) { PyDict_SetItem(PyModule_GetDict(module), Py_BuildValue("s", name), obj); Py_DECREF(obj); } - +#endif + return obj; } diff --git a/src/mod/languages/mod_python/mod_python_extra.h b/src/mod/languages/mod_python/mod_python_extra.h index 72f1725639..00c1b3b6f2 100644 --- a/src/mod/languages/mod_python/mod_python_extra.h +++ b/src/mod/languages/mod_python/mod_python_extra.h @@ -2,9 +2,9 @@ #define MOD_PYTHON_EXTRA SWITCH_BEGIN_EXTERN_C -PyObject *mod_python_conjure_event(PyObject *module, switch_event_t *event, const char *name); -PyObject *mod_python_conjure_stream(PyObject *module, switch_stream_handle_t *stream, const char *name); -PyObject *mod_python_conjure_session(PyObject *module, switch_core_session_t *session, const char *name); +PyObject *mod_python_conjure_event(switch_event_t *event); +PyObject *mod_python_conjure_stream(switch_stream_handle_t *stream); +PyObject *mod_python_conjure_session(PyObject *module, switch_core_session_t *session); PyObject *mod_python_conjure_DTMF(char digit, int32_t duration); SWITCH_END_EXTERN_C diff --git a/src/mod/languages/mod_python/mod_python_wrap.cpp b/src/mod/languages/mod_python/mod_python_wrap.cpp index 896fc0f891..f8555dcc30 100644 --- a/src/mod/languages/mod_python/mod_python_wrap.cpp +++ b/src/mod/languages/mod_python/mod_python_wrap.cpp @@ -5843,6 +5843,189 @@ fail: } +SWIGINTERN PyObject *_wrap_CoreSession_uuid_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + CoreSession *arg1 = (CoreSession *) 0 ; + char *arg2 = (char *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:CoreSession_uuid_set",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_CoreSession, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "CoreSession_uuid_set" "', argument " "1"" of type '" "CoreSession *""'"); + } + arg1 = reinterpret_cast< CoreSession * >(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "CoreSession_uuid_set" "', argument " "2"" of type '" "char *""'"); + } + arg2 = reinterpret_cast< char * >(buf2); + if (arg1->uuid) delete[] arg1->uuid; + if (arg2) { + size_t size = strlen(reinterpret_cast< const char * >(arg2)) + 1; + arg1->uuid = (char *)reinterpret_cast< char* >(memcpy((new char[size]), reinterpret_cast< const char * >(arg2), sizeof(char)*(size))); + } else { + arg1->uuid = 0; + } + resultobj = SWIG_Py_Void(); + if (alloc2 == SWIG_NEWOBJ) delete[] buf2; + return resultobj; +fail: + if (alloc2 == SWIG_NEWOBJ) delete[] buf2; + return NULL; +} + + +SWIGINTERN PyObject *_wrap_CoreSession_uuid_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + CoreSession *arg1 = (CoreSession *) 0 ; + char *result = 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O:CoreSession_uuid_get",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_CoreSession, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "CoreSession_uuid_get" "', argument " "1"" of type '" "CoreSession *""'"); + } + arg1 = reinterpret_cast< CoreSession * >(argp1); + result = (char *) ((arg1)->uuid); + resultobj = SWIG_FromCharPtr((const char *)result); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_CoreSession_tts_name_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + CoreSession *arg1 = (CoreSession *) 0 ; + char *arg2 = (char *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:CoreSession_tts_name_set",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_CoreSession, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "CoreSession_tts_name_set" "', argument " "1"" of type '" "CoreSession *""'"); + } + arg1 = reinterpret_cast< CoreSession * >(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "CoreSession_tts_name_set" "', argument " "2"" of type '" "char *""'"); + } + arg2 = reinterpret_cast< char * >(buf2); + if (arg1->tts_name) delete[] arg1->tts_name; + if (arg2) { + size_t size = strlen(reinterpret_cast< const char * >(arg2)) + 1; + arg1->tts_name = (char *)reinterpret_cast< char* >(memcpy((new char[size]), reinterpret_cast< const char * >(arg2), sizeof(char)*(size))); + } else { + arg1->tts_name = 0; + } + resultobj = SWIG_Py_Void(); + if (alloc2 == SWIG_NEWOBJ) delete[] buf2; + return resultobj; +fail: + if (alloc2 == SWIG_NEWOBJ) delete[] buf2; + return NULL; +} + + +SWIGINTERN PyObject *_wrap_CoreSession_tts_name_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + CoreSession *arg1 = (CoreSession *) 0 ; + char *result = 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O:CoreSession_tts_name_get",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_CoreSession, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "CoreSession_tts_name_get" "', argument " "1"" of type '" "CoreSession *""'"); + } + arg1 = reinterpret_cast< CoreSession * >(argp1); + result = (char *) ((arg1)->tts_name); + resultobj = SWIG_FromCharPtr((const char *)result); + return resultobj; +fail: + return NULL; +} + + +SWIGINTERN PyObject *_wrap_CoreSession_voice_name_set(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + CoreSession *arg1 = (CoreSession *) 0 ; + char *arg2 = (char *) 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + int res2 ; + char *buf2 = 0 ; + int alloc2 = 0 ; + PyObject * obj0 = 0 ; + PyObject * obj1 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"OO:CoreSession_voice_name_set",&obj0,&obj1)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_CoreSession, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "CoreSession_voice_name_set" "', argument " "1"" of type '" "CoreSession *""'"); + } + arg1 = reinterpret_cast< CoreSession * >(argp1); + res2 = SWIG_AsCharPtrAndSize(obj1, &buf2, NULL, &alloc2); + if (!SWIG_IsOK(res2)) { + SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "CoreSession_voice_name_set" "', argument " "2"" of type '" "char *""'"); + } + arg2 = reinterpret_cast< char * >(buf2); + if (arg1->voice_name) delete[] arg1->voice_name; + if (arg2) { + size_t size = strlen(reinterpret_cast< const char * >(arg2)) + 1; + arg1->voice_name = (char *)reinterpret_cast< char* >(memcpy((new char[size]), reinterpret_cast< const char * >(arg2), sizeof(char)*(size))); + } else { + arg1->voice_name = 0; + } + resultobj = SWIG_Py_Void(); + if (alloc2 == SWIG_NEWOBJ) delete[] buf2; + return resultobj; +fail: + if (alloc2 == SWIG_NEWOBJ) delete[] buf2; + return NULL; +} + + +SWIGINTERN PyObject *_wrap_CoreSession_voice_name_get(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { + PyObject *resultobj = 0; + CoreSession *arg1 = (CoreSession *) 0 ; + char *result = 0 ; + void *argp1 = 0 ; + int res1 = 0 ; + PyObject * obj0 = 0 ; + + if (!PyArg_ParseTuple(args,(char *)"O:CoreSession_voice_name_get",&obj0)) SWIG_fail; + res1 = SWIG_ConvertPtr(obj0, &argp1,SWIGTYPE_p_CoreSession, 0 | 0 ); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "CoreSession_voice_name_get" "', argument " "1"" of type '" "CoreSession *""'"); + } + arg1 = reinterpret_cast< CoreSession * >(argp1); + result = (char *) ((arg1)->voice_name); + resultobj = SWIG_FromCharPtr((const char *)result); + return resultobj; +fail: + return NULL; +} + + SWIGINTERN PyObject *_wrap_CoreSession_answer(PyObject *SWIGUNUSEDPARM(self), PyObject *args) { PyObject *resultobj = 0; CoreSession *arg1 = (CoreSession *) 0 ; @@ -9216,6 +9399,12 @@ static PyMethodDef SwigMethods[] = { { (char *)"CoreSession_cb_state_get", _wrap_CoreSession_cb_state_get, METH_VARARGS, NULL}, { (char *)"CoreSession_hook_state_set", _wrap_CoreSession_hook_state_set, METH_VARARGS, NULL}, { (char *)"CoreSession_hook_state_get", _wrap_CoreSession_hook_state_get, METH_VARARGS, NULL}, + { (char *)"CoreSession_uuid_set", _wrap_CoreSession_uuid_set, METH_VARARGS, NULL}, + { (char *)"CoreSession_uuid_get", _wrap_CoreSession_uuid_get, METH_VARARGS, NULL}, + { (char *)"CoreSession_tts_name_set", _wrap_CoreSession_tts_name_set, METH_VARARGS, NULL}, + { (char *)"CoreSession_tts_name_get", _wrap_CoreSession_tts_name_get, METH_VARARGS, NULL}, + { (char *)"CoreSession_voice_name_set", _wrap_CoreSession_voice_name_set, METH_VARARGS, NULL}, + { (char *)"CoreSession_voice_name_get", _wrap_CoreSession_voice_name_get, METH_VARARGS, NULL}, { (char *)"CoreSession_answer", _wrap_CoreSession_answer, METH_VARARGS, NULL}, { (char *)"CoreSession_preAnswer", _wrap_CoreSession_preAnswer, METH_VARARGS, NULL}, { (char *)"CoreSession_hangup", _wrap_CoreSession_hangup, METH_VARARGS, NULL}, diff --git a/src/mod/languages/mod_python/python_example.py b/src/mod/languages/mod_python/python_example.py new file mode 100644 index 0000000000..49f260dc2f --- /dev/null +++ b/src/mod/languages/mod_python/python_example.py @@ -0,0 +1,61 @@ +import os +from freeswitch import * + +# HANGUP HOOK +# +# session is a session object +# what is "hangup" or "transfer" +# if you pass an extra arg to setInputCallback then append 'arg' to get that value +# def hangup_hook(session, what, arg): +def hangup_hook(session, what): + + consoleLog("info","hangup hook for %s!!\n\n" % what) + return + + +# INPUT CALLBACK +# +# session is a session object +# what is "dtmf" or "event" +# obj is a dtmf object or an event object depending on the 'what' var. +# if you pass an extra arg to setInputCallback then append 'arg' to get that value +# def input_callback(session, what, obj, arg): +def input_callback(session, what, obj): + + if (what == "dtmf"): + consoleLog("info", what + " " + obj.digit + "\n") + else: + consoleLog("info", what + " " + obj.serialize() + "\n") + return "pause" + +# APPLICATION +# +# default name for apps is "handler" it can be overridden with :: +# session is a session object +# args is all the args passed after the module name +def handler(session, args): + + session.answer() + session.setHangupHook(hangup_hook) + session.setInputCallback(input_callback) + session.execute("playback", session.getVariable("hold_music")) + +# FSAPI CALL FROM CLI, DP HTTP etc +# +# default name for python FSAPI is "fsapi" it can be overridden with :: +# stream is a switch_stream, anything written with stream.write() is returned to the caller +# env is a switch_event +# args is all the args passed after the module name +# session is a session object when called from the dial plan or the string "na" when not. +def fsapi(session, stream, env, args): + + stream.write("w00t!\n" + env.serialize()) + + +# RUN A FUNCTION IN A THREAD +# +# default name for pyrun is "runtime" it can be overridden with :: +# args is all the args passed after the module name +def runtime(args): + + print args + "\n"