3 function authorizeSlice($sn) {
8 $result= $api->GetSlices( Null, array( "name", "slice_id" ) );
10 foreach ( $result AS $slice )
12 if ( $slice["name"] == $sn )
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) {
26 return TOO_MANY_UNITS;
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;
33 if (array_key_exists($requestedTime, $timesOccupied)) {
34 if ($timesOccupied[$requestedTime] == MAX_JOBS)
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;
49 if (array_key_exists($requestedTime, $timesOccupied)) {
50 if ($timesOccupied[$requestedTime] == MAX_JOBS)
53 $timesOccupied[$requestedTime]++;
56 $timesOccupied[$requestedTime] = 1;
57 if (array_key_exists($sn, $jobArray)) {
58 $ts = $jobArray[$sn]["timestamp"];
59 $timesOccupied[$ts]--;
64 return TOO_MANY_UNITS;
69 function findNextFreeSlot($units, $timesOccupied) {
70 $currYear = gmdate("y");
71 $currMonth = gmdate("m");
72 $currDate = gmdate("d");
73 $currHour = gmdate("H") + 1;
74 $currentTime = time();
75 $reqTime = gmmktime($currHour, 0, 0, $currMonth, $currDate, $currYear);
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;
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)
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
94 //lock in case of concurrent accesses
95 flock($fileHandle, LOCK_EX);
97 if ($which == "schedule") { // need to write timestamp in this case
99 fwrite($fileHandle, $s['sec']);
100 fwrite($fileHandle, "\n");
104 foreach ($buffer as $value) {
106 if ($which == "schedule") {
107 if (strcmp($value["timestamp"], time()) > 0) {
108 $numReps = $value["reps"];
109 $ts = $value["timestamp"];
110 $t = $value["sliceName"]." ".$value["id"]." ".$value["timestamp"]." ".$value["units"]." ".$value["reps"]." \n";
112 else { // job expired, does it need be run again?
113 if ($value["reps"] > 0) {
114 $ts = findNextFreeSlot($value["units"], $timesOccupied);
116 $numReps = $value["reps"] - 1;
119 $t = $value["sliceName"]." ".$value["id"]." ".$ts." ".$value["units"]." ".$numReps." \n";
121 else if ($which == "slices") {
122 $t = $value["sliceName"]." ".$value["units"]." \n";
126 fwrite($fileHandle, $t);
130 flock($fileHandle, LOCK_UN);
135 //update the slice file, takes a slice name (name) and number of units
136 function updateSliceFile($name, $units) {
137 $dummyArray = array();
139 $sliceFile = fopen("slices.txt", "rw");
141 echo "<p>Unable to open remote file.</p>";
145 flock($sliceFile, LOCK_EX);
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
156 $newArray["sliceName"] = $sliceName;
157 if ($name == $sliceName) {
158 $newUnits = $unitsAvailable - $units;
159 if ($newUnits < 0) // error, slice has no more units
162 $newArray["units"] = $newUnits;
165 $newArray["units"] = $unitsAvailable;
166 //append this tuple to the entire array
167 $sliceArray[] = $newArray;
169 flock($sliceFile, LOCK_UN);
171 //do the dump to new file
172 dumpToFile("slices.txt", $sliceArray, "slices", dummyArray);
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
180 function isFirstSliceRequest($name) {
181 $sliceFile = fopen("slices.txt", "r");
183 echo "<p>Unable to open remote file.</p>";
187 flock($sliceFile, LOCK_EX);
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
196 if ($name == $sliceName) {
197 flock($sliceFile, LOCK_UN);
203 flock($sliceFile, LOCK_UN);
209 function cmp ($a, $b) {
210 if ($a["timestamp"] == $b["timestamp"])
213 return ($a["timestamp"] < $b["timestamp"]) ? -1 : 1;
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>");
220 else if ($requestStatus == TOO_MANY_UNITS) {
221 printf("<b> Error: Cannot add your request; only 1 extra unit is allowed.</b> <p>");
223 else if ($requestStatus == NO_UNITS_LEFT) {
224 printf("<b> Error: Cannot add your request; no more units remaining.</b> <p>");
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>");
229 else if ($requestStatus == NO_SUCH_SLICE) {
230 printf("<b> Error: Cannot delete nonexistent slice.</b> <p>");
232 else if ($requestStatus == NOT_SLICE_OWNER) {
233 printf("<b> Error: Only authorized user can manipulate slice.</b> <p>");
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>");
240 function getCurrentSchedule (&$jobArray, &$timesOccupied, &$maxId) {
242 $schedFile = fopen("schedule.txt", "r");
244 echo "<p>Unable to open remote file.</p>";
248 flock($schedFile, LOCK_EX);
250 //first line is timestamp, throw it away.
251 fscanf($schedFile, "%s\n", $str);
253 //read in current file into array
256 $timesOccupied = array();
258 while (!feof($schedFile)) {
259 $num = fscanf($schedFile, "%s %d %s %d %d\n", $sliceName, $id, $timestamp, $units, $reps);
264 //for some reason feof seems to not quite work
265 //precisely, the last entry in the file is read twice (!?!), so hack here
268 $newArray["sliceName"] = $sliceName;
269 $newArray["id"] = $id;
270 $newArray["units"] = $units;
271 $newArray["timestamp"] = $timestamp;
272 $newArray["reps"] = $reps;
273 $jobArray[$sliceName] = $newArray;
275 if (array_key_exists($timestamp, $timesOccupied)) {
276 $timesOccupied[$timestamp]++;
279 $timesOccupied[$timestamp] = 1;
284 flock($schedFile, LOCK_UN);
290 function findNextQueue($units, $timesOccupied) {
292 $currYear = gmdate("y");
293 $currMonth = gmdate("m");
294 $currDate = gmdate("d");
295 $currHour = gmdate("H") + 1;
296 $currentTime = time();
297 $reqTime = gmmktime($currHour, 0, 0, $currMonth, $currDate, $currYear);
302 // outputting table to display the queue
303 // green background will mean slot is open, and red will mean the slot is used
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";
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
313 // while ($reqTime < ( $reqTime + ( 24 * 3600 ) ) ) {
315 $retVal = validateRequest($units, $timesOccupied, $reqTime, $currentTime);
316 if ($retVal == SUCCESS) { // advance timestamp one hour (3600 seconds)
318 echo "<tr bgcolor=\"#339933\"><td><input type=\"radio\" name=\"queue_time\" value=\"" . gmdate("H:i:s", $reqTime) . "\"> " . gmdate("H:i:s", $reqTime) . " </td></tr>\n";
321 echo"<tr bgcolor=\"#CC3333\"><td align=center> " . $arr[$x] . " </td></tr>\n";
325 $reqTime = $reqTime + 3600;
328 echo "</table></td><td width=\"6%\"> </td><td><table cellspacing=1 cellpadding=1 border=0 width=130>\n";
330 while ($i < 24 && $i > 11) {
331 $retVal = validateRequest($units, $timesOccupied, $reqTime, $currentTime);
332 if ($retVal == SUCCESS) { // advance timestamp one hour (3600 seconds)
334 echo "<tr bgcolor=\"#339933\"><td><input type=\"radio\" name=\"queue_time\" value=\"" . gmdate("H:i:s", $reqTime) . "\"> " . gmdate("H:i:s", $reqTime) . " </td></tr>\n"; }
336 echo"<tr bgcolor=\"#CC3333\"><td align=center> " . $arr[$x] . " </td></tr>\n";
340 $reqTime = $reqTime + 3600;
343 echo "</table></td></tr>\n";
350 function sliceDropDown() {
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']);
360 usort($result, '__cmp_slices');
362 foreach ( $result AS $slice )
364 echo "<option value='" . $slice["name"] . "'>" . $slice["name"] . "\n";