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):
23 guid
= config
.getGUID()
25 beacon
.append('tivoconnect=1')
26 beacon
.append('swversion=1')
27 beacon
.append('method=%s' % conntype
)
28 beacon
.append('identity=%s' % guid
)
30 beacon
.append('machine=%s' % gethostname())
31 beacon
.append('platform=pc')
33 beacon
.append('services=' + self
.format_services())
35 beacon
.append('services=TiVoMediaServer:0/http')
37 return '\n'.join(beacon
)
39 def send_beacon(self
):
40 beacon_ips
= config
.getBeaconAddresses()
41 for beacon_ip
in beacon_ips
.split():
42 if beacon_ip
!= 'listen':
44 self
.UDPSock
.sendto(self
.format_beacon('broadcast'),
51 self
.timer
= Timer(60, self
.start
)
58 """ For the direct-connect, TCP-style beacon """
62 TCPSock
= socket(AF_INET
, SOCK_STREAM
)
63 TCPSock
.bind(('', 2190))
67 # Wait for a connection
68 client
, address
= TCPSock
.accept()
70 # Accept the client's beacon
71 client_length
= struct
.unpack('!I', client
.recv(4))[0]
72 client_message
= client
.recv(client_length
)
75 message
= self
.format_beacon('connected')
76 client
.send(struct
.pack('!I', len(message
)))
80 thread
.start_new_thread(server
, ())
82 def get_name(self
, address
):
83 """ Exchange beacons, and extract the machine name. """
84 our_beacon
= self
.format_beacon('connected', False)
85 machine_name
= re
.compile('machine=(.*)\n').search
89 tsock
.connect((address
, 2190))
91 tsock
.send(struct
.pack('!I', len(our_beacon
)))
92 tsock
.send(our_beacon
)
94 length
= struct
.unpack('!I', tsock
.recv(4))[0]
95 tivo_beacon
= tsock
.recv(length
)
99 name
= machine_name(tivo_beacon
).groups()[0]
105 if __name__
== '__main__':
108 b
.add_service('TiVoMediaServer:9032/http')