Class: Switchboard::Core
- Inherits:
-
Object
- Object
- Switchboard::Core
- Includes:
- Timeout
- Defined in:
- lib/switchboard/core.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#jacks ⇒ Object
readonly
Returns the value of attribute jacks.
-
#roster ⇒ Object
readonly
Returns the value of attribute roster.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
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.
-
#initialize(settings = Switchboard::Settings.new, spin = true, &block) ⇒ Core
constructor
A new instance of Core.
-
#on_exception(&block) ⇒ Object
Register a hook to run when the Jabber::Client encounters an exception.
-
#on_iq(&block) ⇒ Object
Register a hook to run when iq stanzas are received.
-
#on_message(&block) ⇒ Object
Register a hook to run when message stanzas are received.
-
#on_presence(&block) ⇒ Object
Register a hook to run when presence stanzas are received.
- #on_roster_loaded(&block) ⇒ Object
- #on_roster_presence(&block) ⇒ Object
- #on_roster_query(&block) ⇒ Object
- #on_roster_subscription(&block) ⇒ Object
- #on_roster_subscription_request(&block) ⇒ Object
- #on_roster_update(&block) ⇒ Object
-
#on_shutdown(&block) ⇒ Object
Register a shutdown hook.
-
#on_startup(&block) ⇒ Object
Register a startup hook.
-
#plug!(*jacks) ⇒ Object
Connect a jack to the switchboard.
-
#run! ⇒ Object
Turn the hydrant on.
Constructor Details
#initialize(settings = Switchboard::Settings.new, spin = true, &block) ⇒ Core
Returns a new instance of Core.
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 |
# File 'lib/switchboard/core.rb', line 23 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? # TODO jid may already have a resource, so account for that @client = Jabber::Client.new([settings["jid"], settings["resource"]] * "/") end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
21 22 23 |
# File 'lib/switchboard/core.rb', line 21 def client @client end |
#jacks ⇒ Object (readonly)
Returns the value of attribute jacks.
21 22 23 |
# File 'lib/switchboard/core.rb', line 21 def jacks @jacks end |
#roster ⇒ Object (readonly)
Returns the value of attribute roster.
21 22 23 |
# File 'lib/switchboard/core.rb', line 21 def roster @roster end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
21 22 23 |
# File 'lib/switchboard/core.rb', line 21 def settings @settings 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
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 |
# File 'lib/switchboard/core.rb', line 63 def defer(callback_name, timeout = 30, &block) puts "Deferring to #{callback_name}..." if debug? @deferreds[callback_name.to_sym] = Thread.new do begin timeout(timeout) do results = instance_eval(&block) send(callback_name.to_sym, results) end puts "Done with #{callback_name}." if debug? # TODO make this thread-safe @deferreds.delete(callback_name.to_sym) 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 |
#on_exception(&block) ⇒ Object
Register a hook to run when the Jabber::Client encounters an exception.
104 105 106 |
# File 'lib/switchboard/core.rb', line 104 def on_exception(&block) register_hook(:exception, &block) end |
#on_iq(&block) ⇒ Object
Register a hook to run when iq stanzas are received.
109 110 111 |
# File 'lib/switchboard/core.rb', line 109 def on_iq(&block) register_hook(:iq, &block) end |
#on_message(&block) ⇒ Object
Register a hook to run when message stanzas are received.
114 115 116 |
# File 'lib/switchboard/core.rb', line 114 def (&block) register_hook(:message, &block) end |
#on_presence(&block) ⇒ Object
Register a hook to run when presence stanzas are received.
119 120 121 |
# File 'lib/switchboard/core.rb', line 119 def on_presence(&block) register_hook(:presence, &block) end |
#on_roster_loaded(&block) ⇒ Object
139 140 141 |
# File 'lib/switchboard/core.rb', line 139 def on_roster_loaded(&block) register_hook(:roster_loaded, &block) end |
#on_roster_presence(&block) ⇒ Object
123 124 125 |
# File 'lib/switchboard/core.rb', line 123 def on_roster_presence(&block) register_hook(:roster_presence, &block) end |
#on_roster_query(&block) ⇒ Object
127 128 129 |
# File 'lib/switchboard/core.rb', line 127 def on_roster_query(&block) register_hook(:roster_query, &block) end |
#on_roster_subscription(&block) ⇒ Object
131 132 133 |
# File 'lib/switchboard/core.rb', line 131 def on_roster_subscription(&block) register_hook(:roster_subscription, &block) end |
#on_roster_subscription_request(&block) ⇒ Object
135 136 137 |
# File 'lib/switchboard/core.rb', line 135 def on_roster_subscription_request(&block) register_hook(:roster_subscription_request, &block) end |
#on_roster_update(&block) ⇒ Object
143 144 145 |
# File 'lib/switchboard/core.rb', line 143 def on_roster_update(&block) register_hook(:roster_update, &block) end |
#on_shutdown(&block) ⇒ Object
Register a shutdown hook. Hooks will be given 5 seconds to complete before moving on.
155 156 157 |
# File 'lib/switchboard/core.rb', line 155 def on_shutdown(&block) register_hook(:shutdown, &block) end |
#on_startup(&block) ⇒ Object
Register a startup hook. Hooks will be given 5 seconds to complete before moving on.
149 150 151 |
# File 'lib/switchboard/core.rb', line 149 def on_startup(&block) register_hook(:startup, &block) end |
#plug!(*jacks) ⇒ Object
Connect a jack to the switchboard
90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/switchboard/core.rb', line 90 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
Turn the hydrant on.
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/switchboard/core.rb', line 50 def run! startup if @main instance_eval(&@main) elsif loop? sleep 5 while !shutdown? end shutdown end |