class Punchblock::Translator::Freeswitch

Attributes

calls[R]
connection[R]

Public Class Methods

new(connection) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 27
def initialize(connection)
  @connection = connection
  @calls, @components = {}, {}
  setup_handlers
end

Public Instance Methods

actor_died(actor, reason) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 146
def actor_died(actor, reason)
  return unless reason
  pb_logger.error "A linked actor (#{actor.inspect}) died due to #{reason.inspect}"
  if id = @calls.key(actor)
    @calls.delete id
    end_event = Punchblock::Event::End.new :target_call_id  => id,
                                           :reason          => :error
    handle_pb_event end_event
  end
end
call_with_id(call_id) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 41
def call_with_id(call_id)
  @calls[call_id]
end
component_with_id(component_id) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 49
def component_with_id(component_id)
  @components[component_id]
end
deregister_call(id) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 37
def deregister_call(id)
  @calls.delete id
end
execute_call_command(command) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 119
def execute_call_command(command)
  if call = call_with_id(command.target_call_id)
    call.async.execute_command command
  else
    command.response = ProtocolError.new.setup :item_not_found, "Could not find a call with ID #{command.target_call_id}", command.target_call_id
  end
end
execute_command(command, options = {}) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 104
def execute_command(command, options = {})
  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
execute_component_command(command) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 127
def execute_component_command(command)
  if (component = component_with_id(command.component_id))
    component.async.execute_command command
  else
    command.response = ProtocolError.new.setup :item_not_found, "Could not find a component with ID #{command.component_id}", command.target_call_id, command.component_id
  end
end
execute_global_command(command) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 135
def execute_global_command(command)
  case command
  when Punchblock::Command::Dial
    call = Call.new_link Punchblock.new_uuid, current_actor, nil, stream
    register_call call
    call.async.dial command
  else
    command.response = ProtocolError.new.setup 'command-not-acceptable', "Did not understand command"
  end
end
finalize() click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 87
def finalize
  @calls.values.each do |call|
    safe_from_dead_actors do
      call.terminate
    end
  end
end
handle_es_event(event) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 95
def handle_es_event(event)
  trigger_handler :es, event
end
handle_pb_event(event) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 100
def handle_pb_event(event)
  connection.handle_event event
end
register_call(call) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 33
def register_call(call)
  @calls[call.id] ||= call
end
register_component(component) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 45
def register_component(component)
  @components[component.id] ||= component
end
setup_handlers() click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 53
def setup_handlers
  register_handler :es, RubyFS::Stream::Connected do
    handle_pb_event Connection::Connected.new
    throw :halt
  end

  register_handler :es, RubyFS::Stream::Disconnected do
    throw :halt
  end

  register_handler :es, :event_name => 'CHANNEL_PARK' do |event|
    throw :pass if es_event_known_call? event
    call = Call.new event[:unique_id], current_actor, event.content.select { |k,v| k.to_s =~ /variable/ }, stream
    link call
    register_call call
    call.async.send_offer
  end

  register_handler :es, :event_name => ['CHANNEL_BRIDGE', 'CHANNEL_UNBRIDGE'], [:has_key?, :other_leg_unique_id] => true do |event|
    call = call_with_id event[:other_leg_unique_id]
    call.async.handle_es_event event if call
    throw :pass
  end

  register_handler :es, lambda { |event| es_event_known_call? event } do |event|
    call = call_with_id event[:unique_id]
    call.async.handle_es_event event
  end
end
stream() click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 83
def stream
  connection.stream
end

Private Instance Methods

es_event_known_call?(event) click to toggle source
# File lib/punchblock/translator/freeswitch.rb, line 159
def es_event_known_call?(event)
  event[:unique_id] && call_with_id(event[:unique_id])
end