4 from threading
import Timer
9 UDPSock
= socket(AF_INET
, SOCK_DGRAM
)
10 UDPSock
.setsockopt(SOL_SOCKET
, SO_BROADCAST
, 1)
13 def add_service(self
, service
):
14 self
.services
.append(service
)
17 def format_services(self
):
18 return ';'.join(self
.services
)
20 def format_beacon(self
, conntype
, services
=True):
21 beacon
= ['tivoconnect=1',
23 'method=%s' % conntype
,
24 'identity=%s' % config
.getGUID(),
25 'machine=%s' % gethostname(),
29 beacon
.append('services=' + self
.format_services())
31 beacon
.append('services=TiVoMediaServer:0/http')
33 return '\n'.join(beacon
)
35 def send_beacon(self
):
36 beacon_ips
= config
.getBeaconAddresses()
37 for beacon_ip
in beacon_ips
.split():
38 if beacon_ip
!= 'listen':
40 self
.UDPSock
.sendto(self
.format_beacon('broadcast'),
47 self
.timer
= Timer(60, self
.start
)
54 """ For the direct-connect, TCP-style beacon """
58 TCPSock
= socket(AF_INET
, SOCK_STREAM
)
59 TCPSock
.bind(('', 2190))
63 # Wait for a connection
64 client
, address
= TCPSock
.accept()
66 # Accept the client's beacon
67 client_length
= struct
.unpack('!I', client
.recv(4))[0]
68 client_message
= client
.recv(client_length
)
71 message
= self
.format_beacon('connected')
72 client
.send(struct
.pack('!I', len(message
)))
76 thread
.start_new_thread(server
, ())
78 def get_name(self
, address
):
79 """ Exchange beacons, and extract the machine name. """
80 our_beacon
= self
.format_beacon('connected', False)
81 machine_name
= re
.compile('machine=(.*)\n').search
85 tsock
.connect((address
, 2190))
87 tsock
.send(struct
.pack('!I', len(our_beacon
)))
88 tsock
.send(our_beacon
)
90 length
= struct
.unpack('!I', tsock
.recv(4))[0]
91 tivo_beacon
= tsock
.recv(length
)
95 name
= machine_name(tivo_beacon
).groups()[0]
101 if __name__
== '__main__':
104 b
.add_service('TiVoMediaServer:9032/http')