Advertisement
Guest User

Untitled

a guest
Sep 20th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using FS = FreeSWITCH;
  6. using System.Threading;
  7.  
  8. namespace FScratch
  9. {
  10.     public class Class1 : FS.ILoadNotificationPlugin, FS.IApiPlugin, IDisposable
  11.     {
  12.         private FS.Native.EventConsumer _ec;
  13.         private Thread _proc;
  14.         private List<IDisposable> _bind = new List<IDisposable>();
  15.         private volatile bool _stop = false;
  16.  
  17.         protected void log(string fmt, params object[] args)
  18.         {
  19.             FS.Log.WriteLine(FS.LogLevel.Info, string.Format(fmt, args));
  20.         }
  21.  
  22.         public bool Load()
  23.         {
  24.            
  25.             log("ILoadNotificationPlugin.Load");
  26.             _ec = new FS.Native.EventConsumer("CUSTOM", "conference::maintenance", 5000);
  27.             _ec.bind("CUSTOM", "avmd::beep");
  28.             _proc = new Thread(new ThreadStart(Tproc));
  29.             _proc.IsBackground = true;
  30.             _proc.Start();
  31.             _bind.Add(FS.EventBinding.Bind("atoid1", FS.Native.switch_event_types_t.SWITCH_EVENT_CUSTOM, "conference::maintenance", ea =>
  32.             {
  33.                 log("BIND BIND EVENT {0}", ea.EventObj.subclass_name);
  34.             }, true));
  35.             _bind.Add(FS.EventBinding.Bind("atoid2", FS.Native.switch_event_types_t.SWITCH_EVENT_SHUTDOWN, null, ea =>
  36.             {
  37.                 this.Dispose();
  38.             }, false));
  39.             return true;
  40.            
  41.         }
  42.  
  43.         protected void Tproc()
  44.         {
  45.             log("Proc thread started");
  46.             try
  47.             {
  48.                 while (!_stop)
  49.                 {
  50.                     var ev = _ec.pop(1, 5000);
  51.                     if (ev != null)
  52.                     {
  53.                         log("Event: {0}\n{1}", ev.GetEventType(), ev.GetHeader("Event-Subclass"));
  54.                     }
  55.                     else
  56.                     {
  57.                         log("NO EVENT!");
  58.                     }
  59.                 }
  60.             }
  61.             catch (Exception ex)
  62.             {
  63.                 log("Error: {0}", ex);
  64.             }
  65.         }
  66.  
  67.  
  68.  
  69.         public FS.PluginOptions GetOptions()
  70.         {
  71.             log("GetOptions!");
  72.             var po = new FS.PluginOptions();
  73.             return po;
  74.         }
  75.  
  76.         public void Execute(FS.ApiContext context)
  77.         {
  78.             log("Execute {0}", context.Arguments);
  79.         }
  80.  
  81.         public void ExecuteBackground(FS.ApiBackgroundContext context)
  82.         {
  83.             log("Execute background {0}", context.Arguments);
  84.         }
  85.  
  86.         public void Dispose()
  87.         {
  88.             log("DISPOSE ****\n\n\n*****\n\n\n");
  89.             _stop = true;
  90.             if (_proc != null)
  91.             {
  92.                 _proc.Interrupt();
  93.                 _proc.Join();
  94.             }
  95.             log("** Thread done!");
  96.             if (_ec != null)
  97.             {
  98.                 _ec.Dispose();
  99.                 _ec = null;
  100.             }
  101.             log("** EC done");
  102.             foreach (var d in _bind)
  103.             {
  104.                 if (d != null) d.Dispose();
  105.             }
  106.             _bind = new List<IDisposable>();
  107.             log("** STOPPED");
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement