summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRefik Hadzialic2011-09-02 17:54:34 +0200
committerRefik Hadzialic2011-09-02 17:54:34 +0200
commit9c5ca9b2a11f6734e6d5e81c6ae8f4c3c3c6d162 (patch)
treee50be7dd4c9d25701f2357bfccb85499d25d8305
parentMerge branch 'master' of lab.ks.uni-freiburg.de:lsfks/projekte/gsm-selftest (diff)
downloadgsm-selftest-9c5ca9b2a11f6734e6d5e81c6ae8f4c3c3c6d162.tar.gz
gsm-selftest-9c5ca9b2a11f6734e6d5e81c6ae8f4c3c3c6d162.tar.xz
gsm-selftest-9c5ca9b2a11f6734e6d5e81c6ae8f4c3c3c6d162.zip
I added the Mutex to the dbClass and example in dbDemo!
-rw-r--r--notFinishedCode/dbClass.py78
-rw-r--r--notFinishedCode/dbClass.pycbin7364 -> 9074 bytes
-rw-r--r--notFinishedCode/dbDemo.py5
3 files changed, 83 insertions, 0 deletions
diff --git a/notFinishedCode/dbClass.py b/notFinishedCode/dbClass.py
index 442e777..969e346 100644
--- a/notFinishedCode/dbClass.py
+++ b/notFinishedCode/dbClass.py
@@ -10,6 +10,7 @@ class DBMySQLConnection:
self.db = dbname
self.connectionCreated = 0
self.tasksList = list()
+ self.lockSet = 0
global debugMode
debugMode = 0
@@ -286,3 +287,80 @@ class DBMySQLConnection:
return 2 #that task wasn't found
else:
return 0
+
+ def lockMutex(self, seconds):
+ if self.connectionCreated == 1:
+ try:
+ successful = self.cur.execute("SELECT IS_FREE_LOCK('SoftwareStarted')")
+ output = self.cur.fetchone()
+ if output[0] == 1:
+ #resource is free which means software is not started yet
+ successful = self.cur.execute("SELECT GET_LOCK('SoftwareStarted', %i)"%(int(seconds)))
+
+ output1 = self.cur.fetchone()
+ if output1[0] == 1:
+ #I got the lock now
+ self.lockSet = 1
+ return 1
+ elif output1[0] == 0:
+ return 7 #if the attempt timed out (for example, because another client has previously locked the name)
+ else:
+ return 6 # if an error occurred (such as running out of memory or the thread was killed with
+ elif output[0] ==0:
+ return 4 #software is already running and somebody has allocated the mutex
+ else:
+ #means some not so good bug
+ return 5 # if an error occurs (such as an incorrect argument).
+ except MySQLdb.Error, e:
+ error = str(e)
+ if error[1:5] == '1062':
+ return 2 #duplicate entry for the key
+ if debugMode == 1:
+ print str(e)
+ return 3
+ else:
+ return 0
+
+ def releaseMutex(self):
+ if self.connectionCreated == 1:
+ try:
+ if self.lockSet == 1:
+ successful = self.cur.execute("SELECT IS_FREE_LOCK('SoftwareStarted')")
+ output = self.cur.fetchone()
+ if output[0] == 1:
+ #the lock seems to be free
+ self.lockSet = 0
+ return 4
+
+ elif output[0] == 0:
+
+ #try to release the lock
+ successful = self.cur.execute("SELECT RELEASE_LOCK('SoftwareStarted')")
+ output1 = self.cur.fetchone()
+ if output1[0] == 1:
+ #the lock was released successfully
+ return 1
+
+ elif output1[0] == 0:
+ # if the lock was not established by this thread (in which case the lock is not released)
+ return 5
+ else:
+ # and NULL if the named lock did not exist. The lock does not exist if it was never obtained by a call to GET_LOCK() or if it has previously been released.
+ return 6
+ else:
+ #some serious problem
+ #and NULL if an error occurs (such as an incorrect argument).
+ return 5
+
+ else:
+ return 7 #the lock wasn't set
+
+ except MySQLdb.Error, e:
+ error = str(e)
+ if error[1:5] == '1062':
+ return 2 #duplicate entry for the key
+ if debugMode == 1:
+ print str(e)
+ return 3
+ else:
+ return 0
diff --git a/notFinishedCode/dbClass.pyc b/notFinishedCode/dbClass.pyc
index 28c4dbf..09a766a 100644
--- a/notFinishedCode/dbClass.pyc
+++ b/notFinishedCode/dbClass.pyc
Binary files differ
diff --git a/notFinishedCode/dbDemo.py b/notFinishedCode/dbDemo.py
index 50dc6b6..713768d 100644
--- a/notFinishedCode/dbDemo.py
+++ b/notFinishedCode/dbDemo.py
@@ -1,4 +1,5 @@
import dbClass
+from time import sleep
user = 'root'
passw = 'randompasswordSQL'
host = 'localhost'
@@ -7,6 +8,10 @@ dbname = 'gsmselftesting'
x = dbClass.DBMySQLConnection(user, passw, host, dbname)
print "connected to the DB ", x.connectDB()
print "do i have anything to do", x.anyTasksToDo()
+print 'mutex says' , x.lockMutex(10)
+print 'release mutex says ', x.releaseMutex()
+sleep(45)
+
for item in x.tasksList:
item = x.tasksList[index]
tempItem = item