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.
- #ready? ⇒ Boolean
-
#run! ⇒ Object
Start running.
Constructor Details
#initialize(settings = Switchboard::Settings.new, spin = true, &block) ⇒ Core
Returns a new instance of Core.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/switchboard/core.rb', line 45 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 die end at_exit do die 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.
28 29 30 |
# File 'lib/switchboard/core.rb', line 28 def jacks @jacks end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
28 29 30 |
# File 'lib/switchboard/core.rb', line 28 def settings @settings end |
Class Method Details
.hook(*events) ⇒ Object
Register a hook
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/switchboard/core.rb', line 31 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
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 |
# File 'lib/switchboard/core.rb', line 85 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" shutdown! end end end |
#hook(*events) ⇒ Object
114 115 116 |
# File 'lib/switchboard/core.rb', line 114 def hook(*events) self.class.hook(*events) end |
#plug!(*jacks) ⇒ Object
Connect a jack to the switchboard
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/switchboard/core.rb', line 119 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 |
#ready? ⇒ Boolean
132 133 134 |
# File 'lib/switchboard/core.rb', line 132 def ready? @ready end |
#run! ⇒ Object
Start running.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/switchboard/core.rb', line 68 def run! @main_thread = Thread.current startup @ready = true if @main instance_eval(&@main) elsif loop? sleep 1 while !shutdown? end shutdown end |