Add. EventConsumer class. (#1741)

Rewrite MWI and CallFlow subscribe handlers based on EventConsumer class.
Also on my test VirtualBox/Debian system Lua function `os.clock` produce
very strange result(delta ~0.015 for 1 second) so I switch to `os.time`.
Now to to stop this background Lua scripts it possible send CUSTOM event
with subclass `fusion::XXX::shutdown`. Where XXX is `mwi` or `flow`.

Usage of EventConsumer class
```Lua
-- create new object with timeout one minute
local events = EventConsumer.new(60000)

-- bind to some FS event
events:bind("SHUTDOW", function(self, name, event) ... end)

-- bind to another FS event with subclass
events:bind("CUSTOM::fusion::mwi::shutdown", function(self, name, event) ... end)

-- handle timeout event
events:on("TIMEOUT", function(self, name) ... end)

--run event loop
events:run()
```
This commit is contained in:
Alexey Melnichuk
2016-07-08 22:10:43 +03:00
committed by FusionPBX
parent 666ccddb34
commit cae644c8a1
5 changed files with 588 additions and 131 deletions

View File

@@ -1,3 +1,15 @@
-- absolute timer
local fs_time if freeswitch then
local api = require "resources.functions.api"
fs_time = {
now = function() return api:getTime() end;
elapsed = function(t) return api:getTime() - t end;
ms_to_time = function(ms) return ms end;
time_to_ms = function(t) return t end;
}
end
-- absolute timer
local os_time = {
now = function() return os.time() end;
elapsed = function(t) return os.difftime(os.time(), t) end;
@@ -5,6 +17,7 @@ local os_time = {
time_to_ms = function(t) return t * 1000 end;
}
-- monotonic timer (not work on my test Debian system)
local os_clock = {
now = function() return os.clock() end;
elapsed = function(t) return os.clock() - t end;
@@ -12,13 +25,19 @@ local os_clock = {
time_to_ms = function(t) return t * 1000 end;
}
local timers = {
freeswitch = fs_time;
time = os_time;
clock = os_clock;
}
local IntervalTimer = {} do
IntervalTimer.__index = IntervalTimer
function IntervalTimer.new(interval, timer)
local o = setmetatable({}, IntervalTimer)
o._interval = interval
o._timer = timer or os_clock
o._timer = timer and assert(timers[timer], "unknown timer: " .. timer) or os_time
return o
end