lib/xmpp4r/robot.rb in xmpp4r-robot-0.2.3 vs lib/xmpp4r/robot.rb in xmpp4r-robot-0.3.0

- old
+ new

@@ -15,16 +15,18 @@ @errback = errback @roster = {:available => Set.new, :away => Set.new, :unavailable => Set.new} - @retry_time = Float(opts[:retry_time] || 0) + @retry_time = 1 @auto_accept_subscription = opts[:auto_accept_subscription] @roster_mutex = Mutex.new @helper_mutex = Mutex.new - @client = nil # eliminate warning + @client_mutex = Mutex.new + @client = nil # eliminate warning + @sleeping = false end def inspect "#<#{self.class.name} username=#{username.inspect}>" end @@ -39,20 +41,24 @@ # Start the robot. This is not thread safe. # # @api public def start - if @client # restart - stop - @client = nil + @client_mutex.synchronize do + @sleeping = false + if @client # restart + stop + @client = nil + end + initialize_callbacks + connect + login + available + roster + @retry_time = 1 # reset retry time after successfully connected + self end - initialize_callbacks - connect - login - available - roster - self end def stop client.close self @@ -132,17 +138,22 @@ def initialize_callbacks client.on_exception do |exp| next unless exp # why exp might be nil? errback.call(exp) if errback - next if retry_time == 0.0 + @client_mutex.synchronize do + @sleeping = true + @retry_time *= 2 - $stderr.puts "ERROR: #{exp}: #{exp.backtrace}" + - " We'll sleep for #{retry_time} seconds and retry." + $stderr.puts "ERROR: #{exp}: #{exp.backtrace}" + + " We'll sleep for #{retry_time} seconds and retry." - clear_roster_semaphore - sleep(retry_time) - start + clear_roster_semaphore + Thread.new do + sleep(retry_time) + start + end + end unless @sleeping end client.add_presence_callback do |presence| if auto_accept_subscription && presence.type == :subscribe subscribe(presence.from)