<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][14786] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=14786">14786</a></dd>
<dt>Author</dt> <dd>gled</dd>
<dt>Date</dt> <dd>2009-09-08 09:25:45 -0500 (Tue, 08 Sep 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>refactor on the way</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#freeswitchtrunkcontribgledesl2agiesl2agic">freeswitch/trunk/contrib/gled/esl2agi/esl2agi.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunkcontribgledesl2agiesl2agic"></a>
<div class="modfile"><h4>Modified: freeswitch/trunk/contrib/gled/esl2agi/esl2agi.c (14785 => 14786)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/contrib/gled/esl2agi/esl2agi.c        2009-09-08 13:35:59 UTC (rev 14785)
+++ freeswitch/trunk/contrib/gled/esl2agi/esl2agi.c        2009-09-08 14:25:45 UTC (rev 14786)
</span><span class="lines">@@ -106,7 +106,7 @@
</span><span class="cx">                 exit(0);
</span><span class="cx">         }
</span><span class="cx">         else if (pid &gt; 0 ) { 
</span><del>-                esl_event_t *event;
</del><ins>+//                esl_event_t *event;
</ins><span class="cx">                 esl_status_t status;
</span><span class="cx">                 char sbuf[2048];
</span><span class="cx"> 
</span><span class="lines">@@ -120,31 +120,33 @@
</span><span class="cx"> 
</span><span class="cx">                 /* TODO: check status */
</span><span class="cx">                 status = esl_send(&amp;eslC,&quot;myevents&quot;);
</span><del>-
</del><ins>+                eslC.async_execute = 0;
</ins><span class="cx">                 while ( eslC.connected &amp;&amp; thread_running) {
</span><span class="cx">                         int r , nfds = 0;
</span><span class="cx">                         fd_set rd;
</span><span class="cx"> 
</span><ins>+                        /* As we are in sync mode, we should NOT care about socket there, there is a binding of esl_execute to find results
+                         * in events
+                        */
</ins><span class="cx">                         FD_ZERO(&amp;rd);
</span><del>-                        FD_SET(esl_req.client_sock, &amp;rd);
-                        nfds = MAX(nfds,esl_req.client_sock);
</del><ins>+//                        FD_SET(esl_req.client_sock, &amp;rd);
+//                        nfds = MAX(nfds,esl_req.client_sock);
</ins><span class="cx">                         FD_SET(pipes.script[0], &amp;rd);
</span><span class="cx">                         nfds = MAX(nfds,pipes.script[0]);
</span><span class="cx"> 
</span><span class="cx">                         r = select(nfds + 1, &amp;rd, NULL, NULL, NULL);
</span><span class="cx">                         if (r == -1 )
</span><span class="cx">                                 break;
</span><del>-                        if (FD_ISSET(esl_req.client_sock, &amp;rd)) { /* ESL SIDE have something */
</del><ins>+/*
+                        if (FD_ISSET(esl_req.client_sock, &amp;rd)) { // Esl side
</ins><span class="cx">                                 status = esl_recv_event(&amp;eslC, 0, &amp;event);
</span><span class="cx">                                 if (status == ESL_FAIL)
</span><span class="cx">                                         goto end;
</span><span class="cx">                                 else if (status == ESL_SUCCESS) {
</span><del>-                                        /* Translation ESL TO AGI
-                                         * - DTMF events.
-                                         * - what else ?
-                                         */
</del><ins>+                                        // Translation ESL TO AGI ? unneeded
</ins><span class="cx">                                 }
</span><span class="cx">                         }
</span><ins>+*/
</ins><span class="cx">                         if (FD_ISSET(pipes.script[0],&amp;rd) ) { /* AGI has something to say */
</span><span class="cx">                                 bzero(&amp;sbuf,2047);
</span><span class="cx">                                 r = read(pipes.script[0],&amp;sbuf,2047);
</span><span class="lines">@@ -394,22 +396,39 @@
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> /*
</span><ins>+ * Execute an application and wait for CHANNEL_EXECUTE_COMPLETE to return, binds esl_execute in a nice way :)
+ */
+static int do_execute(esl_handle_t *eslC,char *cmd,char *args,char *uuid,esl_event_t **save_reply) {
+        int done=0;
+
+        if ( esl_execute(eslC,cmd,args,uuid) != ESL_FAIL) {
+                while (!done &amp;&amp; eslC-&gt;connected) {
+                        if (esl_recv_event(eslC,1,NULL) == ESL_FAIL)
+                                return -1;
+                        else if (eslC-&gt;last_ievent) {
+                                if (eslC-&gt;last_ievent-&gt;event_id == ESL_EVENT_CHANNEL_EXECUTE_COMPLETE )
+                                        done = 1;
+                        }
+                }
+                if (save_reply)
+                        *save_reply = eslC-&gt;last_ievent;
+                return done;
+        }
+        else
+                return -1;
+}
+
+/*
</ins><span class="cx">  * Hangup AGI cmd
</span><span class="cx">  * TODO:        - Add arg support to know which channel we want to hangup.
</span><span class="cx">  */
</span><span class="cx"> static int handle_hangup(esl_handle_t *eslC,int fd,int *argc, char *argv[]) {
</span><del>-        esl_status_t status;
-
-        status = esl_execute(eslC,&quot;hangup&quot;,NULL,NULL);
-        if (status == ESL_FAIL) {
-#ifdef _DEBUG_STDERR
-                perror(&quot;ESL FAILED TO HANGUP&quot;);
-#endif
-                if ( write(fd,&quot;200 result=failure\n\n&quot;,128) &lt; 0 )
</del><ins>+        if (do_execute(eslC,&quot;hangup&quot;,NULL,NULL,NULL) &lt; 0 ) {
+                if ( write(fd,&quot;200 result=-1\n\n&quot;,128) &lt; 0 )
</ins><span class="cx">                         return -1;
</span><span class="cx">         }
</span><del>-        else if (status == ESL_SUCCESS) {
-                if ( write(fd,&quot;200 result=success\n\n&quot;,128) &lt; 0 )
</del><ins>+        else {
+                if ( write(fd,&quot;200 result=1\n\n&quot;,128) &lt; 0 )
</ins><span class="cx">                         return -1;
</span><span class="cx">         }
</span><span class="cx">         fprintf(stderr,&quot;Call hungup\n&quot;);
</span><span class="lines">@@ -420,17 +439,11 @@
</span><span class="cx">  * Answer AGI cmd
</span><span class="cx">  */
</span><span class="cx"> static int handle_answer(esl_handle_t *eslC,int fd,int *argc, char *argv[]) {
</span><del>-        esl_status_t status;
-
-        status = esl_execute(eslC,&quot;answer&quot;,NULL,NULL);
-        if (status == ESL_FAIL) {
-#ifdef _DEBUG_STDERR
-                perror(&quot;ESL FAILED TO HANGUP&quot;);
-#endif
</del><ins>+        if (do_execute(eslC,&quot;answer&quot;,NULL,NULL,NULL) &lt; 0 ) {
</ins><span class="cx">                 if ( write(fd,&quot;200 result=failure\n\n&quot;,128) &lt; 0 )
</span><span class="cx">                         return -1;
</span><span class="cx">         }
</span><del>-        else if (status == ESL_SUCCESS) {
</del><ins>+        else {
</ins><span class="cx">                 if ( write(fd,&quot;200 result=success\n\n&quot;,128) &lt; 0 )
</span><span class="cx">                         return -1;
</span><span class="cx">         }
</span><span class="lines">@@ -441,6 +454,10 @@
</span><span class="cx"> static int handle_streamfile(esl_handle_t *eslC,int fd,int *argc, char *argv[]) {
</span><span class="cx">         esl_status_t status;
</span><span class="cx">         char buf[1024];
</span><ins>+        char *sbuf;
+        int offset=0;
+        int dtmf=0;
+        esl_event_t *reply=NULL;
</ins><span class="cx"> 
</span><span class="cx">         if (*argc &lt; 3 || *argc &gt; 5)
</span><span class="cx">                 return -1;
</span><span class="lines">@@ -448,41 +465,58 @@
</span><span class="cx">         if (argv[3]) {
</span><span class="cx">                 /* We should set playback_terminators var there */
</span><span class="cx">                 sprintf(buf,&quot;playback_terminators=%s&quot;,argv[3]);
</span><del>-                fprintf(stderr,&quot;dtmf terminator i don't care at all '%s'&quot;,buf);
-/*
</del><span class="cx">                 status = esl_execute(eslC,&quot;set&quot;,buf,NULL);
</span><span class="cx">                 if (status == ESL_FAIL)
</span><span class="cx">                         fprintf(stderr,&quot;unable to set playback terminators...\n&quot;);
</span><del>-*/
</del><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (argv[4]) {
</span><del>-                fprintf(stderr,&quot;We don't care about offset at the moment... sorry :)\n&quot;);
</del><ins>+                offset = atoi(argv[4]);
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        fprintf(stderr,&quot;Executing playback of '%s'\n&quot;,argv[2]);
-
-        status = esl_execute(eslC,&quot;playback&quot;,argv[2],NULL);
-
-        /* We should check  playback_samples var to return offset...*/
-
-        if (status == ESL_FAIL) {
-#ifdef _DEBUG_STDERR
-                perror(&quot;ESL FAILED TO HANGUP&quot;);
-#endif
-                if ( write(fd,&quot;200 result=-1\n\n&quot;,128) &lt; 0 )
</del><ins>+        if (offset &gt; 0)
+                sprintf(buf,&quot;%s@@%d&quot;,argv[2],offset);
+        else
+                sprintf(buf,&quot;%s&quot;,argv[2]);
+        fprintf(stderr,&quot;Executing playback of '%s'\n&quot;,buf);
+        offset = 0;
+        if ( do_execute(eslC,&quot;playback&quot;,buf,NULL,&amp;reply) &lt; 0 ) /* We should write about NON success there instead of returning stock */
+                return -1;
+        else {
+                fprintf(stderr,&quot;Playback ended\n&quot;);
+                /* We should check playback_samples var to return offset, sent in the CHANNEL_EXECUTE_COMPLETE and 
+                variable_playback_terminator_used to get the DTMF we had */
+                if (reply) {
+                        if (! (sbuf = esl_event_get_header(reply, &quot;variable_playback_samples&quot;)) )
+                                        offset = -1;
+                        else {
+                                offset = atoi(sbuf);
+                                fprintf(stderr,&quot;Received %s\n&quot;, sbuf);
+                        }
+                        if (! (sbuf = esl_event_get_header(reply, &quot;variable_playback_terminator_used&quot;)) )
+                                        dtmf = 0;
+                        else {
+                                dtmf = atoi(sbuf);
+                                fprintf(stderr,&quot;Received char:%s\n&quot;, sbuf);
+                        }
+                }
+                else {
+                        fprintf(stderr,&quot;There was an error somewhere...\n&quot;);
+                }
+        }
+        if (offset &lt; 0) {
+                if ( write(fd,&quot;200 result=-1 endpos=0\n\n&quot;,128) &lt; 0 )
</ins><span class="cx">                         return -1;
</span><span class="cx">         }
</span><del>-        else if (status == ESL_SUCCESS) {
-                if ( write(fd,&quot;200 result=1\n\n&quot;,128) &lt; 0 )
</del><ins>+        else {
+                sprintf(buf,&quot;200 result=%d endpos=%d\n\n&quot;,dtmf,offset);
+                if ( write(fd,buf,128) &lt; 0 )
</ins><span class="cx">                         return -1;
</span><span class="cx">         }
</span><del>-        fprintf(stderr,&quot;Playback ended\n&quot;);
</del><span class="cx"> 
</span><span class="cx">         return 0;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-
</del><span class="cx"> static void parse_args(char *buf,int *argc,char *argv[_MAX_CMD_ARGS]) {
</span><span class="cx">         char *cur;
</span><span class="cx">         int x = 0;
</span></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>