2 from threading
import Timer
7 UDPSock
= socket(AF_INET
, SOCK_DGRAM
)
8 UDPSock
.setsockopt(SOL_SOCKET
, SO_BROADCAST
, 1)
11 def add_service(self
, service
):
12 self
.services
.append(service
)
15 def format_services(self
):
16 return ';'.join(self
.services
)
18 def format_beacon(self
, conntype
):
21 guid
= config
.getGUID()
23 beacon
.append('tivoconnect=1')
24 beacon
.append('swversion=1')
25 beacon
.append('method=%s' % conntype
)
26 beacon
.append('identity=%s' % guid
)
28 beacon
.append('machine=%s' % gethostname())
29 beacon
.append('platform=pc')
30 beacon
.append('services=' + self
.format_services())
32 return '\n'.join(beacon
)
34 def send_beacon(self
):
35 beacon_ips
= config
.getBeaconAddresses()
36 for beacon_ip
in beacon_ips
.split():
37 if beacon_ip
!= 'listen':
39 self
.UDPSock
.sendto(self
.format_beacon('broadcast'),
46 self
.timer
= Timer(60, self
.start
)
53 """For the direct-connect, TCP-style beacon"""
59 TCPSock
= socket(AF_INET
, SOCK_STREAM
)
60 TCPSock
.bind(('', 2190))
64 # Wait for a connection
65 client
, address
= TCPSock
.accept()
67 # Accept the client's beacon
68 client_length
= struct
.unpack('!I', client
.recv(4))[0]
69 client_message
= client
.recv(client_length
)
74 message
= self
.format_beacon('connected')
75 client
.send(struct
.pack('!I', len(message
)))
79 thread
.start_new_thread(server
, ())
81 if __name__
== '__main__':
84 b
.add_service('TiVoMediaServer:9032/http')