Class: UPnP::RootServer

Inherits:
WEBrick::HTTPServer
  • Object
show all
Defined in:
lib/UPnP/root_server.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_device) ⇒ RootServer

Creates a new UPnP web server with root_device



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#loggerObject (readonly)

The WEBrick logger



16
17
18
# File 'lib/UPnP/root_server.rb', line 16

def logger
  @logger
end

#root_deviceObject (readonly)

This server’s root UPnP device



21
22
23
# File 'lib/UPnP/root_server.rb', line 21

def root_device
  @root_device
end

#scpdsObject (readonly)

This server’s SCPDs



26
27
28
# File 'lib/UPnP/root_server.rb', line 26

def scpds
  @scpds
end

Instance Method Details

#description(req, res) ⇒ Object

Handler for the root device description

Raises:

  • (WEBrick::HTTPStatus::NotFound)


57
58
59
60
61
62
63
# 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) ⇒ Object

Mounts WEBrick::HTTPServer server at path



68
69
70
71
72
73
74
75
# 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) ⇒ Object

Mounts the appropriate paths for service in this service



80
81
82
83
84
85
86
87
# 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) ⇒ Object

A generic display page for the webserver root

Raises:

  • (WEBrick::HTTPStatus::NotFound)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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 = <<-EOF
<title>#{@root_device.friendly_name} - #{@root_device.type}</title>

<p>Devices:

<ul>
#{devices}
</ul>

<p>Services:

<ul>
#{services}
</ul>
  EOF
end

#scpd(req, res) ⇒ Object

Handler for a service control protocol description request

Raises:

  • (WEBrick::HTTPStatus::NotFound)


126
127
128
129
130
131
132
133
# 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) ⇒ Object



135
136
137
138
139
140
# File 'lib/UPnP/root_server.rb', line 135

def service(req, res)
  super

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