git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@8999 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Brian West 2008-07-11 20:58:42 +00:00
parent a3ccefa7e1
commit 21534d46a7
2 changed files with 62 additions and 40 deletions

View File

@ -54,7 +54,7 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_python_load);
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_python_shutdown); SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_python_shutdown);
SWITCH_MODULE_DEFINITION(mod_python, mod_python_load, mod_python_shutdown, NULL); SWITCH_MODULE_DEFINITION(mod_python, mod_python_load, mod_python_shutdown, NULL);
static void eval_some_python(char *uuid, char *args, switch_core_session_t *session) static void eval_some_python(char *args, switch_core_session_t *session, switch_stream_handle_t *stream)
{ {
PyThreadState *tstate = NULL; PyThreadState *tstate = NULL;
char *dupargs = NULL; char *dupargs = NULL;
@ -62,10 +62,11 @@ static void eval_some_python(char *uuid, char *args, switch_core_session_t *sess
int argc; int argc;
int lead = 0; int lead = 0;
char *script = NULL; char *script = NULL;
PyObject *module = NULL, *sp = NULL; PyObject *module = NULL, *sp = NULL, *stp = NULL, *eve = NULL;
PyObject *function = NULL; PyObject *function = NULL;
PyObject *arg = NULL; PyObject *arg = NULL;
PyObject *result = NULL; PyObject *result = NULL;
char *uuid = NULL;
if (args) { if (args) {
dupargs = strdup(args); dupargs = strdup(args);
@ -93,6 +94,7 @@ static void eval_some_python(char *uuid, char *args, switch_core_session_t *sess
// swap in thread state // swap in thread state
PyEval_AcquireThread(tstate); PyEval_AcquireThread(tstate);
if (session) { if (session) {
uuid = switch_core_session_get_uuid(session);
// record the fact that thread state is swapped in // record the fact that thread state is swapped in
switch_channel_t *channel = switch_core_session_get_channel(session); switch_channel_t *channel = switch_core_session_get_channel(session);
switch_channel_set_private(channel, "SwapInThreadState", NULL); switch_channel_set_private(channel, "SwapInThreadState", NULL);
@ -115,6 +117,17 @@ static void eval_some_python(char *uuid, char *args, switch_core_session_t *sess
PyErr_Clear(); PyErr_Clear();
goto done_swap_out; goto done_swap_out;
} }
if (stream) {
printf("doh\n\n\n");
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 // get the handler function to be called
function = PyObject_GetAttrString(module, "handler"); function = PyObject_GetAttrString(module, "handler");
if (!function) { if (!function) {
@ -162,15 +175,16 @@ static void eval_some_python(char *uuid, char *args, switch_core_session_t *sess
Py_XDECREF(sp); Py_XDECREF(sp);
} }
if (stp) {
Py_XDECREF(stp);
}
if (eve) {
Py_XDECREF(eve);
}
done_swap_out: done_swap_out:
// decrement ref counts
Py_XDECREF(module);
Py_XDECREF(function);
Py_XDECREF(arg);
Py_XDECREF(result);
// swap out thread state // swap out thread state
if (session) { if (session) {
//switch_core_session_rwunlock(session); //switch_core_session_rwunlock(session);
@ -199,6 +213,7 @@ static void eval_some_python(char *uuid, char *args, switch_core_session_t *sess
PyEval_ReleaseThread(tstate); PyEval_ReleaseThread(tstate);
} }
switch_safe_free(dupargs); switch_safe_free(dupargs);
@ -206,7 +221,7 @@ static void eval_some_python(char *uuid, char *args, switch_core_session_t *sess
SWITCH_STANDARD_APP(python_function) SWITCH_STANDARD_APP(python_function)
{ {
eval_some_python(switch_core_session_get_uuid(session), (char *) data, session); eval_some_python((char *) data, session, NULL);
} }
@ -220,7 +235,7 @@ static void *SWITCH_THREAD_FUNC py_thread_run(switch_thread_t *thread, void *obj
switch_memory_pool_t *pool; switch_memory_pool_t *pool;
struct switch_py_thread *pt = (struct switch_py_thread *) obj; struct switch_py_thread *pt = (struct switch_py_thread *) obj;
eval_some_python(NULL, strdup(pt->args), NULL); eval_some_python(pt->args, NULL, NULL);
pool = pt->pool; pool = pt->pool;
switch_core_destroy_memory_pool(&pool); switch_core_destroy_memory_pool(&pool);
@ -228,6 +243,14 @@ static void *SWITCH_THREAD_FUNC py_thread_run(switch_thread_t *thread, void *obj
return NULL; return NULL;
} }
SWITCH_STANDARD_API(api_python)
{
eval_some_python((char *) cmd, session, stream);
return SWITCH_STATUS_SUCCESS;
}
SWITCH_STANDARD_API(launch_python) SWITCH_STANDARD_API(launch_python)
{ {
switch_thread_t *thread; switch_thread_t *thread;
@ -288,7 +311,8 @@ SWITCH_MODULE_LOAD_FUNCTION(mod_python_load)
/* connect my internal structure to the blank pointer passed to me */ /* connect my internal structure to the blank pointer passed to me */
*module_interface = switch_loadable_module_create_module_interface(pool, modname); *module_interface = switch_loadable_module_create_module_interface(pool, modname);
SWITCH_ADD_API(api_interface, "python", "run a python script", launch_python, "python </path/to/script>"); SWITCH_ADD_API(api_interface, "pyrun", "run a python script", launch_python, "python </path/to/script>");
SWITCH_ADD_API(api_interface, "python", "run a python script", api_python, "python </path/to/script>");
SWITCH_ADD_APP(app_interface, "python", "Launch python ivr", "Run a python ivr on a channel", python_function, "<script> [additional_vars [...]]", SWITCH_ADD_APP(app_interface, "python", "Launch python ivr", "Run a python ivr on a channel", python_function, "<script> [additional_vars [...]]",
SAF_SUPPORT_NOMEDIA); SAF_SUPPORT_NOMEDIA);

View File

@ -8,7 +8,6 @@ PyObject *mod_python_conjure_event(PyObject *module, switch_event_t *event, cons
obj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Event, SWIG_POINTER_OWN ); obj = SWIG_NewPointerObj(SWIG_as_voidptr(result), SWIGTYPE_p_Event, SWIG_POINTER_OWN );
if (module && name) { if (module && name) {
PyDict_SetItem(PyModule_GetDict(module), Py_BuildValue("s", name), obj); PyDict_SetItem(PyModule_GetDict(module), Py_BuildValue("s", name), obj);
Py_DECREF(obj);
} }
return obj; return obj;
@ -24,7 +23,6 @@ PyObject *mod_python_conjure_stream(PyObject *module, switch_stream_handle_t *st
if (module && name) { if (module && name) {
PyDict_SetItem(PyModule_GetDict(module), Py_BuildValue("s", name), obj); PyDict_SetItem(PyModule_GetDict(module), Py_BuildValue("s", name), obj);
Py_DECREF(obj);
} }
return obj; return obj;