Class: Adhearsion::Translator::Asterisk::Call

Inherits:
Object
  • Object
show all
Includes:
HasGuardedHandlers
Defined in:
lib/adhearsion/translator/asterisk/call.rb

Constant Summary collapse

InvalidCommandError =
Class.new Error
OUTBOUND_CHANNEL_MATCH =
/.* <(?<channel>.*)>/.freeze
HANGUP_CAUSE_TO_END_REASON =
Hash.new { :error }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel, translator, ami_client, connection, agi_env = nil, id = nil) ⇒ Call

Returns a new instance of Call.



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/adhearsion/translator/asterisk/call.rb', line 28

def initialize(channel, translator, ami_client, connection, agi_env = nil, id = nil)
  @channel, @translator, @ami_client, @connection = channel, translator, ami_client, connection
  @agi_env = agi_env || {}
  @id = id || Adhearsion.new_uuid
  @components = {}
  @answered = false
  @pending_joins = {}
  @progress_sent = false
  @block_commands = false
  @channel_variables = {}
  @hangup_cause = nil
end

Instance Attribute Details

#agi_envObject (readonly)

Returns the value of attribute agi_env.



16
17
18
# File 'lib/adhearsion/translator/asterisk/call.rb', line 16

def agi_env
  @agi_env
end

#channelObject

Returns the value of attribute channel.



16
17
18
# File 'lib/adhearsion/translator/asterisk/call.rb', line 16

def channel
  @channel
end

#directionObject (readonly)

Returns the value of attribute direction.



16
17
18
# File 'lib/adhearsion/translator/asterisk/call.rb', line 16

def direction
  @direction
end

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/adhearsion/translator/asterisk/call.rb', line 16

def id
  @id
end

#pending_joinsObject (readonly)

Returns the value of attribute pending_joins.



16
17
18
# File 'lib/adhearsion/translator/asterisk/call.rb', line 16

def pending_joins
  @pending_joins
end

#translatorObject (readonly)

Returns the value of attribute translator.



16
17
18
# File 'lib/adhearsion/translator/asterisk/call.rb', line 16

def translator
  @translator
end

Instance Method Details

#after(*args, &block) ⇒ Object



348
349
350
# File 'lib/adhearsion/translator/asterisk/call.rb', line 348

def after(*args, &block)
  translator.after(*args, &block)
end

#answered?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/adhearsion/translator/asterisk/call.rb', line 96

def answered?
  @answered
end

#channel_var(variable) ⇒ Object



58
59
60
# File 'lib/adhearsion/translator/asterisk/call.rb', line 58

def channel_var(variable)
  @channel_variables[variable] || fetch_channel_var(variable)
end

#component_with_id(component_id) ⇒ Object



49
50
51
# File 'lib/adhearsion/translator/asterisk/call.rb', line 49

def component_with_id(component_id)
  @components[component_id]
end

#deregister_component(id) ⇒ Object



45
46
47
# File 'lib/adhearsion/translator/asterisk/call.rb', line 45

def deregister_component(id)
  @components.delete id
end

#dial(dial_command) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/adhearsion/translator/asterisk/call.rb', line 67

def dial(dial_command)
  @direction = :outbound
  channel = dial_command.to || ''
  channel.match(OUTBOUND_CHANNEL_MATCH) { |m| channel = m[:channel] }
  params = { :async       => true,
             :context     => REDIRECT_CONTEXT,
             :exten       => REDIRECT_EXTENSION,
             :priority    => REDIRECT_PRIORITY,
             :channel     => channel,
             :callerid    => dial_command.from
           }
  params[:variable] = variable_for_headers dial_command.headers
  params[:timeout] = dial_command.timeout unless dial_command.timeout.nil?

  originate_action = Adhearsion::Rayo::Component::Asterisk::AMI::Action.new :name => 'Originate',
                                                                      :params => params
  originate_action.request!
  translator.async.execute_global_command originate_action
  dial_command.response = Adhearsion::Rayo::Ref.new uri: id
end

#execute_agi_command(command, *params) ⇒ Hash

Returns AGI result.

Returns:

  • (Hash)

    AGI result



306
307
308
309
310
311
312
313
314
315
316
# File 'lib/adhearsion/translator/asterisk/call.rb', line 306

def execute_agi_command(command, *params)
  agi = AGICommand.new Adhearsion.new_uuid, channel, command, *params
  response = Celluloid::Future.new
  register_tmp_handler :ami, [{name: 'AsyncAGI', [:[], 'SubEvent'] => 'Exec'}, {name: 'AsyncAGIExec'}], [{[:[], 'CommandID'] => agi.id}, {[:[], 'CommandId'] => agi.id}] do |event|
    response.signal Celluloid::SuccessResponse.new(nil, event)
  end
  agi.execute @ami_client
  event = response.value
  return unless event
  agi.parse_result event
end

#execute_command(command) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/adhearsion/translator/asterisk/call.rb', line 204

def execute_command(command)
  if @block_commands
    command.response = Adhearsion::ProtocolError.new.setup :item_not_found, "Could not find a call with ID #{id}", id
    return
  end
  if command.component_id
    if component = component_with_id(command.component_id)
      component.execute_command command
    else
      command.response = Adhearsion::ProtocolError.new.setup :item_not_found, "Could not find a component with ID #{command.component_id} for call #{id}", id, command.component_id
    end
  end
  case command
  when Adhearsion::Rayo::Command::Accept
    if outbound?
      command.response = true
    else
      execute_agi_command 'EXEC RINGING'
      command.response = true
    end
  when Adhearsion::Rayo::Command::Answer
    execute_agi_command 'ANSWER'
    @answered = true
    send_pb_event Adhearsion::Event::Answered.new
    command.response = true
  when Adhearsion::Rayo::Command::Hangup
    send_hangup_command
    @hangup_cause = :hangup_command
    command.response = true
  when Adhearsion::Rayo::Command::Join
    other_call = translator.call_with_id command.call_uri
    if other_call
      @pending_joins[other_call.channel] = command
      execute_agi_command 'EXEC Bridge', "#{other_call.channel},F(#{REDIRECT_CONTEXT},#{REDIRECT_EXTENSION},#{REDIRECT_PRIORITY})"
    else
      command.response = Adhearsion::ProtocolError.new.setup :service_unavailable, "Could not find join party with address #{command.call_uri}", id
    end
  when Adhearsion::Rayo::Command::Unjoin
    other_call = translator.call_with_id command.call_uri
    redirect_back other_call
    command.response = true
  when Adhearsion::Rayo::Command::Reject
    case command.reason
    when :busy
      execute_agi_command 'EXEC Busy'
    when :decline
      send_hangup_command 21
    when :error
      execute_agi_command 'EXEC Congestion'
    else
      execute_agi_command 'EXEC Congestion'
    end
    command.response = true
  when Adhearsion::Rayo::Command::Redirect
    execute_agi_command 'EXEC Transfer', command.to
    status = channel_var 'TRANSFERSTATUS'
    command.response = case status
    when 'SUCCESS'
      true
    else
      Adhearsion::ProtocolError.new.setup 'error', "TRANSFERSTATUS was #{status}", id
    end
  when Adhearsion::Rayo::Component::Asterisk::AGI::Command
    execute_component Component::Asterisk::AGICommand, command
  when Adhearsion::Rayo::Component::Output
    execute_component Component::Output, command
  when Adhearsion::Rayo::Component::Input
    execute_component Component::Input, command
  when Adhearsion::Rayo::Component::Prompt
    component_class = case command.input.recognizer
    when 'unimrcp'
      case command.output.renderer
      when 'unimrcp'
        Component::MRCPPrompt
      when 'asterisk'
        Component::MRCPNativePrompt
      else
        raise InvalidCommandError, 'Invalid recognizer/renderer combination'
      end
    else
      Component::ComposedPrompt
    end
    execute_component component_class, command
  when Adhearsion::Rayo::Component::Record
    execute_component Component::Record, command
  else
    command.response = Adhearsion::ProtocolError.new.setup 'command-not-acceptable', "Did not understand command for call #{id}", id
  end
rescue InvalidCommandError => e
  command.response = Adhearsion::ProtocolError.new.setup :invalid_command, e.message, id
rescue ChannelGoneError
  command.response = Adhearsion::ProtocolError.new.setup :item_not_found, "Could not find a call with ID #{id}", id
rescue RubyAMI::Error => e
  command.response = Adhearsion::ProtocolError.new.setup 'error', e.message, id
rescue Celluloid::DeadActorError
  command.response = Adhearsion::ProtocolError.new.setup :item_not_found, "Could not find a component with ID #{command.component_id} for call #{id}", id, command.component_id
end

#handle_hangup_event(code = nil, timestamp = nil) ⇒ Object



338
339
340
341
342
343
344
345
346
# File 'lib/adhearsion/translator/asterisk/call.rb', line 338

def handle_hangup_event(code = nil, timestamp = nil)
  code ||= 16
  reason = @hangup_cause || HANGUP_CAUSE_TO_END_REASON[code]
  @block_commands = true
  @components.each_pair do |id, component|
    component.call_ended
  end
  send_end_event reason, code, timestamp
end

#inbound?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/adhearsion/translator/asterisk/call.rb', line 92

def inbound?
  direction == :inbound
end

#logger_idObject



318
319
320
# File 'lib/adhearsion/translator/asterisk/call.rb', line 318

def logger_id
  "#{self.class}: #{id}"
end

#outbound?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/adhearsion/translator/asterisk/call.rb', line 88

def outbound?
  direction == :outbound
end

#process_ami_event(ami_event) ⇒ Object



110
111
112
113
114
115
116
117
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/adhearsion/translator/asterisk/call.rb', line 110

def process_ami_event(ami_event)
  if Asterisk.event_passes_filter?(ami_event)
    send_pb_event Adhearsion::Event::Asterisk::AMI.new(name: ami_event.name, headers: ami_event.headers)
  end

  case ami_event.name
  when 'Hangup'
    handle_hangup_event ami_event['Cause'].to_i, ami_event.best_time
  when 'AsyncAGI'
    component_for_command_id_handle ami_event

    if @answered == false && ami_event['SubEvent'] == 'Start'
      @answered = true
      send_pb_event Adhearsion::Event::Answered.new(timestamp: ami_event.best_time)
    end
  when 'AsyncAGIStart'
    component_for_command_id_handle ami_event

    if @answered == false
      @answered = true
      send_pb_event Adhearsion::Event::Answered.new(timestamp: ami_event.best_time)
    end
  when 'AsyncAGIExec', 'AsyncAGIEnd'
    component_for_command_id_handle ami_event
  when 'Newstate'
    case ami_event['ChannelState']
    when '5'
      send_pb_event Adhearsion::Event::Ringing.new(timestamp: ami_event.best_time)
    end
  when 'BridgeEnter'
    if other_call_channel = translator.bridges.delete(ami_event['BridgeUniqueid'])
      if other_call = translator.call_for_channel(other_call_channel)
        join_command   = @pending_joins.delete other_call_channel
        join_command ||= other_call.pending_joins.delete channel
        join_command.response = true if join_command

        event = Adhearsion::Event::Joined.new call_uri: other_call.id, timestamp: ami_event.best_time
        send_pb_event event

        other_call_event = Adhearsion::Event::Joined.new call_uri: id, timestamp: ami_event.best_time
        other_call_event.target_call_id = other_call.id
        translator.handle_pb_event other_call_event
      end
    else
      translator.bridges[ami_event['BridgeUniqueid']] = ami_event['Channel']
    end
   when 'BridgeLeave'
    if other_call_channel = translator.bridges.delete(ami_event['BridgeUniqueid'] + '_leave')
      if other_call = translator.call_for_channel(other_call_channel)
        event = Adhearsion::Event::Unjoined.new call_uri: other_call.id, timestamp: ami_event.best_time
        send_pb_event event

        other_call_event = Adhearsion::Event::Unjoined.new call_uri: id, timestamp: ami_event.best_time
        other_call_event.target_call_id = other_call.id
        translator.handle_pb_event other_call_event
      end
    else
      translator.bridges[ami_event['BridgeUniqueid'] + '_leave'] = ami_event['Channel']
    end
  when 'OriginateResponse'
    if ami_event['Response'] == 'Failure' && ami_event['Uniqueid'] == '<null>'
      send_end_event :error, nil, ami_event.best_time
    end
  when 'BridgeExec'
    join_command   = @pending_joins.delete ami_event['Channel1']
    join_command ||= @pending_joins.delete ami_event['Channel2']
    join_command.response = true if join_command
  when 'Bridge'
    other_call_channel = ([ami_event['Channel1'], ami_event['Channel2']] - [channel]).first
    if other_call = translator.call_for_channel(other_call_channel)
      event = case ami_event['Bridgestate']
      when 'Link'
        Adhearsion::Event::Joined.new call_uri: other_call.id, timestamp: ami_event.best_time
      when 'Unlink'
        Adhearsion::Event::Unjoined.new call_uri: other_call.id, timestamp: ami_event.best_time
      end
      send_pb_event event
    end
  when 'Unlink'
    other_call_channel = ([ami_event['Channel1'], ami_event['Channel2']] - [channel]).first
    if other_call = translator.call_for_channel(other_call_channel)
      send_pb_event Adhearsion::Event::Unjoined.new(call_uri: other_call.id, timestamp: ami_event.best_time)
    end
  when 'VarSet'
    @channel_variables[ami_event['Variable']] = ami_event['Value']
  end
  trigger_handler :ami, ami_event
end

#redirect_back(other_call = nil) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/adhearsion/translator/asterisk/call.rb', line 322

def redirect_back(other_call = nil)
  redirect_options = {
    'Channel'   => channel,
    'Exten'     => Asterisk::REDIRECT_EXTENSION,
    'Priority'  => Asterisk::REDIRECT_PRIORITY,
    'Context'   => Asterisk::REDIRECT_CONTEXT
  }
  redirect_options.merge!({
    'ExtraChannel' => other_call.channel,
    'ExtraExten'     => Asterisk::REDIRECT_EXTENSION,
    'ExtraPriority'  => Asterisk::REDIRECT_PRIORITY,
    'ExtraContext'   => Asterisk::REDIRECT_CONTEXT
  }) if other_call
  send_ami_action 'Redirect', redirect_options
end

#register_component(component) ⇒ Object



41
42
43
# File 'lib/adhearsion/translator/asterisk/call.rb', line 41

def register_component(component)
  @components[component.id] ||= component
end

#send_message(body) ⇒ Object



199
200
201
202
# File 'lib/adhearsion/translator/asterisk/call.rb', line 199

def send_message(body)
  execute_agi_command 'EXEC SendText', body
rescue
end

#send_offerObject



53
54
55
56
# File 'lib/adhearsion/translator/asterisk/call.rb', line 53

def send_offer
  @direction = :inbound
  send_pb_event offer_event
end

#send_progressObject



100
101
102
103
104
# File 'lib/adhearsion/translator/asterisk/call.rb', line 100

def send_progress
  return if answered? || outbound? || @progress_sent
  @progress_sent = true
  execute_agi_command "EXEC Progress"
end

#to_sObject Also known as: inspect



62
63
64
# File 'lib/adhearsion/translator/asterisk/call.rb', line 62

def to_s
  "#<#{self.class}:#{id} Channel: #{channel.inspect}>"
end