summaryrefslogtreecommitdiffstats
path: root/backupWebsite/devconf.php
blob: c983c148d0f0f8c0283e5e0c1b9ef06bbca15adc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?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 = 'gsmselftesting';
mysql_select_db($dbname);
//End of connection stuff
include 'mutexFunctions.php'; //mutex functions, isfree, lock and release
//include 'insertData.php'; //insert task functions

$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;
															}

													}
											}
									}
								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';
	}
?>