lib/adhearsion/call_controller/dial.rb in adhearsion-2.0.0.alpha3 vs lib/adhearsion/call_controller/dial.rb in adhearsion-2.0.0.beta1
- old
+ new
@@ -30,10 +30,11 @@
# @example Make a call using the IAX provider to the PSTN
# dial "IAX2/my.id@voipjet/19095551234", :from => "John Doe <9095551234>"
#
def dial(to, options = {}, latch = nil)
targets = Array(to)
+ status = DialStatus.new
latch ||= CountDownLatch.new targets.size
call.on_end { |_| latch.countdown! until latch.count == 0 }
@@ -41,11 +42,11 @@
options[:timeout] ||= _for if _for
options[:from] ||= call.from
calls = targets.map do |target|
- new_call = OutboundCall.new options
+ new_call = OutboundCall.new
new_call.on_answer do |event|
calls.each do |call_to_hangup, target|
begin
next if call_to_hangup.id == new_call.id
@@ -55,45 +56,64 @@
# This actor may previously have been shut down due to the call ending
end
end
new_call.register_event_handler Punchblock::Event::Unjoined, :other_call_id => call.id do |event|
- new_call[:"dial_countdown_#{call.id}"] = true
+ new_call["dial_countdown_#{call.id}"] = true
latch.countdown!
throw :pass
end
logger.debug "Joining call #{new_call.id} to #{call.id} due to a #dial"
new_call.join call
+ status.answer!
end
new_call.on_end do |event|
- latch.countdown! unless new_call[:"dial_countdown_#{call.id}"]
+ latch.countdown! unless new_call["dial_countdown_#{call.id}"]
end
[new_call, target]
end
calls.map! do |call, target|
call.dial target, options
call
end
- timeout = latch.wait options[:timeout]
+ status.calls = calls
+ no_timeout = latch.wait options[:timeout]
+ status.timeout! unless no_timeout
+
logger.debug "#dial finished. Hanging up #{calls.size} outbound calls #{calls.inspect}."
calls.each do |outbound_call|
begin
logger.debug "Hanging up #{outbound_call} because the #dial that created it is complete."
outbound_call.hangup
rescue Celluloid::DeadActorError
# This actor may previously have been shut down due to the call ending
end
end
- return timeout unless timeout
+ status
+ end
- calls.size == 1 ? calls.first : calls
+ class DialStatus
+ attr_accessor :calls
+ attr_reader :result
+
+ def initialize
+ @result = :no_answer
+ end
+
+ def answer!
+ @result = :answer
+ end
+
+ def timeout!
+ @result = :timeout
+ end
end
end#module Dial
end
end