summaryrefslogtreecommitdiffstats
path: root/For Weekly Test/tricode/mutexTry.php
blob: e6d4088c82f320d871a56658af09f2135342c4bb (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<html>
<head>
<script src='delayedLoading.js' type="text/javascript"></script>
<!--ADD HERE ALL THE NEEDED META DATA -->
<title>Network test live results!</title>
<link rel="stylesheet" href="css/tableCSS.css" type="text/css" media="screen">
</head>
<body onscroll="scrollEvent();" onload="loaderInit();">
<div>
<?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') //1 means a successful lock
 {
    include 'post.php'; // read posted data and add them to the database
//    echo 'WE had errors: '. $errorAdding;
    //stuff to do here
    //check if the software is running! 
    $softwareLock = 'SoftwareStarted';
  $taskAmount= 1  
  if($taskAmount!=0)
  { 
    $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); //one should set the number of the longest test, so we can define the timeout
          fwrite($startApp, "START APP");
          usleep(2500);
          fclose($startApp);
       }

       //exec("/usr/bin/python /var/www/tricode/gsmselftest2.py > /dev/null 2>&1 &"); //start the software in background and don't wait for any output
       sleep(6); //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, "SMART 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
                   {
		      //test finished successfully 
			$GSMRZ1 = 0;
			$GSMRZ2 = 0;
			$GSMRZ3 = 0;

			$SIP = 0;
			$UNISIP = 0;
			$LANDLINE = 0;
			$LSFKS = 0;

			$GSMEE = 0;
			$GSMEV = 0;
			$GSMET = 0;
			$GSMEO = 0;

			$SIPP = 0;
			$LANDLINEP = 0;
			$UNISIPP = 0;
			$NANOBTS1P = 0;
			$NANOBTS2P = 0;
		      /////////// end of test!	
		      echo '<div id="main">
  		      <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")
                   {

                       echo '</tbody></table>';
                       echo '</div>';
		       echo '<div id="sidebar">';
		       echo "<a class='pChart' href='networkResult.php?S=$SIP&RZ1=$GSMRZ1&RZ2=$GSMRZ2&RZ3=$GSMRZ3&US=$UNISIP&LL=$LANDLINE&LS=$LSFKS&EE=$GSMEE&EV=$GSMEV&ET=$GSMET&EO=$GSMEO&SP=$SIPP&SGP=$LANDLINEP&USP=$UNISIPP&NBP1=$NANOBTS1P&NBP2=$NANOBTS2P' data-pchart-alt='Picture3'>Picture 3</a></div>";
		       echo '<div id="footer">Number of tasks: '.$taskAmount.'<br> Number of tasks tried to add to the DB but had an error: '. $errorAdding .'<br> TEST SUCCESSFULLY FINISHED!</div>';
                       echo str_repeat("\n",7024);
                       flush();
                       usleep(100);
                       fwrite($fp, 'DISCONNECT'); //tell the testing software it may close
                       break;
                   }
		   else
		   {
 		      //parse the received data!
		      $received = str_replace("\n",'',$received);
                      $dataForTable = explode("|", $received);
			$callFrom = strtoupper($dataForTable[1]);
			$callTo = strtoupper($dataForTable[2]);
			$testStatus = strtoupper($dataForTable[3]);
		      //ADD HERE THE TEST CASES BUT MAKE IT INSIDE OF A FILE (e.q. include 'testing.php'); HERE I PUT IT ONLY FOR EXAMPLE PURPOSES!

		      if($callFrom == 'SIP' || $callTo == 'SIP')
 			{
				if($testStatus == '200')
				{
					$SIP = 1;
					echo 'SIP set to 1';
				}
			}
		      if($callFrom == 'GSMRZ1' || $callTo == 'GSMRZ1')
			{
				if($testStatus == '200')
				{
					echo 'GSMRZ1 set to 1';
					$GSMRZ1 = 1;
				}
			}

		      //END OF TEST CASE EXAMPLE
                      echo '<tr> 
		      <th scope = "row" id = "r100">'. $dataForTable[1] . '</th> 
                      <th scope = "row" id = "r100">'. $dataForTable[2] . '</th> 
                      <td> ' . $dataForTable[4] . ' </td> 
                      <td>' . $dataForTable[3] . '</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>";
  }
  else
  {
     echo 'No single test was selected! Please go back and select at least one test';
  }
 }
 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') //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';
}
?>
</div>
</body>
</html>