### Home Classes Methods

### In Files

- lib/xmpp4r/reliable.rb

### Parent

Object

### Methods

- ::new
- #add_exception_handler
- #auth
- #connect
- #run_exception_handlers
- #send_message
- #send_presence
- #setup_connection
- #start
- #stop

### Files

- CHANGELOG
- COPYING
- LICENSE
- README.rdoc
- README_ruby19.txt

### Class/Module Index

Quicksearch

- Jabber
- Jabber::ArgumentError
- Jabber::Bytestreams
- Jabber::Bytestreams::IBB
- Jabber::Bytestreams::IBBInitiator
- Jabber::Bytestreams::IBBQueueItem
- Jabber::Bytestreams::IBBTarget
- Jabber::Bytestreams::IqQueryBytestreams
- Jabber::Bytestreams::IqSi
- Jabber::Bytestreams::IqSiFile
- Jabber::Bytestreams::IqSiFileRange
- Jabber::Bytestreams::SOCKS5Bytestreams
- Jabber::Bytestreams::SOCKS5BytestreamsInitiator
- Jabber::Bytestreams::SOCKS5BytestreamsPeer
- Jabber::Bytestreams::SOCKS5BytestreamsServer
- Jabber::Bytestreams::SOCKS5BytestreamsServerStreamHost
- Jabber::Bytestreams::SOCKS5BytestreamsTarget
- Jabber::Bytestreams::SOCKS5Socket
- Jabber::Bytestreams::StreamHost
- Jabber::Bytestreams::StreamHostUsed
- Jabber::Callback
- Jabber::CallbackList
- Jabber::Caps
- Jabber::Caps::C
- Jabber::Caps::Helper
- Jabber::Client
- Jabber::ClientAuthenticationFailure
- Jabber::Command
- Jabber::Command::IqCommand
- Jabber::Command::Responder
- Jabber::Component
- Jabber::ComponentAuthenticationFailure
- Jabber::Connection
- Jabber::Connection::SSLSocketUTF8
- Jabber::Dataforms
- Jabber::Dataforms::XData
- Jabber::Dataforms::XDataField
- Jabber::Dataforms::XDataInstructions
- Jabber::Dataforms::XDataReported
- Jabber::Dataforms::XDataTitle
- Jabber::Delay
- Jabber::Delay::XDelay
- Jabber::Discovery
- Jabber::Discovery::Feature
- Jabber::Discovery::Helper
- Jabber::Discovery::Identity
- Jabber::Discovery::IqQueryDiscoInfo
- Jabber::Discovery::IqQueryDiscoItems
- Jabber::Discovery::Item
- Jabber::Discovery::Responder
- Jabber::EntityTime
- Jabber::EntityTime::IqTime
- Jabber::EntityTime::Responder
- Jabber::ErrorResponse
- Jabber::FeatureNegotiation
- Jabber::FeatureNegotiation::IqFeature
- Jabber::FileTransfer
- Jabber::FileTransfer::FileSource
- Jabber::FileTransfer::Helper
- Jabber::FileTransfer::TransferSource
- Jabber::Framework
- Jabber::Framework::Base
- Jabber::Framework::Bot
- Jabber::HTTPBinding
- Jabber::HTTPBinding::Client
- Jabber::IdGenerator
- Jabber::InvalidChatState
- Jabber::Iq
- Jabber::IqQuery
- Jabber::JID
- Jabber::JabberError
- Jabber::LastActivity
- Jabber::LastActivity::Helper
- Jabber::LastActivity::IqQueryLastActivity
- Jabber::MUC
- Jabber::MUC::IqQueryMUCAdmin
- Jabber::MUC::IqQueryMUCAdminItem
- Jabber::MUC::IqQueryMUCOwner
- Jabber::MUC::MUCBrowser
- Jabber::MUC::MUCClient
- Jabber::MUC::SimpleMUCClient
- Jabber::MUC::UserItem
- Jabber::MUC::XMUC
- Jabber::MUC::XMUCUser
- Jabber::MUC::XMUCUserInvite
- Jabber::MUC::XMUCUserItem
- Jabber::Message
- Jabber::NoNameXmlnsRegistered
- Jabber::NotConnected
- Jabber::Observable
- Jabber::Observable::Contact
- Jabber::Observable::PubSub
- Jabber::Observable::PubSub::AlreadySet
- Jabber::Observable::PubSub::NoService
- Jabber::Observable::Subscriptions
- Jabber::Presence
- Jabber::PubSub
- Jabber::PubSub::Configuration
- Jabber::PubSub::Event
- Jabber::PubSub::EventItem
- Jabber::PubSub::EventItems
- Jabber::PubSub::IqPubSub
- Jabber::PubSub::IqPubSubOwner
- Jabber::PubSub::Item
- Jabber::PubSub::Items
- Jabber::PubSub::NodeBrowser
- Jabber::PubSub::NodeConfig
- Jabber::PubSub::NodeHelper
- Jabber::PubSub::OAuthPubSubStreamHelper
- Jabber::PubSub::OAuthServiceHelper
- Jabber::PubSub::OwnerNodeConfig
- Jabber::PubSub::Publish
- Jabber::PubSub::Retract
- Jabber::PubSub::ServiceHelper
- Jabber::PubSub::Subscription
- Jabber::PubSub::SubscriptionConfig
- Jabber::PubSub::Unsubscribe
- Jabber::RPC
- Jabber::RPC::Client
- Jabber::RPC::IqQueryRPC
- Jabber::RPC::Server
- Jabber::Reliable
- Jabber::Reliable::Connection
- Jabber::Reliable::Listener
- Jabber::Roster
- Jabber::Roster::Helper
- Jabber::Roster::Helper::RosterItem
- Jabber::Roster::IqQueryRoster
- Jabber::Roster::RosterItem
- Jabber::Roster::RosterX
- Jabber::Roster::RosterXItem
- Jabber::Roster::XRoster
- Jabber::Roster::XRosterItem
- Jabber::SASL
- Jabber::SASL::Anonymous
- Jabber::SASL::Base
- Jabber::SASL::DigestMD5
- Jabber::SASL::Plain
- Jabber::SOCKS5Error
- Jabber::Semaphore
- Jabber::ServerDisconnected
- Jabber::Stream
- Jabber::Stream::ThreadBlock
- Jabber::StreamParser
- Jabber::Test
- Jabber::Test::ListenerMocker
- Jabber::UserLocation
- Jabber::UserLocation::Helper
- Jabber::UserLocation::Location
- Jabber::UserTune
- Jabber::UserTune::Helper
- Jabber::UserTune::Tune
- Jabber::Vcard
- Jabber::Vcard::Helper
- Jabber::Vcard::IqVcard
- Jabber::Version
- Jabber::Version::IqQueryVersion
- Jabber::Version::Responder
- Jabber::Version::SimpleResponder
- Jabber::X
- Jabber::XHTML
- Jabber::XHTML::Body
- Jabber::XHTML::HTML
- Jabber::XMPPElement
- Jabber::XMPPStanza
- Jabber::XParent
- REXML
- REXML::Element
- REXML::IOSource
- XMLRPC
- XMLRPC::Create
- Base64
- ObservableThing
- QObserver
- ThreadStore

No matching classes.

# Jabber::Reliable::Listener

### Public Class Methods

new(full_jid, password, config, &block) click to toggle source

    # File lib/xmpp4r/reliable.rb, line 49
    def initialize(full_jid, password, config, &block)
      @on_message_block = block
      @full_jid = full_jid
      @config = config
      @password = password
      @max_retry = config[:max_retry] || 30
    end

### Public Instance Methods

add_exception_handler(&block) click to toggle source

    # File lib/xmpp4r/reliable.rb, line 100
    def add_exception_handler(&block)
      @exception_handlers << block
    end

auth() click to toggle source

    # File lib/xmpp4r/reliable.rb, line 125
    def auth
      @connection.auth(@password)
    end

connect() click to toggle source

    # File lib/xmpp4r/reliable.rb, line 121
    def connect
      @connection.connect
    end

run_exception_handlers(e, connection, where_failed) click to toggle source

    # File lib/xmpp4r/reliable.rb, line 88
    def run_exception_handlers(e, connection, where_failed)
      @exception_handlers.each do |ex_handler|
        ex_handler.call(e, connection, where_failed)
      end
      if where_failed == :sending
        @message_to_send_on_reconnect = @message_now_sending
      end
      if where_failed != :close && !@connection.is_connected?
        @reconnection_thread.raise(e)
      end
    end

send_message(message) click to toggle source

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
    def send_message(message)
      unless @connection
        raise ::ArgumentError, "Can't send messages while listener is stopped.  Plase 'start' the listener first."
      end
      retry_count = 0
      begin
        while(not @connection.is_connected?)
          #wait
          Thread.pass
        end
        @message_now_sending = message
        @connection.send(message)
        return true #true, message was sent
      rescue => e
        if e.is_a?(Interrupt)
          raise e
        end
        if(retry_count > @max_retry.to_i)
          Jabber::debuglog "reached max retry count on message re-send, failing"
          raise e
        end
        retry_count += 1
        Jabber::debuglog "retrying message send.." + e.inspect
        retry
      end
    end

send_presence() click to toggle source

    # File lib/xmpp4r/reliable.rb, line 129
    def send_presence
      presence_message = @config[:presence_message]
      if presence_message && !presence_message.empty?
        @connection.send(Jabber::Presence.new.set_show(:chat).set_status(presence_message))
      end
    end

setup_connection() click to toggle source

    # File lib/xmpp4r/reliable.rb, line 57
    def setup_connection
      @connection = Connection.new(@full_jid, @config)
      if @on_message_block
        @connection.add_message_callback(&@on_message_block)
      else
        @connection.add_message_callback do |msg|
          self.on_message(msg)
        end
      end

      #We could just reconnect in @connection.on_exception,
      #but by raising into this seperate thread, we avoid growing our stack trace
      @reconnection_thread = Thread.new do
        first_run = true
        begin
          self.start unless first_run
          loop do
            sleep(1)
            Thread.pass
          end
        rescue => e
          first_run = false
          retry
        end
      end
      @exception_handlers = []
      @connection.on_exception do |e, connection, where_failed|
        self.run_exception_handlers(e, connection, where_failed)
      end
    end

start() click to toggle source

    # File lib/xmpp4r/reliable.rb, line 104
    def start
      setup_connection unless @connection
      connect
      auth
      send_presence
      if @message_to_send_on_reconnect
        send_message(@message_to_send_on_reconnect)
      end
      @message_to_send_on_reconnect = nil
    end

stop() click to toggle source

Stop the listener. (close the connection)

    # File lib/xmpp4r/reliable.rb, line 116
    def stop
      @connection.close if @connection and @connection.is_connected?
      @connection = nil
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.