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/asterisk/ami_action.rb,
lib/punchblock/translator/asterisk/component/asterisk/agi_command.rb

Defined Under Namespace

Modules: Component Classes: Call

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ami_client, connection, media_engine = nil) ⇒ Asterisk

Returns a new instance of Asterisk.



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

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

#ami_clientObject (readonly)

Returns the value of attribute ami_client.



14
15
16
# File 'lib/punchblock/translator/asterisk.rb', line 14

def ami_client
  @ami_client
end

#callsObject (readonly)

Returns the value of attribute calls.



14
15
16
# File 'lib/punchblock/translator/asterisk.rb', line 14

def calls
  @calls
end

#connectionObject (readonly)

Returns the value of attribute connection.



14
15
16
# File 'lib/punchblock/translator/asterisk.rb', line 14

def connection
  @connection
end

#media_engineObject (readonly)

Returns the value of attribute media_engine.



14
15
16
# File 'lib/punchblock/translator/asterisk.rb', line 14

def media_engine
  @media_engine
end

Instance Method Details

#call_for_channel(channel) ⇒ Object



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

def call_for_channel(channel)
  call = call_with_id @channel_to_call_id[channel]
  pb_logger.trace "Looking up call for channel #{channel} from #{@channel_to_call_id}. Found: #{call || 'none'}"
  call
end

#call_with_id(call_id) ⇒ Object



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

def call_with_id(call_id)
  @calls[call_id]
end

#component_with_id(component_id) ⇒ Object



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

def component_with_id(component_id)
  @components[component_id]
end

#execute_call_command(command) ⇒ Object



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

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

#execute_command(command, options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/punchblock/translator/asterisk.rb', line 87

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

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

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

#execute_component_command(command) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/punchblock/translator/asterisk.rb', line 111

def execute_component_command(command)
  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}", command.call_id, command.component_id
  end
end

#execute_global_command(command) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/punchblock/translator/asterisk.rb', line 119

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 'command-not-acceptable', "Did not understand command"
  end
end

#handle_ami_event(event) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/punchblock/translator/asterisk.rb', line 52

def handle_ami_event(event)
  return unless event.is_a? RubyAMI::Event
  pb_logger.trace "Handling AMI event #{event.inspect}"
  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
    end
    return
  end

  if event.name == 'VarSet' && event['Variable'] == 'punchblock_call_id' && (call = call_with_id event['Value'])
    pb_logger.trace "Received a VarSet event indicating the full channel for call #{call}"
    @channel_to_call_id.delete call.channel
    pb_logger.trace "Removed call with old channel from channel map: #{@channel_to_call_id}"
    call.channel = event['Channel']
    register_call call
  end

  if call = call_for_channel(event['Channel'])
    pb_logger.trace "Found call by channel matching this event. Sending to call #{call.id}"
    call.process_ami_event! event
  elsif event.name.downcase == "asyncagi" && event['SubEvent'] == "Start"
    handle_async_agi_start_event event
  end

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

#handle_pb_event(event) ⇒ Object



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

def handle_pb_event(event)
  connection.handle_event event
end

#register_call(call) ⇒ Object



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

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

#register_component(component) ⇒ Object



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

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

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



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

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

#shutdownObject



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

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