Class: TivoHMO::Beacon

Inherits:
Object
  • Object
show all
Includes:
GemLogger::LoggerSupport
Defined in:
lib/tivohmo/beacon.rb

Overview

Provides a mechanism for broadcasting the presence of a TivoHMO::Server to Tivo dvrs on the local network

Instance Method Summary collapse

Constructor Details

#initialize(service_port, limit: -1,, interval: 10) ⇒ Beacon

Returns a new instance of Beacon.



10
11
12
13
14
15
16
# File 'lib/tivohmo/beacon.rb', line 10

def initialize(service_port, limit: -1, interval: 10)
  @interval = interval
  @limit = limit
  @uid = SecureRandom.uuid
  @services = ['TiVoMediaServer:%s/http' % service_port]
  @running = false
end

Instance Method Details

#beacon_data(method) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/tivohmo/beacon.rb', line 48

def beacon_data(method)
  payload = {
      tivoconnect: 1,
      method: method,
      identity: "{#{@uid}}",
      machine: Socket.gethostname,
      platform: 'pc/tivohmo',
      services: @services.join(';'),
      swversion: TivoHMO::VERSION
  }
  data = payload.collect {|k,v| "#{k}=#{v}" }.join("\n") << "\n"
  data
end

#broadcastObject



62
63
64
65
66
67
68
69
70
# File 'lib/tivohmo/beacon.rb', line 62

def broadcast
  bcast_ip = '<broadcast>' #'255.255.255.255'
  bcast_port = 2190
  packet = beacon_data('broadcast')
  logger.debug "Sending beacon packet: #{packet}"
  socket = UDPSocket.new(Socket::AF_INET)
  socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_BROADCAST, true)
  socket.send(packet, 0, bcast_ip, bcast_port)
end

#joinObject



44
45
46
# File 'lib/tivohmo/beacon.rb', line 44

def join
  @broadcast_thread.join
end

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/tivohmo/beacon.rb', line 18

def start
  if ! @running
    logger.info "Starting beacon(limit=#@limit, interval=#@interval) for #{@services.inspect}"
    @running = true
    @broadcast_thread = Thread.new do
      while @running
        begin
          broadcast
        rescue => e
          logger.log_exception(e, "Ignoring exception in beacon thread", level: :warn)
        end
        sleep(@interval)
        @limit = @limit - 1
        break if @limit == 0
      end
      @running = false
      logger.info "Beacon thread exiting"
    end
  end
end

#stopObject



39
40
41
42
# File 'lib/tivohmo/beacon.rb', line 39

def stop
  logger.info "Stopping beacon"
  @running = false
end