summaryrefslogblamecommitdiffstats
path: root/Website/devconf.php
blob: bd13067db988b7466faea4f40c3ef01be0054870 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11



                  
                           





                                                                                     


                                                                         
 





























































                                                                                                                                                          




                                                                                                                                          















































                                                                                                                                                
<?php

//Connection stuff
//DB
include 'dbconnection.php';

//Socket
$port = 34500;
$ip = 'localhost';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('Error connecting to mysql');
mysql_select_db($dbname);
//End of connection stuff
include 'mutexFunctions.php'; //mutex functions, isfree, lock and release


$nameOfLock = 'Webpage';
$timeToKeepTheMutex = 360; 

$locking = isLocked($nameOfLock);//Check if the webpage is already open
if ($locking == '1') //1 means nobody is on the website 
	{
		
 		$lockMe = Lock($nameOfLock,$timeToKeepTheMutex); //Set a mutex for the webpage 
 		if ($lockMe == '1')
			{
				
				$softwareLock = 'SoftwareStarted';
				$softwareRunning = isLocked($softwareLock);
				if($softwareRunning == '1') //1 means software is not started yet, start it 
    					{
						
						$startApp = fsockopen($ip, 34600, $errno, $error, 5);
       						if (!$startApp)
       							{
          							printf('THE SOFTWARE THAT SHOULD RUN IN BACKGROUND IS NOT RUNNING!');
       							}
       						else
       							{
								
          							socket_set_timeout( $startApp, 50);
          							fwrite($startApp, "START APP");
          							usleep(2500);
          							fclose($startApp);
       							}
						sleep(6); 
						$softwareRunning1 = isLocked($softwareLock);
       						if($softwareRunning1 == '0')
       							{
								$fp = fsockopen($ip, $port, $errno, $error, 5);
								if (!$fp)
 	  								{
             									echo 'Could not open the socket or connect to the testing software!';
              
 	  								}
								else
          								{
										
										socket_set_timeout($fp,50);
										fwrite($fp, "UPDATE DEVICE");
										if(!feof($fp))
             										{
												while(!feof($fp))
                											{
														$received =  fgets($fp, 128);
														if ($received == "CONFIRM\n") 
                   													{
																flush();
															}
														elseif ($received == "TEST DONE\n")
                   													{
																flush();
																usleep(100);
																echo 'Update device Done';
																fwrite($fp, 'DISCONNECT');
																break;
															}
														else {
															flush();
															usleep(100);
															echo ''.$received;
															}

													}
											}
									}
								fclose($fp);
							}
       						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!';
    					}
			}
		elseif($lockMe == '0')
 			{
    				echo 'Someone was faster than you';
 			}
 		else
 			{

    				echo 'We have an error here';
 			}
	}

elseif ($locking == '0') //0 means somebody is already on the web site
	{
		echo 'Somebody is using the web site already!' . "<br>";
	}
else
	{
		echo 'We have some serious error here';
	}
?>