Class: Punchblock::Translator::Freeswitch

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload
Includes:
Celluloid, HasGuardedHandlers
Defined in:
lib/punchblock/translator/freeswitch.rb,
lib/punchblock/translator/freeswitch/call.rb,
lib/punchblock/translator/freeswitch/component.rb,
lib/punchblock/translator/freeswitch/component/input.rb,
lib/punchblock/translator/freeswitch/component/output.rb,
lib/punchblock/translator/freeswitch/component/record.rb,
lib/punchblock/translator/freeswitch/component/tts_output.rb,
lib/punchblock/translator/freeswitch/component/flite_output.rb,
lib/punchblock/translator/freeswitch/component/abstract_output.rb

Defined Under Namespace

Modules: Component Classes: Call

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, media_engine = nil, default_voice = nil) ⇒ Freeswitch

Returns a new instance of Freeswitch.



21
22
23
24
25
# File 'lib/punchblock/translator/freeswitch.rb', line 21

def initialize(connection, media_engine = nil, default_voice = nil)
  @connection, @media_engine, @default_voice = connection, media_engine, default_voice
  @calls, @components = {}, {}
  setup_handlers
end

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls.



17
18
19
# File 'lib/punchblock/translator/freeswitch.rb', line 17

def calls
  @calls
end

#connectionObject (readonly)

Returns the value of attribute connection.



17
18
19
# File 'lib/punchblock/translator/freeswitch.rb', line 17

def connection
  @connection
end

#default_voiceObject (readonly)

Returns the value of attribute default_voice.



17
18
19
# File 'lib/punchblock/translator/freeswitch.rb', line 17

def default_voice
  @default_voice
end

#media_engineObject (readonly)

Returns the value of attribute media_engine.



17
18
19
# File 'lib/punchblock/translator/freeswitch.rb', line 17

def media_engine
  @media_engine
end

Instance Method Details

#actor_died(actor, reason) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/punchblock/translator/freeswitch.rb', line 135

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) ⇒ Object



35
36
37
# File 'lib/punchblock/translator/freeswitch.rb', line 35

def call_with_id(call_id)
  @calls[call_id]
end

#component_with_id(component_id) ⇒ Object



43
44
45
# File 'lib/punchblock/translator/freeswitch.rb', line 43

def component_with_id(component_id)
  @components[component_id]
end

#deregister_call(call) ⇒ Object



31
32
33
# File 'lib/punchblock/translator/freeswitch.rb', line 31

def deregister_call(call)
  @calls.delete call.id
end

#execute_call_command(command) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/punchblock/translator/freeswitch.rb', line 108

def execute_call_command(command)
  if call = call_with_id(command.target_call_id)
    call.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 = {}) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/punchblock/translator/freeswitch.rb', line 93

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) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/punchblock/translator/freeswitch.rb', line 116

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

#execute_global_command(command) ⇒ Object



124
125
126
127
128
129
130
131
132
133
# File 'lib/punchblock/translator/freeswitch.rb', line 124

def execute_global_command(command)
  case command
  when Punchblock::Command::Dial
    call = Call.new_link Punchblock.new_uuid, current_actor, nil, stream, @media_engine, @default_voice
    register_call call
    call.dial! command
  else
    command.response = ProtocolError.new.setup 'command-not-acceptable', "Did not understand command"
  end
end

#finalizeObject



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

def finalize
  @calls.values.each(&:terminate)
end

#handle_es_event(event) ⇒ Object



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

def handle_es_event(event)
  trigger_handler :es, event
end

#handle_pb_event(event) ⇒ Object



89
90
91
# File 'lib/punchblock/translator/freeswitch.rb', line 89

def handle_pb_event(event)
  connection.handle_event event
end

#register_call(call) ⇒ Object



27
28
29
# File 'lib/punchblock/translator/freeswitch.rb', line 27

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

#register_component(component) ⇒ Object



39
40
41
# File 'lib/punchblock/translator/freeswitch.rb', line 39

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

#setup_handlersObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/punchblock/translator/freeswitch.rb', line 47

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, @media_engine, @default_voice
    link call
    register_call call
    call.send_offer!
  end

  register_handler :es, [:has_key?, :other_leg_unique_id] => true do |event|
    call = call_with_id event[:other_leg_unique_id]
    call.handle_es_event! event if call
  end

  register_handler :es, lambda { |event| es_event_known_call? event } do |event|
    call = call_with_id event[:unique_id]
    call.handle_es_event! event
  end
end

#streamObject



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

def stream
  connection.stream
end