Example #1
0
void IvrPython::onDTMFEvent(int detectedKey) {
  dtmfKey.set(detectedKey); // wake up waiting functions...

   if (onDTMFCallback == NULL) {
    DBG("IvrPython::onDTMFEvent, but script did not set onDTMF callback!\n");
    return;
  }
  DBG("IvrPython::onDTMFEvent(): calling onDTMFCallback key is %d...\n", detectedKey);

#ifndef IVR_PERL
  PyThreadState *tstate;

  /* interp is your reference to an interpreter object. */
  tstate = PyThreadState_New(mainInterpreterThreadState->interp);
  PyEval_AcquireThread(tstate);

  /* Perform Python actions here.  */
  PyObject *arglist = Py_BuildValue("(i)", detectedKey);
  PyObject *result = PyEval_CallObject(onDTMFCallback, arglist);
  Py_DECREF(arglist);

  if (result == NULL) {
      DBG("Calling IVR" SCRIPT_TYPE "onDTMF failed.\n");
       PyErr_Print();
      //return ;
  } else {
      Py_DECREF(result);
  }

  /* Release the thread. No Python API allowed beyond this point. */
  PyEval_ReleaseThread(tstate);

  /* You can either delete the thread state, or save it
     until you need it the next time. */
  PyThreadState_Delete(tstate);
#else   //IVR_PERL
  DBG("IvrPython::onDTMFEvent(): calling onDTMFCallback func is %s...\n", onDTMFCallback);

	PERL_SET_CONTEXT(my_perl_interp);
	DBG("context is %ld\n", (long) Perl_get_context());
	dSP ;
	ENTER ;
	SAVETMPS ;
	PUSHMARK(SP) ;
	XPUSHs(sv_2mortal(newSViv(detectedKey)));
	PUTBACK ;
	call_pv(onDTMFCallback, G_DISCARD);
	FREETMPS ;
	LEAVE ;
#endif	//IVR_PERL
  DBG("IvrPython::onDTMFEvent done...\n");
}
void *my_Perl_get_context(void) {
    PerlInterpreter *my_perl=Perl_get_context();
    if (!my_perl) {
	my_perl=perl_alloc();
	PERL_SET_CONTEXT(my_perl);
	perl_construct(my_perl);
	perl_parse(my_perl, xs_init, 3, pargs, NULL);
	PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
	perl_run(my_perl);
	PL_thread_at_exit(clear_perl, NULL, 0);
	/* warn ("new perl interpreter created %x (thread=%i)",
	      my_perl,
	      PL_thread_self()); */
    }
    return my_perl;
}
Example #3
0
void IvrPython::onMediaQueueEmpty() {
  isMediaQueueEmpty.set(true);

    DBG("executiong MQE callback...\n");
    if (onMediaQueueEmptyCallback == NULL) {
	DBG("IvrPython::onMediaQueueEmpty, but script did not set onMediaQueueEmpty callback.\n");
	return;
    }

#ifndef IVR_PERL
    PyThreadState *tstate;

    /* interp is your reference to an interpreter object. */
    tstate = PyThreadState_New(mainInterpreterThreadState->interp);
    PyEval_AcquireThread(tstate);

    /* Perform Python actions here.  */
    PyObject *arglist = Py_BuildValue("()");
    PyObject *result = PyEval_CallObject(onMediaQueueEmptyCallback, arglist);
    Py_DECREF(arglist);

    if (result == NULL) {
	DBG("Calling IVR" SCRIPT_TYPE "onMediaQueueEmpty failed.\n");
	    // PyErr_Print();
	//return ;
    } else {
	Py_DECREF(result);
    }

    /* Release the thread. No Python API allowed beyond this point. */
    PyEval_ReleaseThread(tstate);

    /* You can either delete the thread state, or save it
       until you need it the next time. */
    PyThreadState_Delete(tstate);
#else   //IVR_PERL
	PERL_SET_CONTEXT(my_perl_interp);
	DBG("context is %ld\n", (long) Perl_get_context());

	dSP ;
	PUSHMARK(SP) ;
	call_pv(onMediaQueueEmptyCallback, G_DISCARD|G_NOARGS) ;
#endif	//IVR_PERL
    DBG("IvrPython::onMediaQueueEmpty done.\n");
}
Example #4
0
void IvrPython::onBye(AmRequest* req) {
  if (onByeCallback == NULL) {
    DBG("Python script did not set onBye callback!\n");
    return;
  }
  DBG("IvrPython::onBye(): calling onByeCallback ...\n");

#ifndef IVR_PERL
  PyThreadState *tstate;

  tstate = PyThreadState_New(mainInterpreterThreadState->interp);
  PyEval_AcquireThread(tstate);

  /* Perform Python actions here.  */
  PyObject *arglist = Py_BuildValue("()");
  PyObject *result = PyEval_CallObject(onByeCallback, arglist);
  Py_DECREF(arglist);

  if (result == NULL) {
      DBG("Calling IVR" SCRIPT_TYPE "onMediaQueueEmpty failed.\n");
      // PyErr_Print();
      //return ;
  } else {
      Py_DECREF(result);
  }

  /* Release the thread. No Python API allowed beyond this point. */
  PyEval_ReleaseThread(tstate);
  PyThreadState_Delete(tstate);
#else   //IVR_PERL
	PERL_SET_CONTEXT(my_perl_interp);
	DBG("context is %ld\n", (long) Perl_get_context());

	dSP ;
	PUSHMARK(SP) ;
	call_pv(onByeCallback, G_DISCARD|G_NOARGS) ;
#endif	//IVR_PERL

  DBG("IvrPython::onBye done...\n");
}
Example #5
0
void IvrPython::onNotify(AmSessionEvent* event) {
   if (onNotifyCallback == NULL) {
    DBG("IvrPython::onNotify, but script did not set onNotify callback!\n");
    return;
  }
  DBG("IvrPython::onNotify(): calling onNotifyCallback ...\n");
#ifndef IVR_PERL
  PyThreadState* pyThreadState;
  if ( (pyThreadState = Py_NewInterpreter()) != NULL){
    PyObject *arglist;
    PyObject *result;
    arglist = Py_BuildValue("(s)", event->request.getBody().c_str());;
    result = PyEval_CallObject(onNotifyCallback, arglist);
    Py_DECREF(arglist);
    if (result == NULL) {
      DBG("Calling IVR" SCRIPT_TYPE "onNotify failed.\n");
      // PyErr_Print();
      return ;
    }
    Py_DECREF(result);
  }
  Py_EndInterpreter(pyThreadState);
#else   //IVR_PERL
	PERL_SET_CONTEXT(my_perl_interp);
	DBG("context is %ld\n", (long) Perl_get_context());

	dSP ;
	ENTER ;
	SAVETMPS ;
	PUSHMARK(SP) ;
	XPUSHs(sv_2mortal(newSVpv((event->request.getBody().c_str()), 0)));
	PUTBACK ;
	call_pv(onNotifyCallback, G_DISCARD);
	FREETMPS ;
	LEAVE ;
#endif	//IVR_PERL
}