lib/adhearsion/voip/asterisk/manager_interface.rb in adhearsion-1.0.2 vs lib/adhearsion/voip/asterisk/manager_interface.rb in adhearsion-1.0.3
- old
+ new
@@ -283,20 +283,24 @@
# Used to directly send a new action to Asterisk. Note: NEVER supply an ActionID; these are handled internally.
#
# @param [String, Symbol] action_name The name of the action (e.g. Originate)
# @param [Hash] headers Other key/value pairs to send in this action. Note: don't provide an ActionID
# @return [FutureResource] Call resource() on this object if you wish to access the response (optional). Note: if the response has not come in yet, your Thread will wait until it does.
+ # @deprecated Async AMI is deprecated
#
def send_action_asynchronously(action_name, headers={})
- check_action_name action_name
- action = ManagerInterfaceAction.new(action_name, headers)
- if action.replies_with_action_id?
- @write_queue << action
- action
- else
- raise NotImplementedError
- end
+ ahn_log.ami.deprecation.warn <<WARN
+\n
+ Asterisk only supports one outstanding action ID at a time, making the
+ asynchronous AMI interface dangerous to use. This interface is now
+ deprecated and will disappear in future versions of Adhearsion. If you need
+ to do background processing while Asterisk processes a long-running action
+ (such as a synchronous Originate) then you will need to handle this manually
+ with Ruby threads. Note that Originate specifically has an :Async header
+ which tells Asterisk to process the Originate event asynchronously.
+WARN
+ _send_action_asynchronously(action_name, headers)
end
##
# Sends an action over the AMI connection and blocks your Thread until the response comes in. If there was an error
# for some reason, the error will be raised as an ManagerInterfaceError.
@@ -305,39 +309,24 @@
# @param [Hash] headers Other key/value pairs to send in this action. Note: don't provide an ActionID
# @raise [ManagerInterfaceError] When Asterisk can't execute this action, it sends back an Error which is converted into an ManagerInterfaceError object and raised. Access ManagerInterfaceError#message for the reported message from Asterisk.
# @return [ManagerInterfaceResponse, ImmediateResponse] Contains the response from Asterisk and all headers
#
def send_action_synchronously(*args)
- send_action_asynchronously(*args).response.tap do |response|
+ _send_action_asynchronously(*args).response.tap do |response|
raise response if response.kind_of?(ManagerInterfaceError)
end
end
alias send_action send_action_synchronously
-
- ####### #######
- ########### ###########
- ################# SOON-DEPRECATED COMMANDS #################
- ########### ###########
- ####### #######
-
# ping sends an action to the Asterisk Manager Interface that returns a pong
# more details here: http://www.voip-info.org/wiki/index.php?page=Asterisk+Manager+API+Action+Ping
def ping
- #deprecation_warning
send_action "Ping"
true
end
- def deprecation_warning
- ahn_log.ami.deprecation.warn "The implementation of the ping, originate, introduce, hangup, call_into_context " +
- "and call_and_exec methods will soon be moved from this class to SuperManager. At the moment, the " +
- "SuperManager abstractions are not completed. Don't worry. The migration to SuperManager will be very easy."+
- " See http://docs.adhearsion.com/AMI for more information."
- end
-
# The originate method launches a call to Asterisk, full details here:
# http://www.voip-info.org/tiki-index.php?page=Asterisk+Manager+API+Action+Originate
# Takes these arguments as a hash:
#
# Channel: Channel on which to originate the call (The same as you specify in the Dial application command)
@@ -358,11 +347,10 @@
# originate { :channel => 'SIP/1000@sipnetworks.com',
# :context => 'my_context',
# :exten => 's',
# :priority => '1' }
def originate(options={})
- #deprecation_warning
options = options.clone
options[:callerid] = options.delete :caller_id if options.has_key? :caller_id
options[:exten] = options.delete :extension if options.has_key? :extension
if options.has_key?(:variables) && options[:variables].kind_of?(Hash)
options[:variable] = options.delete(:variables).map {|pair| pair.join('=')}.join(@coreSettings["ArgumentDelimiter"])
@@ -380,27 +368,24 @@
# technology.
#
# TODO: Provide an example when this works.
#
def introduce(caller, callee, opts={})
- #deprecation_warning
dial_args = callee
dial_args += "|#{opts[:options]}" if opts[:options]
call_and_exec caller, "Dial", :args => dial_args, :caller_id => opts[:caller_id]
end
# hangup terminates a call accepts a channel as the argument
# full details here: http://www.voip-info.org/wiki/index.php?page=Asterisk+Manager+API+Action+Hangup
def hangup(channel)
- #deprecation_warning
send_action "Hangup", :channel => channel
end
# call_and_exec allows you to make a call to a channel and then execute an Astersik application
# on that call
def call_and_exec(channel, app, opts={})
- #deprecation_warning
args = { :channel => channel, :application => app }
args[:caller_id] = opts[:caller_id] if opts[:caller_id]
args[:data] = opts[:args] if opts[:args]
args[:variables] = opts[:variables] if opts[:variables]
originate args
@@ -409,26 +394,32 @@
# call_into_context is syntactic sugar for the Asterisk originate command that allows you to
# launch a call into a particular context. For example:
#
# call_into_context('SIP/1000@sipnetworks.com', 'my_context', { :variables => { :session_guid => new_guid }})
def call_into_context(channel, context, options={})
- #deprecation_warning
args = {:channel => channel, :context => context}
args[:priority] = options[:priority] || 1
args[:exten] = options[:extension] if options[:extension]
args[:caller_id] = options[:caller_id] if options[:caller_id]
args[:variables] = options[:variables] if options[:variables]
originate args
end
- ####### #######
- ########### ###########
- ################# END SOON-DEPRECATED COMMANDS #################
- ########### ###########
- ####### #######
+ private
+ def _send_action_asynchronously(action_name, headers={})
+ check_action_name action_name
+ action = ManagerInterfaceAction.new(action_name, headers)
+ if action.replies_with_action_id?
+ @write_queue << action
+ action
+ else
+ raise NotImplementedError
+ end
+ end
+
protected
##
# This class will be removed once this AMI library fully supports all known protocol anomalies.
#
@@ -580,28 +571,25 @@
ahn_log.ami.warn "EVENTS thread connection refused! Retrying in #{RETRY_SLEEP} seconds..."
sleep RETRY_SLEEP
retry
end
- def login_actions
- action = send_action_asynchronously "Login", "Username" => @username, "Secret" => @password, "Events" => "Off"
- response = action.response
- if response.kind_of? ManagerInterfaceError
- raise AuthenticationFailedException, "Incorrect username and password! #{response.message}"
+ def login_actions
+ response = send_action "Login", "Username" => @username, "Secret" => @password, "Events" => "Off"
+ ahn_log.ami "Successful AMI actions-only connection into #{@username}@#{@host}"
+ if @actions_lexer.ami_version < 1.1
+ @coreSettings = Hash.new
+ @coreSettings["AsteriskVersion"] = "1.4.0"
+ @coreSettings["AMIversion"] = "1.0"
+ @coreSettings["ArgumentDelimiter"] = "|"
else
- ahn_log.ami "Successful AMI actions-only connection into #{@username}@#{@host}"
- if @actions_lexer.ami_version < 1.1
- @coreSettings = Hash.new
- @coreSettings["AsteriskVersion"] = "1.4.0"
- @coreSettings["AMIversion"] = "1.0"
- @coreSettings["ArgumentDelimiter"] = "|"
- else
- @coreSettings = send_action_synchronously("CoreSettings").headers
- @coreSettings["ArgumentDelimiter"] = ","
- end
- UnsupportedActionName::preinitialize(@coreSettings["AsteriskVersion"].to_f)
- response
+ @coreSettings = send_action_synchronously("CoreSettings").headers
+ @coreSettings["ArgumentDelimiter"] = ","
end
+ UnsupportedActionName::preinitialize(@coreSettings["AsteriskVersion"].to_f)
+ response
+ rescue ManagerInterfaceError => e
+ raise AuthenticationFailedException, "Incorrect username and password! #{e.message}"
end
def disconnect_events_connection
# Clean up the EventSocket we may have
if @events_connection