Sha256: 667a42a51f6e705ea83c12df3f31af7ba20c4cc05fbcafbf20249ef0dfecd00b

Contents?: true

Size: 1.5 KB

Versions: 23

Compression:

Stored size: 1.5 KB

Contents

module Blather
class Stream

  # @private
  class Resource < Features
    BIND_NS = 'urn:ietf:params:xml:ns:xmpp-bind'.freeze
    register BIND_NS

    def initialize(stream, succeed, fail)
      super
      @jid = stream.jid
    end

    def receive_data(stanza)
      @node = stanza
      case stanza.element_name
      when 'bind' then  bind
      when 'iq'   then  result
      else              fail!(UnknownResponse.new(@node))
      end
    end

  private
    ##
    # Respond to the bind request
    # If @jid has a resource set already request it from the server
    def bind
      response = Stanza::Iq.new :set
      @id = response.id

      response << (binder = XMPPNode.new('bind', response.document))
      binder.namespace = BIND_NS

      if @jid.resource
        binder << (resource = XMPPNode.new('resource', binder.document))
        resource.content = @jid.resource
      end

      @stream.send response
    end

    ##
    # Process the result from the server
    # Sets the sends the JID (now bound to a resource)
    # back to the stream
    def result
      if @node[:type] == 'error'
        fail! StanzaError.import(@node)
        return
      end

      # ensure this is a response to our original request
      if @id == @node['id']
        @stream.jid = JID.new @node.find_first('bind_ns:bind/bind_ns:jid', :bind_ns => BIND_NS).content
        succeed!
      else
        fail!("BIND result ID mismatch. Expected: #{@id}. Received: #{@node['id']}")
      end
    end
  end #Resource

end #Stream
end #Blather

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
blather-2.0.0 lib/blather/stream/features/resource.rb
blather-1.2.0 lib/blather/stream/features/resource.rb
blather-1.1.4 lib/blather/stream/features/resource.rb
blather-1.1.3 lib/blather/stream/features/resource.rb
blather-1.1.2 lib/blather/stream/features/resource.rb
blather-1.1.1 lib/blather/stream/features/resource.rb
blather-1.1.0 lib/blather/stream/features/resource.rb
blather-1.0.0 lib/blather/stream/features/resource.rb
blather-0.8.8 lib/blather/stream/features/resource.rb
blather-0.8.7 lib/blather/stream/features/resource.rb
blather-0.8.6 lib/blather/stream/features/resource.rb
blather-0.8.5 lib/blather/stream/features/resource.rb
blather-0.8.4 lib/blather/stream/features/resource.rb
blather-0.8.3 lib/blather/stream/features/resource.rb
blather-0.8.2 lib/blather/stream/features/resource.rb
tp-blather-0.8.5 lib/blather/stream/features/resource.rb
tp-blather-0.8.4 lib/blather/stream/features/resource.rb
tp-blather-0.8.3 lib/blather/stream/features/resource.rb
tp-blather-0.8.2 lib/blather/stream/features/resource.rb
blather-0.8.1 lib/blather/stream/features/resource.rb