Class: Switchboard::Core
- Inherits:
-
Object
- Object
- Switchboard::Core
- Includes:
- Timeout
- Defined in:
- lib/switchboard/core.rb
Instance Attribute Summary collapse
-
#jacks ⇒ Object
readonly
Returns the value of attribute jacks.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
Class Method Summary collapse
-
.hook(*events) ⇒ Object
Register a hook.
Instance Method Summary collapse
-
#defer(callback_name, timeout = 30, &block) ⇒ Object
TODO don’t start threads yet; wait until all startup hooks have been run.
- #hook(*events) ⇒ Object
-
#initialize(settings = Switchboard::Settings.new, spin = true, &block) ⇒ Core
constructor
A new instance of Core.
-
#plug!(*jacks) ⇒ Object
Connect a jack to the switchboard.
-
#run! ⇒ Object
Start running.
Constructor Details
#initialize(settings = Switchboard::Settings.new, spin = true, &block) ⇒ Core
Returns a new instance of Core.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/switchboard/core.rb', line 38 def initialize(settings = Switchboard::Settings.new, spin = true, &block) # register a handler for SIGINTs trap(:INT) do # exit on a second ^C trap(:INT) do exit end @deferreds.each do |name, deferred| puts "Killing #{name}" if debug? deferred.kill end shutdown! end @settings = settings @loop = spin @shutdown = false @deferreds = {} @main = block if block_given? end |
Instance Attribute Details
#jacks ⇒ Object (readonly)
Returns the value of attribute jacks.
21 22 23 |
# File 'lib/switchboard/core.rb', line 21 def jacks @jacks end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
21 22 23 |
# File 'lib/switchboard/core.rb', line 21 def settings @settings end |
Class Method Details
.hook(*events) ⇒ Object
Register a hook
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/switchboard/core.rb', line 24 def self.hook(*events) events.each do |event| module_eval(<<-EOS, __FILE__, __LINE__) def on_#{event}(method = nil, &block) if block_given? register_hook(:#{event}, &block) elsif method register_hook(:#{event}, method) end end EOS end end |
Instance Method Details
#defer(callback_name, timeout = 30, &block) ⇒ Object
TODO don’t start threads yet; wait until all startup hooks have been run
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 |
# File 'lib/switchboard/core.rb', line 75 def defer(callback_name, timeout = 30, &block) puts "Deferring to #{callback_name}..." if debug? @deferreds[callback_name.to_sym] = Thread.new(callback_name.to_sym) do |callback| begin timeout(timeout) do begin results = instance_eval(&block) send(callback, results) if respond_to?(callback) rescue Jabber::ServerError => e puts "Server error: #{e}" end end puts "Done with #{callback}." if debug? # TODO make this thread-safe @deferreds.delete(callback) rescue Timeout::Error puts "Deferred method timed out." rescue puts "An error occurred while running a deferred: #{$!}" puts $!.backtrace * "\n" puts "Initiating shutdown..." @shutdown = true end end end |
#hook(*events) ⇒ Object
105 106 107 |
# File 'lib/switchboard/core.rb', line 105 def hook(*events) self.class.hook(*events) end |
#plug!(*jacks) ⇒ Object
Connect a jack to the switchboard
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/switchboard/core.rb', line 110 def plug!(*jacks) @jacks ||= [] jacks.each do |jack| puts "Connecting jack: #{jack}" if debug? @jacks << jack if jack.connect(self, settings) == false puts "A jack was unable to connect. Shutting down..." shutdown(false) exit 1 end end end |
#run! ⇒ Object
Start running.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/switchboard/core.rb', line 62 def run! startup if @main instance_eval(&@main) elsif loop? sleep 5 while !shutdown? end shutdown end |