jicksta-adhearsion 0.7.999
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +6 -0
- data/EVENTS +11 -0
- data/LICENSE +456 -0
- data/README.txt +5 -0
- data/Rakefile +120 -0
- data/adhearsion.gemspec +146 -0
- data/app_generators/ahn/USAGE +5 -0
- data/app_generators/ahn/ahn_generator.rb +87 -0
- data/app_generators/ahn/templates/.ahnrc +34 -0
- data/app_generators/ahn/templates/README +8 -0
- data/app_generators/ahn/templates/Rakefile +23 -0
- data/app_generators/ahn/templates/components/ami_remote/ami_remote.rb +15 -0
- data/app_generators/ahn/templates/components/disabled/HOW_TO_ENABLE +7 -0
- data/app_generators/ahn/templates/components/disabled/stomp_gateway/README.markdown +47 -0
- data/app_generators/ahn/templates/components/disabled/stomp_gateway/config.yml +12 -0
- data/app_generators/ahn/templates/components/disabled/stomp_gateway/stomp_gateway.rb +34 -0
- data/app_generators/ahn/templates/components/restful_rpc/README.markdown +11 -0
- data/app_generators/ahn/templates/components/restful_rpc/config.yml +34 -0
- data/app_generators/ahn/templates/components/restful_rpc/example-client.rb +48 -0
- data/app_generators/ahn/templates/components/restful_rpc/restful_rpc.rb +87 -0
- data/app_generators/ahn/templates/components/simon_game/simon_game.rb +56 -0
- data/app_generators/ahn/templates/config/startup.rb +53 -0
- data/app_generators/ahn/templates/dialplan.rb +3 -0
- data/app_generators/ahn/templates/events.rb +32 -0
- data/bin/ahn +28 -0
- data/bin/ahnctl +68 -0
- data/bin/jahn +42 -0
- data/examples/asterisk_manager_interface/standalone.rb +51 -0
- data/lib/adhearsion/cli.rb +223 -0
- data/lib/adhearsion/component_manager/spec_framework.rb +24 -0
- data/lib/adhearsion/component_manager.rb +208 -0
- data/lib/adhearsion/events_support.rb +84 -0
- data/lib/adhearsion/foundation/all.rb +9 -0
- data/lib/adhearsion/foundation/blank_slate.rb +5 -0
- data/lib/adhearsion/foundation/custom_daemonizer.rb +45 -0
- data/lib/adhearsion/foundation/event_socket.rb +203 -0
- data/lib/adhearsion/foundation/future_resource.rb +36 -0
- data/lib/adhearsion/foundation/global.rb +1 -0
- data/lib/adhearsion/foundation/metaprogramming.rb +17 -0
- data/lib/adhearsion/foundation/numeric.rb +13 -0
- data/lib/adhearsion/foundation/pseudo_guid.rb +10 -0
- data/lib/adhearsion/foundation/relationship_properties.rb +42 -0
- data/lib/adhearsion/foundation/string.rb +26 -0
- data/lib/adhearsion/foundation/synchronized_hash.rb +96 -0
- data/lib/adhearsion/foundation/thread_safety.rb +7 -0
- data/lib/adhearsion/host_definitions.rb +67 -0
- data/lib/adhearsion/initializer/asterisk.rb +81 -0
- data/lib/adhearsion/initializer/configuration.rb +254 -0
- data/lib/adhearsion/initializer/database.rb +49 -0
- data/lib/adhearsion/initializer/drb.rb +31 -0
- data/lib/adhearsion/initializer/freeswitch.rb +22 -0
- data/lib/adhearsion/initializer/rails.rb +40 -0
- data/lib/adhearsion/initializer.rb +373 -0
- data/lib/adhearsion/logging.rb +92 -0
- data/lib/adhearsion/tasks/database.rb +5 -0
- data/lib/adhearsion/tasks/deprecations.rb +59 -0
- data/lib/adhearsion/tasks/generating.rb +20 -0
- data/lib/adhearsion/tasks/lint.rb +4 -0
- data/lib/adhearsion/tasks/testing.rb +37 -0
- data/lib/adhearsion/tasks.rb +16 -0
- data/lib/adhearsion/version.rb +9 -0
- data/lib/adhearsion/voip/asterisk/agi_server.rb +81 -0
- data/lib/adhearsion/voip/asterisk/commands.rb +1284 -0
- data/lib/adhearsion/voip/asterisk/config_generators/agents.conf.rb +140 -0
- data/lib/adhearsion/voip/asterisk/config_generators/config_generator.rb +101 -0
- data/lib/adhearsion/voip/asterisk/config_generators/queues.conf.rb +250 -0
- data/lib/adhearsion/voip/asterisk/config_generators/voicemail.conf.rb +240 -0
- data/lib/adhearsion/voip/asterisk/config_manager.rb +71 -0
- data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rb +1754 -0
- data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rl.rb +286 -0
- data/lib/adhearsion/voip/asterisk/manager_interface/ami_messages.rb +78 -0
- data/lib/adhearsion/voip/asterisk/manager_interface/ami_protocol_lexer_machine.rl +87 -0
- data/lib/adhearsion/voip/asterisk/manager_interface.rb +562 -0
- data/lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb +80 -0
- data/lib/adhearsion/voip/asterisk/super_manager.rb +19 -0
- data/lib/adhearsion/voip/asterisk.rb +4 -0
- data/lib/adhearsion/voip/call.rb +440 -0
- data/lib/adhearsion/voip/call_routing.rb +64 -0
- data/lib/adhearsion/voip/commands.rb +9 -0
- data/lib/adhearsion/voip/constants.rb +39 -0
- data/lib/adhearsion/voip/conveniences.rb +18 -0
- data/lib/adhearsion/voip/dial_plan.rb +218 -0
- data/lib/adhearsion/voip/dsl/dialing_dsl/dialing_dsl_monkey_patches.rb +37 -0
- data/lib/adhearsion/voip/dsl/dialing_dsl.rb +151 -0
- data/lib/adhearsion/voip/dsl/dialplan/control_passing_exception.rb +27 -0
- data/lib/adhearsion/voip/dsl/dialplan/dispatcher.rb +124 -0
- data/lib/adhearsion/voip/dsl/dialplan/parser.rb +71 -0
- data/lib/adhearsion/voip/dsl/dialplan/thread_mixin.rb +16 -0
- data/lib/adhearsion/voip/dsl/numerical_string.rb +117 -0
- data/lib/adhearsion/voip/freeswitch/basic_connection_manager.rb +48 -0
- data/lib/adhearsion/voip/freeswitch/event_handler.rb +58 -0
- data/lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb +129 -0
- data/lib/adhearsion/voip/freeswitch/inbound_connection_manager.rb +38 -0
- data/lib/adhearsion/voip/freeswitch/oes_server.rb +195 -0
- data/lib/adhearsion/voip/menu_state_machine/calculated_match.rb +80 -0
- data/lib/adhearsion/voip/menu_state_machine/matchers.rb +123 -0
- data/lib/adhearsion/voip/menu_state_machine/menu_builder.rb +58 -0
- data/lib/adhearsion/voip/menu_state_machine/menu_class.rb +149 -0
- data/lib/adhearsion.rb +37 -0
- data/lib/theatre/README.markdown +64 -0
- data/lib/theatre/callback_definition_loader.rb +84 -0
- data/lib/theatre/guid.rb +23 -0
- data/lib/theatre/invocation.rb +121 -0
- data/lib/theatre/namespace_manager.rb +153 -0
- data/lib/theatre/version.rb +2 -0
- data/lib/theatre.rb +151 -0
- metadata +177 -0
@@ -0,0 +1,123 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'calculated_match')
|
2
|
+
|
3
|
+
module Adhearsion
|
4
|
+
module VoIP
|
5
|
+
class MatchCalculator
|
6
|
+
|
7
|
+
class << self
|
8
|
+
|
9
|
+
def build_with_pattern(pattern, match_payload, &block)
|
10
|
+
class_for_pattern_type(pattern.class.name).new(pattern, match_payload, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def inherited(klass)
|
14
|
+
subclasses << klass
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def class_for_pattern_type(pattern_type)
|
20
|
+
sought_class_name = "Adhearsion::VoIP::#{pattern_type.camelize}MatchCalculator"
|
21
|
+
subclasses.find { |klass| klass.name == sought_class_name }
|
22
|
+
end
|
23
|
+
|
24
|
+
def subclasses
|
25
|
+
@@subclasses ||= []
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
attr_reader :pattern, :match_payload
|
31
|
+
def initialize(pattern, match_payload)
|
32
|
+
@pattern, @match_payload = pattern, match_payload
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
|
37
|
+
def new_calculated_match(options)
|
38
|
+
CalculatedMatch.new({:pattern => pattern, :match_payload => match_payload}.merge(options))
|
39
|
+
end
|
40
|
+
|
41
|
+
def coerce_to_numeric(victim)
|
42
|
+
victim.kind_of?(Numeric) ? victim : (victim.to_s =~ /^\d+$/ ? victim.to_s.to_i : nil )
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class RangeMatchCalculator < MatchCalculator
|
47
|
+
|
48
|
+
def initialize(pattern, match_payload)
|
49
|
+
raise unless pattern.first.kind_of?(Numeric) && pattern.last.kind_of?(Numeric)
|
50
|
+
super
|
51
|
+
end
|
52
|
+
|
53
|
+
def match(query)
|
54
|
+
numerical_query = coerce_to_numeric query
|
55
|
+
if numerical_query
|
56
|
+
exact_match = pattern.include?(numerical_query) ? query : nil
|
57
|
+
potential_matches = numbers_in_range_like numerical_query
|
58
|
+
potential_matches.reject! { |m| m.to_s == exact_match.to_s } if exact_match
|
59
|
+
|
60
|
+
new_calculated_match :query => query, :exact_matches => exact_match,
|
61
|
+
:potential_matches => potential_matches
|
62
|
+
else
|
63
|
+
CalculatedMatch.failed_match!(pattern, query, match_payload)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
# Returns all numbers in the range (@pattern) that +begin with+ the number given
|
70
|
+
# as the first argument.
|
71
|
+
#
|
72
|
+
# NOTE! If you're having trouble reading what this method is actually doing, it's
|
73
|
+
# effectively a much more efficient version of this:
|
74
|
+
#
|
75
|
+
# pattern.to_a.select { |x| x.to_s.starts_with? num.to_s }.flatten
|
76
|
+
#
|
77
|
+
# Huge thanks to Dave Troy (http://davetroy.blogspot.com) for this awesomely
|
78
|
+
# efficient code!
|
79
|
+
def numbers_in_range_like(num)
|
80
|
+
return (pattern === 0 ? [0] : nil) if num == 0
|
81
|
+
raise ArgumentError unless num.kind_of?(Numeric)
|
82
|
+
returning Array.new do |matches|
|
83
|
+
first, last = pattern.first, pattern.last
|
84
|
+
power = 0
|
85
|
+
while num < last
|
86
|
+
ones_count = 10**power - 1
|
87
|
+
matches.concat((([num, first].max)..[(num+ones_count), last].min).to_a)
|
88
|
+
num *= 10
|
89
|
+
power += 1
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
class FixnumMatchCalculator < MatchCalculator
|
96
|
+
def match(query)
|
97
|
+
numeric_query = coerce_to_numeric query
|
98
|
+
exact_match, potential_match = nil
|
99
|
+
if pattern == numeric_query
|
100
|
+
exact_match = pattern
|
101
|
+
elsif pattern.to_s.starts_with? query.to_s
|
102
|
+
potential_match = pattern
|
103
|
+
end
|
104
|
+
new_calculated_match :query => query, :exact_matches => exact_match, :potential_matches => potential_match
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class StringMatchCalculator < MatchCalculator
|
109
|
+
def match(query)
|
110
|
+
args = { :query => query, :exact_matches => nil,
|
111
|
+
:potential_matches => nil }
|
112
|
+
|
113
|
+
if pattern == query.to_s
|
114
|
+
args[:exact_matches] = [pattern]
|
115
|
+
elsif pattern.starts_with? query.to_s
|
116
|
+
args[:potential_matches] = [pattern]
|
117
|
+
end
|
118
|
+
|
119
|
+
new_calculated_match args
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'matchers.rb')
|
2
|
+
|
3
|
+
module Adhearsion
|
4
|
+
module VoIP
|
5
|
+
class MenuBuilder
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@patterns = []
|
9
|
+
@menu_callbacks = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def method_missing(match_payload, *patterns, &block)
|
13
|
+
name_string = match_payload.to_s
|
14
|
+
if patterns.any?
|
15
|
+
patterns.each do |pattern|
|
16
|
+
@patterns << MatchCalculator.build_with_pattern(pattern, match_payload)
|
17
|
+
end
|
18
|
+
else
|
19
|
+
raise ArgumentError, "You cannot call this method without patterns!"
|
20
|
+
end
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def weighted_match_calculators
|
25
|
+
@patterns
|
26
|
+
end
|
27
|
+
|
28
|
+
def execute_hook_for(symbol, input)
|
29
|
+
callback = @menu_callbacks[symbol]
|
30
|
+
callback.call input if callback
|
31
|
+
end
|
32
|
+
|
33
|
+
def on_invalid(&block)
|
34
|
+
raise LocalJumpError, "Must supply a block!" unless block_given?
|
35
|
+
@menu_callbacks[:invalid] = block
|
36
|
+
end
|
37
|
+
|
38
|
+
def on_premature_timeout(&block)
|
39
|
+
raise LocalJumpError, "Must supply a block!" unless block_given?
|
40
|
+
@menu_callbacks[:premature_timeout] = block
|
41
|
+
end
|
42
|
+
|
43
|
+
def on_failure(&block)
|
44
|
+
raise LocalJumpError, "Must supply a block!" unless block_given?
|
45
|
+
@menu_callbacks[:failure] = block
|
46
|
+
end
|
47
|
+
|
48
|
+
def calculate_matches_for(result)
|
49
|
+
returning CalculatedMatchCollection.new do |collection|
|
50
|
+
weighted_match_calculators.each do |pattern|
|
51
|
+
collection << pattern.match(result)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'adhearsion/voip/menu_state_machine/menu_builder'
|
2
|
+
module Adhearsion
|
3
|
+
module VoIP
|
4
|
+
class Menu
|
5
|
+
|
6
|
+
DEFAULT_MAX_NUMBER_OF_TRIES = 1
|
7
|
+
DEFAULT_TIMEOUT = 5 # seconds
|
8
|
+
|
9
|
+
relationships :menu_builder => MenuBuilder
|
10
|
+
|
11
|
+
attr_reader :builder, :timeout, :tries_count, :max_number_of_tries
|
12
|
+
def initialize(options={}, &block)
|
13
|
+
@tries_count = 0 # Counts the number of tries the menu's been executed
|
14
|
+
|
15
|
+
@timeout = options[:timeout] || DEFAULT_TIMEOUT
|
16
|
+
@max_number_of_tries = options[:tries] || DEFAULT_MAX_NUMBER_OF_TRIES
|
17
|
+
|
18
|
+
@builder = menu_builder.new
|
19
|
+
yield @builder
|
20
|
+
|
21
|
+
initialize_digit_buffer
|
22
|
+
end
|
23
|
+
|
24
|
+
def <<(other)
|
25
|
+
digit_buffer << other
|
26
|
+
end
|
27
|
+
|
28
|
+
def digit_buffer
|
29
|
+
@digit_buffer
|
30
|
+
end
|
31
|
+
|
32
|
+
def digit_buffer_string
|
33
|
+
digit_buffer.to_s
|
34
|
+
end
|
35
|
+
|
36
|
+
def digit_buffer_empty?
|
37
|
+
digit_buffer.empty?
|
38
|
+
end
|
39
|
+
|
40
|
+
def continue
|
41
|
+
raise MenuGetAnotherDigitOrTimeout if digit_buffer_empty?
|
42
|
+
|
43
|
+
calculated_matches = builder.calculate_matches_for digit_buffer_string
|
44
|
+
|
45
|
+
if calculated_matches.exact_match_count >= 1
|
46
|
+
first_exact_match = calculated_matches.exact_matches.first
|
47
|
+
if calculated_matches.potential_match_count.zero?
|
48
|
+
# Match found with no extenuating ambiguities! Go with the first exact match
|
49
|
+
menu_result_found! first_exact_match.match_payload, digit_buffer_string
|
50
|
+
else
|
51
|
+
get_another_digit_or_finish!(first_exact_match.match_payload, first_exact_match.query)
|
52
|
+
end
|
53
|
+
elsif calculated_matches.potential_match_count >= 1
|
54
|
+
get_another_digit_or_timeout!
|
55
|
+
else
|
56
|
+
invalid!
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def should_continue?
|
61
|
+
tries_count < max_number_of_tries
|
62
|
+
end
|
63
|
+
|
64
|
+
def restart!
|
65
|
+
@tries_count += 1
|
66
|
+
digit_buffer.clear!
|
67
|
+
end
|
68
|
+
|
69
|
+
def execute_invalid_hook
|
70
|
+
builder.execute_hook_for(:invalid, digit_buffer_string)
|
71
|
+
end
|
72
|
+
|
73
|
+
def execute_timeout_hook
|
74
|
+
builder.execute_hook_for(:premature_timeout, digit_buffer_string)
|
75
|
+
end
|
76
|
+
|
77
|
+
def execute_failure_hook
|
78
|
+
builder.execute_hook_for(:failure, digit_buffer_string)
|
79
|
+
end
|
80
|
+
|
81
|
+
protected
|
82
|
+
|
83
|
+
# If you're using a more complex class in subclasses, you may want to override this method in addition to the
|
84
|
+
# digit_buffer, digit_buffer_empty, and digit_buffer_string methods
|
85
|
+
def initialize_digit_buffer
|
86
|
+
@digit_buffer = ClearableStringBuffer.new
|
87
|
+
end
|
88
|
+
|
89
|
+
def invalid!
|
90
|
+
raise MenuResultInvalid
|
91
|
+
end
|
92
|
+
|
93
|
+
def menu_result_found!(match_payload, new_extension)
|
94
|
+
raise MenuResultFound.new(match_payload, new_extension)
|
95
|
+
end
|
96
|
+
|
97
|
+
def get_another_digit_or_finish!(match_payload, new_extension)
|
98
|
+
raise MenuGetAnotherDigitOrFinish.new(match_payload, new_extension)
|
99
|
+
end
|
100
|
+
|
101
|
+
def get_another_digit_or_timeout!
|
102
|
+
raise MenuGetAnotherDigitOrTimeout
|
103
|
+
end
|
104
|
+
|
105
|
+
# The superclass from which all message-like exceptions decend. It should never
|
106
|
+
# be instantiated directly.
|
107
|
+
class MenuResult < Exception; end
|
108
|
+
|
109
|
+
# Raised when the user's input matches
|
110
|
+
class MenuResultFound < MenuResult
|
111
|
+
attr_reader :match_payload, :new_extension
|
112
|
+
def initialize(match_payload, new_extension)
|
113
|
+
super()
|
114
|
+
@match_payload = match_payload
|
115
|
+
@new_extension = new_extension
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
module MenuGetAnotherDigit; end
|
120
|
+
|
121
|
+
class MenuGetAnotherDigitOrFinish < MenuResultFound
|
122
|
+
include MenuGetAnotherDigit
|
123
|
+
end
|
124
|
+
|
125
|
+
class MenuGetAnotherDigitOrTimeout < MenuResult
|
126
|
+
include MenuGetAnotherDigit
|
127
|
+
end
|
128
|
+
|
129
|
+
class MenuResultFound < MenuResult; end
|
130
|
+
|
131
|
+
# Raised when the user's input matches no patterns
|
132
|
+
class MenuResultInvalid < MenuResult; end
|
133
|
+
|
134
|
+
# For our default purposes, we need the digit_buffer to behave much like a normal String except that it should
|
135
|
+
# handle its own resetting (clearing).
|
136
|
+
class ClearableStringBuffer < String
|
137
|
+
def clear!
|
138
|
+
replace ""
|
139
|
+
end
|
140
|
+
|
141
|
+
def <<(other)
|
142
|
+
super other.to_s
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
data/lib/adhearsion.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Check the Ruby version
|
2
|
+
STDERR.puts "WARNING: You are running Adhearsion in an unsupported
|
3
|
+
version of Ruby (Ruby #{RUBY_VERSION} #{RUBY_RELEASE_DATE})!
|
4
|
+
Please upgrade to at least Ruby v1.8.5." if RUBY_VERSION < "1.8.5"
|
5
|
+
|
6
|
+
$: << File.expand_path(File.dirname(__FILE__))
|
7
|
+
|
8
|
+
require 'rubygems'
|
9
|
+
|
10
|
+
require 'adhearsion/version'
|
11
|
+
require 'adhearsion/voip/call'
|
12
|
+
require 'adhearsion/voip/dial_plan'
|
13
|
+
require 'adhearsion/voip/asterisk/special_dial_plan_managers'
|
14
|
+
require 'adhearsion/foundation/all'
|
15
|
+
require 'adhearsion/events_support'
|
16
|
+
require 'adhearsion/logging'
|
17
|
+
require 'adhearsion/component_manager'
|
18
|
+
require 'adhearsion/initializer/configuration'
|
19
|
+
require 'adhearsion/initializer'
|
20
|
+
require 'adhearsion/voip/dsl/numerical_string'
|
21
|
+
require 'adhearsion/voip/dsl/dialplan/parser'
|
22
|
+
require 'adhearsion/voip/commands'
|
23
|
+
require 'adhearsion/voip/asterisk/commands'
|
24
|
+
require 'adhearsion/voip/dsl/dialing_dsl'
|
25
|
+
require 'adhearsion/voip/call_routing'
|
26
|
+
|
27
|
+
module Adhearsion
|
28
|
+
# Sets up the Gem require path.
|
29
|
+
AHN_INSTALL_DIR = File.expand_path(File.dirname(__FILE__) + "/..")
|
30
|
+
AHN_CONFIG = Configuration.new
|
31
|
+
|
32
|
+
##
|
33
|
+
# This Array holds all the Threads whose life matters. Adhearsion will not exit until all of these have died.
|
34
|
+
#
|
35
|
+
IMPORTANT_THREADS = []
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
Theatre
|
2
|
+
=======
|
3
|
+
|
4
|
+
Present status: stable
|
5
|
+
|
6
|
+
A library for choreographing a dynamic pool of hierarchically organized actors. This was originally extracted from the [Adhearsion](http://adhearsion.com) framework by Jay Phillips.
|
7
|
+
|
8
|
+
In the Adhearsion framework, it was necessary to develop an internal message-passing system that could work either synchronously or asynchronously. This is used by the framework itself and for framework extensions (called _components_) to talk with each other. The source of the events is completely undefined -- events could originate from within the framework out outside the framework. For example, a Message Queue such as [Stomp](http://stomp.codehaus.org) can wire incoming events into Theatre and catch events going to a particular destination so it can proxy them out to the server.
|
9
|
+
|
10
|
+
Motivations and Design Decisions
|
11
|
+
--------------------------------
|
12
|
+
|
13
|
+
* Must maintain Ruby 1.8 and JRuby compatibility
|
14
|
+
* Must be Thread-safe
|
15
|
+
* Must provide some level of transparency into the events going through it
|
16
|
+
* Must be dynamic enough to reallocate the number of triggerrs based on load
|
17
|
+
* Must help facilitate test-driven development of Actor functionality
|
18
|
+
* Must allow external persistence in case of a crash
|
19
|
+
|
20
|
+
Example
|
21
|
+
-------
|
22
|
+
|
23
|
+
Below is an example taken from Adhearsion for executing framework-level callbacks. Note: the framework treats this callback synchronously.
|
24
|
+
|
25
|
+
events.framework.asterisk.before_call.each do |event|
|
26
|
+
# Pull headers from event and respond to it here.
|
27
|
+
end
|
28
|
+
|
29
|
+
Below is an example of integration with [Stomp](http://stomp.codehaus.org/), a simple yet robust open-protocol message queue.
|
30
|
+
|
31
|
+
events.stomp.new_call.each do |event|
|
32
|
+
# Handle all events from the Stomp MQ server whose name is "new_call" (the String)
|
33
|
+
end
|
34
|
+
|
35
|
+
This will filter all events whose name is "new_call" and yield the Stomp::Message to the block.
|
36
|
+
|
37
|
+
Framework terminology
|
38
|
+
--------------------
|
39
|
+
|
40
|
+
Below are definitions of terms I use in Theatre. See the respective links for more information.
|
41
|
+
|
42
|
+
* **callback**: This is the block given to the `each` method which triggers events coming in.
|
43
|
+
* **payload**: This is the "message" sent to the Theatre and is what will ultimately be yielded to the callback
|
44
|
+
* **[Actor](http://en.wikipedia.org/wiki/Actor_model)**: This refers to concurrent responders to events in a concurrent system.
|
45
|
+
|
46
|
+
Synchronous vs. Asynchronous
|
47
|
+
----------------------------
|
48
|
+
|
49
|
+
With Theatre, all events are asynchronous with the optional ability to synchronously block until the event is scheduled, triggered, and has returned. If you wish to synchronously trigger the event, simple call `wait` on an `Invocation` object returned from `trigger` and then check the `Invocation#current_state` property for `:success` or `:error`. Optionally the `Invocation#success?` and `Invocation#error?` methods also provide more intuitive access to the finished state. If the event finished with `:success`, you may retrieve the returned value of the event Proc by calling `Invocation#returned_value`. If the event finished with `:error`, you may get the Exception with `Invocation#error`.
|
50
|
+
|
51
|
+
Because many callbacks can be registered for a particular namespace, each needing its own Invocation object, the `Theatre#trigger` method returns an Array of Invocation objects.
|
52
|
+
|
53
|
+
# We'll assume only one callback is registered and call #first on the Array of Invocations returned by #trigger
|
54
|
+
invocation = my_theatre.trigger("your/namespace/here", YourSpecialClass.new("this can be anything")).first
|
55
|
+
invocation.wait
|
56
|
+
raise invocation.error if invocation.error?
|
57
|
+
log "Actor finished with return value #{invocation.returned_value}"
|
58
|
+
|
59
|
+
Ruby 1.8 vs. Ruby 1.9 vs. JRuby
|
60
|
+
-------------------------------
|
61
|
+
|
62
|
+
Theatre was created for Ruby 1.8 because no good Actor system existed on Ruby 1.8 that met Adhearsion's needs (e.g. hierarchal with synchronous and asynchronous modes. If you wish to achieve real processor-level concurrency, use JRuby.
|
63
|
+
|
64
|
+
Presently Ruby 1.9 compatibility is not a priority but patches for compatibility will be accepted, as long as they preserve compatibility with both MRI and JRuby.
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module Theatre
|
2
|
+
|
3
|
+
##
|
4
|
+
# This class provides the a wrapper aroung which an events.rb file can be instance_eval'd.
|
5
|
+
#
|
6
|
+
class CallbackDefinitionLoader
|
7
|
+
|
8
|
+
attr_reader :theatre, :root_name
|
9
|
+
def initialize(theatre, root_name=:events)
|
10
|
+
@theatre = theatre
|
11
|
+
@root_name = root_name
|
12
|
+
|
13
|
+
create_recorder_method root_name
|
14
|
+
end
|
15
|
+
|
16
|
+
def anonymous_recorder
|
17
|
+
BlankSlateMessageRecorder.new(&method(:callback_registered))
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# Parses the given Ruby source code file and returns this object.
|
22
|
+
#
|
23
|
+
# @param [String, File] file The filename or File object for the Ruby source code file to parse.
|
24
|
+
#
|
25
|
+
def load_events_file(file)
|
26
|
+
file = File.open(file) if file.kind_of? String
|
27
|
+
instance_eval file.read, file.path
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Parses the given Ruby source code and returns this object.
|
33
|
+
#
|
34
|
+
# NOTE: Only use this if you're generating the code yourself! If you're loading a file from the filesystem, you should
|
35
|
+
# use load_events_file() since load_events_file() will properly attribute errors in the code to the file from which the
|
36
|
+
# code was loaded.
|
37
|
+
#
|
38
|
+
# @param [String] code The Ruby source code to parse
|
39
|
+
#
|
40
|
+
def load_events_code(code)
|
41
|
+
instance_eval code
|
42
|
+
self
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
##
|
48
|
+
# Immediately register the namespace and callback with the Theatre instance given to the constructor. This method is only
|
49
|
+
# called when a new BlankSlateMessageRecorder is instantiated and receives #each().
|
50
|
+
#
|
51
|
+
def callback_registered(namespaces, callback)
|
52
|
+
# Get rid of all arguments passed to the namespaces. Will support arguments in the future.
|
53
|
+
namespaces = namespaces.map { |namespace| namespace.first }
|
54
|
+
|
55
|
+
theatre.namespace_manager.register_callback_at_namespace namespaces, callback
|
56
|
+
end
|
57
|
+
|
58
|
+
def create_recorder_method(record_method_name)
|
59
|
+
(class << self; self; end).send(:alias_method, record_method_name, :anonymous_recorder)
|
60
|
+
end
|
61
|
+
|
62
|
+
class BlankSlateMessageRecorder
|
63
|
+
|
64
|
+
(instance_methods - %w[__send__ __id__]).each { |m| undef_method m }
|
65
|
+
|
66
|
+
def initialize(¬ify_on_completion)
|
67
|
+
@notify_on_completion = notify_on_completion
|
68
|
+
@namespaces = []
|
69
|
+
end
|
70
|
+
|
71
|
+
def method_missing(*method_name_and_args)
|
72
|
+
raise ArgumentError, "Supplying a block is not supported" if block_given?
|
73
|
+
@namespaces << method_name_and_args
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
def each(&callback)
|
78
|
+
@notify_on_completion.call(@namespaces, callback)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
end
|
data/lib/theatre/guid.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Right now Adhearsion also defines this method. The eventual solution will be to extract the Adhearsion features on which
|
2
|
+
# Theatre depends and make that a dependent library.
|
3
|
+
|
4
|
+
unless respond_to? :new_guid
|
5
|
+
|
6
|
+
def random_character
|
7
|
+
case random_digit = rand(62)
|
8
|
+
when 0...10 : random_digit.to_s
|
9
|
+
when 10...36 : (random_digit + 55).chr
|
10
|
+
when 36...62 : (random_digit + 61).chr
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def random_string(length_of_string=8)
|
15
|
+
Array.new(length_of_string) { random_character }.join
|
16
|
+
end
|
17
|
+
|
18
|
+
# This GUID implementation doesn't adhere to the RFC which wants to make certain segments based on the MAC address of a
|
19
|
+
# network interface card and other wackiness. It's sufficiently random for our needs.
|
20
|
+
def new_guid
|
21
|
+
[8,4,4,4,12].map { |segment_length| random_string(segment_length) }.join('-')
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'theatre/guid'
|
2
|
+
require 'thread'
|
3
|
+
require 'monitor'
|
4
|
+
|
5
|
+
module Theatre
|
6
|
+
|
7
|
+
##
|
8
|
+
# An Invocation is an object which Theatre generates and returns from Theatre#trigger.
|
9
|
+
#
|
10
|
+
class Invocation
|
11
|
+
|
12
|
+
attr_reader :queued_time, :started_time, :finished_time, :unique_id, :callback, :namespace, :error, :returned_value
|
13
|
+
|
14
|
+
class InvalidStateError < Exception; end
|
15
|
+
|
16
|
+
##
|
17
|
+
# Create a new Invocation.
|
18
|
+
#
|
19
|
+
# @param [String] namespace The "/foo/bar/qaz" path to the namespace to which this Invocation belongs.
|
20
|
+
# @param [Proc] callback The block which should be executed by an Actor scheduler.
|
21
|
+
# @param [Object] payload The message that will be sent to the callback for processing.
|
22
|
+
#
|
23
|
+
def initialize(namespace, callback, payload=:theatre_no_payload)
|
24
|
+
raise ArgumentError, "Callback must be a Proc" unless callback.kind_of? Proc
|
25
|
+
@payload = payload
|
26
|
+
@unique_id = new_guid.freeze
|
27
|
+
@callback = callback
|
28
|
+
@current_state = :new
|
29
|
+
@state_lock = Mutex.new
|
30
|
+
|
31
|
+
# Used just to protect access to the @returned_value instance variable
|
32
|
+
@returned_value_lock = Monitor.new
|
33
|
+
|
34
|
+
# Used when wait() is called to notify all waiting threads by using a ConditionVariable
|
35
|
+
@returned_value_blocker = Monitor::ConditionVariable.new @returned_value_lock
|
36
|
+
end
|
37
|
+
|
38
|
+
def queued
|
39
|
+
with_state_lock do
|
40
|
+
raise InvalidStateError unless @current_state == :new
|
41
|
+
@current_state = :queued
|
42
|
+
@queued_time = Time.now.freeze
|
43
|
+
end
|
44
|
+
true
|
45
|
+
end
|
46
|
+
|
47
|
+
def current_state
|
48
|
+
with_state_lock { @current_state }
|
49
|
+
end
|
50
|
+
|
51
|
+
def start
|
52
|
+
with_state_lock do
|
53
|
+
raise InvalidStateError unless @current_state == :queued
|
54
|
+
@current_state = :running
|
55
|
+
end
|
56
|
+
@started_time = Time.now.freeze
|
57
|
+
|
58
|
+
begin
|
59
|
+
self.returned_value = if @payload.equal? :theatre_no_payload
|
60
|
+
@callback.call
|
61
|
+
else
|
62
|
+
@callback.call @payload
|
63
|
+
end
|
64
|
+
with_state_lock { @current_state = :success }
|
65
|
+
rescue => @error
|
66
|
+
with_state_lock { @current_state = :error }
|
67
|
+
ensure
|
68
|
+
@finished_time = Time.now.freeze
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def execution_duration
|
73
|
+
return nil unless @finished_time
|
74
|
+
@finished_time - @started_time
|
75
|
+
end
|
76
|
+
|
77
|
+
def error?
|
78
|
+
current_state.equal? :error
|
79
|
+
end
|
80
|
+
|
81
|
+
def success?
|
82
|
+
current_state.equal? :success
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# When this Invocation has been queued, started, and entered either the :success or :error state, this method will
|
87
|
+
# finally return. Until then, it blocks the Thread.
|
88
|
+
#
|
89
|
+
# @return [Object] The result of invoking this Invocation's callback
|
90
|
+
#
|
91
|
+
def wait
|
92
|
+
with_returned_value_lock { return @returned_value if defined? @returned_value }
|
93
|
+
@returned_value_blocker.wait
|
94
|
+
# Return the returned_value
|
95
|
+
with_returned_value_lock { @returned_value }
|
96
|
+
end
|
97
|
+
|
98
|
+
protected
|
99
|
+
|
100
|
+
##
|
101
|
+
# Protected setter which does some other housework when the returned value is found (such as notifying wait()ers)
|
102
|
+
#
|
103
|
+
# @param [returned_value] The value to set this returned value to.
|
104
|
+
#
|
105
|
+
def returned_value=(returned_value)
|
106
|
+
with_returned_value_lock do
|
107
|
+
@returned_value = returned_value
|
108
|
+
@returned_value_blocker.broadcast
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def with_returned_value_lock(&block)
|
113
|
+
@returned_value_lock.synchronize(&block)
|
114
|
+
end
|
115
|
+
|
116
|
+
def with_state_lock(&block)
|
117
|
+
@state_lock.synchronize(&block)
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
121
|
+
end
|