Class: TivoHMO::Beacon
- Inherits:
-
Object
- Object
- TivoHMO::Beacon
- 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
- #beacon_data(method) ⇒ Object
- #broadcast ⇒ Object
-
#initialize(service_port, limit: -1,, interval: 10) ⇒ Beacon
constructor
A new instance of Beacon.
- #join ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
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 |
#broadcast ⇒ Object
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 |
#join ⇒ Object
44 45 46 |
# File 'lib/tivohmo/beacon.rb', line 44 def join @broadcast_thread.join end |
#start ⇒ Object
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 |
#stop ⇒ Object
39 40 41 42 |
# File 'lib/tivohmo/beacon.rb', line 39 def stop logger.info "Stopping beacon" @running = false end |