Sha256: 1f79567bb6a0c5e763d07673e4534ee1b7d1efba1ccc9a03c361b9d9d8e6c39c

Contents?: true

Size: 1.73 KB

Versions: 19

Compression:

Stored size: 1.73 KB

Contents

module Adhearsion
  module VoIP
    module FreeSwitch

      # Subclass this to register a new event handler
      class EventHandler

        @@events = {}
        @@compound_events = {}

        @@connection = nil

        def self.start!(hash=nil)
          login hash if hash
          raise "You must login to the FreeSWITCH EventSocket!" unless @@connection
          loop do
            # debug "Waiting for an event"
            dispatch_event! @@connection.get_header
          end
        end

        def self.dispatch_event!(data)
          #puts "\nHandling an event! #{data.inspect}"
          name = data['Event-Name']
          normal_event = name && @@events[name.underscore.to_sym]
          # puts "THIS IS WHAT I THINK IT MIGHT BE : #{normal_event.inspect} (with #{name.underscore.to_sym.inspect})"
          if normal_event then normal_event.call(data)
          else
            #debug "Trying compound events"
            @@compound_events.each do |(event, block)|
              mini_event = {}
              event.keys.each { |k| mini_event[k] = data[k] }
              block.call(data) if event == mini_event
            end
          end
        rescue => e
          p e
          puts e.backtrace.map { |x| " " * 4 + x }
        end

        protected

        # Can be specified in the subclass
        def self.login(hash)
          debug "Creating a new event connection manager"
          @@connection = InboundConnectionManager.new hash
          debug "Enabling events"
          @@connection.enable_events!
        end

        def self.on(event, &block)
          event = event.underscore.to_sym if event.is_a? String
          (event.kind_of?(Hash) ? @@compound_events : @@events)[event] = block
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 3 rubygems

Version Path
adhearsion-1.2.6 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.2.5 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.2.4 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.2.3 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.2.1 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.2.0 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.1.1 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.1.0 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.0.3 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.0.2 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-cw-1.0.2.3 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-cw-1.0.2.2 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-cw-1.0.2.1 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.0.1 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-1.0.0 lib/adhearsion/voip/freeswitch/event_handler.rb
rene-adhearsion-0.8.6 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-0.8.6 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-0.8.5 lib/adhearsion/voip/freeswitch/event_handler.rb
adhearsion-0.8.4 lib/adhearsion/voip/freeswitch/event_handler.rb