adhearsion-cw 1.0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. data/CHANGELOG +109 -0
  2. data/EVENTS +11 -0
  3. data/Gemfile +3 -0
  4. data/LICENSE +456 -0
  5. data/Rakefile +134 -0
  6. data/adhearsion.gemspec +174 -0
  7. data/app_generators/ahn/USAGE +5 -0
  8. data/app_generators/ahn/ahn_generator.rb +97 -0
  9. data/app_generators/ahn/templates/.ahnrc +34 -0
  10. data/app_generators/ahn/templates/Gemfile +7 -0
  11. data/app_generators/ahn/templates/README +8 -0
  12. data/app_generators/ahn/templates/Rakefile +27 -0
  13. data/app_generators/ahn/templates/components/ami_remote/ami_remote.rb +15 -0
  14. data/app_generators/ahn/templates/components/disabled/HOW_TO_ENABLE +7 -0
  15. data/app_generators/ahn/templates/components/disabled/restful_rpc/README.markdown +11 -0
  16. data/app_generators/ahn/templates/components/disabled/restful_rpc/example-client.rb +48 -0
  17. data/app_generators/ahn/templates/components/disabled/restful_rpc/restful_rpc.rb +91 -0
  18. data/app_generators/ahn/templates/components/disabled/restful_rpc/restful_rpc.yml +34 -0
  19. data/app_generators/ahn/templates/components/disabled/restful_rpc/spec/restful_rpc_spec.rb +251 -0
  20. data/app_generators/ahn/templates/components/disabled/sandbox/sandbox.rb +104 -0
  21. data/app_generators/ahn/templates/components/disabled/sandbox/sandbox.yml +2 -0
  22. data/app_generators/ahn/templates/components/disabled/stomp_gateway/README.markdown +47 -0
  23. data/app_generators/ahn/templates/components/disabled/stomp_gateway/stomp_gateway.rb +34 -0
  24. data/app_generators/ahn/templates/components/disabled/stomp_gateway/stomp_gateway.yml +12 -0
  25. data/app_generators/ahn/templates/components/disabled/xmpp_gateway/README.markdown +3 -0
  26. data/app_generators/ahn/templates/components/disabled/xmpp_gateway/xmpp_gateway.rb +11 -0
  27. data/app_generators/ahn/templates/components/disabled/xmpp_gateway/xmpp_gateway.yml +0 -0
  28. data/app_generators/ahn/templates/components/simon_game/simon_game.rb +56 -0
  29. data/app_generators/ahn/templates/config/startup.rb +74 -0
  30. data/app_generators/ahn/templates/dialplan.rb +3 -0
  31. data/app_generators/ahn/templates/events.rb +32 -0
  32. data/bin/ahn +29 -0
  33. data/bin/ahnctl +68 -0
  34. data/bin/jahn +43 -0
  35. data/examples/asterisk_manager_interface/standalone.rb +51 -0
  36. data/lib/adhearsion/cli.rb +296 -0
  37. data/lib/adhearsion/component_manager/component_tester.rb +53 -0
  38. data/lib/adhearsion/component_manager/spec_framework.rb +18 -0
  39. data/lib/adhearsion/component_manager.rb +272 -0
  40. data/lib/adhearsion/events_support.rb +84 -0
  41. data/lib/adhearsion/foundation/all.rb +15 -0
  42. data/lib/adhearsion/foundation/blank_slate.rb +3 -0
  43. data/lib/adhearsion/foundation/custom_daemonizer.rb +45 -0
  44. data/lib/adhearsion/foundation/event_socket.rb +205 -0
  45. data/lib/adhearsion/foundation/future_resource.rb +36 -0
  46. data/lib/adhearsion/foundation/metaprogramming.rb +17 -0
  47. data/lib/adhearsion/foundation/numeric.rb +13 -0
  48. data/lib/adhearsion/foundation/pseudo_guid.rb +10 -0
  49. data/lib/adhearsion/foundation/relationship_properties.rb +42 -0
  50. data/lib/adhearsion/foundation/string.rb +26 -0
  51. data/lib/adhearsion/foundation/synchronized_hash.rb +96 -0
  52. data/lib/adhearsion/foundation/thread_safety.rb +7 -0
  53. data/lib/adhearsion/host_definitions.rb +67 -0
  54. data/lib/adhearsion/initializer/asterisk.rb +87 -0
  55. data/lib/adhearsion/initializer/configuration.rb +321 -0
  56. data/lib/adhearsion/initializer/database.rb +60 -0
  57. data/lib/adhearsion/initializer/drb.rb +31 -0
  58. data/lib/adhearsion/initializer/freeswitch.rb +22 -0
  59. data/lib/adhearsion/initializer/ldap.rb +57 -0
  60. data/lib/adhearsion/initializer/rails.rb +41 -0
  61. data/lib/adhearsion/initializer/xmpp.rb +42 -0
  62. data/lib/adhearsion/initializer.rb +394 -0
  63. data/lib/adhearsion/logging.rb +92 -0
  64. data/lib/adhearsion/tasks/components.rb +32 -0
  65. data/lib/adhearsion/tasks/database.rb +5 -0
  66. data/lib/adhearsion/tasks/deprecations.rb +59 -0
  67. data/lib/adhearsion/tasks/generating.rb +20 -0
  68. data/lib/adhearsion/tasks/lint.rb +4 -0
  69. data/lib/adhearsion/tasks/testing.rb +37 -0
  70. data/lib/adhearsion/tasks.rb +17 -0
  71. data/lib/adhearsion/version.rb +35 -0
  72. data/lib/adhearsion/voip/asterisk/agi_server.rb +115 -0
  73. data/lib/adhearsion/voip/asterisk/commands.rb +1581 -0
  74. data/lib/adhearsion/voip/asterisk/config_generators/agents.conf.rb +140 -0
  75. data/lib/adhearsion/voip/asterisk/config_generators/config_generator.rb +102 -0
  76. data/lib/adhearsion/voip/asterisk/config_generators/queues.conf.rb +250 -0
  77. data/lib/adhearsion/voip/asterisk/config_generators/voicemail.conf.rb +240 -0
  78. data/lib/adhearsion/voip/asterisk/config_manager.rb +71 -0
  79. data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rb +1681 -0
  80. data/lib/adhearsion/voip/asterisk/manager_interface/ami_lexer.rl.rb +341 -0
  81. data/lib/adhearsion/voip/asterisk/manager_interface/ami_messages.rb +78 -0
  82. data/lib/adhearsion/voip/asterisk/manager_interface/ami_protocol_lexer_machine.rl +87 -0
  83. data/lib/adhearsion/voip/asterisk/manager_interface.rb +705 -0
  84. data/lib/adhearsion/voip/asterisk/special_dial_plan_managers.rb +80 -0
  85. data/lib/adhearsion/voip/asterisk/super_manager.rb +19 -0
  86. data/lib/adhearsion/voip/asterisk.rb +4 -0
  87. data/lib/adhearsion/voip/call.rb +498 -0
  88. data/lib/adhearsion/voip/call_routing.rb +64 -0
  89. data/lib/adhearsion/voip/commands.rb +9 -0
  90. data/lib/adhearsion/voip/constants.rb +39 -0
  91. data/lib/adhearsion/voip/conveniences.rb +18 -0
  92. data/lib/adhearsion/voip/dial_plan.rb +250 -0
  93. data/lib/adhearsion/voip/dsl/dialing_dsl/dialing_dsl_monkey_patches.rb +37 -0
  94. data/lib/adhearsion/voip/dsl/dialing_dsl.rb +151 -0
  95. data/lib/adhearsion/voip/dsl/dialplan/control_passing_exception.rb +27 -0
  96. data/lib/adhearsion/voip/dsl/dialplan/dispatcher.rb +124 -0
  97. data/lib/adhearsion/voip/dsl/dialplan/parser.rb +69 -0
  98. data/lib/adhearsion/voip/dsl/dialplan/thread_mixin.rb +16 -0
  99. data/lib/adhearsion/voip/dsl/numerical_string.rb +128 -0
  100. data/lib/adhearsion/voip/freeswitch/basic_connection_manager.rb +48 -0
  101. data/lib/adhearsion/voip/freeswitch/event_handler.rb +58 -0
  102. data/lib/adhearsion/voip/freeswitch/freeswitch_dialplan_command_factory.rb +129 -0
  103. data/lib/adhearsion/voip/freeswitch/inbound_connection_manager.rb +38 -0
  104. data/lib/adhearsion/voip/freeswitch/oes_server.rb +195 -0
  105. data/lib/adhearsion/voip/menu_state_machine/calculated_match.rb +80 -0
  106. data/lib/adhearsion/voip/menu_state_machine/matchers.rb +123 -0
  107. data/lib/adhearsion/voip/menu_state_machine/menu_builder.rb +57 -0
  108. data/lib/adhearsion/voip/menu_state_machine/menu_class.rb +149 -0
  109. data/lib/adhearsion/xmpp/connection.rb +61 -0
  110. data/lib/adhearsion.rb +46 -0
  111. data/lib/theatre/README.markdown +64 -0
  112. data/lib/theatre/callback_definition_loader.rb +84 -0
  113. data/lib/theatre/guid.rb +23 -0
  114. data/lib/theatre/invocation.rb +121 -0
  115. data/lib/theatre/namespace_manager.rb +153 -0
  116. data/lib/theatre/version.rb +2 -0
  117. data/lib/theatre.rb +151 -0
  118. metadata +323 -0
@@ -0,0 +1,272 @@
1
+ module Adhearsion
2
+ module Components
3
+
4
+ mattr_accessor :component_manager
5
+
6
+ class ConfigurationError < Exception; end
7
+
8
+ class ComponentManager
9
+
10
+ class << self
11
+
12
+ def scopes_valid?(*scopes)
13
+ unrecognized_scopes = (scopes.flatten - SCOPE_NAMES).map(&:inspect)
14
+ raise ArgumentError, "Unrecognized scopes #{unrecognized_scopes.to_sentence}" if unrecognized_scopes.any?
15
+ true
16
+ end
17
+
18
+ end
19
+
20
+ SCOPE_NAMES = [:dialplan, :events, :generators, :rpc, :global]
21
+
22
+ attr_reader :scopes, :lazy_config_loader
23
+ def initialize(path_to_container_directory)
24
+ @path_to_container_directory = path_to_container_directory
25
+ @scopes = SCOPE_NAMES.inject({}) do |scopes, name|
26
+ scopes[name] = Module.new
27
+ scopes
28
+ end
29
+ @lazy_config_loader = LazyConfigLoader.new(self)
30
+ end
31
+
32
+ ##
33
+ # Includes the anonymous Module created for the :global scope in Object, making its methods globally accessible.
34
+ #
35
+ def globalize_global_scope!
36
+ Object.send :include, @scopes[:global]
37
+ end
38
+
39
+ def load_components
40
+ components = Dir.glob(File.join(@path_to_container_directory + "/*")).select do |path|
41
+ File.directory?(path)
42
+ end
43
+ components.map! { |path| File.basename path }
44
+ components.each do |component|
45
+ next if component == "disabled"
46
+ component_file = File.join(@path_to_container_directory, component, 'lib', component + ".rb")
47
+ if File.exists? component_file
48
+ load_file component_file
49
+ next
50
+ end
51
+
52
+ # Try the old-style components/<component>/<component>.rb
53
+ component_file = File.join(@path_to_container_directory, component, component + ".rb")
54
+ if File.exists? component_file
55
+ load_file component_file
56
+ else
57
+ ahn_log.warn "Component directory does not contain a matching .rb file! Was expecting #{component_file.inspect}"
58
+ end
59
+ end
60
+
61
+ # Load configured system- or gem-provided components
62
+ AHN_CONFIG.components_to_load.each do |component|
63
+ require component
64
+ end
65
+
66
+ end
67
+
68
+ ##
69
+ # Loads the configuration file for a given component name.
70
+ #
71
+ # @return [Hash] The loaded YAML for the given component name. An empty Hash if no YAML file exists.
72
+ #
73
+ def configuration_for_component_named(component_name)
74
+ # Look for configuration in #{AHN_ROOT}/config/components first
75
+ if File.exists?("#{AHN_ROOT}/config/components/#{component_name}.yml")
76
+ return YAML.load_file "#{AHN_ROOT}/config/components/#{component_name}.yml"
77
+ end
78
+
79
+ # Next try the local app component directory
80
+ component_dir = File.join(@path_to_container_directory, component_name)
81
+ config_file = File.join component_dir, "#{component_name}.yml"
82
+ if File.exists?(config_file)
83
+ YAML.load_file config_file
84
+ else
85
+ # Nothing found? Return an empty hash
86
+ ahn_log.warn "No configuration found for requested component #{component_name}"
87
+ return {}
88
+ end
89
+ end
90
+
91
+ def extend_object_with(object, *scopes)
92
+ raise ArgumentError, "Must supply at least one scope!" if scopes.empty?
93
+
94
+ self.class.scopes_valid? scopes
95
+
96
+ scopes.each do |scope|
97
+ methods = @scopes[scope]
98
+ if object.kind_of?(Module)
99
+ object.send :include, methods
100
+ else
101
+ object.extend methods
102
+ end
103
+ end
104
+ object
105
+ end
106
+
107
+ def load_code(code)
108
+ load_container ComponentDefinitionContainer.load_code(code)
109
+ end
110
+
111
+ def load_file(filename)
112
+ load_container ComponentDefinitionContainer.load_file(filename)
113
+ end
114
+
115
+ def require(filename)
116
+ load_container ComponentDefinitionContainer.require(filename)
117
+ end
118
+
119
+ protected
120
+
121
+ def load_container(container)
122
+ container.constants.each do |constant_name|
123
+ constant_value = container.const_get(constant_name)
124
+ Object.const_set(constant_name, constant_value)
125
+ end
126
+ metadata = container.metaclass.send(:instance_variable_get, :@metadata)
127
+ metadata[:initialization_block].call if metadata[:initialization_block]
128
+
129
+ self.class.scopes_valid? metadata[:scopes].keys
130
+
131
+ metadata[:scopes].each_pair do |scope, method_definition_blocks|
132
+ method_definition_blocks.each do |method_definition_block|
133
+ @scopes[scope].module_eval(&method_definition_block)
134
+ end
135
+ end
136
+ container
137
+ end
138
+
139
+ class ComponentDefinitionContainer < Module
140
+
141
+ class << self
142
+ def load_code(code)
143
+ new.tap do |instance|
144
+ instance.module_eval code
145
+ end
146
+ end
147
+
148
+ def load_file(filename)
149
+ new.tap do |instance|
150
+ instance.module_eval File.read(filename), filename
151
+ end
152
+ end
153
+
154
+ def require(filename)
155
+ filename = filename + ".rb" if !(filename =~ /\.rb$/)
156
+ begin
157
+ # Try loading the exact filename first
158
+ load_file(filename)
159
+ rescue LoadError, Errno::ENOENT
160
+ end
161
+
162
+ # Next try Rubygems
163
+ filepath = get_gem_path_for(filename)
164
+ return load_file(filepath) if !filepath.nil?
165
+
166
+ # Finally try the system search path
167
+ filepath = get_system_path_for(filename)
168
+ return load_file(filepath) if !filepath.nil?
169
+
170
+ # Raise a LoadError exception if the file is still not found
171
+ raise LoadError, "File not found: #{filename}"
172
+ end
173
+
174
+ end
175
+
176
+ def initialize(&block)
177
+ # Hide our instance variables in the singleton class
178
+ metadata = {}
179
+ metaclass.send(:instance_variable_set, :@metadata, metadata)
180
+
181
+ metadata[:scopes] = ComponentManager::SCOPE_NAMES.inject({}) do |scopes, name|
182
+ scopes[name] = []
183
+ scopes
184
+ end
185
+
186
+ super
187
+
188
+ meta_def(:initialize) { raise "This object has already been instantiated. Are you sure you didn't mean initialization()?" }
189
+ end
190
+
191
+ def methods_for(*scopes, &block)
192
+ raise ArgumentError if scopes.empty?
193
+
194
+ ComponentManager.scopes_valid? scopes
195
+
196
+ metadata = metaclass.send(:instance_variable_get, :@metadata)
197
+ scopes.each { |scope| metadata[:scopes][scope] << block }
198
+ end
199
+
200
+ def initialization(&block)
201
+ # Raise an exception if the initialization block has already been set
202
+ metadata = metaclass.send(:instance_variable_get, :@metadata)
203
+ if metadata[:initialization_block]
204
+ raise "You should only have one initialization() block!"
205
+ else
206
+ metadata[:initialization_block] = block
207
+ end
208
+ end
209
+ alias initialisation initialization
210
+
211
+ protected
212
+
213
+ class << self
214
+ def self.method_added(method_name)
215
+ @methods ||= []
216
+ @methods << method_name
217
+ end
218
+
219
+ def get_gem_path_for(filename)
220
+ # Look for component files provided by rubygems
221
+ spec = Gem.searcher.find(filename)
222
+ return nil if spec.nil?
223
+ File.join(spec.full_gem_path, spec.require_path, filename)
224
+ rescue NameError
225
+ # In case Rubygems are not available
226
+ nil
227
+ end
228
+
229
+ def get_system_path_for(filename)
230
+ $:.each do |path|
231
+ filepath = File.join(path, filename)
232
+ return filepath if File.exists?(filepath)
233
+ end
234
+
235
+ # Not found? Return nil
236
+ return nil
237
+ end
238
+ end
239
+
240
+ end
241
+
242
+ class ComponentMethodDefinitionContainer < Module
243
+ class << self
244
+ def method_added(method_name)
245
+ @methods ||= []
246
+ @methods << method_name
247
+ end
248
+ end
249
+
250
+ attr_reader :scopes
251
+ def initialize(*scopes, &block)
252
+ @scopes = []
253
+ super(&block)
254
+ end
255
+
256
+ end
257
+
258
+ class LazyConfigLoader
259
+ def initialize(component_manager)
260
+ @component_manager = component_manager
261
+ end
262
+
263
+ def method_missing(component_name)
264
+ config = @component_manager.configuration_for_component_named(component_name.to_s)
265
+ (class << self; self; end).send(:define_method, component_name) { config }
266
+ config
267
+ end
268
+ end
269
+
270
+ end
271
+ end
272
+ end
@@ -0,0 +1,84 @@
1
+ %w[theatre jicksta-theatre].each do |theatre_gem_name|
2
+ # Some older versions of the master branch required the theatre gem be installed. This is deprecation logic to inform the
3
+ # user to uninstall the gem if they happened to have used theatre in the past.
4
+ begin
5
+ gem theatre_gem_name
6
+ rescue LoadError
7
+ # Good. It should not be installed.
8
+ else
9
+ abort <<-MESSAGE
10
+ It seems you have the "#{theatre_gem_name}" gem installed. As of Dec. 7th, 2008 Theatre has been rolled into Adhearsion
11
+ and will be distributed with it.
12
+
13
+ Please uninstall the gem by doing "sudo gem uninstall #{theatre_gem_name}".
14
+
15
+ You will not need to install it again in the future. Sorry for the inconvenience.
16
+ MESSAGE
17
+ end
18
+ end
19
+
20
+ require 'theatre'
21
+
22
+
23
+ module Adhearsion
24
+ module Events
25
+
26
+ DEFAULT_FRAMEWORK_EVENT_NAMESPACES = %w[
27
+ /after_initialized
28
+ /shutdown
29
+ /asterisk/manager_interface
30
+ /asterisk/before_call
31
+ /asterisk/after_call
32
+ /asterisk/hungup_call
33
+ /asterisk/failed_call
34
+ ]
35
+
36
+ class << self
37
+
38
+ def framework_theatre
39
+ defined?(@@framework_theatre) ? @@framework_theatre : reinitialize_theatre!
40
+ end
41
+
42
+ def trigger(*args)
43
+ framework_theatre.trigger(*args)
44
+ end
45
+
46
+ def trigger_immediately(*args)
47
+ framework_theatre.trigger_immediately(*args)
48
+ end
49
+
50
+ def reinitialize_theatre!
51
+ @@framework_theatre.gracefully_stop! if defined? @@framework_theatre
52
+ rescue
53
+ # Recover and reinitalize
54
+ ensure
55
+ # TODO: Extract number of threads to use from AHN_CONFIG
56
+ @@framework_theatre = Theatre::Theatre.new
57
+ DEFAULT_FRAMEWORK_EVENT_NAMESPACES.each do |namespace|
58
+ register_namespace_name namespace
59
+ end
60
+ return @@framework_theatre
61
+ end
62
+
63
+ def register_namespace_name(name)
64
+ framework_theatre.register_namespace_name name
65
+ end
66
+
67
+ def stop!
68
+ Events.trigger :shutdown
69
+ framework_theatre.graceful_stop!
70
+ framework_theatre.join
71
+ end
72
+
73
+ def register_callback(namespace, block_arg=nil, &method_block)
74
+ raise ArgumentError, "Cannot supply two blocks!" if block_arg && block_given?
75
+ block = method_block || block_arg
76
+ raise ArgumentError, "Must supply a callback!" unless block
77
+
78
+ framework_theatre.register_callback_at_namespace(namespace, block)
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,15 @@
1
+ require 'English'
2
+ require 'tmpdir'
3
+ require 'tempfile'
4
+ begin
5
+ # Try ActiveSupport >= 2.3.0
6
+ require 'active_support/all'
7
+ rescue LoadError
8
+ # Assume ActiveSupport < 2.3.0
9
+ require 'active_support'
10
+ end
11
+
12
+ # Require all other files here.
13
+ Dir.glob File.join(File.dirname(__FILE__), "*rb") do |file|
14
+ require file
15
+ end
@@ -0,0 +1,3 @@
1
+ class BlankSlate
2
+ (instance_methods.map{|m| m.to_sym} - [:instance_eval, :object_id]).each { |m| undef_method m unless m.to_s =~ /^__/ }
3
+ end
@@ -0,0 +1,45 @@
1
+ # This is largely based on the Daemonize library by Travis Whitton and
2
+ # Judson Lester. http://grub.ath.cx/daemonize. I cleaned it up a bit to
3
+ # meet Adhearsion's quality standards.
4
+ module Adhearsion
5
+ module CustomDaemonizer
6
+
7
+ # Try to fork if at all possible retrying every 5 sec if the
8
+ # maximum process limit for the system has been reached
9
+ def safefork
10
+ begin
11
+ pid = fork
12
+ return pid if pid
13
+ rescue Errno::EWOULDBLOCK
14
+ sleep 5
15
+ retry
16
+ end
17
+ end
18
+
19
+ # This method causes the current running process to become a daemon
20
+ def daemonize(log_file='/dev/null')
21
+ oldmode = 0
22
+ srand # Split rand streams between spawning and daemonized process
23
+ safefork and exit # Fork and exit from the parent
24
+
25
+ # Detach from the controlling terminal
26
+ unless sess_id = Process.setsid
27
+ raise 'Cannot detach from controlled terminal'
28
+ end
29
+
30
+ # Prevent the possibility of acquiring a controlling terminal
31
+ if oldmode.zero?
32
+ trap 'SIGHUP', 'IGNORE'
33
+ exit if pid = safefork
34
+ end
35
+
36
+ Dir.chdir "/" # Release old working directory
37
+ File.umask 0000 # Ensure sensible umask
38
+
39
+ STDIN.reopen "/dev/null"
40
+ STDOUT.reopen '/dev/null', "a"
41
+ STDERR.reopen log_file, "a"
42
+ return oldmode ? sess_id : 0
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,205 @@
1
+ ##
2
+ # EventSocket is a small abstraction of TCPSocket which causes it to behave much like an EventMachine Connection object for
3
+ # the sake of better testability. The EventMachine Connection paradigm (as well as other networking libraries such as the
4
+ # Objective-C HTTP library) uses callbacks to signal different stages of a socket's lifecycle.
5
+ #
6
+ # A handler can be registered in one of two ways: through registrations on an object yielded by the constructor or
7
+ # pre-defined on the object given as a constructor parameter. Below is an example definition which uses the block way:
8
+ #
9
+ # EventSocket.new do |handler|
10
+ # def handler.receive_data(data)
11
+ # # Do something here
12
+ # end
13
+ # def handler.disconnected
14
+ # # Do something here
15
+ # end
16
+ # def handler.connected
17
+ # # Do something here
18
+ # end
19
+ # end
20
+ #
21
+ # Note: this is also a valid way of defining block callbacks:
22
+ #
23
+ # EventSocket.new do |handler|
24
+ # handler.receive_data { |data| do_something }
25
+ # handler.disconnected { do_something }
26
+ # handler.connected { do_something }
27
+ # end
28
+ #
29
+ # and here is an example of using a handler object:
30
+ #
31
+ # class MyCallbackHandler
32
+ # def receive_data(data) end
33
+ # def connected() end
34
+ # def disconnected() end
35
+ # end
36
+ # EventSocket.new(MyCallbackHandler.new)
37
+ #
38
+ # If you wish to ask the EventSocket what state it is in, you can call the Thread-safe EventSocket#state method. The
39
+ # supported states are:
40
+ #
41
+ # - :new
42
+ # - :connected
43
+ # - :stopped
44
+ # - :connection_dropped
45
+ #
46
+ # Note: the EventSocket's state will be changed before these callbacks are executed. For example, if your "connected"
47
+ # callback queried its own EventSocket for its state, it will have already transitioned to the connected() state.
48
+ #
49
+ # Warning: If an exception occurs in your EventSocket callbacks, they will be "eaten" and never bubbled up the call stack.
50
+ # You should always wrap your callbacks in a begin/rescue clause and handle exceptions explicitly.
51
+ #
52
+ require "thread"
53
+ require "socket"
54
+
55
+ class EventSocket
56
+
57
+ class << self
58
+
59
+ ##
60
+ # Creates and returns a connected EventSocket instance.
61
+ #
62
+ def connect(*args, &block)
63
+ instance = new(*args, &block)
64
+ instance.connect!
65
+ instance
66
+ end
67
+ end
68
+
69
+ MAX_CHUNK_SIZE = 256 * 1024
70
+
71
+ def initialize(host, port, handler=nil, &block)
72
+ raise ArgumentError, "Cannot supply both a handler object and a block" if handler && block_given?
73
+ raise ArgumentError, "Must supply either a handler object or a block" if !handler && !block_given?
74
+
75
+ @state_lock = Mutex.new
76
+ @host = host
77
+ @port = port
78
+
79
+ @state = :new
80
+ @handler = handler || new_handler_from_block(&block)
81
+ end
82
+
83
+ def state
84
+ @state_lock.synchronize { @state }
85
+ end
86
+
87
+ def connect!
88
+ @state_lock.synchronize do
89
+ if @state.equal? :connected
90
+ raise ConnectionError, "Already connected!"
91
+ else
92
+ @socket = TCPSocket.new(@host, @port)
93
+ @state = :connected
94
+ end
95
+ end
96
+ @handler.connected rescue nil
97
+ @reader_thread = spawn_reader_thread
98
+ self
99
+ rescue => error
100
+ @state = :failed
101
+ raise error
102
+ end
103
+
104
+ ##
105
+ # Thread-safe implementation of write.
106
+ #
107
+ # @param [String] data Data to write
108
+ #
109
+ def send_data(data)
110
+ # Note: TCPSocket#write is intrinsically Thread-safe
111
+ @socket.write data
112
+ rescue
113
+ connection_dropped!
114
+ end
115
+ ##
116
+ # Disconnects this EventSocket and sets the state to :stopped
117
+ #
118
+ def disconnect!
119
+ @state_lock.synchronize do
120
+ @socket.close rescue nil
121
+ @state = :stopped
122
+ end
123
+ end
124
+
125
+ ##
126
+ # Joins the Thread which reads data off the socket.
127
+ #
128
+ def join
129
+ @state_lock.synchronize do
130
+ if @state.equal? :connected
131
+ @reader_thread.join
132
+ else
133
+ nil
134
+ end
135
+ end
136
+ end
137
+
138
+ def receive_data(data)
139
+ @handler.receive_data(data)
140
+ end
141
+
142
+ protected
143
+
144
+ def connection_dropped!
145
+ @state_lock.synchronize do
146
+ unless @state.equal? :connection_dropped
147
+ @socket.close rescue nil
148
+ @state = :connection_dropped
149
+ @handler.disconnected
150
+ end
151
+ end
152
+ end
153
+
154
+ def spawn_reader_thread
155
+ Thread.new(&method(:reader_loop))
156
+ end
157
+
158
+ def reader_loop
159
+ until state.equal? :stopped
160
+ data = @socket.readpartial(MAX_CHUNK_SIZE)
161
+ @handler.receive_data data
162
+ end
163
+ rescue EOFError
164
+ connection_dropped!
165
+ end
166
+
167
+ def new_handler_from_block(&handler_block)
168
+ handler = Object.new
169
+ handler.metaclass.send :attr_accessor, :set_callbacks
170
+ handler.metaclass.send :public, :set_callbacks, :set_callbacks=
171
+ handler.set_callbacks = {:receive_data => false, :disconnected => false, :connected => false }
172
+
173
+ def handler.receive_data(&block)
174
+ self.metaclass.send(:remove_method, :receive_data)
175
+ self.metaclass.send(:define_method, :receive_data) { |data| block.call data }
176
+ set_callbacks[:receive_data] = true
177
+ end
178
+ def handler.connected(&block)
179
+ self.metaclass.send(:remove_method, :connected)
180
+ self.metaclass.send(:define_method, :connected) { block.call }
181
+ set_callbacks[:connected] = true
182
+ end
183
+ def handler.disconnected(&block)
184
+ self.metaclass.send(:remove_method, :disconnected)
185
+ self.metaclass.send(:define_method, :disconnected) { block.call }
186
+ set_callbacks[:disconnected] = true
187
+ end
188
+
189
+ def handler.singleton_method_added(name)
190
+ set_callbacks[name.to_sym] = true
191
+ end
192
+
193
+ yield handler
194
+
195
+ handler.set_callbacks.each_pair do |callback_name,was_set|
196
+ handler.send(callback_name) {} unless was_set
197
+ end
198
+
199
+ handler
200
+
201
+ end
202
+
203
+ class ConnectionError < StandardError; end
204
+
205
+ end
@@ -0,0 +1,36 @@
1
+ require "thread"
2
+
3
+ class FutureResource
4
+
5
+ def initialize
6
+ @resource_lock = Monitor.new
7
+ @resource_value_blocker = @resource_lock.new_cond
8
+ end
9
+
10
+ def set_yet?
11
+ @resource_lock.synchronize { defined? @resource }
12
+ end
13
+
14
+ def resource
15
+ @resource_lock.synchronize do
16
+ @resource_value_blocker.wait unless defined? @resource
17
+ @resource
18
+ end
19
+ end
20
+
21
+ def resource=(resource)
22
+ @resource_lock.synchronize do
23
+ raise ResourceAlreadySetException if defined? @resource
24
+ @resource = resource
25
+ @resource_value_blocker.broadcast
26
+ @resource_value_blocker = nil # Don't really need it anymore.
27
+ end
28
+ end
29
+
30
+ class ResourceAlreadySetException < StandardError
31
+ def initialize
32
+ super "Cannot set this resource twice!"
33
+ end
34
+ end
35
+
36
+ end
@@ -0,0 +1,17 @@
1
+ class Object
2
+ def metaclass
3
+ class << self
4
+ self
5
+ end
6
+ end
7
+
8
+ def meta_eval(&block)
9
+ metaclass.instance_eval &block
10
+ end
11
+
12
+ def meta_def(name, &block)
13
+ meta_eval do
14
+ define_method name, &block
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ class Numeric
2
+
3
+ def digit()
4
+ ahn_log.deprecation 'Please do not use Fixnum#digit() and Fixnum#digits() in the future! These will be deprecated soon'
5
+ self
6
+ end
7
+
8
+ def digits()
9
+ ahn_log.deprecation 'Please do not use Fixnum#digit() and Fixnum#digits() in the future! These will be deprecated soon'
10
+ self
11
+ end
12
+
13
+ end
@@ -0,0 +1,10 @@
1
+ module PseudoGuidGenerator
2
+ ##
3
+ # Generates a new 128-bit Globally Unique Identifier. It is a "pseudo" in that it does not adhere to the RFC which mandates
4
+ # that a certain section be reserved for a fragment of the NIC MAC address.
5
+ def new_guid(separator="-")
6
+ [8,4,4,4,12].map { |segment_length| String.random(segment_length) }.join(separator)
7
+ end
8
+ end
9
+
10
+ include PseudoGuidGenerator