Class: SonyCameraRemote::Discovery::Client

Inherits:
Object
  • Object
show all
Includes:
Socket::Constants
Defined in:
lib/sony_camera_remote/discovery/client.rb

Defined Under Namespace

Classes: UnknownInterface

Constant Summary collapse

SSDP_ADDR =
{
  nil => '239.255.255.250',
  :ipv6_link_local => 'FF02::C',
  :ipv6_subnet => 'FF03::C',
  :ipv6_administrative => 'FF04::C',
  :ipv6_site_local => 'FF05::C',
  :ipv6_global => 'FF0E::C',
}
SSDP_PORT =
1900

Instance Method Summary collapse

Constructor Details

#initialize(local_addrs = nil, timeout = 10, scope = nil) ⇒ Client

TODO Move the enumeration and address lookup to its own class. This class does too much.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sony_camera_remote/discovery/client.rb', line 28

def initialize(local_addrs = nil, timeout = 10, scope = nil)
  if local_addrs.nil?
    # Use all of the system's IP addresses
    @local_addrs = ip_addresses
  elsif local_addrs !~ /\b(?:\d{1,3}\.){3}\d{1,3}\b/ # http://www.regular-expressions.info/examples.html
    # Not exactly an IP address, so we treat it as interface name
    # TODO Support not just one, but a list of interfaces
    # TODO System.get_ifaddrs seems to return IPv4 addresses only
    if_addr = System.get_ifaddrs.fetch(local_addrs.to_sym) do |ip|
      raise UnknownInterface.new(ip, System.get_ifaddrs.keys)
    end[:inet_addr]

    @local_addrs = addr_info(if_addr)
  else
    @local_addrs = addr_info(local_addrs)
  end

  @addr = SSDP_ADDR[@scope]
  @timeout = timeout
end

Instance Method Details

#discoverObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sony_camera_remote/discovery/client.rb', line 49

def discover
  Array(@local_addrs).map do |local_addr|
    begin
      DeviceInfo.fetch(inquire(local_addr).location)
    rescue Errno::EADDRNOTAVAIL
      # TODO Notify observers instead of writing to STDOUT
      STDOUT.puts("Warning: Could not bind to #{local_addr.ip_address}.")
    rescue Errno::EINVAL
      STDOUT.puts("Warning: Address #{local_addr.ip_address} not supported.")
    rescue Timeout::Error
      STDOUT.puts("Warning: Timeout binding to #{local_addr.ip_address}.")
    end
  end.compact
end