Class: Tweet::Monitor
- Inherits:
-
GServer
- Object
- GServer
- Tweet::Monitor
- Defined in:
- lib/tweet.rb
Overview
Listens on a network port for notifications, firing off the appropriate action (via a Notifier) when one is received.
A proper request looks like this (fields can be in any order, semicolons not required):
app "MyApplication";title "hello world"; "Hello World!";duration "2";priority "3"
A request may also include a list of requested notifier plugins, space separated, like this:
app "MyApplication";title "hello world"; "Hello World!";duration "2";notifier "MyPlugin1 MyPlugin2";priority "3"
If the server is configured to allow requested notifiers (see Monitor.allow_requested_notifiers), then it will try to use the plugins that were requested instead of the configured notifier plugins for that request.
Instance Attribute Summary collapse
-
#active_notifiers ⇒ Object
readonly
A list of the Notifier objects to send notifications to.
-
#allow_priority ⇒ Object
Don’t show (silently drop) notifications of priority under this.
-
#allow_requested_notifiers ⇒ Object
If true, the client can request a notifier plugins in its request string.
Instance Method Summary collapse
-
#activate_notifier(name) ⇒ Object
Instantiates a (registered) Notifier and adds it to the list of active notifiers for this Watcher.
-
#get_notifier(name) ⇒ Object
Instantiates a (registered) Notifier and returns it, or returns false if it failed.
-
#initialize(allow_priority = ) ⇒ Monitor
constructor
Creates a new Monitor.
-
#notify(note) ⇒ Object
Calls the Notifier#show method for each active notifier.
- #old_start ⇒ Object
- #old_stop ⇒ Object
-
#serve(client) ⇒ Object
Handles incoming TCP connections.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(allow_priority = ) ⇒ Monitor
Creates a new Monitor. Defaults to showing notifications of INFO level and higher. Change allow_priority to modify this setting either here or after the creation of the object.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/tweet.rb', line 244 def initialize(allow_priority=PRI['INFO']) super(PORT) @allow_priority = allow_priority @allow_requested_notifiers = true @active_notifiers = [] Signal.trap('INT') { puts "Tweet stopped by user interrupt."; stop } if USE_SYSLOG begin require 'syslog' @syslog = Syslog.open("tweetd"); @syslog.info('tweetd starting.') rescue LoadError end end if USE_GTK_TRAY_ICON begin set_up_gtk_tray_icon @syslog.info('gtk tray icon loaded.') if @syslog rescue LoadError @syslog.info('gtk tray icon not loaded.') if @syslog end end end |
Instance Attribute Details
#active_notifiers ⇒ Object (readonly)
A list of the Notifier objects to send notifications to.
236 237 238 |
# File 'lib/tweet.rb', line 236 def active_notifiers @active_notifiers end |
#allow_priority ⇒ Object
Don’t show (silently drop) notifications of priority under this.
233 234 235 |
# File 'lib/tweet.rb', line 233 def allow_priority @allow_priority end |
#allow_requested_notifiers ⇒ Object
If true, the client can request a notifier plugins in its request string.
239 240 241 |
# File 'lib/tweet.rb', line 239 def allow_requested_notifiers @allow_requested_notifiers end |
Instance Method Details
#activate_notifier(name) ⇒ Object
Instantiates a (registered) Notifier and adds it to the list of active notifiers for this Watcher. Returns false if it fails.
308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/tweet.rb', line 308 def activate_notifier(name) begin return false unless n = get_notifier(name) @syslog.info("activating notifier: #{n.class}") if @syslog @active_notifiers << n return true rescue Exception=>e @syslog.info("problem activating notifier: #{name} (#{e})") if @syslog end return false end |
#get_notifier(name) ⇒ Object
Instantiates a (registered) Notifier and returns it, or returns false if it failed.
322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/tweet.rb', line 322 def get_notifier(name) begin klass = eval(name) if Notifier.registered_notifiers.include? klass return klass.new else @syslog.info("notifier not registered: #{klass}") if @syslog end rescue Exception=>e @syslog.info("notifier not found: #{name} (#{e})") if @syslog end return false end |
#notify(note) ⇒ Object
Calls the Notifier#show method for each active notifier.
390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/tweet.rb', line 390 def notify(note) notifiers_to_use = [] if @allow_requested_notifiers notifiers_to_use = note.requested_notifiers.collect { |n| get_notifier(n) } end notifiers_to_use = @active_notifiers if notifiers_to_use.empty? notifiers_to_use.each do |n| next unless n and n.respond_to? :show Thread.new { n.show(note) } end end |
#old_start ⇒ Object
336 |
# File 'lib/tweet.rb', line 336 alias_method :old_start, :start |
#old_stop ⇒ Object
342 |
# File 'lib/tweet.rb', line 342 alias_method :old_stop, :stop |
#serve(client) ⇒ Object
Handles incoming TCP connections.
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
# File 'lib/tweet.rb', line 350 def serve(client) client.puts "200 Tweet #{VERSION} ready." data = client.gets.chomp unless data =~ /app "([^"]*)"/ client.puts "500 I don't know who you are." return end app = $1 unless data =~ /title "([^"]*)"/ client.puts "500 Title required." return end title = $1 unless data =~ /message "([^"]*)"/ client.puts "500 Message required." return end = $1.gsub('\n', "\n") unless data =~ /duration "([^"]*)"/ client.puts "500 Duration required." return end duration = $1 notifier = '' if data =~ /notifier "([^"]*)"/ notifier = $1 end unless data =~ /priority "([^"]*)"/ client.puts "500 Priority required." return end priority = $1.to_i client.puts "200 Request accepted." note = Note.new(title, , duration, priority, app, notifier.split(' '), :tcp) notify(note) unless priority > @allow_priority end |
#start ⇒ Object
337 338 339 340 |
# File 'lib/tweet.rb', line 337 def start Pidify.start old_start end |
#stop ⇒ Object
343 344 345 346 347 |
# File 'lib/tweet.rb', line 343 def stop @syslog.info('tweetd stopping.') if @syslog Pidify.stop old_stop end |