lib/adhearsion/initializer/asterisk.rb in adhearsion-0.8.3 vs lib/adhearsion/initializer/asterisk.rb in adhearsion-0.8.4

- old
+ new

@@ -1,33 +1,39 @@ require 'adhearsion/voip/asterisk' module Adhearsion class Initializer - + class AsteriskInitializer - + cattr_accessor :config, :agi_server, :ami_client class << self - + def start self.config = AHN_CONFIG.asterisk self.agi_server = initialize_agi self.ami_client = VoIP::Asterisk.manager_interface = initialize_ami if config.ami_enabled? join_server_thread_after_initialized + + # Make sure we stop everything when we shutdown + Events.register_callback(:shutdown) do + ahn_log.info "Shutting down with #{Adhearsion.active_calls.size} active calls" + self.stop + end end def stop - agi_server.stop + agi_server.graceful_shutdown ami_client.disconnect! if ami_client end private def initialize_agi VoIP::Asterisk::AGI::Server.new :host => config.listening_host, :port => config.listening_port end - + def initialize_ami options = ami_options start_ami_after_initialized returning VoIP::Asterisk::Manager::ManagerInterface.new(options) do class << VoIP::Asterisk @@ -40,18 +46,18 @@ end end end end end - + def ami_options %w(host port username password events).inject({}) do |options, property| options[property.to_sym] = config.ami.send property options end end - + def join_server_thread_after_initialized Events.register_callback(:after_initialized) do begin agi_server.start rescue => e @@ -59,11 +65,11 @@ abort end end IMPORTANT_THREADS << agi_server end - + def start_ami_after_initialized Events.register_callback(:after_initialized) do begin self.ami_client.connect! rescue Errno::ECONNREFUSED @@ -74,8 +80,8 @@ end end end end - + end end