6 changed files with 111 additions and 148 deletions
-
1TODO
-
104ajax+json_home/ajax.php
-
2ajax+json_home/test1.html
-
4libs/errException.php
-
58libs/mutualExclusion.php
-
90pvtest.php
@ -1,67 +1,49 @@ |
|||||
<?php |
<?php |
||||
|
|
||||
// $english = mysql_escape_string($_REQUEST['translate']);
|
|
||||
// Der Service ist zur Zeit leider deaktiviert....
|
|
||||
// $trans = new SoapClient(
|
|
||||
// "http://www.xmethods.net/sd/2001/BabelFishService.wsdl");
|
|
||||
|
|
||||
/* |
|
||||
try |
|
||||
{ |
|
||||
$german = $trans->BabelFish("en_de",$english); |
|
||||
$french = $trans->BabelFish("en_fr",$english); |
|
||||
} |
|
||||
catch(SoapFault $e) |
|
||||
{ |
|
||||
$english = "not found"; |
|
||||
$german = "not found"; |
|
||||
$french = "not found"; |
|
||||
} |
|
||||
*/ |
|
||||
class dummy |
|
||||
{ |
|
||||
// nur public member werden via json verschickt.
|
|
||||
public $german; |
|
||||
public $english; |
|
||||
public $french; |
|
||||
|
|
||||
function __construct ($english, $german, $french) |
|
||||
{ |
|
||||
$this->german = $german; |
|
||||
$this->english = $english; |
|
||||
$this->french["val1"] = $french; |
|
||||
$this->french["val2"] = "jokus"; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
$encoding = FALSE; |
|
||||
if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && |
|
||||
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE) |
|
||||
$encoding = "x-gzip"; |
|
||||
if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && |
|
||||
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
|
||||
$encoding = "gzip"; |
|
||||
|
|
||||
header ('Content-type: text/plain'); |
|
||||
|
|
||||
$result = FALSE; |
|
||||
if ($encoding !== FALSE) |
|
||||
$result = gzcompress (json_encode (new dummy ( |
|
||||
'doing gzip', 'gezipedte Daten', 'la zippo'))); |
|
||||
|
|
||||
if ($result !== FALSE) |
|
||||
|
class dummy |
||||
|
{ |
||||
|
// nur public member werden via json verschickt.
|
||||
|
public $german; |
||||
|
public $english; |
||||
|
public $french; |
||||
|
|
||||
|
function __construct ($english, $german, $french) |
||||
{ |
{ |
||||
header ('Content-Encoding: ' . $encoding); |
|
||||
|
|
||||
print ("\x1f\x8b\x08\x00\x00\x00\x00\x00"); |
|
||||
print ($result); |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
$result = json_encode (new dummy ( |
|
||||
'doing no gzip', 'nicht gezipedte Daten', 'no la zippo')); |
|
||||
|
|
||||
print ($result); |
|
||||
|
$this->german = $german; |
||||
|
$this->english = $english; |
||||
|
$this->french["val1"] = $french; |
||||
|
$this->french["val2"] = "jokus"; |
||||
} |
} |
||||
|
} |
||||
|
|
||||
|
$encoding = FALSE; |
||||
|
if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && |
||||
|
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE) |
||||
|
$encoding = "x-gzip"; |
||||
|
if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && |
||||
|
strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) |
||||
|
$encoding = "gzip"; |
||||
|
|
||||
|
header ('Content-type: text/plain'); |
||||
|
|
||||
|
$result = FALSE; |
||||
|
if ($encoding !== FALSE) |
||||
|
$result = gzcompress (json_encode (new dummy ( |
||||
|
'doing gzip', 'gezipedte Daten', 'la zippo'))); |
||||
|
|
||||
|
if ($result !== FALSE) |
||||
|
{ |
||||
|
header ('Content-Encoding: ' . $encoding); |
||||
|
|
||||
|
print ("\x1f\x8b\x08\x00\x00\x00\x00\x00"); |
||||
|
print ($result); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
$result = json_encode (new dummy ( |
||||
|
'doing no gzip', 'nicht gezipedte Daten', 'no la zippo')); |
||||
|
|
||||
|
print ($result); |
||||
|
} |
||||
|
|
||||
?>
|
?>
|
||||
@ -0,0 +1,58 @@ |
|||||
|
<?php |
||||
|
|
||||
|
require_once dirname(__FILE__) . '/../config.php'; |
||||
|
require_once LIBDIR . 'errException.php'; |
||||
|
|
||||
|
|
||||
|
function acquireLock ($lockFile, $csId) |
||||
|
{ |
||||
|
$fName = $lockFile . $csId . '.lck'; |
||||
|
$lock = NULL; |
||||
|
|
||||
|
setErrExceptionMapping (); |
||||
|
|
||||
|
$lockHandle = fopen ($fName, 'w'); |
||||
|
|
||||
|
// if available use semaphores for mutual exclusion because flock
|
||||
|
// doesn't work reliably in threaded environments.
|
||||
|
if (function_exists ('sem_get')) |
||||
|
{ |
||||
|
$lock = sem_get (ftok ($fName, $csId), 1, 0644, TRUE); |
||||
|
|
||||
|
$state = sem_acquire ($lock); |
||||
|
while ($state === FALSE) |
||||
|
$state = sem_acquire ($lock); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
$state = flock ($lock, LOCK_EX); |
||||
|
while ($state === FALSE) |
||||
|
$state = flock ($lock, LOCK_EX); |
||||
|
} |
||||
|
|
||||
|
// Here one could write informations in the lockfile...time, pid, etc.
|
||||
|
fwrite ($lockHandle, session_id () . '::' . time ()); |
||||
|
fflush ($lockHandle); |
||||
|
|
||||
|
resetErrExceptionMapping (); |
||||
|
|
||||
|
$userAbort = ignore_user_abort(TRUE); |
||||
|
return array ($lockHandle, $lock, $userAbort); |
||||
|
} |
||||
|
|
||||
|
function releaseLock ($lock) |
||||
|
{ |
||||
|
setErrExceptionMapping (); |
||||
|
|
||||
|
if ($lock[1] !== NULL) |
||||
|
sem_release ($lock[1]); |
||||
|
|
||||
|
ftruncate ($lock[0], 0); |
||||
|
fclose ($lock[0]); |
||||
|
|
||||
|
resetErrExceptionMapping (); |
||||
|
|
||||
|
ignore_user_abort($lock[2]); |
||||
|
} |
||||
|
|
||||
|
?>
|
||||
@ -1,93 +1,15 @@ |
|||||
<?php |
<?php |
||||
|
|
||||
/* |
|
||||
* Zunächst mal den Gedanken skizziert |
|
||||
* Da es kein wirklich zuverlässiges Locking fuer kritische Bereiche |
|
||||
* in php gibt. Semaphore existiren nur unter Unix und flock arbeitet nicht |
|
||||
* wenn der webserver threaded ist und keine separaten prozesse pro |
|
||||
* request erzeugt habe ich folgenden Gedankengang...nicht 100% Atomar, aber |
|
||||
* möglicherweise das beste was man bekommen kann. |
|
||||
* |
|
||||
* ACQUIRE |
|
||||
* ======= |
|
||||
* | |
|
||||
* - leere Datei erzeugen (tmp) |
|
||||
* - link (tmp) nach (lock) |
|
||||
* | |
|
||||
* |----------------------------- |
|
||||
* OK | | Fehler | |
|
||||
* | | | |
|
||||
* | | - lese (lock): sollte blockieren solange diese leer ist |
|
||||
* | | - unlink (lock) |
|
||||
* | | - link (tmp) nach (lock) |
|
||||
* | Fehler | | |
|
||||
* | ---------------| |
|
||||
* | | OK |
|
||||
* | | |
|
||||
* |----------------------------- |
|
||||
* | |
|
||||
* - (tmp) loeschen |
|
||||
* | |
|
||||
* (ENDE) |
|
||||
* |
|
||||
* RELEASE |
|
||||
* ======= |
|
||||
* | |
|
||||
* - schreibe nach (lock) |
|
||||
* | |
|
||||
* (ENDE) |
|
||||
* |
|
||||
* Da link angeblick atomar ist muesste das zumindest zuverlaessig Sperren... |
|
||||
* Zusaettzlich sollte man vielleicht noch ein Priorisierung irgendwo |
|
||||
* festhalten, um zu verhindern das ein Prozess ewig wartet, weil er immer |
|
||||
* Pech bei der zuweisung von CPU Zeit hat. |
|
||||
* |
|
||||
* So ein Pech, read blockiert natuerlich nicht bei regulären Dateien, da |
|
||||
* ja EOF erreich wird....da muss ich mir was anderes ausdenken.... |
|
||||
* OK, unter UNIX kann ich exec mkfifo machen um eine named pipe zu generieren. |
|
||||
* Fuer windows muss was anderes oder busy waiting (iiieeh) rein.... |
|
||||
* |
|
||||
* !!! Zur info, schon der read open auf eine FIFO blocked wenn kein Writer |
|
||||
* existiert. Das war auch ein aspekt den man berücksichtigen muß... !!! |
|
||||
*/ |
|
||||
|
require_once dirname(__FILE__) . '/config.php'; |
||||
|
require_once LIBDIR . 'mutualExclusion.php'; |
||||
|
|
||||
function acquire ($lock, $tmp) |
|
||||
{ |
|
||||
if (! file_exists ($tmp)) |
|
||||
exec ('mkfifo ' . $tmp); |
|
||||
// hier koennte irgendwas bzgl. der Priorisierung in nem else folgen...
|
|
||||
|
|
||||
if (link ($tmp, $lock) === FALSE) |
|
||||
{ |
|
||||
while (true) |
|
||||
{ |
|
||||
$handle = fopen ($lock, 'w'); |
|
||||
fwrite ($handel, '1'); |
|
||||
fclose ($handle); |
|
||||
if (unlink ($lock) === FALSE) continue; |
|
||||
if (link ($tmp, $lock) === FALSE) continue; |
|
||||
break; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
unlink ($tmp); |
|
||||
} |
|
||||
|
|
||||
function release ($lock) |
|
||||
{ |
|
||||
$handle = fopen ($lock, 'r'); |
|
||||
stream_set_blocking ($handle, 0); |
|
||||
if (fread ($handle, 1) === 0) |
|
||||
unlink ($lock); |
|
||||
fclose ($handle); |
|
||||
} |
|
||||
|
|
||||
|
|
||||
acquire ('lock', 'tmp'); |
|
||||
|
//session_start ();
|
||||
|
$lock = acquireLock ('lock/lock', '1'); |
||||
|
|
||||
echo "Lock bekommen\n"; |
echo "Lock bekommen\n"; |
||||
sleep (30); |
|
||||
|
sleep (2); |
||||
|
|
||||
release ('lock'); |
|
||||
|
releaseLock ($lock); |
||||
|
|
||||
?>
|
?>
|
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue