Class: Punchblock::Translator::Asterisk::Call
- Includes:
- Celluloid, HasGuardedHandlers
- Defined in:
- lib/punchblock/translator/asterisk/call.rb
Constant Summary collapse
- HANGUP_CAUSE_TO_END_REASON =
Hash.new { :error }
Instance Attribute Summary collapse
-
#agi_env ⇒ Object
readonly
Returns the value of attribute agi_env.
-
#channel ⇒ Object
Returns the value of attribute channel.
-
#direction ⇒ Object
readonly
Returns the value of attribute direction.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#translator ⇒ Object
readonly
Returns the value of attribute translator.
Instance Method Summary collapse
- #answer_if_not_answered ⇒ Object
- #answered? ⇒ Boolean
- #component_with_id(component_id) ⇒ Object
- #dial(dial_command) ⇒ Object
- #execute_command(command) ⇒ Object
- #inbound? ⇒ Boolean
-
#initialize(channel, translator, agi_env = '') ⇒ Call
constructor
A new instance of Call.
- #logger_id ⇒ Object
- #outbound? ⇒ Boolean
- #process_ami_event(ami_event) ⇒ Object
- #register_component(component) ⇒ Object
- #send_agi_action(command, *params, &block) ⇒ Object
- #send_ami_action(name, headers = {}, &block) ⇒ Object
- #send_offer ⇒ Object
- #shutdown ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(channel, translator, agi_env = '') ⇒ Call
Returns a new instance of Call.
21 22 23 24 25 26 27 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 21 def initialize(channel, translator, agi_env = '') @channel, @translator = channel, translator @agi_env = parse_environment agi_env @id, @components = UUIDTools::UUID.random_create.to_s, {} @answered = false pb_logger.debug "Starting up call with channel #{channel}, id #{@id}" end |
Instance Attribute Details
#agi_env ⇒ Object (readonly)
Returns the value of attribute agi_env.
10 11 12 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 10 def agi_env @agi_env end |
#channel ⇒ Object
Returns the value of attribute channel.
10 11 12 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 10 def channel @channel end |
#direction ⇒ Object (readonly)
Returns the value of attribute direction.
10 11 12 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 10 def direction @direction end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 10 def id @id end |
#translator ⇒ Object (readonly)
Returns the value of attribute translator.
10 11 12 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 10 def translator @translator end |
Instance Method Details
#answer_if_not_answered ⇒ Object
81 82 83 84 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 81 def answer_if_not_answered return if answered? || outbound? execute_command Command::Answer.new end |
#answered? ⇒ Boolean
77 78 79 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 77 def answered? @answered end |
#component_with_id(component_id) ⇒ Object
33 34 35 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 33 def component_with_id(component_id) @components[component_id] end |
#dial(dial_command) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 51 def dial(dial_command) @direction = :outbound params = { :async => true, :application => 'AGI', :data => 'agi:async', :channel => dial_command.to, :callerid => dial_command.from, :variable => "punchblock_call_id=#{id}" } params[:timeout] = dial_command.timeout unless dial_command.timeout.nil? originate_action = Punchblock::Component::Asterisk::AMI::Action.new :name => 'Originate', :params => params originate_action.request! translator.execute_global_command! originate_action dial_command.response = Ref.new :id => id end |
#execute_command(command) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 118 def execute_command(command) pb_logger.debug "Executing command: #{command.inspect}" if command.component_id if component = component_with_id(command.component_id) component.execute_command! command else command.response = ProtocolError.new 'component-not-found', "Could not find a component with ID #{command.component_id} for call #{id}", id, command.component_id end end case command when Command::Accept if outbound? pb_logger.trace "Attempting to accept an outbound call. Skipping RINGING." command.response = true else pb_logger.trace "Attempting to accept an inbound call. Executing RINGING." send_agi_action 'EXEC RINGING' do |response| command.response = true end end when Command::Answer send_agi_action 'EXEC ANSWER' do |response| command.response = true end when Command::Hangup send_ami_action 'Hangup', 'Channel' => channel do |response| command.response = true end when Punchblock::Component::Asterisk::AGI::Command execute_component Component::Asterisk::AGICommand, command when Punchblock::Component::Output execute_component Component::Output, command when Punchblock::Component::Input execute_component Component::Input, command else command.response = ProtocolError.new 'command-not-acceptable', "Did not understand command for call #{id}", id end end |
#inbound? ⇒ Boolean
73 74 75 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 73 def inbound? direction == :inbound end |
#logger_id ⇒ Object
176 177 178 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 176 def logger_id "#{self.class}: #{id}" end |
#outbound? ⇒ Boolean
69 70 71 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 69 def outbound? direction == :outbound end |
#process_ami_event(ami_event) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 91 def process_ami_event(ami_event) pb_logger.trace "Processing AMI event #{ami_event.inspect}" case ami_event.name when 'Hangup' pb_logger.debug "Received a Hangup AMI event. Sending End event." send_end_event HANGUP_CAUSE_TO_END_REASON[ami_event['Cause'].to_i] when 'AsyncAGI' pb_logger.debug "Received an AsyncAGI event. Looking for matching AGICommand component." if component = component_with_id(ami_event['CommandID']) pb_logger.debug "Found component #{component.id} for event. Forwarding event..." component.handle_ami_event! ami_event else pb_logger.debug "Could not find component for AMI event: #{ami_event}" end when 'Newstate' pb_logger.debug "Received a Newstate AMI event with state #{ami_event['ChannelState']}: #{ami_event['ChannelStateDesc']}" case ami_event['ChannelState'] when '5' send_pb_event Event::Ringing.new when '6' @answered = true send_pb_event Event::Answered.new end end trigger_handler :ami, ami_event end |
#register_component(component) ⇒ Object
29 30 31 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 29 def register_component(component) @components[component.id] ||= component end |
#send_agi_action(command, *params, &block) ⇒ Object
157 158 159 160 161 162 163 164 165 166 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 157 def send_agi_action(command, *params, &block) pb_logger.debug "Sending AGI action #{command}" @current_agi_command = Punchblock::Component::Asterisk::AGI::Command.new :name => command, :params => params, :call_id => id @current_agi_command.request! @current_agi_command.register_handler :internal, Punchblock::Event::Complete do |e| pb_logger.debug "AGI action received complete event #{e.inspect}" block.call e end execute_component Component::Asterisk::AGICommand, @current_agi_command, :internal => true end |
#send_ami_action(name, headers = {}, &block) ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 168 def send_ami_action(name, headers = {}, &block) (name.is_a?(RubyAMI::Action) ? name : RubyAMI::Action.new(name, headers, &block)).tap do |action| @current_ami_action = action pb_logger.debug "Sending AMI action #{action.inspect}" translator.send_ami_action! action end end |
#send_offer ⇒ Object
37 38 39 40 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 37 def send_offer @direction = :inbound send_pb_event offer_event end |
#shutdown ⇒ Object
42 43 44 45 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 42 def shutdown pb_logger.debug "Shutting down" current_actor.terminate! end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/punchblock/translator/asterisk/call.rb', line 47 def to_s "#<#{self.class}:#{id} Channel: #{channel.inspect}>" end |