aboutsummaryrefslogtreecommitdiff
path: root/lib/mauve/notifier.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mauve/notifier.rb')
-rw-r--r--lib/mauve/notifier.rb81
1 files changed, 52 insertions, 29 deletions
diff --git a/lib/mauve/notifier.rb b/lib/mauve/notifier.rb
index 2853485..14e188c 100644
--- a/lib/mauve/notifier.rb
+++ b/lib/mauve/notifier.rb
@@ -4,33 +4,45 @@ require 'mauve/notifiers/xmpp'
module Mauve
+ # The Notifier is reponsible for popping notifications off the
+ # notification_buffer run by the Mauve::Server instance. This ensures that
+ # notifications are sent in a separate thread to the main processing /
+ # updating threads, and stops notifications delaying updates.
+ #
+ #
class Notifier < MauveThread
include Singleton
- def main_loop
- #
- # Cycle through the buffer.
- #
- sz = Server.notification_buffer_size
+ # Stop the notifier thread. This just makes sure that all the
+ # notifications in the buffer have been sent before closing the XMPP
+ # connection.
+ #
+ def stop
+ super
- # Thread.current[:notification_threads] ||= []
- logger.info "Sending #{sz} alerts" if sz > 0
-
- sz.times do
- person, *args = Server.notification_pop
-
- #
- # Nil person.. that's craaazy too!
- #
- next if person.nil?
+ #
+ # Flush the queue.
+ #
+ main_loop
- person.send_alert(*args)
+ if Configuration.current.notification_methods['xmpp']
+ Configuration.current.notification_methods['xmpp'].close
end
+
end
- def start
- if Configuration.current.notification_methods['xmpp']
+ private
+
+ # This is the main loop that is executed in the thread.
+ #
+ #
+ def main_loop
+
+ #
+ # Make sure we're connected to the XMPP server if needed on every iteration.
+ #
+ if Configuration.current.notification_methods['xmpp'] and !Configuration.current.notification_methods['xmpp'].ready?
#
# Connect to XMPP server
#
@@ -41,37 +53,48 @@ module Mauve
#
# Ignore people without XMPP stanzas.
#
- next unless person.xmpp
+ next unless person.xmpp
+
+ #
+ # Can't do this unless we're ready.
+ #
+ next unless xmpp.ready?
#
# For each JID, either ensure they're on our roster, or that we're in
# that chat room.
#
jid = if xmpp.is_muc?(person.xmpp)
- xmpp.join_muc(person.xmpp)
+ xmpp.join_muc(person.xmpp)
else
xmpp.ensure_roster_and_subscription!(person.xmpp)
end
Configuration.current.people[username].xmpp = jid unless jid.nil?
end
+
end
- super
- end
+ #
+ # Cycle through the buffer.
+ #
+ sz = Server.notification_buffer_size
- def stop
- super
+ logger.info "Sending #{sz} alerts" if sz > 0
#
- # Flush the queue.
+ # Empty the buffer, one notification at a time.
#
- main_loop
+ sz.times do
+ person, *args = Server.notification_pop
+
+ #
+ # Nil person.. that's craaazy too!
+ #
+ next if person.nil?
- if Configuration.current.notification_methods['xmpp']
- Configuration.current.notification_methods['xmpp'].close
+ person.send_alert(*args)
end
-
end
end