summaryrefslogtreecommitdiff
path: root/misc/freeswitch/scripts/event/event.lua
blob: 8e67bc96aa917393882d2215022596e005b7247a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
-- Gemeinschaft 5 module: event manager class
-- (c) AMOOMA GmbH 2012
-- 

module(...,package.seeall)

EventManager = {}

-- create event manager object
function EventManager.new(self, arg)
  arg = arg or {}
  object = arg.object or {}
  setmetatable(object, self);
  self.__index = self;
  self.log = arg.log;
  self.class = 'eventmanager'
  self.database = arg.database;
  self.domain = arg.domain;

  return object;
end


function EventManager.register(self)
  self.consumer = freeswitch.EventConsumer('all');
  return (self.consumer ~= nil);
end


function EventManager.load_event_modules(self)
  local CONFIG_FILE_NAME = '/opt/freeswitch/scripts/ini/events.ini';

  require 'common.configuration_file'
  self.config = common.configuration_file.get(CONFIG_FILE_NAME);

  return self.config.modules;
end


function EventManager.load_event_handlers(self, event_modules)
  event_handlers = {}

  for index, event_module_name in ipairs(event_modules) do
    event_module = require('event.' .. event_module_name);
    if event_module then
      self.log:info('[event] EVENT_MANAGER - loading handler module: ', event_module_name);
      handler_class = event_module.handler_class();

      if handler_class then
        module_event_handlers = handler_class:new{ log = self.log, database = self.database, domain = self.domain }:event_handlers();
        if module_event_handlers then
          for event_name, event_subclasses in pairs(module_event_handlers) do
            if not event_handlers[event_name] then
              event_handlers[event_name] = {};
            end

            for event_subclass, module_event_handler in pairs(event_subclasses) do
              if not event_handlers[event_name][event_subclass] then
                event_handlers[event_name][event_subclass] = {};
              end

              table.insert(event_handlers[event_name][event_subclass], { class = handler_class, method = module_event_handler } );
              self.log:info('[event] EVENT_MANAGER - module: ', event_module_name, ', handling events: ', event_name, ', subclass:', event_subclass);
            end
          end
        end
      end
    end
  end

  return event_handlers;
end


function EventManager.run(self)

  local event_modules = self:load_event_modules();
  local event_handlers = self:load_event_handlers(event_modules);

  if not event_handlers then
    self.log:error('[event] EVENT_MANAGER - no handlers specified');
    return nil;
  end

  if not self:register() then
    return nil;
  end

  freeswitch.setGlobalVariable('gs_event_manager', 'true');
  while freeswitch.getGlobalVariable('gs_event_manager') == 'true' do
    local event = self.consumer:pop(1, 100);
    if event then
      local event_type = event:getType();
      local event_subclass = event:getHeader('Event-Subclass');
      if event_handlers[event_type] then
        if event_handlers[event_type][event_subclass] and #event_handlers[event_type][event_subclass] > 0 then
          for index, event_handler in ipairs(event_handlers[event_type][event_subclass]) do
            event_handler.method(event_handler.class, event);
          end
        end
        if event_handlers[event_type][true] and #event_handlers[event_type][true] > 0 then
          for index, event_handler in ipairs(event_handlers[event_type][true]) do
            event_handler.method(event_handler.class, event);
          end
        end
      end
    end
  end
end