parent
9901935b83
commit
7199561573
1 changed files with 21 additions and 112 deletions
|
@ -215,94 +215,14 @@ class plexgdm:
|
||||||
return self.server_list
|
return self.server_list
|
||||||
|
|
||||||
def discover(self):
|
def discover(self):
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
|
|
||||||
# Set a timeout so the socket does not block indefinitely
|
|
||||||
sock.settimeout(0.6)
|
|
||||||
|
|
||||||
# Set the time-to-live for messages to 1 for local network
|
|
||||||
ttl = struct.pack('b', 1)
|
|
||||||
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
|
|
||||||
|
|
||||||
returnData = []
|
|
||||||
try:
|
|
||||||
# Send data to the multicast group
|
|
||||||
self.logMsg("Sending discovery messages: %s"
|
|
||||||
% self.discover_message, 2)
|
|
||||||
sock.sendto(self.discover_message, self.discover_group)
|
|
||||||
|
|
||||||
# Look for responses from all recipients
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
data, server = sock.recvfrom(1024)
|
|
||||||
self.logMsg("Received data from %s, %s" % server, 2)
|
|
||||||
returnData.append({'from': server,
|
|
||||||
'data': data})
|
|
||||||
except socket.timeout:
|
|
||||||
break
|
|
||||||
except:
|
|
||||||
# if we can't send our discovery query, just abort and try again
|
|
||||||
# on the next loop
|
|
||||||
return
|
|
||||||
finally:
|
|
||||||
sock.close()
|
|
||||||
|
|
||||||
self.discovery_complete = True
|
|
||||||
|
|
||||||
discovered_servers = []
|
|
||||||
|
|
||||||
if returnData:
|
|
||||||
|
|
||||||
for response in returnData:
|
|
||||||
update = {'server': response.get('from')[0]}
|
|
||||||
|
|
||||||
# Check if we had a positive HTTP reponse
|
|
||||||
if "200 OK" in response.get('data'):
|
|
||||||
for each in response.get('data').split('\r\n'):
|
|
||||||
update['discovery'] = "auto"
|
|
||||||
update['owned'] = '1'
|
|
||||||
update['master'] = 1
|
|
||||||
update['role'] = 'master'
|
|
||||||
update['class'] = None
|
|
||||||
if "Content-Type:" in each:
|
|
||||||
update['content-type'] = each.split(':')[1].strip()
|
|
||||||
elif "Resource-Identifier:" in each:
|
|
||||||
update['uuid'] = each.split(':')[1].strip()
|
|
||||||
elif "Name:" in each:
|
|
||||||
update['serverName'] = each.split(':')[1].strip()
|
|
||||||
elif "Port:" in each:
|
|
||||||
update['port'] = each.split(':')[1].strip()
|
|
||||||
elif "Updated-At:" in each:
|
|
||||||
update['updated'] = each.split(':')[1].strip()
|
|
||||||
elif "Version:" in each:
|
|
||||||
update['version'] = each.split(':')[1].strip()
|
|
||||||
elif "Server-Class:" in each:
|
|
||||||
update['class'] = each.split(':')[1].strip()
|
|
||||||
|
|
||||||
# Quickly test if we need https
|
|
||||||
https = PMSHttpsEnabled(
|
|
||||||
'%s:%s' % (update['server'], update['port']))
|
|
||||||
if https is None:
|
|
||||||
# Error contacting server
|
|
||||||
continue
|
|
||||||
elif https:
|
|
||||||
update['protocol'] = 'https'
|
|
||||||
else:
|
|
||||||
update['protocol'] = 'http'
|
|
||||||
discovered_servers.append(update)
|
|
||||||
|
|
||||||
# Append REMOTE PMS that we haven't found yet; if necessary
|
|
||||||
currServer = window('pms_server')
|
currServer = window('pms_server')
|
||||||
if currServer:
|
if not currServer:
|
||||||
|
return
|
||||||
currServerProt, currServerIP, currServerPort = \
|
currServerProt, currServerIP, currServerPort = \
|
||||||
currServer.split(':')
|
currServer.split(':')
|
||||||
currServerIP = currServerIP.replace('/', '')
|
currServerIP = currServerIP.replace('/', '')
|
||||||
for server in discovered_servers:
|
|
||||||
if server['server'] == currServerIP:
|
|
||||||
break
|
|
||||||
else:
|
|
||||||
# Currently active server was not discovered via GDM; ADD
|
# Currently active server was not discovered via GDM; ADD
|
||||||
update = {
|
self.server_list = [{
|
||||||
'port': currServerPort,
|
'port': currServerPort,
|
||||||
'protocol': currServerProt,
|
'protocol': currServerProt,
|
||||||
'class': None,
|
'class': None,
|
||||||
|
@ -316,18 +236,7 @@ class plexgdm:
|
||||||
'updated': int(time.time()),
|
'updated': int(time.time()),
|
||||||
'uuid': window('plex_machineIdentifier'),
|
'uuid': window('plex_machineIdentifier'),
|
||||||
'version': 'irrelevant'
|
'version': 'irrelevant'
|
||||||
}
|
}]
|
||||||
discovered_servers.append(update)
|
|
||||||
|
|
||||||
self.server_list = discovered_servers
|
|
||||||
|
|
||||||
if not self.server_list:
|
|
||||||
self.logMsg("No servers have been discovered", 0)
|
|
||||||
else:
|
|
||||||
self.logMsg("Number of servers Discovered: %s"
|
|
||||||
% len(self.server_list), 2)
|
|
||||||
for items in self.server_list:
|
|
||||||
self.logMsg("Server Discovered: %s" % items, 2)
|
|
||||||
|
|
||||||
def setInterval(self, interval):
|
def setInterval(self, interval):
|
||||||
self.discovery_interval = interval
|
self.discovery_interval = interval
|
||||||
|
|
Loading…
Reference in a new issue