[Freeswitch-users] Mod Event Socket

Will Smith willbelair at yahoo.com
Fri Oct 29 14:03:29 PDT 2010


hi Michael,
I fixed the ESL.so prob, and run the  test.php, I got response.Next step is running the php script from browser. Hope that I can do that. Thank you for your help. If you have any tip, or samples, and have free time, please give.
Thanks,Will

--- On Fri, 10/29/10, Will Smith <willbelair at yahoo.com> wrote:

From: Will Smith <willbelair at yahoo.com>
Subject: Re: [Freeswitch-users] Mod Event Socket
To: "FreeSWITCH Users Help" <freeswitch-users at lists.freeswitch.org>
Received: Friday, October 29, 2010, 1:01 PM

hi Michael,Thank you for your input. Really appreciate when having someone giving help when in need.I bought FS ebook, read chapter 9 and 10. And did what you said:1- put the event_socket_conf.xml back to original state, just modify the ip address, because I work through a vpn network. Here , could you confirm that mod_event_socket supports different browsers ? This is really important to me, because if client could not access from www, there is no point to work on this. Thank you.-------- the
 event_socket_conf.xml<configuration name="event_socket.conf" description="Socket Client">  <settings>    <param name="nat-map" value="false"/>    <param name="listen-ip" value="192.168.51.133"/>    <param name="listen-port" value="8021"/>    <param name="password" value="ClueCon"/>    <!--param name="apply-inbound-acl" value="lan"/-->  </settings></configuration>-----------
2- I put this in both dialplan/default.xml  and dialplan/public.xml ------------<extension name="outbound-socket">		<condition field="destination_number" expression="^444$">		<action application="socket" data="192.168.51.133:8021 async full"/>		</condition></extension>------------
3- I have a simple php script just to connect to the FS, this is a sample script from FS wiki page------<?php
//require_once('ESL.php');require_once('/usr/src/freeswitch/freeswitch-snapshot/libs/esl/php/ESL.php');set_time_limit(0); // Remove the PHP time limit of 30 seconds for completion due to loop watching eventsecho
 "HERE";// Connect to FreeSWITCH$sock = new ESLconnection('localhost', '8021', 'ClueCon');// We want all Events (probably will want to change this depending on your needs)$sock->sendRecv("event plain ALL");

// Grab Events until process is killed//while($sock->connected()){//echo "CONECTED";//$event = $sock->recvEvent();//print_r($event->serialize());//}
?>------
when running this from web browser, I got blank page. So, I run from console, this is the error messages:--------PHP Warning:  dl(): Unable to load dynamic library '/usr/lib/php/modules/ESL.so' - /usr/lib/php/modules/ESL.so: cannot open shared
 object file: No such file or directory in /usr/src/freeswitch/freeswitch-snapshot/libs/esl/php/ESL.php on line 23PHP Stack trace:PHP   1. {main}() /var/www/html/eventsocket.php:0PHP   2. require_once() /var/www/html/eventsocket.php:4PHP   3. dl() /usr/src/freeswitch/freeswitch-snapshot/libs/esl/php/ESL.php:23HEREPHP Fatal error:  Call to undefined function new_ESLconnection() in /usr/src/freeswitch/freeswitch-snapshot/libs/esl/php/ESL.php on line 119PHP Stack trace:PHP   1. {main}() /var/www/html/eventsocket.php:0PHP   2. ESLconnection->__construct() /var/www/html/eventsocket.php:8----------
So, I guess
 the ESL.so is missing. I tried to  "locate ESL.so" , but nothing returned. I tried to --------yum install libxml2-devel pcre-devel bzip2-devel curl-devel gmp-devel aspell-devel php-devel libtermcap-devel gdbm-devel
 db4-devel--------it ran fine. But I did not do this:-------NOTE: For PHP you must edit the libs/esl/php/Makefile and add -lpthread to the LOCAL_LDFLAGS line.-------because I could not fine the LOCAL_LDFLAGS line.
Please give me some help, I am banging my head against the wall here. If I could get the file ESL.so , is the set up above working? I thought it would be simple just to connect.
Thank you.
--- On Thu, 10/28/10, Michael Collins <msc at freeswitch.org> wrote:

From: Michael Collins <msc at freeswitch.org>
Subject: Re: [Freeswitch-users] Mod Event Socket
To: "FreeSWITCH Users Help" <freeswitch-users at lists.freeswitch.org>
Received: Thursday, October 28, 2010, 5:57 PM

Be sure to learn the difference between inbound and outbound event socket. In your case you are doing outbound event socket. The dialplan calls the socket app which does an outbound socket connection to port 9090 on the localhost. Make sure that your app is listening on port 9090.


The event_socket.conf.xml file is for configuring inbound socket connections, e.g. when you have a script that attempts to connect to FS where FS is listening on port 8021. So change your event_socket.conf.xml file back to the default and retry. 


Report back here if you have trouble. Also, buy or borrow the FreeSWITCH book and check out chapter 9. Lots of good stuff there on how the even system works.

-MC

On Thu, Oct 28, 2010 at 2:14 PM, Will Smith <willbelair at yahoo.com> wrote:

Hi,I am new to FS, and trying to get the mod event socket installed and running. I have FS running, with SIP account, can dial in/out via gateway.
Now I want to dial in, FS will send some info to client browser, here is one question, does this work over the internet, or just local ? Info sent could be the uuid, so that client browser could decide to bridge the call, send to IVR or transfer ...
Please give me some guide line how to set this up.I added to dialplan/default.xml----- <extension name="socketTest">
      <condition field="destination_number" expression="^444$">
        <action application="socket" data="127.0.0.1:9090 async full"/>
      </condition>
 </extension>-----
got the php sample file: <?php

/**
 * Based loosely on the NET_Server code in PEAR.
 * This is only an example - considerable additional work is needed
 * Specifically, the code in the handleConnection method should be 
 * handled in a subclass
 *
 * 
 */

class Message
{
  var $properties = array();
  var $content = null;
}

class EventSocketListener
{

  var $host;
  var $port;
  var $sock;
  var $is_parent = true;
  var $clientInfo;
  var $clientFD;

  var $connectionContext = array();


  function &create($port, $host = "localhost")
      {
	$esl = new EventSocketListener;
	$esl->port = $port;
	$esl->host = $host;
        if (!function_exists('socket_create')) {
            return PEAR::raiseError('Sockets extension not available.');
        }
	return $esl;
      }

  function start()
  {
	if (($this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
	  echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
	}

	if (!socket_set_option($this->sock, SOL_SOCKET, SO_REUSEADDR, 1)) {
	  echo 'Unable to set option on socket: '. socket_strerror(socket_last_error()) . PHP_EOL;
	}

	if (socket_bind($this->sock, $this->host, $this->port) === false) {
	  echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($this->sock)) . "\n";
	}

	if (socket_listen($this->sock, 5) === false) {
	  echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($this->sock)) . "\n";
	}

        // Dear children, please do not become zombies
        pcntl_signal(SIGCHLD, SIG_IGN);
        
        // wait for incmoning connections
        while (true)
        {
            // new connection
            if(($fd = socket_accept($this->sock)))
            {
	      $pid = pcntl_fork();
	      if($pid == -1) {
		  return  PEAR::raiseError('Could not fork child process.');
                }
                // This is the child => handle the request
                elseif($pid == 0) {
                    // this is not the parent
                    $this->_isParent = false;
                    // store the new file descriptor
                    $this->clientFD = $fd;

                    $peer_host    =    "";
                    $peer_port    =    "";
                    socket_getpeername($this->clientFD, $peer_host, $peer_port);
                    $this->clientInfo    =    array(
                                                      "host"        =>    $peer_host,
                                                      "port"        =>    $peer_port,
                                                      "connectOn"   =>    time()
                                                   );
		    $this->handleConnection();
		    socket_shutdown($this->clientFD, 2);
		    socket_close($this->clientFD);
		}
		else /* Parent does nothing */
		  {
		  }
	    }
	}
  }

  function handleConnection()
  {
    $fd = $this->clientFD;
    //first, read headers & setup a state for this connection
    $line = "";
    socket_write($fd, "CONNECT\n\n");
    do 
      {
	$line = socket_read($fd, 2048, PHP_NORMAL_READ);
	if (trim($line) == "")
	  break;
	//we got a header, we need to add it to the 
	list($key, $value) = explode(":", $line);
	$key = trim($key);
	$value = trim(urldecode($value));
	$this->connectionContext[$key] = $value;
      }
    while ($line != "");
    
    //    print_r($this->connectionContext);
    $this->callConnected();

    exit();
    
  }

  function processMessages($returnOnReply = false)
  {
    $fd = $this->clientFD;
    $result = new Message();
    $props = array();
    while (true)
      {
	do 
	  {
	    $line = @socket_read($fd, 2048, PHP_NORMAL_READ);
	    if (socket_last_error($fd) == 104)
	      return null;
	    if ($line == null || $line == FALSE || trim($line) == "")
	      break;
	    //we got a header, we need to add it to the message
	    list($key, $value) = explode(":", $line);
	    $key = trim($key);
	    $value = trim(urldecode($value));
	    $props[$key] = $value;
	  }
	while ($line != "");
	$result->properties = $props;

	if (isset($props['Content-Length']))
	  {
	    $length = $props['Content-Length'];
	    print("Reading content - $length\n");
	    $data = socket_read($fd, $length);
	    $result->content = $data;
	  }
	if (isset($props['Content-Type']))
	  {
	    $type = $props['Content-Type'];
	    if ($returnOnReply && 
		($type == "command/reply" || $type == "api/response"))
	      {
		return $result;
	      }
	    else if ($type == "text/event-plain") //only plain events for now
	      {
		$this->handleEvent($result);
	      }
	  }
	else
	  {
	    print("UNKNOWN MESSAGE: \n");
	    print_r($result);
	  }
      }    
  }
    


  function invokeCommand($command)
  {
    //Send the command
    print("Invoking: $command\n");
    $this->sendCommand($command);
    // Wait for the response
    $result = $this->processMessages(true);
    return $result;
  }

  function sendCommand($command)
  {
    $fd = $this->clientFD;
    socket_write($fd, trim($command) . "\n\n"); 
  }

/*-----------------------------------------------------*/
  /* Abstract Methods - should move to subclass*/

  function callConnected()
  {
    print_r($this->connectionContext);
    print("----------------\n");
    $result = $this->invokeCommand("log DEBUG");
    print_r($result);
    $result = $this->invokeCommand("event plain ALL");
    print_r($result);

    $this->processMessages(false);
    print("DONE PROCESSING MESSAGES");
    print_r($this->connectionContext);
  }

  function handleCommandResponse($response)
  {
    print("Recieved Unhandled Response:\n");
    print_r($response);
  }

  function handleEvent($event)
  {
    print("Recieved Unhandled Event:\n");
    print_r($event);
  }
}

  

  
// create a server that forks new processes
$server  = &EventSocketListener::create(9090);

// start the server
$server->start();
?>--------( this is the original file, not perfect sample) I tried to run this, and got error with auth.
Also, I modify the even_socket_conf.xml  in autoload_configs/  change listen-ip to  0.0.0.0 , port = 9090 , disable password

What did I miss? Thankyou for your help
Will


_______________________________________________


FreeSWITCH-users mailing list

FreeSWITCH-users at lists.freeswitch.org

http://lists.freeswitch.org/mailman/listinfo/freeswitch-users

UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users

http://www.freeswitch.org





-----Inline Attachment Follows-----

_______________________________________________
FreeSWITCH-users mailing list
FreeSWITCH-users at lists.freeswitch.org
http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
http://www.freeswitch.org


-----Inline Attachment Follows-----

_______________________________________________
FreeSWITCH-users mailing list
FreeSWITCH-users at lists.freeswitch.org
http://lists.freeswitch.org/mailman/listinfo/freeswitch-users
UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-users
http://www.freeswitch.org


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.freeswitch.org/pipermail/freeswitch-users/attachments/20101029/302b754e/attachment-0001.html 


More information about the FreeSWITCH-users mailing list