summaryrefslogtreecommitdiffstats
path: root/notFinishedCode/web/mutex1.php
diff options
context:
space:
mode:
Diffstat (limited to 'notFinishedCode/web/mutex1.php')
-rw-r--r--notFinishedCode/web/mutex1.php186
1 files changed, 157 insertions, 29 deletions
diff --git a/notFinishedCode/web/mutex1.php b/notFinishedCode/web/mutex1.php
index 488a9fa..ea44fe7 100644
--- a/notFinishedCode/web/mutex1.php
+++ b/notFinishedCode/web/mutex1.php
@@ -1,56 +1,184 @@
<?php
+//Connection stuff
+//DB
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'randompasswordSQL';
+//Socket
+$port = 34500;
+$ip = 'localhost';
+
+
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
$dbname = 'mutexDB';
mysql_select_db($dbname);
-//$seconds = 10;
+//End of connection stuff
-function Lock($seconds)
- {
- $sql = "SELECT GET_LOCK('Locked', $seconds);";
+//Mutex functions
+function Lock($lockName,$seconds)
+{
+ $sql = "SELECT GET_LOCK('$lockName', $seconds);";
$res = mysql_query($sql);
- return showValue($res);
+ if (!$res)
+ {
+ die('Could not query:' . mysql_error());
+ }
+ return mysql_result($res,0);
}
-function IsLocked()
- {
- $sql = "SELECT IS_FREE_LOCK('Locked');";
+function IsLocked($lockName)
+{
+ $sql = "SELECT IS_FREE_LOCK('$lockName');";
$res = mysql_query($sql);
- return showValue($res);
+ if (!$res)
+ {
+ die('Could not query:' . mysql_error());
+ }
+ return mysql_result($res,0);
}
-function ReleaseLock()
- {
- $sql = "SELECT RELEASE_LOCK('Locked');";
+function ReleaseLock($lockName)
+{
+ $sql = "SELECT RELEASE_LOCK('$lockName');";
$res = mysql_query($sql);
- return showValue($res);
-}
-
-function showValue($res){
- if (!$res) {
+ if (!$res)
+ {
die('Could not query:' . mysql_error());
}
- $num=mysql_numrows($res);
- $i=0;
- $first=mysql_result($res,$i);
- return $first;
+ return mysql_result($res,0);
}
+//End of mutex functions
+
+$nameOfLock = 'Webpage';
+$timeToKeepTheMutex = 360;
+
+$locking = isLocked($nameOfLock);//Check if the webpage is already open
-$locking = isLocked();
-if ($locking == '1')
+if ($locking == '1') //1 means nobody is on the website
{
- $lockMe = Lock(50);
- echo $lockMe . "<br>";
- sleep(10);
- $release = ReleaseLock();
- echo $release . "<br>";
+ $lockMe = Lock($nameOfLock,$timeToKeepTheMutex); //Set a mutex for the webpage
+ if ($lockMe == '1') //1 means a successful lock
+ {
+ //stuff to do here
+ //check if the software is running!
+ $softwareLock = 'SoftwareStarted';
+
+
+ $softwareRunning = isLocked($softwareLock);
+ if($softwareRunning == '1') //1 means software is not started yet, start it
+ {
+
+ exec("/usr/bin/python /var/www/server1.py > /dev/null 2>&1 &"); //start the software in background and don't wait for any output
+ sleep(1); //sleep half second till the software starts
+ //the software should be running already and set the lock, now we test it and then connect to it!
+
+ $softwareRunning1 = isLocked($softwareLock);
+ if($softwareRunning1 == '0') //Software running and waiting for connection!
+ {
+ //try to open the socket and connect to the software
+ $fp = fsockopen($ip, $port, $errno, $error, 5);
+
+ if (!$fp)
+ {
+ echo 'Could not open the socket or connect to the testing software! Check ports on both sides!';
+
+ }
+ else
+ {
+ socket_set_timeout($fp,50); //one should set the number of the longest test, so we can define the timeout function (it is the number after "$fp,")
+
+ fwrite($fp, "START TEST"); //send command to start the tests
+ if(!feof($fp))
+ {
+ while(!feof($fp))
+ {
+ $received = fgets($fp, 128); //receive data!
+
+ if ($received == "CONFIRM\n") //we got the confirmation from the testing software
+ {
+ echo '<table summary = "Results">
+ <thead>
+ <tr>
+ <th scope = "col"> From: </th>
+ <th scope = "col" > To: </th>
+ <th scope = "col"> Message: </th>
+ <th scope = "col"> Status: </th>
+ </tr>
+ </thead>
+ <!-- start of the table rows-->
+ <tbody>';
+ echo str_repeat("\n",7024);
+ flush();
+ }
+ elseif ($received == "TEST DONE\n")
+ {
+ //test finished successfully
+ echo '</tbody></table>';
+ echo 'Test successfully finished!<br>';
+ echo str_repeat("\n",7024);
+ flush();
+ usleep(100);
+ fwrite($fp, 'DISCONNECT'); //tell the testing software it may close
+ break;
+ }
+ else
+ {
+ //parse the received data!
+ echo '<tr>
+ <th scope = "row" id = "r100"> SIP</th>
+ <th scope = "row" id = "r100">GSM1 </th>
+ <td> '. $received . ' </td>
+ <td> 200</td>
+ </tr>';
+ //echo $received . '<br>';
+ echo str_repeat("\n",7024);
+ flush();
+ usleep(100);
+ fwrite($fp, "CONTINUE"); //tell the software to proceed with the test and confirm receiving data
+ }
+ }
+ }
+
+ fclose($fp);//close the soccket and the connection
+
+ }
+
+ }
+ elseif($softwareRunning1 == '1')
+ {
+ echo 'Testing software was not started(didn\'t obtain the lock)! Check it!';
+ }
+ else
+ {
+ echo 'Testing software didn\'t obtain the lock';
+ }
+ }
+ elseif($softwareRunning == '0') //0 means the software is still running in the background
+ {
+ echo 'Testing software is still running! Maybe you should kill it (if not started manually!)';
+ }
+ else
+ {
+ echo 'We have an unknown error! Refresh te web page!';
+ }
+
+ //$release = ReleaseLock($nameOfLock);
+ //echo $release . "<br>";
+ }
+ elseif($lockMe == '0') //0 means if the attempt timed out (for example, because another client has previously locked the name)
+ {
+ echo 'Someone was faster than you';
+ }
+ else
+ {
+ // # if an error occurred (such as running out of memory or the thread was killed with
+ echo 'We have an error here';
+ }
}
-elseif ($locking == '0')
+elseif ($locking == '0') //0 means somebody is already on the web site
{
echo 'Somebody is using the web site already!' . "<br>";
}