Sha256: 265943976729f0ad5e5ce5f0804af3878af847abc37641549a13146693863946

Contents?: true

Size: 1.74 KB

Versions: 3

Compression:

Stored size: 1.74 KB

Contents

require 'em-http-request'


module RUPNP

  # CP module to group all control point's classes
  # @author Sylvain Daubert
  module CP

    # Base class for devices and services
    # @author Sylvain Daubert
    class Base
      include EM::Deferrable
      include Tools
      include LogMixin

      # Common HTTP headers for description requests
      HTTP_COMMON_CONFIG = {
        :head => {
          :user_agent => USER_AGENT,
          :host => "#{HOST_IP}:#{DISCOVERY_PORT}",
        },
      }

      def initialize
        @parser = Nori.new(:convert_tags_to => ->(tag){ tag.snakecase.to_sym })
      end

      # Get description from +location+
      # @param [String] location
      # @param [EM::Defferable] getter deferrable to advice about failure
      #  or success. On fail, +getter+ receive a message. On success, it
      #  receive a description (XML Nori hash)
      # @return [void]
      def get_description(location, getter)
        log :info, "getting description for #{location}"
        http = EM::HttpRequest.new(location).get(HTTP_COMMON_CONFIG)

        http.errback do |error|
          getter.set_deferred_status :failed, 'Cannot get description'
        end

        callback = Proc.new do
          description = @parser.parse(http.response)
          log :debug, 'Description received'
          getter.succeed description
        end

        http.headers do |h|
          unless h['SERVER'] =~ /UPnP\/1\.\d/
            log :error, "Not a supported UPnP response : #{h['SERVER']}"
            http.cancel_callback callback
            http.fail
          end
        end

        http.callback &callback
      end

      # @return String
      def inspect
        "#<#{self.class}:#{object_id} type=#{type.inspect}>"
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rupnp-0.3.0 lib/rupnp/cp/base.rb
rupnp-0.2.2 lib/rupnp/cp/base.rb
rupnp-0.2.1 lib/rupnp/cp/base.rb