]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Add new examples about use ESL managed. Inbound and Outbound modes were documented.
authorDiego Toro <dftoro@yahoo.com>
Sat, 30 Jan 2010 05:32:56 +0000 (05:32 +0000)
committerDiego Toro <dftoro@yahoo.com>
Sat, 30 Jan 2010 05:32:56 +0000 (05:32 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@16541 d0543943-73ff-0310-b7d9-9358b9ac24b2

libs/esl/managed/ManagedEslTest/Program.cs

index 5f26fee144b109ad58f5734e66742387edb5628a..82f1b964ae83b2e721991aefc8ff72e2883f0002 100644 (file)
-using System;\r
+/*
+ * C# ESL managed examples 
+ * 
+ * Version: MPL 1.1\r
+ *\r
+ * The contents of this file are subject to the Mozilla Public License Version\r
+ * 1.1 (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ * http://www.mozilla.org/MPL/\r
+ *\r
+ * Software distributed under the License is distributed on an "AS IS" basis,\r
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License\r
+ * for the specific language governing rights and limitations under the\r
+ * License.
+ * 
+ * Contributor: \r
+ * Diego Toro <dftoro@yahoo.com>\r
+ */\r
+\r
+using System;\r
+using System.Net;\r
+using System.Net.Sockets;\r
+using System.Threading;\r
 \r
 namespace ManagedEslTest\r
 {\r
+  [Flags]\r
+  public enum enLogLevel\r
+  {\r
+    EMERG = 0,\r
+    ALERT,\r
+    CRIT,\r
+    ERROR,\r
+    WARNING,\r
+    NOTICE,\r
+    INFO,\r
+    DEBUG\r
+  }\r
+\r
+\r
   class Program\r
   {\r
+    public static readonly int ESL_SUCCESS = 1;\r
+\r
     static void Main(string[] args)\r
     {\r
-      // Connect to FreeSWITCH\r
+      /*\r
+       * The next lines are usefull to view simples C# examples about how ESL managed works.  \r
+       * Active only a mode uncomment it: inbound, outbound sync or outbound async. \r
+       * If you active two o more on this application you may have problems.\r
+       * Remember modify your dialplan for testing outbound modes (see OutboundModeSync and OutboundModeAsync members)\r
+       */\r
+      ThreadPool.QueueUserWorkItem(new WaitCallback(InboundMode));\r
+      //ThreadPool.QueueUserWorkItem(new WaitCallback(OutboundModeSync));\r
+      //ThreadPool.QueueUserWorkItem(new WaitCallback(OutboundModeAsync));\r
+\r
+      Console.ReadLine();\r
+    }\r
+\r
+    /// <summary>\r
+    /// Example using event socket in "Inbound" mode\r
+    /// </summary>\r
+    /// <param name="stateInfo">Object state info</param>\r
+    static void InboundMode(Object stateInfo)\r
+    {\r
+      //Initializes a new instance of ESLconnection, and connects to the host $host on the port $port, and supplies $password to freeswitch\r
       ESLconnection eslConnection = new ESLconnection("localhost", "8021", "ClueCon");\r
-      // We want all Events (probably will want to change this depending on your needs)\r
-      eslConnection.SendRecv("event plain ALL");\r
 \r
+      if (eslConnection.Connected() != ESL_SUCCESS)\r
+      {\r
+        Console.WriteLine("Error connecting to FreeSwitch");\r
+        return;\r
+      }\r
+\r
+      //Set log level\r
+      //ESL.eslSetLogLevel((int)enLogLevel.DEBUG);\r
+\r
+      // Subscribe to all events \r
+      ESLevent eslEvent = eslConnection.SendRecv("event plain ALL");\r
+\r
+      if (eslEvent == null)\r
+      {\r
+        Console.WriteLine("Error subscribing to all events");\r
+        return;\r
+      }\r
+\r
+      //Turns an event into colon-separated 'name: value' pairs. The format parameter isn't used\r
+      Console.WriteLine(eslEvent.Serialize(String.Empty));\r
 \r
       // Grab Events until process is killed\r
-      while (eslConnection.Connected() == 1)\r
+      while (eslConnection.Connected() == ESL_SUCCESS)\r
       {\r
-        ESLevent eslEvent = eslConnection.RecvEvent();\r
+        eslEvent = eslConnection.RecvEvent();\r
         Console.WriteLine(eslEvent.Serialize(String.Empty));\r
       }\r
     }\r
+\r
+    /// <summary>\r
+    /// Example using event socket in "Outbound" mode synchronic\r
+    /// </summary>\r
+    /// <param name="stateInfo">Object state info</param>\r
+    static void OutboundModeSync(Object stateInfo)\r
+    {\r
+      /* add next line to a dialplan\r
+      <action application="socket" data="localhost:8022 sync full"/> \r
+      */\r
+      TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8022);\r
+\r
+      try\r
+      {\r
+        tcpListener.Start();\r
+\r
+        Console.WriteLine("OutboundModeSync, waiting for a connection...");\r
+\r
+        while (true)\r
+        {\r
+          Socket sckClient = tcpListener.AcceptSocket();\r
+\r
+          //Initializes a new instance of ESLconnection, and connects to the host $host on the port $port, and supplies $password to freeswitch\r
+          ESLconnection eslConnection = new ESLconnection(sckClient.Handle.ToInt32());\r
+\r
+          Console.WriteLine("Execute(\"answer\")");\r
+          eslConnection.Execute("answer", String.Empty, String.Empty);\r
+          Console.WriteLine("Execute(\"playback\")");\r
+          eslConnection.Execute("playback", "music/8000/suite-espanola-op-47-leyenda.wav", String.Empty);\r
+          Console.WriteLine("Execute(\"hangup\")");\r
+          eslConnection.Execute("hangup", String.Empty, String.Empty);\r
+        }\r
+      }\r
+      catch (Exception ex)\r
+      {\r
+        Console.WriteLine(ex);\r
+      }\r
+      finally\r
+      {\r
+        tcpListener.Stop();\r
+      }\r
+    }\r
+\r
+    /// <summary>\r
+    /// Example using event socket in "Outbound" mode asynchronic\r
+    /// </summary>\r
+    /// <param name="stateInfo">Object state info</param>\r
+    static void OutboundModeAsync(Object stateInfo)\r
+    {\r
+      /* add next line to a dialplan\r
+       <action application="socket" data="localhost:8022 async full" />\r
+      */\r
+      TcpListener tcpListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 8022);\r
+\r
+      try\r
+      {\r
+        tcpListener.Start();\r
+\r
+        Console.WriteLine("OutboundModeAsync, waiting for connections...");\r
+\r
+        while (true)\r
+        {\r
+          tcpListener.BeginAcceptSocket((asyncCallback) =>\r
+         {\r
+           TcpListener tcpListened = (TcpListener)asyncCallback.AsyncState;\r
+\r
+           Socket sckClient = tcpListened.EndAcceptSocket(asyncCallback);\r
+\r
+           //Initializes a new instance of ESLconnection, and connects to the host $host on the port $port, and supplies $password to freeswitch\r
+           ESLconnection eslConnection = new ESLconnection(sckClient.Handle.ToInt32());\r
+\r
+           ESLevent eslEvent = eslConnection.GetInfo();\r
+           string strUuid = eslEvent.GetHeader("UNIQUE-ID");\r
+\r
+           eslConnection.SendRecv("myevents");\r
+           eslConnection.SendRecv("divert_events on");\r
+\r
+           eslConnection.Execute("answer", String.Empty, String.Empty);\r
+           eslConnection.Execute("playback", "music/8000/suite-espanola-op-47-leyenda.wav", String.Empty);\r
+\r
+           while (eslConnection.Connected() == ESL_SUCCESS)\r
+           {\r
+             eslEvent = eslConnection.RecvEvent();\r
+             Console.WriteLine(eslEvent.Serialize(String.Empty));\r
+           }\r
+\r
+           sckClient.Close();\r
+           Console.WriteLine("Connection closed uuid:{0}", strUuid);\r
+\r
+         }, tcpListener);\r
+\r
+          Thread.Sleep(50);\r
+        }\r
+      }\r
+      catch (Exception ex)\r
+      {\r
+        Console.WriteLine(ex);\r
+      }\r
+      finally\r
+      {\r
+        tcpListener.Stop();\r
+      }\r
+    }\r
   }\r
 }\r