From b5833c5654f5adca96495c030b1609efb4e5f57f Mon Sep 17 00:00:00 2001 From: William McBrine Date: Sat, 29 Dec 2007 22:45:50 -0500 Subject: [PATCH] Added the ability to listen for/send beacons on TCP port 2190, for use with the manual server menu. (Use "beacon = listen" in pyTivo.conf to listen only, or "beacon = 255.255.255.255 listen" (or whatever) to both broadcast and listen. With no beacon keyword, no listening occurs.) --- beacon.py | 47 ++++++++++++++++++++++++++++++++++++++--------- pyTivo.py | 5 ++--- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/beacon.py b/beacon.py index 26f454f..c322514 100644 --- a/beacon.py +++ b/beacon.py @@ -15,14 +15,14 @@ class Beacon: def format_services(self): return ';'.join(self.services) - def format_beacon(self): + def format_beacon(self, conntype): beacon = [] guid = config.getGUID() beacon.append('tivoconnect=1') beacon.append('swversion=1') - beacon.append('method=broadcast') + beacon.append('method=%s' % conntype) beacon.append('identity=%s' % guid) beacon.append('machine=%s' % gethostname()) @@ -34,11 +34,12 @@ class Beacon: def send_beacon(self): beacon_ips = config.getBeaconAddresses() for beacon_ip in beacon_ips.split(): - try: - self.UDPSock.sendto(self.format_beacon(), (beacon_ip, 2190)) - except error, e: - print e - pass + if beacon_ip != 'listen': + try: + self.UDPSock.sendto(self.format_beacon('broadcast'), + (beacon_ip, 2190)) + except error, e: + print e def start(self): self.send_beacon() @@ -48,9 +49,37 @@ class Beacon: def stop(self): self.timer.cancel() + def listen(self): + """For the direct-connect, TCP-style beacon""" + import thread + + def server(): + import struct + + TCPSock = socket(AF_INET, SOCK_STREAM) + TCPSock.bind(('', 2190)) + TCPSock.listen(5) + + while True: + # Wait for a connection + client, address = TCPSock.accept() + + # Accept the client's beacon + client_length = struct.unpack('!I', client.recv(4))[0] + client_message = client.recv(client_length) + + print client_length + + # Send ours + message = self.format_beacon('connected') + client.send(struct.pack('!I', len(message))) + client.send(message) + client.close() + + thread.start_new_thread(server, ()) + if __name__ == '__main__': b = Beacon() - b.add_service('TiVoMediaServer:9032/http') - b.send_beacon_timer() + b.send_beacon() diff --git a/pyTivo.py b/pyTivo.py index 475d2ec..e41423e 100755 --- a/pyTivo.py +++ b/pyTivo.py @@ -1,8 +1,6 @@ #!/usr/bin/env python - import beacon, httpserver, os, sys - import config port = config.getPort() @@ -15,9 +13,10 @@ for section, settings in config.getShares(): b = beacon.Beacon() b.add_service('TiVoMediaServer:' + str(port) + '/http') b.start() +if 'listen' in config.getBeaconAddresses(): + b.listen() try: httpd.serve_forever() except KeyboardInterrupt: b.stop() - -- 2.11.4.GIT