parent
9901935b83
commit
7199561573
1 changed files with 21 additions and 112 deletions
|
@ -215,119 +215,28 @@ 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:
|
||||||
currServerProt, currServerIP, currServerPort = \
|
return
|
||||||
currServer.split(':')
|
currServerProt, currServerIP, currServerPort = \
|
||||||
currServerIP = currServerIP.replace('/', '')
|
currServer.split(':')
|
||||||
for server in discovered_servers:
|
currServerIP = currServerIP.replace('/', '')
|
||||||
if server['server'] == currServerIP:
|
# Currently active server was not discovered via GDM; ADD
|
||||||
break
|
self.server_list = [{
|
||||||
else:
|
'port': currServerPort,
|
||||||
# Currently active server was not discovered via GDM; ADD
|
'protocol': currServerProt,
|
||||||
update = {
|
'class': None,
|
||||||
'port': currServerPort,
|
'content-type': 'plex/media-server',
|
||||||
'protocol': currServerProt,
|
'discovery': 'auto',
|
||||||
'class': None,
|
'master': 1,
|
||||||
'content-type': 'plex/media-server',
|
'owned': '1',
|
||||||
'discovery': 'auto',
|
'role': 'master',
|
||||||
'master': 1,
|
'server': currServerIP,
|
||||||
'owned': '1',
|
'serverName': window('plex_servername'),
|
||||||
'role': 'master',
|
'updated': int(time.time()),
|
||||||
'server': currServerIP,
|
'uuid': window('plex_machineIdentifier'),
|
||||||
'serverName': window('plex_servername'),
|
'version': 'irrelevant'
|
||||||
'updated': int(time.time()),
|
}]
|
||||||
'uuid': window('plex_machineIdentifier'),
|
|
||||||
'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