Class: Punchblock::Translator::Asterisk::Call

Inherits:
Object
  • Object
show all
Includes:
Celluloid, HasGuardedHandlers, DeadActorSafety
Defined in:
lib/punchblock/translator/asterisk/call.rb

Constant Summary collapse

HANGUP_CAUSE_TO_END_REASON =
Hash.new { :error }

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DeadActorSafety

#safe_from_dead_actors

Constructor Details

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

Returns a new instance of Call.



25
26
27
28
29
30
31
32
# File 'lib/punchblock/translator/asterisk/call.rb', line 25

def initialize(channel, translator, agi_env = nil)
  @channel, @translator = channel, translator
  @agi_env = agi_env || {}
  @id, @components = Punchblock.new_uuid, {}
  @answered = false
  @pending_joins = {}
  @progress_sent = false
end

Instance Attribute Details

#agi_envObject (readonly)

Returns the value of attribute agi_env.



11
12
13
# File 'lib/punchblock/translator/asterisk/call.rb', line 11

def agi_env
  @agi_env
end

#channelObject

Returns the value of attribute channel.



11
12
13
# File 'lib/punchblock/translator/asterisk/call.rb', line 11

def channel
  @channel
end

#directionObject (readonly)

Returns the value of attribute direction.



11
12
13
# File 'lib/punchblock/translator/asterisk/call.rb', line 11

def direction
  @direction
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/punchblock/translator/asterisk/call.rb', line 11

def id
  @id
end

#pending_joinsObject (readonly)

Returns the value of attribute pending_joins.



11
12
13
# File 'lib/punchblock/translator/asterisk/call.rb', line 11

def pending_joins
  @pending_joins
end

#translatorObject (readonly)

Returns the value of attribute translator.



11
12
13
# File 'lib/punchblock/translator/asterisk/call.rb', line 11

def translator
  @translator
end

Instance Method Details

#actor_died(actor, reason) ⇒ Object



258
259
260
261
262
263
264
265
# File 'lib/punchblock/translator/asterisk/call.rb', line 258

def actor_died(actor, reason)
  return unless reason
  if id = @components.key(actor)
    @components.delete id
    complete_event = Punchblock::Event::Complete.new :component_id => id, :reason => Punchblock::Event::Complete::Error.new
    send_pb_event complete_event
  end
end

#answered?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/punchblock/translator/asterisk/call.rb', line 84

def answered?
  @answered
end

#component_with_id(component_id) ⇒ Object



38
39
40
# File 'lib/punchblock/translator/asterisk/call.rb', line 38

def component_with_id(component_id)
  @components[component_id]
end

#dial(dial_command) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/punchblock/translator/asterisk/call.rb', line 56

def dial(dial_command)
  @direction = :outbound
  channel = dial_command.to || ''
  channel.match(/.* <(?<channel>.*)>/) { |m| channel = m[:channel] }
  params = { :async       => true,
             :application => 'AGI',
             :data        => 'agi:async',
             :channel     => channel,
             :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



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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/punchblock/translator/asterisk/call.rb', line 156

def execute_command(command)
  if command.component_id
    if component = component_with_id(command.component_id)
      component.execute_command command
    else
      command.response = 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 Command::Accept
    if outbound?
      command.response = true
    else
      send_agi_action 'EXEC RINGING' do |response|
        command.response = true
      end
    end
  when Command::Answer
    send_agi_action 'ANSWER' do |response|
      command.response = true
    end
  when Command::Hangup
    send_ami_action 'Hangup', 'Channel' => channel do |response|
      command.response = true
    end
  when Command::Join
    other_call = translator.call_with_id command.call_id
    pending_joins[other_call.channel] = command
    send_agi_action 'EXEC Bridge', other_call.channel
  when Command::Unjoin
    other_call = translator.call_with_id command.call_id
    redirect_back other_call do |response|
      case response
      when RubyAMI::Error
        command.response = ProtocolError.new.setup 'error', response.message
      else
        command.response = true
      end
    end
  when Command::Reject
    rejection = case command.reason
    when :busy
      'EXEC Busy'
    when :decline
      'EXEC Busy'
    when :error
      'EXEC Congestion'
    else
      'EXEC Congestion'
    end
    send_agi_action rejection 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
  when Punchblock::Component::Record
    execute_component Component::Record, command
  else
    command.response = ProtocolError.new.setup 'command-not-acceptable', "Did not understand command for call #{id}", id
  end
end

#inbound?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/punchblock/translator/asterisk/call.rb', line 80

def inbound?
  direction == :inbound
end

#logger_idObject



238
239
240
# File 'lib/punchblock/translator/asterisk/call.rb', line 238

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

#outbound?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/punchblock/translator/asterisk/call.rb', line 76

def outbound?
  direction == :outbound
end

#process_ami_event(ami_event) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
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
# File 'lib/punchblock/translator/asterisk/call.rb', line 98

def process_ami_event(ami_event)
  send_pb_event Event::Asterisk::AMI::Event.new(:name => ami_event.name, :attributes => ami_event.headers)

  case ami_event.name
  when 'Hangup'
    @components.dup.each_pair do |id, component|
      safe_from_dead_actors do
        component.call_ended if component.alive?
      end
    end
    send_end_event HANGUP_CAUSE_TO_END_REASON[ami_event['Cause'].to_i]
  when 'AsyncAGI'
    if component = component_with_id(ami_event['CommandID'])
      component.handle_ami_event ami_event
    end
  when 'Newstate'
    case ami_event['ChannelState']
    when '5'
      send_pb_event Event::Ringing.new
    when '6'
      @answered = true
      send_pb_event Event::Answered.new
    end
  when 'OriginateResponse'
    if ami_event['Response'] == 'Failure' && ami_event['Uniqueid'] == '<null>'
      send_end_event :error
    end
  when 'BridgeExec'
    if join_command = pending_joins[ami_event['Channel2']]
      join_command.response = true
    end
  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'
        Event::Joined.new.tap do |e|
          e.call_id = other_call.id
        end
      when 'Unlink'
        Event::Unjoined.new.tap do |e|
          e.call_id = other_call.id
        end
      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)
      event = Event::Unjoined.new.tap do |e|
        e.call_id = other_call.id
      end
      send_pb_event event
    end
  end
  trigger_handler :ami, ami_event
end

#redirect_back(other_call = nil, &block) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/punchblock/translator/asterisk/call.rb', line 242

def redirect_back(other_call = nil, &block)
  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, &block
end

#register_component(component) ⇒ Object



34
35
36
# File 'lib/punchblock/translator/asterisk/call.rb', line 34

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

#send_agi_action(command, *params, &block) ⇒ Object



222
223
224
225
226
227
228
229
# File 'lib/punchblock/translator/asterisk/call.rb', line 222

def send_agi_action(command, *params, &block)
  @current_agi_command = Punchblock::Component::Asterisk::AGI::Command.new :name => command, :params => params, :target_call_id => id
  @current_agi_command.request!
  @current_agi_command.register_handler :internal, Punchblock::Event::Complete do |e|
    block.call e if block
  end
  execute_component Component::Asterisk::AGICommand, @current_agi_command, :internal => true
end

#send_ami_action(name, headers = {}, &block) ⇒ Object



231
232
233
234
235
236
# File 'lib/punchblock/translator/asterisk/call.rb', line 231

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
    translator.send_ami_action action
  end
end

#send_offerObject



42
43
44
45
# File 'lib/punchblock/translator/asterisk/call.rb', line 42

def send_offer
  @direction = :inbound
  send_pb_event offer_event
end

#send_progressObject



88
89
90
91
92
# File 'lib/punchblock/translator/asterisk/call.rb', line 88

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

#shutdownObject



47
48
49
# File 'lib/punchblock/translator/asterisk/call.rb', line 47

def shutdown
  current_actor.terminate!
end

#to_sObject Also known as: inspect



51
52
53
# File 'lib/punchblock/translator/asterisk/call.rb', line 51

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