Class: Punchblock::Translator::Freeswitch

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Autoload, ActorHasGuardedHandlers
Includes:
Celluloid, HasGuardedHandlers, DeadActorSafety
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

Methods included from ActorHasGuardedHandlers

execute_guarded_handlers_on_receiver

Methods included from DeadActorSafety

#safe_from_dead_actors

Constructor Details

#initialize(connection) ⇒ Freeswitch


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

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

Instance Attribute Details

#callsObject (readonly)

Returns the value of attribute calls


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

def calls
  @calls
end

#connectionObject (readonly)

Returns the value of attribute connection


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

def connection
  @connection
end

Instance Method Details

#actor_died(actor, reason) ⇒ Object


146
147
148
149
150
151
152
153
154
155
# 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) ⇒ Object


41
42
43
# File 'lib/punchblock/translator/freeswitch.rb', line 41

def call_with_id(call_id)
  @calls[call_id]
end

#component_with_id(component_id) ⇒ Object


49
50
51
# File 'lib/punchblock/translator/freeswitch.rb', line 49

def component_with_id(component_id)
  @components[component_id]
end

#deregister_call(id) ⇒ Object


37
38
39
# File 'lib/punchblock/translator/freeswitch.rb', line 37

def deregister_call(id)
  @calls.delete id
end

#execute_call_command(command) ⇒ Object


119
120
121
122
123
124
125
# 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 = {}) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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) ⇒ Object


127
128
129
130
131
132
133
# 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) ⇒ Object


135
136
137
138
139
140
141
142
143
144
# 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

#finalizeObject


87
88
89
90
91
92
93
# 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) ⇒ Object


95
96
97
# File 'lib/punchblock/translator/freeswitch.rb', line 95

def handle_es_event(event)
  trigger_handler :es, event
end

#handle_pb_event(event) ⇒ Object


100
101
102
# File 'lib/punchblock/translator/freeswitch.rb', line 100

def handle_pb_event(event)
  connection.handle_event event
end

#register_call(call) ⇒ Object


33
34
35
# File 'lib/punchblock/translator/freeswitch.rb', line 33

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

#register_component(component) ⇒ Object


45
46
47
# File 'lib/punchblock/translator/freeswitch.rb', line 45

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

#setup_handlersObject


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/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

#streamObject


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

def stream
  connection.stream
end