Module: MiniDisc::Discover::Network

Extended by:
Network
Included in:
Network
Defined in:
lib/minidisc/discover/network.rb

Constant Summary collapse

DEFAULT_TIMEOUT_LIMIT =
2

Instance Method Summary collapse

Instance Method Details

#services(service_type) ⇒ Array<Hash>

Parameters:

  • service_type (String)

    with protocol eg “_telnet._tcp”

Returns:

  • (Array<Hash>)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/minidisc/discover/network.rb', line 31

def services(service_type)
  Thread.abort_on_exception = true
  replies = {}
  DNSSD.browse!(service_type) do |reply|
    replies[reply.name] = reply
    if !reply.flags.more_coming?
      available_replies = replies.select do |_, service|
        service.flags.add?
      end
      return available_replies.map do |_, service|
        resolve = service.resolve
        {
          name: service.name,
          target: resolve.target,
          port: resolve.port
        }
      end
    end

  end
rescue Errno::EBADF, DNSSD::ServiceNotRunningError
  []
end

#services_with_timeout(service_type, options = {}, &block) ⇒ Array<Hash>

Parameters:

  • service_type (String)

    with protocol eg “_telnet._tcp”

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

Options Hash (options):

  • :timeout (Integer)

    Timeout in seconds

Returns:

  • (Array<Hash>)


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/minidisc/discover/network.rb', line 17

def services_with_timeout(service_type, options = {}, &block)
  timeout = options[:timeout] || DEFAULT_TIMEOUT_LIMIT
  Timeout::timeout(timeout) do
    result = services(service_type, &block)
  end
rescue Timeout::Error => e
  result = []
ensure
  yield(result) if block_given?
  result
end