Class: RUPNP::ControlPoint

Inherits:
Object
  • Object
show all
Includes:
LogMixin, Tools
Defined in:
lib/rupnp/control_point.rb

Overview

This class is the base one for control points (clients in UPnP terminology).

To create a control point :

EM.run do
  cp = RUPNP::ControlPoint.new(:root)
  cp.start do |new_devices, disappeared_devices|
    new_devices.subscribe do |device|
      puts "New device: #{device.udn}"
    end
  end
end

Author:

  • Sylvain Daubert

Constant Summary collapse

DEFAULT_RESPONSE_WAIT_TIME =

Default response wait time for searching devices. This is set to the maximum value from UPnP 1.1 specification.

5

Constants included from LogMixin

LogMixin::LOG_LEVEL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Tools

#build_url, #snake_case, #urn_are_equivalent?, #usn2udn

Methods included from LogMixin

#log

Constructor Details

#initialize(search_target, search_options = {}) ⇒ ControlPoint

Returns a new instance of ControlPoint.

Parameters:

  • search_target (Symbol, String)

    target to search for. May be :all, :root or a device identifier

  • search_options (Hash) (defaults to: {})

Options Hash (search_options):

  • :response_wait_time (Integer)

    time to wait for responses from devices

  • :try_number (Integer)

    number or search requests to send (specification says that 2 is a minimum)



39
40
41
42
43
44
45
46
47
# File 'lib/rupnp/control_point.rb', line 39

def initialize(search_target, search_options={})
  @search_target = search_target
  @search_options = search_options
  @search_options[:response_wait_time] ||= DEFAULT_RESPONSE_WAIT_TIME

  @devices = []
  @new_device_channel = EM::Channel.new
  @bye_device_channel = EM::Channel.new
end

Instance Attribute Details

#devicesArray<CP::RemoteDevice> (readonly)

Return remote devices controlled by this control point

Returns:



29
30
31
# File 'lib/rupnp/control_point.rb', line 29

def devices
  @devices
end

#event_portInteger (readonly)

Get event listening port

Returns:

  • (Integer)


26
27
28
# File 'lib/rupnp/control_point.rb', line 26

def event_port
  @event_port
end

Instance Method Details

#add_device(device) ⇒ void

This method returns an undefined value.

Add a device to the control point

Parameters:

  • device (Device)

    device to add



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rupnp/control_point.rb', line 92

def add_device(device)
  if has_already_device?(device)
   log :info, "Device already in database: #{device.udn}"
    existing_device = self.find_device_by_udn(device.udn)
    if existing_device.expiration < device.expiration
      log :info, 'update expiration time for device #{device.udn}'
      @devices.delete existing_device
      @devices << device
    end
  else
    log :info, "adding device #{device.udn}"
    @devices << device
    @new_device_channel << device
  end
end

#find_device_by_udn(udn) ⇒ Device?

Find a device from control point’s device list by its UDN

Parameters:

  • udn (String)

Returns:

  • (Device, nil)


112
113
114
# File 'lib/rupnp/control_point.rb', line 112

def find_device_by_udn(udn)
  @devices.find { |d| d.udn == udn }
end

#search_onlyvoid

This method returns an undefined value.

Start a search for devices. No listen for update is made.

Found devices are accessible through #devices.



66
67
68
69
70
# File 'lib/rupnp/control_point.rb', line 66

def search_only
  options = @search_options.dup
  options[:search_only] = true
  search_devices_and_listen @search_target, options
end

#start {|new_device_channel, bye_device_channel| ... } ⇒ void

This method returns an undefined value.

Start control point. This methos starts a search for devices. Then, listening is performed for device notifications.

Yield Parameters:

  • new_device_channel (EM::Channel)

    channel on which new devices are announced

  • bye_device_channel (EM::Channel)

    channel on which byebye device notifications are announced



57
58
59
60
# File 'lib/rupnp/control_point.rb', line 57

def start
  search_devices_and_listen @search_target, @search_options
  yield @new_device_channel, @bye_device_channel if block_given?
end

#start_event_server(port = EVENT_SUB_DEFAULT_PORT) ⇒ void

This method returns an undefined value.

Start event server for listening for device events

Parameters:

  • port (Integer) (defaults to: EVENT_SUB_DEFAULT_PORT)

    port to listen for



75
76
77
78
# File 'lib/rupnp/control_point.rb', line 75

def start_event_server(port=EVENT_SUB_DEFAULT_PORT)
  @event_port ||= port
  @event_server ||= EM.start_server('0.0.0.0', port, CP::EventServer)
end

#stop_event_servervoid

This method returns an undefined value.

Stop event server



83
84
85
86
87
# File 'lib/rupnp/control_point.rb', line 83

def stop_event_server
  @event_port = nil
  EM.stop_server @event_server
  @event_server = nil
end