Advertisement
mytemike

DialOut function using mod_managed

Jan 9th, 2012
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.29 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using System.Collections.Generic;
  5. using FreeSWITCH.Native;
  6.  
  7. namespace FreeSWITCH.Managed.DialOut
  8. {
  9.     public class DialOut : IAppPlugin
  10.     {
  11.         public FreeSWITCH.Native.Api fsApi;
  12.  
  13.         public void Run(AppContext context)
  14.         // ***************************************************************************************************            
  15.         {
  16.             fsApi = new FreeSWITCH.Native.Api();
  17.             context.Session.HangupFunction = () => HandleHangup(context.Session);
  18.  
  19.             simpleCall(context.Session, "316*********@*.*.*.*", "3134*******", 30);
  20.  
  21.             if (context.Session.Ready())
  22.             {
  23.                 Debug("A-Leg was still here, so hanging up...");
  24.                 context.Session.Hangup("NORMAL_CLEARING");
  25.             }
  26.             Debug("End of run...");
  27.         }
  28.  
  29.  
  30.         private string simpleCall(ManagedSession leg_a, string route, string orig, int timeout)
  31.         {
  32.             Debug("SimpleCall()");
  33.             string obCause = "ERROR";
  34.             Debug("Creating the new session...");
  35.             ManagedSession leg_b = new ManagedSession("{ignore_early_media=true,origination_caller_id_number=" + orig + ",originate_timeout=20}sofia/external/" + route);
  36.             if (leg_b.Ready())
  37.             {
  38.                 if (leg_b.answered())
  39.                 {
  40.                     leg_a.Answer();
  41.                     leg_a.sleep(1000, 0);
  42.                    
  43.                     string apiResult = fsApi.ExecuteString(string.Format("uuid_bridge {1} {0}", leg_a.GetUuid(), leg_b.GetUuid()));
  44.                     Debug("apiResult=" + apiResult);
  45.                     obCause = "SUCCESS";
  46.  
  47.                     Debug("As long as both legs are Ready(), we wait......");
  48.                     while (leg_a.bridged() && leg_b.bridged())
  49.                     {
  50.                         leg_a.sleep(1000, 0); // Slow it down...
  51.                     }
  52.  
  53.                     Debug("There is one leg no longer ready...");
  54.                     if (leg_a.bridged()) { Debug("Session A still ready..."); } else { Debug("Session A NO LONGER ready..."); }
  55.                     if (leg_b.bridged()) { Debug("Session B still ready..."); } else { Debug("Session B NO LONGER ready..."); }
  56.                 }
  57.                 else
  58.                 {
  59.                     Debug("A-Leg aborted while dialing...");
  60.                 }
  61.  
  62.                 if (leg_b.Ready())
  63.                 {
  64.                     Debug("B-Leg still here, so hanging up B-Leg...");
  65.                     leg_b.Hangup("NORMAL_CALL_CLEARING");
  66.                     leg_b.destroy();
  67.                 }
  68.             }
  69.             else
  70.             {
  71.                 obCause = leg_b.hangupCause();
  72.             }
  73.             Debug("SimpleCall() ended with: " + obCause);
  74.             return obCause;
  75.         }
  76.  
  77.         private string simpleCall2(ManagedSession leg_a, string route, string orig, int timeout)
  78.         {
  79.             Debug("SimpleCall2()");
  80.             leg_a.Execute("bridge", "{ignore_early_media=true,origination_caller_id_number=" + orig + ",originate_timeout=20}sofia/external/" + route);
  81.             return "";
  82.         }
  83.  
  84.         private string simpleCall3(ManagedSession leg_a, string route, string orig, int timeout)
  85.         {
  86.             Debug("SimpleCall3()");
  87.             string Uuid_b = fsApi.ExecuteString("create_uuid");
  88.  
  89.             Debug(fsApi.ExecuteString("originate {ignore_early_media=true,origination_uuid=" + Uuid_b + ",origination_caller_id_number=" + orig + ",originate_timeout=20}sofia/external/" + route + " &managed(fsMCXessOutbound)"));
  90.             Debug("Answered, will now bridge...");
  91.             Debug(fsApi.ExecuteString(string.Format("uuid_bridge {1} {0}", leg_a.GetUuid(), Uuid_b)));
  92.  
  93.             while (leg_a.bridged()) { }
  94.             Debug("No longer bridged so exiting...");
  95.             return "";
  96.         }
  97.  
  98.         private void HandleHangup(ManagedSession session)
  99.         {
  100.             Debug("HandleHangUp();");
  101.             Debug("Finalizing call in CDR table");
  102.             // a_leg should always come here at the end of it's call
  103.         }
  104.  
  105.         private void Debug(string Message)
  106.         {
  107.             Log.WriteLine(LogLevel.Error, "=============> " + Message);
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement