use the new get machine id code to fix a race condition bug

This commit is contained in:
faush01 2015-03-14 13:04:18 +11:00
parent 50d3129010
commit 5e580e6e2c
3 changed files with 77 additions and 20 deletions

View file

@ -2,7 +2,8 @@ from uuid import uuid4 as uuid4
import xbmc
import xbmcaddon
import xbmcgui
import os
from Lock import Lock
class ClientInformation():
@ -11,21 +12,39 @@ class ClientInformation():
WINDOW = xbmcgui.Window( 10000 )
clientId = WINDOW.getProperty("client_id")
self.addonSettings = xbmcaddon.Addon(id='plugin.video.mb3sync')
if(clientId == None or clientId == ""):
xbmc.log("CLIENT_ID - > No Client ID in WINDOW")
clientId = self.addonSettings.getSetting('client_id')
if(clientId != None and clientId != ""):
return clientId
if(clientId == None or clientId == ""):
xbmc.log("CLIENT_ID - > No Client ID in SETTINGS")
uuid = uuid4()
clientId = str("%012X" % uuid)
# we need to load and or generate a client machine id
__addon__ = xbmcaddon.Addon(id='plugin.video.mb3sync')
__addondir__ = xbmc.translatePath( __addon__.getAddonInfo('path'))
machine_guid_lock_path = os.path.join(__addondir__, "machine_guid.lock")
machine_guid_path = os.path.join(__addondir__, "machine_guid")
clientId = ""
try:
lock = Lock(machine_guid_lock_path)
locked = lock.acquire()
if(locked == True):
fd = os.open(machine_guid_path, os.O_CREAT|os.O_RDWR)
clientId = os.read(fd, 256)
if(len(clientId) == 0):
uuid = uuid4()
clientId = str("%012X" % uuid)
xbmc.log("CLIENT_ID - > Client ID saved to FILE : " + clientId)
os.write(fd, clientId)
os.fsync(fd)
os.close(fd)
xbmc.log("CLIENT_ID - > Client ID saved to WINDOW : " + clientId)
WINDOW.setProperty("client_id", clientId)
self.addonSettings.setSetting('client_id', clientId)
xbmc.log("CLIENT_ID - > New Client ID : " + clientId)
else:
WINDOW.setProperty('client_id', clientId)
xbmc.log("CLIENT_ID - > Client ID saved to WINDOW from Settings : " + clientId)
finally:
lock.release()
return clientId

View file

@ -102,12 +102,10 @@ class ConnectionManager():
if(return_value > -1):
selected_user = userList[return_value]
self.printDebug("Setting Selected User : " + selected_user)
if self.addonSettings.getSetting("port") != server_port:
self.addonSettings.setSetting("port", server_port)
if self.addonSettings.getSetting("ipaddress") != server_address:
self.addonSettings.setSetting("ipaddress", server_address)
if self.addonSettings.getSetting("username") != selected_user:
self.addonSettings.setSetting("username", selected_user)
self.addonSettings.setSetting("port", server_port)
self.addonSettings.setSetting("ipaddress", server_address)
self.addonSettings.setSetting("username", selected_user)
downloadUtils.authenticate()
def getServerDetails(self):

40
resources/lib/Lock.py Normal file
View file

@ -0,0 +1,40 @@
import os
import time
import errno
import xbmc
class Lock:
def __init__(self, filename):
self.filename = filename
self.delay = 0.5
self.timeout = 10
self.is_locked = False
self.fd = None
def acquire(self):
start_time = time.time()
while True:
try:
self.fd = os.open(self.filename, os.O_CREAT|os.O_RDWR|os.O_EXCL)
break;
except OSError as e:
if (time.time() - start_time) >= self.timeout:
xbmc.log("File_Lock_On " + self.filename + " timed out")
return False
#xbmc.log("File_Lock_On " + self.filename + " error " + str(e))
time.sleep(self.delay)
self.is_locked = True
xbmc.log("File_Lock_On " + self.filename + " obtained")
return True
def release(self):
if self.is_locked:
os.close(self.fd)
os.unlink(self.filename)
self.is_locked = False
xbmc.log("File_Lock_On " + self.filename + " released")
def __del__(self):
self.release()