Class: Jabber::Reliable::Connection

Inherits:
Client show all
Defined in:
lib/xmpp4r/reliable.rb

Constant Summary

Constants inherited from Stream

Stream::CONNECTED, Stream::DISCONNECTED

Instance Attribute Summary

Attributes inherited from Client

#jid

Attributes inherited from Connection

#allow_tls, #features_timeout, #host, #keepalive_interval, #port, #ssl_capath, #ssl_verifycb, #use_ssl

Attributes inherited from Stream

#fd, #processing, #status

Instance Method Summary collapse

Methods inherited from Client

#auth, #auth_anonymous, #auth_anonymous_sasl, #auth_nonsasl, #auth_sasl, #bind, #close, #password=, #register, #register_info, #remove_registration, #restart, #start, #supports_anonymous?, #unbind

Methods inherited from Connection

#accept_features, #close!, #is_tls?, #start, #starttls

Methods inherited from Stream

#add_iq_callback, #add_message_callback, #add_presence_callback, #add_stanza_callback, #add_xml_callback, #close, #close!, #delete_iq_callback, #delete_message_callback, #delete_presence_callback, #delete_stanza_callback, #delete_xml_callback, #iq_callbacks, #is_connected?, #is_disconnected?, #message_callbacks, #on_exception, #parse_failure, #parser_end, #presence_callbacks, #receive, #send, #send_data, #send_with_id, #stanza_callbacks, #start, #stop, #xml_callbacks

Constructor Details

#initialize(full_jid, config) ⇒ Connection

Returns a new instance of Connection.



7
8
9
10
11
12
13
14
15
16
# File 'lib/xmpp4r/reliable.rb', line 7

def initialize(full_jid, config)
  super(full_jid)
  @servers = config[:servers]
  @port = config[:port] || 5222
  @max_retry = config[:max_retry] || 30
  @retry_sleep = config[:retry_sleep] || 2
  if(@servers.nil? or @servers.empty?)
    @servers = [@jid.domain]
  end
end

Instance Method Details

#connectObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xmpp4r/reliable.rb', line 18

def connect
  retry_count = 0
  server_to_use = nil
  server_pool = @servers.dup.sort{ rand <=> rand }
  begin
    server_to_use = server_pool.shift
    server_pool.push(server_to_use)

    Jabber::debuglog "timeout will be: #{@retry_sleep.to_f}"
    Timeout.timeout(@retry_sleep.to_f){
      Jabber::debuglog "trying to connect to #{server_to_use}"
      super(server_to_use, @port)
    }

    Jabber::debuglog self.jid.to_s + " connected to " + server_to_use.to_s
    Jabber::debuglog "out of possible servers " + @servers.inspect
  rescue Exception, Timeout::Error => e
    Jabber::warnlog "#{server_to_use} error: #{e.inspect}. Will attempt to reconnect in #{@retry_sleep}"
    sleep(@retry_sleep.to_f)
    if(retry_count >= @max_retry.to_i)
      Jabber::warnlog "reached max retry count on exception, failing"
      raise e
    end
    retry_count += 1
    retry
  end
end