Advertisement
Guest User

Untitled

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