-
Notifications
You must be signed in to change notification settings - Fork 128
/
call_controller.rb
341 lines (296 loc) · 8.25 KB
/
call_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# encoding: utf-8
require 'countdownlatch'
%w(
dial
input
output
record
).each { |r| require "adhearsion/call_controller/#{r}" }
module Adhearsion
class CallController
include Dial
include Input
include Output
include Record
class_attribute :callbacks
self.callbacks = {:before_call => [], :after_call => [], :on_error => []}
self.callbacks.keys.each do |name|
class_eval <<-STOP
def self.#{name}(method_name = nil, &block)
callback = if method_name
lambda { send method_name }
elsif block
block
end
self.callbacks = self.callbacks.dup.tap { |cb| cb[:#{name}] += Array(callback) }
end
STOP
end
class << self
#
# Execute a call controller, allowing passing control to another controller
#
# @param [CallController] controller
#
def exec(controller)
controller.exec
end
#
# Include another module into all CallController classes
#
def mixin(mod)
include mod
end
end
# @return [Call] The call object on which the controller is executing
attr_reader :call
# @return [Hash] The controller's metadata provided at invocation
attr_reader :metadata
# @private
attr_reader :block
delegate :[], :[]=, :to => :@metadata
delegate :variables, :send_message, :to => :call
#
# Create a new instance
#
# @param [Call] call the call to operate the controller on
# @param [Hash] metadata generic key-value storage applicable to the controller
# @param block to execute on the call
#
def initialize(call, metadata = nil, &block)
@call, @metadata, @block = call, metadata || {}, block
@block_context = eval "self", @block.binding if @block
@active_components = []
end
def method_missing(method_name, *args, &block)
if @block_context
@block_context.send method_name, *args, &block
else
super
end
end
#
# Execute the controller, allowing passing control to another controller
#
def exec(controller = self)
new_controller = catch :pass_controller do
controller.execute!
nil
end
exec new_controller if new_controller
end
def bg_exec(completion_callback = nil)
Celluloid::ThreadHandle.new do
catching_standard_errors do
exec_with_callback completion_callback
end
end
end
def exec_with_callback(completion_callback = nil)
exec
ensure
completion_callback.call call if completion_callback
end
# @private
def execute!(*options)
call.async.register_controller self
execute_callbacks :before_call
run
rescue Call::Hangup, Call::ExpiredError
rescue SyntaxError, StandardError => e
Events.trigger :exception, [e, logger]
on_error e
raise
ensure
after_call
logger.debug "Finished executing controller #{self.inspect}"
end
#
# Invoke the block supplied when creating the controller
#
def run
instance_exec(&block) if block
end
#
# Invoke another controller class within this controller, returning to this context on completion.
#
# @param [Class] controller_class The class of controller to execute
# @param [Hash] metadata generic key-value storage applicable to the controller
# @return The return value of the controller's run method
#
def invoke(controller_class, metadata = nil)
controller = controller_class.new call, metadata
controller.run
end
#
# Cease execution of this controller, and pass to another.
#
# @param [Class] controller_class The class of controller to pass to
# @param [Hash] metadata generic key-value storage applicable to the controller
#
def pass(controller_class, metadata = nil)
throw :pass_controller, controller_class.new(call, metadata)
end
#
# Stop execution of all the components currently running in the controller.
#
def stop_all_components
logger.info "Stopping all controller components"
@active_components.each do |component|
begin
component.stop!
rescue Punchblock::Component::InvalidActionError
end
end
end
#
# Cease execution of this controller, including any components it is executing, and pass to another.
#
# @param [Class] controller_class The class of controller to pass to
# @param [Hash] metadata generic key-value storage applicable to the controller
#
def hard_pass(controller_class, metadata = nil)
logger.info "Hard passing with active components #{@active_components.inspect}"
stop_all_components
pass controller_class, metadata
end
# @private
def execute_callbacks(type)
self.class.callbacks[type].each do |callback|
catching_standard_errors do
instance_exec(&callback)
end
end
end
# @private
def after_call
@after_call ||= execute_callbacks :after_call
end
# @private
def on_error(e)
@error = e
@on_error ||= execute_callbacks :on_error
end
# @private
def write_and_await_response(command)
block_until_resumed
call.write_and_await_response command
if command.is_a?(Punchblock::Component::ComponentNode)
command.register_event_handler Punchblock::Event::Complete do |event|
@active_components.delete command
throw :pass
end
@active_components << command
end
end
# @private
def execute_component_and_await_completion(component)
write_and_await_response component
yield component if block_given?
complete_event = component.complete_event
raise Adhearsion::Error, [complete_event.reason.details, component.inspect].join(": ") if complete_event.reason.is_a? Punchblock::Event::Complete::Error
component
end
#
# Answer the call
#
# @see Call#answer
#
def answer(*args)
block_until_resumed
call.answer(*args)
end
#
# Hangup the call, and execute after_call callbacks
#
# @param [Hash] headers
#
def hangup(headers = nil)
block_until_resumed
call.hangup headers
raise Call::Hangup
end
#
# Reject the call
#
# @see Call#reject
#
def reject(*args)
block_until_resumed
call.reject(*args)
raise Call::Hangup
end
#
# Redirect the call to some other target
#
# @see Call#redirect
#
def redirect(*args)
block_until_resumed
call.redirect(*args)
raise Call::Hangup
end
#
# Mute the call
#
# @see Call#mute
#
def mute(*args)
block_until_resumed
call.mute(*args)
end
#
# Unmute the call
#
# @see Call#unmute
#
def unmute(*args)
block_until_resumed
call.unmute(*args)
end
#
# Join the call to another call or a mixer, and block until the call is unjoined (by hangup or otherwise).
#
# @param [Object] target See Call#join for details
# @param [Hash] options
# @option options [Boolean] :async Return immediately, without waiting for the calls to unjoin. Defaults to false.
#
# @see Call#join
#
def join(target, options = {})
block_until_resumed
async = (target.is_a?(Hash) ? target : options).delete :async
join = call.join target, options
waiter = async ? join[:joined_condition] : join[:unjoined_condition]
waiter.wait
end
alias :safely :catching_standard_errors
# @private
def block_until_resumed
instance_variable_defined?(:@pause_latch) && @pause_latch.wait
end
# @private
def pause!
@pause_latch = CountDownLatch.new 1
end
# @private
def resume!
return unless @pause_latch
@pause_latch.countdown!
@pause_latch = nil
end
# @private
def inspect
"#<#{self.class} call=#{call.alive? ? call.id : ''}, metadata=#{metadata.inspect}>"
end
def eql?(other)
other.instance_of?(self.class) && call == other.call && metadata == other.metadata
end
alias :== :eql?
def logger
call.logger
rescue Celluloid::DeadActorError
super
end
end#class
end