3d9a161be8d5c8c03622481706ea3c17e13698ed
[plewww.git] / planetlab / sirius / sirius_func.php
1 <?php
2
3 function authorizeSlice($sn) {
4         
5         global $api;
6   
7   $slice_list= array();
8   $result= $api->GetSlices( Null, array( "name", "slice_id" ) );
9   
10   foreach ( $result AS $slice )
11   {
12         if ( $slice["name"] == $sn )
13                 return 1;
14         
15   }
16   
17   return 0;
18   
19 }
20
21 //can a request be satisfied?  Currently, answer is yes unless
22 //either this time is taken or you asked for more than one unit
23 //probably will need to change this.  
24 function validateRequest ($units, $timesOccupied, $requestedTime, $currentTime) {
25   if ($units != 1)
26     return TOO_MANY_UNITS;
27
28   // buffer so we aren't too close to deadline, if your request is late
29   // OR if it's within 1 minute of deadline, it's too late
30   if ($requestedTime - 60 <= $currentTime)
31     return TIME_ALREADY_OCCURRED;
32
33   if (array_key_exists($requestedTime, $timesOccupied)) {
34     if ($timesOccupied[$requestedTime] == MAX_JOBS)
35       return NO_ROOM;
36   }
37   return SUCCESS;
38 }
39
40 //can a request be satisfied?  Currently, answer is yes unless
41 //either this time is taken or you asked for more than one unit
42 //probably will need to change this.  
43 function validateAndMarkRequest ($units, &$timesOccupied, $requestedTime, $currentTime, $sn, $jobArray) {
44   // buffer so we aren't too close to deadline, if your request is late
45   // OR if it's within 1 minute of deadline, it's too late
46   if ($requestedTime - 60 <= $currentTime)
47     return TIME_ALREADY_OCCURRED;
48
49   if (array_key_exists($requestedTime, $timesOccupied)) {
50     if ($timesOccupied[$requestedTime] == MAX_JOBS)
51       return NO_ROOM;
52     else
53       $timesOccupied[$requestedTime]++;
54   }
55   else {
56     $timesOccupied[$requestedTime] = 1;
57     if (array_key_exists($sn, $jobArray)) {
58       $ts = $jobArray[$sn]["timestamp"];
59       $timesOccupied[$ts]--;
60     }
61   }
62
63   if ($units != 1)
64     return TOO_MANY_UNITS;
65
66   return SUCCESS;
67 }
68
69 function findNextFreeSlot($units, $timesOccupied) {
70   $currYear = gmdate("y");
71   $currMonth = gmdate("m");
72   $currDate = gmdate("d");
73   $currHour = gmdate("H") + 1;
74   $currentTime = gmmktime();
75   $reqTime = gmmktime($currHour, 0, 0, $currMonth, $currDate, $currYear);
76   $retVal = 1;
77   while ($retVal != SUCCESS) {
78     $retVal = validateRequest($units, $timesOccupied, $reqTime, $currentTime);
79     if ($retVal == NO_ROOM || $retVal == TIME_ALREADY_OCCURRED) { // advance timestamp one hour (3600 seconds)
80       $reqTime = $reqTime + 3600;
81     }
82   }
83   return $reqTime;
84 }
85
86 function dumpToFile($fileName, $buffer, $which, $timesOccupied) {
87   //open file, and dump newly list into it (buffer is the list)
88   //we're just currently overwriting (fopen with "w" truncates)
89
90   $fileHandle = fopen($fileName, "w");
91   //periodically, updateSliceUnits program will update the slices file
92   //this function is general, works for schedule or slices file
93
94   //lock in case of concurrent accesses
95   flock($fileHandle, LOCK_EX);
96   
97   if ($which == "schedule") {  // need to write timestamp in this case
98     $s = gettimeofday();
99     fwrite($fileHandle, $s['sec']);
100     fwrite($fileHandle, "\n");
101   }
102
103   //do the dump here
104   foreach ($buffer as $value) {
105     $t = "";
106     if ($which == "schedule") {  
107       if (strcmp($value["timestamp"], mktime()) > 0) {
108         $numReps = $value["reps"];
109         $ts = $value["timestamp"];
110         $t = $value["sliceName"]." ".$value["id"]." ".$value["timestamp"]." ".$value["units"]." ".$value["reps"]." \n";
111       }
112       else {  // job expired, does it need be run again?
113         if ($value["reps"] > 0) {
114           $ts = findNextFreeSlot($value["units"], $timesOccupied);
115         }
116         $numReps = $value["reps"] - 1;
117       }
118       if ($numReps >= 0)
119         $t = $value["sliceName"]." ".$value["id"]." ".$ts." ".$value["units"]." ".$numReps." \n";
120     }
121     else if ($which == "slices") {
122       $t = $value["sliceName"]." ".$value["units"]." \n";
123     }
124
125     if ($t != "")
126       fwrite($fileHandle, $t);
127
128   }
129
130   flock($fileHandle, LOCK_UN);
131
132   fclose($fileHandle);
133 }
134
135 //update the slice file, takes a slice name (name) and number of units
136 function updateSliceFile($name, $units) {
137   $dummyArray = array();
138
139   $sliceFile = fopen("slices.txt", "rw");
140   if (!$sliceFile) {
141     echo "<p>Unable to open remote file.</p>"; 
142     
143   }
144
145   flock($sliceFile, LOCK_EX);
146
147   //we'll construct a new list here, will be current slice file except 
148   //the slice in question will have it's units decreased, if there are any...
149   while (!feof($sliceFile)) {
150     $num = fscanf($sliceFile, "%s %d\n", $sliceName, $unitsAvailable);
151     //for some reason feof seems to not quite work
152     //precisely, the last entry in the file is read twice (!?!), so hack here
153     if ($num == 0)
154       break;
155
156     $newArray["sliceName"] = $sliceName;
157     if ($name == $sliceName) {
158       $newUnits = $unitsAvailable - $units;
159       if ($newUnits < 0)  // error, slice has no more units
160         return -1;
161       else
162         $newArray["units"] = $newUnits;
163     }
164     else
165       $newArray["units"] = $unitsAvailable;
166     //append this tuple to the entire array
167     $sliceArray[] = $newArray;
168   }
169   flock($sliceFile, LOCK_UN);
170   fclose($sliceFile);
171   //do the dump to new file
172   dumpToFile("slices.txt", $sliceArray, "slices", dummyArray);
173   return 0;
174 }
175
176
177 //pretty obvious what this does; basically, does the slice exist in
178 //the slice file yet?  (New user of calendar service user may not have 
179 //an entry)
180 function isFirstSliceRequest($name) {
181   $sliceFile = fopen("slices.txt", "r");
182   if (!$sliceFile) {
183     echo "<p>Unable to open remote file.</p>"; 
184     
185   }
186
187   flock($sliceFile, LOCK_EX);
188
189   while (!feof($sliceFile)) {
190     $num = fscanf($sliceFile, "%s %d\n", $sliceName, $unitsAvailable);
191     //for some reason feof seems to not quite work
192     //precisely, the last entry in the file is read twice (!?!), so hack here
193     if ($num == 0)
194       break;
195
196     if ($name == $sliceName) {
197       flock($sliceFile, LOCK_UN);
198       fclose($sliceFile);
199       return 0;
200     }
201   }
202
203   flock($sliceFile, LOCK_UN);
204   fclose($sliceFile);
205   return 1;
206 }
207
208
209 function cmp ($a, $b) {
210   if ($a["timestamp"] == $b["timestamp"])
211     return 0;
212   else
213     return ($a["timestamp"] < $b["timestamp"]) ? -1 : 1;
214 }
215
216 function checkForErrors($requestStatus) {
217   if ($requestStatus == NO_ROOM) {
218     printf("<b> Error: Cannot add your request; that time slot is currently full. </b> <p>");
219   }
220   else if ($requestStatus == TOO_MANY_UNITS) {
221     printf("<b> Error: Cannot add your request; only 1 extra unit is allowed.</b> <p>");
222   }
223   else if ($requestStatus == NO_UNITS_LEFT) {
224     printf("<b> Error: Cannot add your request; no more units remaining.</b> <p>");
225   }
226   else if ($requestStatus == TIME_ALREADY_OCCURRED) {
227     printf("<b> Error: Cannot add your request; that time has already occurred, or is too close to the current time.</b> <p>");
228   }
229   else if ($requestStatus == NO_SUCH_SLICE) {
230     printf("<b> Error: Cannot delete nonexistent slice.</b> <p>");
231   }
232   else if ($requestStatus == NOT_SLICE_OWNER) {
233     printf("<b> Error: Only authorized user can manipulate slice.</b> <p>");
234   }
235   else if ($requestStatus == TOO_CLOSE_TO_DEADLINE) {
236     printf("<b> Error: Cannot delete your request; it is too close to the time that your slice will receive its priority increase.</b> <p>");
237   }
238 }
239
240 function getCurrentSchedule (&$jobArray, &$timesOccupied, &$maxId) {
241
242   $schedFile = fopen("schedule.txt", "r");
243   if (!$schedFile) {
244     echo "<p>Unable to open remote file.</p>"; 
245     
246   }
247
248   flock($schedFile, LOCK_EX);
249
250   //first line is timestamp, throw it away.
251   fscanf($schedFile, "%s\n", $str);
252
253   //read in current file into array
254   $jobArray = array();
255   $newArray = array();
256   $timesOccupied = array();
257   $maxId = 0;
258   while (!feof($schedFile)) {
259     $num = fscanf($schedFile, "%s %d %s %d %d\n", $sliceName, $id, $timestamp, $units, $reps);
260
261     if ($id > $maxId)
262       $maxId = $id;
263
264     //for some reason feof seems to not quite work
265     //precisely, the last entry in the file is read twice (!?!), so hack here
266     if ($num == 0)
267       break;
268     $newArray["sliceName"] = $sliceName;
269     $newArray["id"] = $id;
270     $newArray["units"] = $units;
271     $newArray["timestamp"] = $timestamp;
272     $newArray["reps"] = $reps;
273     $jobArray[$sliceName] = $newArray;
274
275     if (array_key_exists($timestamp, $timesOccupied)) {
276       $timesOccupied[$timestamp]++;
277     }
278     else {
279       $timesOccupied[$timestamp] = 1;
280     }
281
282   }
283
284   flock($schedFile, LOCK_UN);
285   fclose($schedFile);
286
287 }
288
289
290 function findNextQueue($units, $timesOccupied) {
291   global $arr;
292   $currYear = gmdate("y");
293   $currMonth = gmdate("m");
294   $currDate = gmdate("d");
295   $currHour = gmdate("H") + 1;
296   $currentTime = gmmktime();
297   $reqTime = gmmktime($currHour, 0, 0, $currMonth, $currDate, $currYear);
298   $retVal = 1;
299   $i = 0;
300         
301         // DAVE
302         // outputting table to display the queue
303         // green background will mean slot is open, and red will mean the slot is used
304         // 
305   echo "<table cellspacing=\"2\" cellpadding=\"1\" border=\"0\" width=550>\n";
306   echo "<tr><td colspan=\"3\"><span class='bold'>24 hour Queue:</span> Choose the GMT time slot you desire (<font color=\"#339933\">green</font> slots are open, <font color=\"#CC3333\">red</font> are taken) <p></td></tr>\n";
307   echo "<tr><td width=\"47%\" align=\"right\"><table cellspacing=1 cellpadding=1 border=0 width=130>\n";
308
309   // here's what this does below: it goes through each hour, and sees if the slot is occupied
310   // if so, it outputs in red, w/ slice name ($arr[$x], where $x is the number request, i.e.
311   // earlier when we dump out the list of slices on the schedule, we do $arr[$x++] = $slicename
312   $x= 0;
313   //  while ($reqTime < ( $reqTime + ( 24 * 3600 ) ) ) {
314   while ($i < 12) {
315     $retVal = validateRequest($units, $timesOccupied, $reqTime, $currentTime);
316     if ($retVal == SUCCESS) { // advance timestamp one hour (3600 seconds)
317           
318         echo "<tr bgcolor=\"#339933\"><td><input type=\"radio\" name=\"queue_time\" value=\"" . gmdate("H:i:s", $reqTime) . "\"> " . gmdate("H:i:s", $reqTime) . " &nbsp;  </td></tr>\n";
319     }
320     else {
321         echo"<tr bgcolor=\"#CC3333\"><td align=center> " . $arr[$x] . " </td></tr>\n";
322         $x++;
323     }
324
325     $reqTime = $reqTime + 3600;
326     $i++;
327   }
328   echo "</table></td><td width=\"6%\"> &nbsp; </td><td><table cellspacing=1 cellpadding=1 border=0 width=130>\n";
329
330   while ($i < 24 && $i > 11) {
331     $retVal = validateRequest($units, $timesOccupied, $reqTime, $currentTime);
332     if ($retVal == SUCCESS) { // advance timestamp one hour (3600 seconds)
333
334         echo "<tr bgcolor=\"#339933\"><td><input type=\"radio\" name=\"queue_time\" value=\"" . gmdate("H:i:s", $reqTime) . "\"> " . gmdate("H:i:s", $reqTime) . " &nbsp;  </td></tr>\n";     }
335     else {
336         echo"<tr bgcolor=\"#CC3333\"><td align=center> " . $arr[$x] . " </td></tr>\n";
337         $x++;
338     }
339
340     $reqTime = $reqTime + 3600;
341     $i++;
342   }
343   echo "</table></td></tr>\n";
344
345   echo "</table>\n";
346
347 }
348
349
350 function sliceDropDown() {
351
352         global $api;
353   
354   $slice_list= array();
355   $result= $api->GetSlices( Null, array( "name", "slice_id" ) );
356   // sort_slices( $result ); --> slice sort on name
357   function __cmp_slices($a, $b) {
358     return strcasecmp($a['name'], $b['name']);
359   }
360   usort($result, '__cmp_slices');
361
362   foreach ( $result AS $slice )
363   {
364         echo "<option value='" . $slice["name"] . "'>" . $slice["name"] . "\n";
365         
366   }
367   
368 }
369
370
371
372
373
374 ?>