lib/larynx/call_handler.rb in larynx-0.1.1 vs lib/larynx/call_handler.rb in larynx-0.1.2
- old
+ new
@@ -1,6 +1,5 @@
-# FIXME interrupted commands callback can be fired out of order if new command sent on break
module Larynx
class CallHandler < EventMachine::Protocols::HeaderAndContentProtocol
include Observable
include Commands
@@ -34,20 +33,22 @@
def caller_id
@session[:caller_caller_id_number]
end
- def interrupt_command
- if @state == :executing && current_command.interruptable?
- break!
- end
- end
-
def clear_input
@input = []
end
+ def current_command
+ @queue.first
+ end
+
+ def next_command
+ @queue[1]
+ end
+
def execute(command, immediately=false)
log "Queued: #{command.name}"
if immediately
@queue.unshift command
send_next_command
@@ -57,14 +58,17 @@
command
end
def timer(name, timeout, &block)
@timers[name] = [RestartableTimer.new(timeout) {
- timer = @timers.delete(name)
- timer[1].call if timer[1]
- notify_observers :timed_out
- send_next_command if @state == :ready
+ 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
}, block]
end
def cancel_timer(name)
if timer = @timers.delete(name)
@@ -89,35 +93,31 @@
if timer = @timers[name]
timer[0].restart
end
end
- def cleanup
- break! if @state == :executing
- cancel_all_timers
- clear_observers!
- end
-
def receive_request(header, content)
@response = Response.new(header, content)
case
- when @response.reply? && !current_command.is_a?(AppCommand)
+ when @response.reply? && current_command && !current_command.is_a?(AppCommand)
log "Completed: #{current_command.name}"
finalize_command
@state = :ready
send_next_command
when @response.executing?
log "Executing: #{current_command.name}"
run_command_setup
@state = :executing
when @response.executed? && current_command
+ this_command = current_command
finalize_command
- unless interrupted?
- @state = :ready
- send_next_command
- end
+ unless this_command.interrupted?
+ current_command.fire_callback(:after) 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?
@@ -134,37 +134,42 @@
interrupt_command
notify_observers :dtmf_received, @response.body[:dtmf_digit]
send_next_command if @state == :ready
end
- def current_command
- @queue.first
+ def interrupt_command
+ if @state == :executing && current_command.interruptable?
+ current_command.interrupted = true
+ break!
+ end
end
- def interrupting?
- current_command && current_command.command == 'break'
- end
-
- def interrupted?
- last_command && last_command.command == 'break'
- end
-
def run_command_setup
current_command.fire_callback :before
end
def finalize_command
if command = @queue.shift
- command.fire_callback :after
+ command.fire_callback(:after) unless command.interrupted?
@last_command = command
end
end
+ def command_to_send
+ current_command.try(:interrupted?) ? next_command : current_command
+ end
+
def send_next_command
- if current_command
+ if command = command_to_send
@state = :sending
- send_data current_command.to_s
+ send_data command.to_s
end
+ end
+
+ def cleanup
+ break! if @state == :executing
+ cancel_all_timers
+ clear_observers!
end
def log(msg)
LARYNX_LOGGER.info msg
end