[Freeswitch-branches] [commit] r5576 - in freeswitch/branches/greenlizard/src: . include mod/languages/mod_python

Freeswitch SVN greenlizard at freeswitch.org
Mon Aug 6 21:33:15 EDT 2007


Author: greenlizard
Date: Mon Aug  6 21:33:15 2007
New Revision: 5576

Modified:
   freeswitch/branches/greenlizard/src/include/switch_cpp.h
   freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch.py
   freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.cpp
   freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.h
   freeswitch/branches/greenlizard/src/mod/languages/mod_python/mod_python_wrap.cpp
   freeswitch/branches/greenlizard/src/switch_cpp.cpp

Log:
1. add collectInput method.  2. modify the dtmf callback handlers to better deal with event inputs, as opposed to dtmf inputs.  currently returns a dictionary where one key contains the body, and another contains the headers as a subdictionary.

Modified: freeswitch/branches/greenlizard/src/include/switch_cpp.h
==============================================================================
--- freeswitch/branches/greenlizard/src/include/switch_cpp.h	(original)
+++ freeswitch/branches/greenlizard/src/include/switch_cpp.h	Mon Aug  6 21:33:15 2007
@@ -158,7 +158,7 @@
      * 
      * The DTMF callback function will be set and persist
      * for the life of the session, and be called when a dtmf
-     * is pressed by user during playFile(), streamfile(), and 
+     * is pressed by user during streamfile(), collectDigits(), and 
      * certain other methods are executing.
      *
 	 */
@@ -168,6 +168,19 @@
 	int speak(char *text);
 	void set_tts_parms(char *tts_name, char *voice_name);
 
+	/**
+     * For timeout milliseconds, call the dtmf function set previously
+	 * by setDTMFCallback whenever a dtmf or event is received
+	 */
+	int collectDigits(int timeout);
+
+	/** 
+     * Collect up to maxdigits digits worth of digits
+     * and store them in dtmf_buf.  In the case of mod_python, the 
+	 * dtmf_buf parameter is configured to act as a _return_ value,
+	 * (see mod_python.i).  This does NOT call any callbacks upon
+	 * receiving dtmf digits.  For that, use collectDigits.
+	 */
 	int getDigits(char *dtmf_buf, 
 				  int buflen, 
 				  int maxdigits, 

Modified: freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch.py
==============================================================================
--- freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch.py	(original)
+++ freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch.py	Mon Aug  6 21:33:15 2007
@@ -118,6 +118,7 @@
     def setDTMFCallback(*args): return _freeswitch.CoreSession_setDTMFCallback(*args)
     def speak(*args): return _freeswitch.CoreSession_speak(*args)
     def set_tts_parms(*args): return _freeswitch.CoreSession_set_tts_parms(*args)
+    def collectDigits(*args): return _freeswitch.CoreSession_collectDigits(*args)
     def getDigits(*args): return _freeswitch.CoreSession_getDigits(*args)
     def transfer(*args): return _freeswitch.CoreSession_transfer(*args)
     def playAndGetDigits(*args): return _freeswitch.CoreSession_playAndGetDigits(*args)

Modified: freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.cpp
==============================================================================
--- freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.cpp	(original)
+++ freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.cpp	Mon Aug  6 21:33:15 2007
@@ -105,10 +105,12 @@
 }
 
 switch_status_t PySession::run_dtmf_callback(void *input, 
-					     switch_input_type_t itype) {
+											 switch_input_type_t itype) {
 
    PyObject *func, *arglist;
    PyObject *pyresult;
+   PyObject* headerdict;
+
    char *resultStr;
    char *funcargs;
    switch_file_handle_t *fh = NULL;	
@@ -137,7 +139,47 @@
 
    funcargs = (char *) cb_state.funcargs;
 
-   arglist = Py_BuildValue("(sis)", input, itype, funcargs);
+   if (itype == SWITCH_INPUT_TYPE_DTMF) {
+
+	 arglist = Py_BuildValue("(sis)", input, itype, funcargs);
+   }
+   else if (itype == SWITCH_INPUT_TYPE_EVENT) {
+	 // DUNNO if this is correct in the case we have an event
+	 // will be of type switch_event_t *event;
+	 // http://www.freeswitch.org/docs/structswitch__event.html
+	 switch_event_t *event = (switch_event_t *) input;
+	 arglist = Py_BuildValue("({s:s}is)",
+							 "body", event->body, 
+							 itype, 
+							 funcargs);
+
+	 // build a dictionary with all the headers
+
+	 switch_event_header_t *hp;
+	 headerdict = PyDict_New();	
+	 for (hp = event->headers; hp; hp = hp->next) {
+	   switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Adding event header to result");	
+	   PyDict_SetItem(headerdict, 
+					  Py_BuildValue("s", hp->name),
+					  Py_BuildValue("s", hp->value));
+
+	 }
+
+	 // add it to the main event dictionary (first arg in list)
+	 // under key 'headers'
+	 PyObject *dict = PyTuple_GetItem(arglist, 0);
+	 PyDict_SetItemString(dict,
+						  "headers",
+						  headerdict);
+
+	 //arglist = Py_BuildValue("(sis)", event->body, itype, funcargs);
+   }
+   else {
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Unknown input type: %d\n", itype);	
+	return SWITCH_STATUS_FALSE;
+   }
+
+
    if (!arglist) {
         switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "error building arglist");	
 	return SWITCH_STATUS_FALSE;
@@ -150,13 +192,15 @@
    
 
    Py_XDECREF(arglist);                           // Trash arglist
+   Py_XDECREF(headerdict);                          
+
    if (pyresult && pyresult != Py_None) {                       
        resultStr = (char *) PyString_AsString(pyresult);
        switch_status_t cbresult = process_callback_result(resultStr, &cb_state, session);
        return cbresult;
    }
    else {
-       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Error calling python callback\n");
+       switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Python callback\n returned None");
        PyErr_Print();
        PyErr_Clear();
    }

Modified: freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.h
==============================================================================
--- freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.h	(original)
+++ freeswitch/branches/greenlizard/src/mod/languages/mod_python/freeswitch_python.h	Mon Aug  6 21:33:15 2007
@@ -40,8 +40,19 @@
     bool begin_allow_threads();
     bool end_allow_threads();
 
+	/**
+	 * Run DTMF callback
+	 * 
+     * A static method in CoreSession is the first thing called
+	 * upon receving a dtmf/event callback from fs engine, and then
+	 * it gets the PySession instance and calls this method with
+	 * dtmf/event object.
+	 *
+	 * @param input - a dtmf char buffer, or an event 'object' (not sure..)
+	 * @param itype - a SWITCH_INPUT_TYPE_DTMF or a SWITCH_INPUT_TYPE_EVENT
+     */
     switch_status_t run_dtmf_callback(void *input, 
-				      switch_input_type_t itype);
+									  switch_input_type_t itype);
 
 };
 

Modified: freeswitch/branches/greenlizard/src/mod/languages/mod_python/mod_python_wrap.cpp
==============================================================================
--- freeswitch/branches/greenlizard/src/mod/languages/mod_python/mod_python_wrap.cpp	(original)
+++ freeswitch/branches/greenlizard/src/mod/languages/mod_python/mod_python_wrap.cpp	Mon Aug  6 21:33:15 2007
@@ -4080,6 +4080,37 @@
 }
 
 
+SWIGINTERN PyObject *_wrap_CoreSession_collectDigits(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
+  PyObject *resultobj = 0;
+  CoreSession *arg1 = (CoreSession *) 0 ;
+  int arg2 ;
+  int result;
+  void *argp1 = 0 ;
+  int res1 = 0 ;
+  int val2 ;
+  int ecode2 = 0 ;
+  PyObject * obj0 = 0 ;
+  PyObject * obj1 = 0 ;
+  
+  if (!PyArg_ParseTuple(args,(char *)"OO:CoreSession_collectDigits",&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_collectDigits" "', argument " "1"" of type '" "CoreSession *""'"); 
+  }
+  arg1 = reinterpret_cast< CoreSession * >(argp1);
+  ecode2 = SWIG_AsVal_int(obj1, &val2);
+  if (!SWIG_IsOK(ecode2)) {
+    SWIG_exception_fail(SWIG_ArgError(ecode2), "in method '" "CoreSession_collectDigits" "', argument " "2"" of type '" "int""'");
+  } 
+  arg2 = static_cast< int >(val2);
+  result = (int)(arg1)->collectDigits(arg2);
+  resultobj = SWIG_From_int(static_cast< int >(result));
+  return resultobj;
+fail:
+  return NULL;
+}
+
+
 SWIGINTERN PyObject *_wrap_CoreSession_getDigits(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
   PyObject *resultobj = 0;
   CoreSession *arg1 = (CoreSession *) 0 ;
@@ -5228,6 +5259,7 @@
 	 { (char *)"CoreSession_setDTMFCallback", _wrap_CoreSession_setDTMFCallback, METH_VARARGS, NULL},
 	 { (char *)"CoreSession_speak", _wrap_CoreSession_speak, METH_VARARGS, NULL},
 	 { (char *)"CoreSession_set_tts_parms", _wrap_CoreSession_set_tts_parms, METH_VARARGS, NULL},
+	 { (char *)"CoreSession_collectDigits", _wrap_CoreSession_collectDigits, METH_VARARGS, NULL},
 	 { (char *)"CoreSession_getDigits", _wrap_CoreSession_getDigits, METH_VARARGS, NULL},
 	 { (char *)"CoreSession_transfer", _wrap_CoreSession_transfer, METH_VARARGS, NULL},
 	 { (char *)"CoreSession_playAndGetDigits", _wrap_CoreSession_playAndGetDigits, METH_VARARGS, NULL},

Modified: freeswitch/branches/greenlizard/src/switch_cpp.cpp
==============================================================================
--- freeswitch/branches/greenlizard/src/switch_cpp.cpp	(original)
+++ freeswitch/branches/greenlizard/src/switch_cpp.cpp	Mon Aug  6 21:33:15 2007
@@ -92,7 +92,7 @@
 	const switch_application_interface_t *application_interface;
 	sanity_check_noreturn;
 
-    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "CoreSession::execute.  data:%s\n", data);
+    switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "CoreSession::execute.  app: %s data:%s\n", app, data);
 	if ((application_interface = switch_loadable_module_get_application_interface(app))) {
 		begin_allow_threads();
 		switch_core_session_exec(session, application_interface, data);
@@ -158,6 +158,15 @@
     voice_name = strdup(voice_name_p);
 }
 
+
+
+int CoreSession::collectDigits(int timeout) {
+	sanity_check(-1);
+    begin_allow_threads();
+	switch_ivr_collect_digits_callback(session, ap, timeout);
+    end_allow_threads();
+} 
+
 int CoreSession::getDigits(char *dtmf_buf, 
 						   int buflen, 
 						   int maxdigits, 
@@ -214,7 +223,8 @@
 										 terminators, 
 										 audio_files, 
 										 bad_input_audio_files, 
-										 dtmf_buf, 128, 
+										 dtmf_buf, 
+										 128, 
 										 digits_regex);
 
 	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "playAndGetDigits dtmf_buf: %s\n", dtmf_buf);
@@ -522,7 +532,7 @@
 	CoreSession *coresession = NULL;
 	switch_status_t result;
 
-	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "dtmf_callback called\n");
+	switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "dtmf_callback called\n");
 	fflush(stdout);
 
 	channel = switch_core_session_get_channel(session_cb);



More information about the Freeswitch-branches mailing list