### Home Classes Methods

### In Files

- lib/xmpp4r/observable/helper.rb
- lib/xmpp4r/observable/subscription.rb
- lib/xmpp4r/observable/pubsub.rb
- lib/xmpp4r/observable/contact.rb

### Parent

Object

### Namespace

- CLASS Jabber::Observable::Contact
- CLASS Jabber::Observable::PubSub
- CLASS Jabber::Observable::Subscriptions

### Methods

- ::new
- #attach_auto_observer
- #client
- #connected?
- #contacts
- #deferred_max_wait
- #deferred_max_wait=
- #deliver
- #deliver_deferred
- #dettach_auto_observer
- #disconnect
- #notification_count
- #observer_count
- #reconnect
- #roster
- #send!
- #status

### Included Modules

- ObservableThing

### 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::Observable

Jabber::Observable - Creates observable Jabber Clients

### Attributes

auto[R]
jid[R]
pubsub[R]
subs[R]

### Public Class Methods

new(jid, password, status=nil, status_message="Available", host=nil, port=5222) click to toggle source

Create a new Jabber::Observable client. You will be automatically connected to the Jabber server and your status message will be set to the string passed in as the status_message argument.

jabber = Jabber::Observable.new("me@example.com", "password", nil, "Talk to me - Please!")

jid | your jid (either a string or a JID object)
password | your password
status | your status. Check Jabber::Observable#status for documentation
status_message | some funny string
host | the server host (if different from the one in the jid)
port | the server port (default: 5222)

    # File lib/xmpp4r/observable/helper.rb, line 39
    def initialize(jid, password, status=nil, status_message="Available", host=nil, port=5222)

      # Basic stuff
      @jid = jid.respond_to?(:resource) ? jid : Jabber::JID.new(jid)
      @password = password
      @host = host
      @port = port

      # Message dealing
      @delivered_messages = 0
      @deferred_messages = Queue.new
      start_deferred_delivery_thread

      # Connection stuff
      @connect_mutex = Mutex.new
      @client = nil
      @roster = nil
      @disconnected = false

      # Tell everybody I am here
      status(status, status_message)

      # Subscription Accessor
      @subs = Jabber::Observable::Subscriptions.new(self)

      # PubSub Accessor
      @pubsub = Jabber::Observable::PubSub.new(self)

      # Auto Observer placeholder
      @auto = nil

      # Our contacts Hash
      @contacts = Hash.new
    end

### Public Instance Methods

attach_auto_observer() click to toggle source

Attach an auto-observer based on QObserver

    # File lib/xmpp4r/observable/helper.rb, line 97
    def attach_auto_observer
      raise StandardError, "Already attached." unless @auto.nil?

      @auto = QObserver.new
      [ :message, :presence, :iq, :new_subscription, :subscription_request, :event ].each do |thing|
        self.add_observer(thing, @auto)
      end
    end

client() click to toggle source

Pass the underlying Jabber client.

    # File lib/xmpp4r/observable/helper.rb, line 199
    def client
      connect! unless connected?
      @client
    end

connected?() click to toggle source

Returns true if the Jabber client is connected to the Jabber server, false otherwise.

    # File lib/xmpp4r/observable/helper.rb, line 188
    def connected?
      @client.respond_to?(:is_connected?) && @client.is_connected?
    end

contacts(*contact) click to toggle source

Transform a passed list of contacts in one or more Jabber::Observable::Contact objects.

contact | one of more jids of contacts

    # File lib/xmpp4r/observable/helper.rb, line 172
    def contacts(*contact)
      ret = []
      contact.each do |c|
        jid = c.to_s
        # Do we already have it?
        if ! @contacts.include?(jid)
          # Nope.
          @contacts[jid] = c.instance_of?(Jabber::Observable::Contact) ? c : Jabber::Observable::Contact.new(c, self)
        end
        ret << @contacts[jid]
      end
      ret
    end

deferred_max_wait() click to toggle source

Get the maximum time to wait for a message to be delivered. Default: 600 seconds (10 minutes).

    # File lib/xmpp4r/observable/helper.rb, line 260
    def deferred_max_wait
      @deferred_max_wait || 600
    end

deferred_max_wait=(seconds) click to toggle source

Sets the maximum time to wait for a message to be delivered (in seconds). It will be removed of the queue afterwards.

    # File lib/xmpp4r/observable/helper.rb, line 254
    def deferred_max_wait=(seconds)
      @deferred_max_wait = seconds
    end

deliver(jid, message, type=nil) click to toggle source

Send a message to jabber user jid.

jid | jid of the recipient
message | what is to be delivered (either a string or a Jabber::Message)
type | can be either one of:

    * :normal: a normal message.
    * :chat (default): a one-to-one chat message.
    * :groupchat: a group-chat message.
    * :headline: a "headline" message.
    * :error: an error message.

If the recipient is not in your contacts list, the message will be queued for later delivery, and the Contact will be automatically asked for authorization (see Jabber::Observable#add).

    # File lib/xmpp4r/observable/helper.rb, line 130
    def deliver(jid, message, type=nil)
      contacts(jid).each do |contact|
        # Check if we're subscribed to contact
        if @subs.subscribed_to?(contact)
          # Yes! we are!
          if message.instance_of?(Jabber::Message)
            msg = message
            msg.to = contact.jid
            msg.type = type unless type.nil?   # Let's keep the Jabber::Message type unless passed
          else
            msg = Jabber::Message.new(contact.jid)
            msg.body = message
            msg.type = type.nil? ? :chat : type
          end
          @delivered_messages += 1
          send!(msg)
        else
          # No... Let's add it and defer the delivery.
          @subs.add(contact.jid)
          deliver_deferred(contact.jid, message, type)
        end
      end
    end

deliver_deferred(jid, message, type) click to toggle source

Queue messages for delivery once a user has accepted our authorization request. Works in conjunction with the deferred delivery thread.

You can use this method if you want to manually add friends and still have the message queued for later delivery.

    # File lib/xmpp4r/observable/helper.rb, line 247
    def deliver_deferred(jid, message, type)
      msg = {:to => jid, :message => message, :type => type, :time => Time.now}
      @deferred_messages.enq msg
    end

dettach_auto_observer() click to toggle source

Dettach the auto-observer

    # File lib/xmpp4r/observable/helper.rb, line 107
    def dettach_auto_observer
      raise StandardError, "Not attached." if @auto.nil?

      [ :message, :presence, :iq, :new_subscription, :subscription_request, :event ].each do |thing|
        self.delete_observer(thing, @auto)
      end
      @auto = nil
    end

disconnect() click to toggle source

Use this to force the client to disconnect and not automatically reconnect.

    # File lib/xmpp4r/observable/helper.rb, line 238
    def disconnect
      disconnect!(false)
    end

notification_count() click to toggle source

Count the notifications really sent for each thing

    # File lib/xmpp4r/observable/helper.rb, line 88
    def notification_count
      h = {}
      [ :message, :presence, :iq, :new_subscription, :subscription_request, :event ].each do |thing|
        h[thing] = count_notifications(thing)
      end
      h
    end

observer_count() click to toggle source

Count the registered observers in each thing

    # File lib/xmpp4r/observable/helper.rb, line 79
    def observer_count
      h = {}
      [ :message, :presence, :iq, :new_subscription, :subscription_request, :event ].each do |thing|
        h[thing] = count_observers(thing)
      end
      h
    end

reconnect() click to toggle source

Use this to force the client to reconnect after a disconnect.

    # File lib/xmpp4r/observable/helper.rb, line 231
    def reconnect
      @disconnected = false
      connect!
    end

roster() click to toggle source

Pass the underlying Roster helper.

    # File lib/xmpp4r/observable/helper.rb, line 193
    def roster
      return @roster unless @roster.nil?
      @roster = Jabber::Roster::Helper.new(client)
    end

send!(msg) click to toggle source

Send a Jabber stanza over-the-wire.

msg | the stanza to be sent.

    # File lib/xmpp4r/observable/helper.rb, line 207
    def send!(msg)
      retries = 0
      max = 4
      begin
        retries += 1
        client.send(msg)
      rescue Errno::ECONNRESET => e
        # Connection reset. Sleep progressively and retry until success or exhaustion.
        sleep ((retries^2) * 60) + 30
        disconnect
        reconnect
        retry unless retries > max
        raise e
      rescue Errno::EPIPE, IOError => e
        # Some minor error. Sleep 2 seconds and retry until success or exhaustion.
        sleep 2
        disconnect
        reconnect
        retry unless retries > max
        raise e
      end
    end

status(presence, message) click to toggle source

Set your presence, with a message.

presence | any of these:

    * nil: online.
    * :chat: free for chat.
    * :away: away from the computer.
    * :dnd: do not disturb.
    * :xa: extended away.

message | a string that you wish your contacts see when you change your presence.

    # File lib/xmpp4r/observable/helper.rb, line 163
    def status(presence, message)
      @status_message = message
      @presence = presence
      send!(Jabber::Presence.new(@presence, @status_message))
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.