Class: Punchblock::Translator::Asterisk

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Includes:
Celluloid
Defined in:
lib/punchblock/translator/asterisk.rb,
lib/punchblock/translator/asterisk/call.rb,
lib/punchblock/translator/asterisk/component.rb,
lib/punchblock/translator/asterisk/component/input.rb,
lib/punchblock/translator/asterisk/component/output.rb,
lib/punchblock/translator/asterisk/component/asterisk.rb,
lib/punchblock/translator/asterisk/component/stop_by_redirect.rb,
lib/punchblock/translator/asterisk/component/asterisk/ami_action.rb,
lib/punchblock/translator/asterisk/component/asterisk/agi_command.rb

Defined Under Namespace

Modules: Component Classes: Call

Constant Summary

REDIRECT_CONTEXT =
'adhearsion-redirect'
REDIRECT_EXTENSION =
'1'
REDIRECT_PRIORITY =
'1'

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Asterisk) initialize(ami_client, connection, media_engine = nil)

A new instance of Asterisk



22
23
24
25
26
27
# File 'lib/punchblock/translator/asterisk.rb', line 22

def initialize(ami_client, connection, media_engine = nil)
  pb_logger.debug "Starting up..."
  @ami_client, @connection, @media_engine = ami_client, connection, media_engine
  @calls, @components, @channel_to_call_id = {}, {}, {}
  @fully_booted_count = 0
end

Instance Attribute Details

- (Object) ami_client (readonly)

Returns the value of attribute ami_client



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

def ami_client
  @ami_client
end

- (Object) calls (readonly)

Returns the value of attribute calls



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

def calls
  @calls
end

- (Object) connection (readonly)

Returns the value of attribute connection



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

def connection
  @connection
end

- (Object) media_engine (readonly)

Returns the value of attribute media_engine



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

def media_engine
  @media_engine
end

Instance Method Details

- (Object) call_for_channel(channel)



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

def call_for_channel(channel)
  call_with_id @channel_to_call_id[channel]
end

- (Object) call_with_id(call_id)



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

def call_with_id(call_id)
  @calls[call_id]
end

- (Object) component_with_id(component_id)



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

def component_with_id(component_id)
  @components[component_id]
end

- (Object) execute_call_command(command)



97
98
99
100
101
102
103
# File 'lib/punchblock/translator/asterisk.rb', line 97

def execute_call_command(command)
  if call = call_with_id(command.target_call_id)
    call.execute_command! command
  else
    command.response = ProtocolError.new.setup 'call-not-found', "Could not find a call with ID #{command.target_call_id}", command.target_call_id
  end
end

- (Object) execute_command(command, options = {})



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/punchblock/translator/asterisk.rb', line 81

def execute_command(command, options = {})
  pb_logger.trace "Executing command #{command.inspect}"
  command.request!

  command.target_call_id ||= options[:call_id]
  command.component_id ||= options[:component_id]

  if command.target_call_id
    execute_call_command command
  elsif command.component_id
    execute_component_command command
  else
    execute_global_command command
  end
end

- (Object) execute_component_command(command)



105
106
107
108
109
110
111
# File 'lib/punchblock/translator/asterisk.rb', line 105

def execute_component_command(command)
  if (component = component_with_id(command.component_id))
    component.execute_command! command
  else
    command.response = ProtocolError.new.setup 'component-not-found', "Could not find a component with ID #{command.component_id}", command.target_call_id, command.component_id
  end
end

- (Object) execute_global_command(command)



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/punchblock/translator/asterisk.rb', line 113

def execute_global_command(command)
  case command
  when Punchblock::Component::Asterisk::AMI::Action
    component = Component::Asterisk::AMIAction.new command, current_actor
    register_component component
    component.execute!
  when Punchblock::Command::Dial
    call = Call.new command.to, current_actor
    register_call call
    call.dial! command
  else
    command.response = ProtocolError.new.setup 'command-not-acceptable', "Did not understand command"
  end
end

- (Object) handle_ami_event(event)



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

def handle_ami_event(event)
  return unless event.is_a? RubyAMI::Event

  if event.name.downcase == "fullybooted"
    pb_logger.trace "Counting FullyBooted event"
    @fully_booted_count += 1
    if @fully_booted_count >= 2
      handle_pb_event Connection::Connected.new
      @fully_booted_count = 0
      run_at_fully_booted
    end
    return
  end

  handle_varset_ami_event event

  ami_dispatch_to_or_create_call event

  handle_pb_event Event::Asterisk::AMI::Event.new(:name => event.name, :attributes => event.headers)
end

- (Object) handle_pb_event(event)



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

def handle_pb_event(event)
  connection.handle_event event
end

- (Object) register_call(call)



29
30
31
32
# File 'lib/punchblock/translator/asterisk.rb', line 29

def register_call(call)
  @channel_to_call_id[call.channel] = call.id
  @calls[call.id] ||= call
end

- (Object) register_component(component)



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

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

- (Object) run_at_fully_booted



132
133
134
135
136
137
# File 'lib/punchblock/translator/asterisk.rb', line 132

def run_at_fully_booted
  send_ami_action('Command', {
    'Command' => "dialplan add extension #{REDIRECT_EXTENSION},#{REDIRECT_PRIORITY},AGI,agi:async into #{REDIRECT_CONTEXT}"
  })
  pb_logger.trace "Added extension extension #{REDIRECT_EXTENSION},#{REDIRECT_PRIORITY},AGI,agi:async into #{REDIRECT_CONTEXT}"
end

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



128
129
130
# File 'lib/punchblock/translator/asterisk.rb', line 128

def send_ami_action(name, headers = {}, &block)
  ami_client.send_action name, headers, &block
end

- (Object) shutdown



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

def shutdown
  pb_logger.debug "Shutting down"
  @calls.values.each(&:shutdown!)
  current_actor.terminate!
end