lib/larynx/call_handler.rb in larynx-0.1.2 vs lib/larynx/call_handler.rb in larynx-0.1.3

- old
+ new

@@ -6,27 +6,20 @@ attr_reader :state, :session, :response, :input, :observers, :last_command # EM hook which is run when call is received def post_init @queue, @input, @timers = [], [], {} - connect { - @session = Session.new(@response.header) - log "Call received from #{caller_id}" - Larynx.fire_callback(:connect, self) - start_session - } + connect { start_session } send_next_command end def start_session - myevents { - linger - answer { - log 'Answered call' - Larynx.fire_callback(:answer, self) - } - } + @session = Session.new(@response.header) + log "Call received from #{caller_id}" + myevents + linger + Larynx.fire_callback(:connect, self) end def called_number @session[:caller_destination_number] end @@ -56,37 +49,39 @@ @queue << command end command end - def timer(name, timeout, &block) + def add_timer(name, timeout, &block) @timers[name] = [RestartableTimer.new(timeout) { - if timer = @timers.delete(name) - timer[1].call if timer[1] - notify_observers :timed_out - send_next_command if @state == :ready - else - puts name - end + timer = @timers.delete(name) + timer[1].call if timer[1] + notify_observers :timed_out + send_next_command if @state == :ready }, block] end def cancel_timer(name) - if timer = @timers.delete(name) + if timer = @timers[name] timer[0].cancel + @timers.delete(name) send_next_command if @state == :ready end end def cancel_all_timers @timers.values.each {|t| t[0].cancel } + @timers = {} end def stop_timer(name) - if timer = @timers.delete(name) - timer[0].cancel - timer[1].call if timer[1] + if timer = @timers[name] + # only run callback if it was actually cancelled (i.e. returns false) + if timer[0].cancel == false && timer[1] + timer[1].call + end + @timers.delete(name) send_next_command if @state == :ready end end def restart_timer(name) @@ -95,33 +90,41 @@ end end def receive_request(header, content) @response = Response.new(header, content) + handle_request + end + def handle_request case - when @response.reply? && current_command && !current_command.is_a?(AppCommand) + when @response.reply? && current_command.is_a?(CallCommand) log "Completed: #{current_command.name}" finalize_command @state = :ready send_next_command + when @response.answered? + log 'Answered call' + finalize_command + @state = :ready + Larynx.fire_callback(:answer, self) + send_next_command when @response.executing? log "Executing: #{current_command.name}" - run_command_setup + current_command.setup @state = :executing when @response.executed? && current_command this_command = current_command finalize_command unless this_command.interrupted? - current_command.fire_callback(:after) if this_command.command == 'break' + current_command.finalize if this_command.command == 'break' @state = :ready send_next_command end when @response.dtmf? log "Button pressed: #{@response.body[:dtmf_digit]}" handle_dtmf - when @response.speech? when @response.disconnect? log "Disconnected." cleanup notify_observers :hungup Larynx.fire_callback(:hungup, self) @@ -141,16 +144,12 @@ current_command.interrupted = true break! end end - def run_command_setup - current_command.fire_callback :before - end - def finalize_command if command = @queue.shift - command.fire_callback(:after) unless command.interrupted? + command.finalize unless command.interrupted? @last_command = command end end def command_to_send