Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using FS = FreeSWITCH;
- using System.Threading;
- namespace FScratch
- {
- public class Class1 : FS.ILoadNotificationPlugin, FS.IApiPlugin, IDisposable
- {
- private Thread _proc;
- private List<IDisposable> _bind = new List<IDisposable>();
- private volatile bool _stop = false;
- protected void log(string fmt, params object[] args)
- {
- FS.Log.WriteLine(FS.LogLevel.Info, "*SCRATCH* " + string.Format(fmt, args));
- }
- public bool Load()
- {
- log("ILoadNotificationPlugin.Load");
- _proc = new Thread(new ThreadStart(Tproc));
- _proc.IsBackground = true;
- _proc.Start();
- _bind.Add(FS.EventBinding.Bind("atoid1", FS.Native.switch_event_types_t.SWITCH_EVENT_CUSTOM, "conference::maintenance", ea =>
- {
- log("BIND BIND EVENT {0}", ea.EventObj.subclass_name);
- }, true));
- _bind.Add(FS.EventBinding.Bind("atoid2", FS.Native.switch_event_types_t.SWITCH_EVENT_SHUTDOWN, null, ea =>
- {
- this.Dispose();
- }, false));
- return true;
- }
- protected void Tproc()
- {
- log("Proc thread started");
- try
- {
- using (var ec = new FS.Native.EventConsumer("CUSTOM", "conference::maintenance", 5000))
- {
- ec.bind("CUSTOM", "avmd::beep");
- while (!_stop)
- {
- var ev = ec.pop(1, 5000);
- if (ev != null)
- {
- log("POLL Event: {0} {1}", ev.GetEventType(), ev.GetHeader("Event-Subclass"));
- }
- else
- {
- log("NO EVENT!");
- }
- }
- log("Proc exiting");
- }
- }
- catch (Exception ex)
- {
- log("Error: {0}", ex);
- }
- }
- public FS.PluginOptions GetOptions()
- {
- log("GetOptions!");
- var po = new FS.PluginOptions();
- return po;
- }
- public void Execute(FS.ApiContext context)
- {
- log("Execute {0}", context.Arguments);
- }
- public void ExecuteBackground(FS.ApiBackgroundContext context)
- {
- log("Execute background {0}", context.Arguments);
- }
- public void Dispose()
- {
- log("DISPOSE ****\n\n\n*****\n\n\n");
- _stop = true;
- if (_proc != null)
- {
- _proc.Interrupt();
- _proc.Join();
- }
- log("** Thread done!");
- foreach (var d in _bind)
- {
- if (d != null) d.Dispose();
- }
- _bind = new List<IDisposable>();
- log("** STOPPED");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement