Class | Jabber::Reliable::Listener |
In: |
lib/xmpp4r/reliable.rb
|
Parent: | Object |
# File lib/xmpp4r/reliable.rb, line 49 49: def initialize(full_jid, password, config, &block) 50: @on_message_block = block 51: @full_jid = full_jid 52: @config = config 53: @password = password 54: @max_retry = config[:max_retry] || 30 55: end
# File lib/xmpp4r/reliable.rb, line 100 100: def add_exception_handler(&block) 101: @exception_handlers << block 102: end
# File lib/xmpp4r/reliable.rb, line 88 88: def run_exception_handlers(e, connection, where_failed) 89: @exception_handlers.each do |ex_handler| 90: ex_handler.call(e, connection, where_failed) 91: end 92: if where_failed == :sending 93: @message_to_send_on_reconnect = @message_now_sending 94: end 95: if where_failed != :close && !@connection.is_connected? 96: @reconnection_thread.raise(e) 97: end 98: end
TODO: test and fix situation where we get disconnected while sending but then successfully reconnect
(and make sure in such cases we resent)
# File lib/xmpp4r/reliable.rb, line 138 138: def send_message(message) 139: unless @connection 140: raise ::ArgumentError, "Can't send messages while listener is stopped. Plase 'start' the listener first." 141: end 142: retry_count = 0 143: begin 144: while(not @connection.is_connected?) 145: #wait 146: Thread.pass 147: end 148: @message_now_sending = message 149: @connection.send(message) 150: return true #true, message was sent 151: rescue => e 152: if e.is_a?(Interrupt) 153: raise e 154: end 155: if(retry_count > @max_retry.to_i) 156: Jabber::debuglog "reached max retry count on message re-send, failing" 157: raise e 158: end 159: retry_count += 1 160: Jabber::debuglog "retrying message send.." + e.inspect 161: retry 162: end 163: end
# File lib/xmpp4r/reliable.rb, line 129 129: def send_presence 130: presence_message = @config[:presence_message] 131: if presence_message && !presence_message.empty? 132: @connection.send(Jabber::Presence.new.set_show(:chat).set_status(presence_message)) 133: end 134: end
# File lib/xmpp4r/reliable.rb, line 57 57: def setup_connection 58: @connection = Connection.new(@full_jid, @config) 59: if @on_message_block 60: @connection.add_message_callback(&@on_message_block) 61: else 62: @connection.add_message_callback do |msg| 63: self.on_message(msg) 64: end 65: end 66: 67: #We could just reconnect in @connection.on_exception, 68: #but by raising into this seperate thread, we avoid growing our stack trace 69: @reconnection_thread = Thread.new do 70: first_run = true 71: begin 72: self.start unless first_run 73: loop do 74: sleep(1) 75: Thread.pass 76: end 77: rescue => e 78: first_run = false 79: retry 80: end 81: end 82: @exception_handlers = [] 83: @connection.on_exception do |e, connection, where_failed| 84: self.run_exception_handlers(e, connection, where_failed) 85: end 86: end