class UPnP::RootServer

Master WEBrick server that publishes a root device’s sub-devices and services for consumption by a UPnP control device.

A root server is created automatiaclly for a device when you call run on your device instance.

Attributes

root_device[R]

This server’s root UPnP device

Public Class Methods

new(root_device) click to toggle source

Creates a new UPnP web server with root_device

# File lib/UPnP/root_server.rb, line 31
def initialize(root_device)
  @root_device = root_device

  server_info = "RubyUPnP/#{UPnP::VERSION}"
  device_info = "Ruby#{root_device.type}/#{root_device.version}"
  @server_version = [server_info, 'UPnP/1.0', device_info].join ' '

  @scpds = {}

  level = if @root_device.class.debug? then
            WEBrick::BasicLog::DEBUG
          else
            WEBrick::BasicLog::FATAL
          end

  @logger = WEBrick::Log.new $stderr, level

  super :Logger => @logger, :Port => 0

  mount_proc '/description', method(:description)
  mount_proc '/',            method(:root)
end

Public Instance Methods

description(req, res) click to toggle source

Handler for the root device description

# File lib/UPnP/root_server.rb, line 57
def description(req, res)
  raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." unless
    req.path == '/description'

  res['content-type'] = 'text/xml'
  res.body << @root_device.description
end
mount_server(path, server) click to toggle source

Mounts WEBrick::HTTPServer server at path

# File lib/UPnP/root_server.rb, line 68
def mount_server(path, server)
  server.config[:Logger] = @logger

  mount_proc path do |req, res|
    server.service req, res

  end
end
mount_service(service) click to toggle source

Mounts the appropriate paths for service in this service

# File lib/UPnP/root_server.rb, line 80
def mount_service(service)
  mount_server service.control_url, service.server

  service.mount_extra self

  @scpds[service.scpd_url] = service
  mount_proc service.scpd_url, method(:scpd)
end
root(req, res) click to toggle source

A generic display page for the webserver root

# File lib/UPnP/root_server.rb, line 92
  def root(req, res)
    raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." unless
      req.path == '/'

    res['content-type'] = 'text/html'

    devices = @root_device.devices[1..-1].map do |d|
      "<li>#{d.friendly_name} - #{d.type}"
    end.join "\n"

    services = @root_device.services.map do |s|
      "<li>#{s.type}"
    end.join "\n"

    res.body = "<title>#{@root_device.friendly_name} - #{@root_device.type}</title>

<p>Devices:

<ul>
#{devices}
</ul>

<p>Services:

<ul>
#{services}
</ul>
"
  end
scpd(req, res) click to toggle source

Handler for a service control protocol description request

# File lib/UPnP/root_server.rb, line 126
def scpd(req, res)
  service = @scpds[req.path]
  raise WEBrick::HTTPStatus::NotFound, "`#{req.path}' not found." unless
    service

  res['content-type'] = 'text/xml'
  res.body << service.scpd
end
service(req, res) click to toggle source
# File lib/UPnP/root_server.rb, line 135
def service(req, res)
  super

  res['Server'] = @server_version
  res['EXT'] = ''
end