Example #1
0
 /**
  * Releases the lock.
  *
  * @param bool $semRelease If true then will be released the semaphore when counter of lockings equals 0
  */
 private function unlock($semRelease = false)
 {
     if ($this->numberLocks === 0) {
         return;
     }
     $this->numberLocks--;
     if ($this->numberLocks === 0 || $semRelease) {
         $this->semaphore->release();
     }
 }
Example #2
0
function Semaphore_Test()
{
    define('MEMCACHED_HOST', 'localhost');
    define('MEMCACHED_PORT', '11211');
    $S1 = new Semaphore("Test");
    if ($S1->On()) {
        echo "OK: Semaforo S1 adquirido.\n";
        $S2 = new Semaphore("Test");
        if ($S2->On()) {
            echo "ERROR: NO DEBERIA ADQUIRIRSE S2->ON()!!!!\n";
        } else {
            echo "OK: Semaforo S2 no adquirido.\n";
        }
    } else {
        echo "WARN: Semaforo S1 NO adquirido, hay otro proceso simultaneo???\n";
    }
    unset($S1);
    $S3 = new Semaphore("Test");
    if ($S3->On()) {
        echo "OK: Semaforo S3 adquirido.\n";
    } else {
        echo "WARN: Semaforo S3 NO adquirido, hay otro pcoeso simultaneo???\n";
    }
}
Example #3
0
 /**
  * Svuota il contenuto della clipboard.
  * @return bool
  * @access public
  */
 public function erase()
 {
     $result = false;
     if ($this->IsValid()) {
         $sem = new Semaphore('clipboard', $this->mFileName);
         $sem->WaitGreen();
         $sem->SetRed();
         $result = unlink($this->mFileName);
         $sem->SetGreen();
     } else {
         $result = true;
     }
     return $result;
 }
    public function run(){
        for($i = 0; $i < 10; $i++){
            $this->map->put(__CLASS__ . (string)$this->index++, $i);
            $this->sleep(20);
        }
        //
        // とりあえず、ここで出力してみる
        //
        var_dump($this->map);
    }

    public function setMap(S2Dao_Map $map){
        $this->map = $map;
    }

    public function getMap(){
        return $this->map;
    }
}

$a = new A_Ex(new B_Ex, 'setMap');
$a->start();
*/
/*
$map = Semaphore::synchronizedMap(new S2Dao_HashMap);
$map->put("hoge", "bar");
var_dump($map);
*/
$m = Semaphore::synchronizedMap(new S2Dao_HashMap());
$m->put("hoge", "bar");
var_dump($m);
Example #5
0
/**
 * semaphore_content_store When content is stored, we need to remove the semaphore from the database
 * 
 * @param array $pObject Currently loaded object
 * @access public
 * @return void
 * @note We can't use the semaphore_content_store to display a warning since it's called to late - half way through the store process
 */
function semaphore_content_store(&$pObject)
{
    if (is_object($pObject) && $pObject->isValid()) {
        $semaphore = new Semaphore();
        $semaphore->expunge($pObject->mContentId);
    }
}
Example #6
0
 public function destroy()
 {
     $result = false;
     $sem = new Semaphore('cache', $this->mItemFile);
     $sem->WaitGreen();
     $sem->SetRed();
     if (strlen($this->mItemFile) and file_exists($this->mItemFile)) {
         $result = @unlink($this->mItemFile);
     } else {
         $this->mResult = AMPOLIROS_CACHE_ITEM_NOT_FOUND;
     }
     if ($result) {
         $result = $this->mrAmpDb->Execute('DELETE FROM cacheditems WHERE module=' . $this->mrAmpDb->Format_Text($this->mModule) . ' AND itemid=' . $this->mrAmpDb->Format_Text($this->mItemId));
     }
     $sem->SetGreen();
     return $result;
 }
Example #7
0
 /**
  * Sets the Semaphore, that will be used within the worker processes
  * @param \QXS\WorkerPool\Semaphore $semaphore the Semaphore, that should be used for the workers
  * @return WorkerPool
  * @throws \QXS\WorkerPool\WorkerPoolException in case the WorkerPool has already been created
  * @throws \InvalidArgumentException in case the semaphre hasn't been created
  */
 public function setSemaphore(Semaphore $semaphore)
 {
     if ($this->created) {
         throw new WorkerPoolException('Cannot set the Worker Pool Size for a created pool.');
     }
     if (!$semaphore->isCreated()) {
         throw new \InvalidArgumentException('The Semaphore hasn\'t yet been created.');
     }
     $this->semaphore = $semaphore;
     return $this;
 }
Example #8
0
 /**
  * Returns a raw mail, that it gets from the getArray function
  *
  * @static
  * @param $killID the ID of the kill
  * @return string
  */
 public static function getRawMail($killID, $array = array(), $edk = true)
 {
     $cacheName = $killID;
     $sem = Semaphore::fetch($killID);
     if ($edk) {
         $cacheName = $killID . "EDK";
     }
     // Check if the mail has already been generated, then return it from the cache..
     $Cache = Cache::get($cacheName);
     if ($Cache) {
         Semaphore::release($sem);
         return $Cache;
     }
     // Find all groupIDs where they contain Deadspace
     $deadspaceIDs = array();
     $dIDs = Db::query("SELECT groupID FROM ccp_invGroups WHERE groupName LIKE '%deadspace%' OR groupName LIKE 'FW%' OR groupName LIKE 'Asteroid%'");
     foreach ($dIDs as $dd) {
         $deadspaceIDs[] = $dd["groupID"];
     }
     // ADD ALL THE FLAGS!!!!!!!!!!!
     //$flags = array("(Cargo)" => 5, "(Drone Bay)" => 87, "(Implant)" => 89);
     $dbFlags = Db::query("SELECT flagText, flagID FROM ccp_invFlags", array(), 3600);
     $flags = array();
     foreach ($dbFlags as $f) {
         $flags[(int) $f["flagID"]] = $f["flagText"];
     }
     if (!$array) {
         $k = self::getArray($killID);
     } else {
         $k = $array;
     }
     $mail = $k["killTime"] . "\n";
     $mail .= "\n";
     $mail .= "Victim: " . $k["victim"]["characterName"] . "\n";
     $mail .= "Corp: " . $k["victim"]["corporationName"] . "\n";
     if (!isset($k["victim"]["allianceName"]) || $k["victim"]["allianceName"] == "") {
         $k["victim"]["allianceName"] = "None";
     }
     $mail .= "Alliance: " . $k["victim"]["allianceName"] . "\n";
     if (!isset($k["victim"]["factionName"]) || $k["victim"]["factionName"] == "") {
         $k["victim"]["factionName"] = "None";
     }
     $mail .= "Faction: " . $k["victim"]["factionName"] . "\n";
     if (!isset($k["victim"]["shipName"]) || $k["victim"]["shipName"] == "") {
         $k["victim"]["shipName"] = "None";
     }
     $mail .= "Destroyed: " . $k["victim"]["shipName"] . "\n";
     if (!isset($k["solarSystemName"]) || $k["solarSystemName"] == "") {
         $k["solarSystemName"] = "None";
     }
     $mail .= "System: " . $k["solarSystemName"] . "\n";
     if (!isset($k["solarSystemSecurity"]) || $k["solarSystemSecurity"] == "") {
         $k["solarSystemSecurity"] = (int) 0;
     }
     $mail .= "Security: " . $k["solarSystemSecurity"] . "\n";
     if (!isset($k["victim"]["damageTaken"]) || $k["victim"]["damageTaken"] == "") {
         $k["victim"]["damageTaken"] = (int) 0;
     }
     $mail .= "Damage Taken: " . $k["victim"]["damageTaken"] . "\n\n";
     if (isset($k["attackers"])) {
         $mail .= "Involved parties:\n\n";
         foreach ($k["attackers"] as $inv) {
             // find groupID for the ship
             if (!isset($inv["shipName"])) {
                 $inv["shipName"] = "Unknown";
             }
             $groupID = Db::queryField("SELECT groupID FROM ccp_invTypes WHERE typeName LIKE :shipName", "groupID", array(":shipName" => $inv["shipName"]));
             if (in_array($groupID, $deadspaceIDs)) {
                 // shipName isn't set, but it's an npc.. fml..
                 if ($inv["finalBlow"] == 1) {
                     $mail .= "Name: " . $inv["shipName"] . " / " . $inv["corporationName"] . " (laid the final blow)\n";
                 } else {
                     $mail .= "Name: " . $inv["shipName"] . " / " . $inv["corporationName"] . "\n";
                 }
                 $mail .= "Damage Done: " . $inv["damageDone"] . "\n\n";
             } else {
                 if ($inv["finalBlow"] == 1) {
                     $mail .= "Name: " . $inv["characterName"] . " (laid the final blow)\n";
                 } else {
                     if (strlen($inv["characterName"])) {
                         $mail .= "Name: " . $inv["characterName"] . "\n";
                     }
                 }
                 if (strlen($inv["characterName"])) {
                     $mail .= "Security: " . $inv["securityStatus"] . "\n";
                 }
                 $mail .= "Corp: " . $inv["corporationName"] . "\n";
                 if (!isset($inv["allianceName"]) || $inv["allianceName"] == "") {
                     $inv["allianceName"] = "None";
                 }
                 $mail .= "Alliance: " . $inv["allianceName"] . "\n";
                 if (!isset($inv["factionName"]) || $inv["factionName"] == "") {
                     $inv["factionName"] = "None";
                 }
                 $mail .= "Faction: " . $inv["factionName"] . "\n";
                 if (!isset($inv["shipName"]) || $inv["shipName"] == "") {
                     $inv["shipName"] = "None";
                 }
                 $mail .= "Ship: " . $inv["shipName"] . "\n";
                 if (!isset($inv["weaponName"]) || $inv["weaponName"] == "") {
                     $inv["weaponName"] = $inv["shipName"];
                 }
                 $mail .= "Weapon: " . $inv["weaponName"] . "\n";
                 $mail .= "Damage Done: " . $inv["damageDone"] . "\n\n";
             }
         }
     }
     $mail .= "\n";
     $dropped = array();
     $destroyed = array();
     if (isset($k["items"])) {
         foreach ($k["items"] as $itm) {
             // Take the flags we get from $itemToSlot and replace it with the proper flag from the database
             $itm["flagName"] = $flags[$itm["flag"]];
             // create the flag!
             $copy = null;
             if ($itm["singleton"] == 2) {
                 $copy = " (Copy)";
             }
             $edkValidFlags = array("Cargo", "Drone Bay");
             if ($edk && !in_array($itm["flagName"], $edkValidFlags)) {
                 $flagName = null;
             } else {
                 $flagName = " (" . $itm["flagName"] . ")";
             }
             if ($itm["qtyDropped"]) {
                 $line = $itm["typeName"] . ", Qty: " . $itm["qtyDropped"] . $flagName . ($copy ? $copy : null);
                 $dropped[] = $line;
             }
             if ($itm["qtyDestroyed"]) {
                 $line = $itm["typeName"] . ", Qty: " . $itm["qtyDestroyed"] . $flagName . ($copy ? $copy : null);
                 $destroyed[] = $line;
             }
             if (isset($itm["items"])) {
                 foreach ($itm["items"] as $key => $sub) {
                     if ($sub["qtyDropped"]) {
                         $line = $sub["typeName"] . ", Qty: " . $sub["qtyDropped"] . $flagName . ($copy ? $copy : null) . " (In Container)";
                         $dropped[] = $line;
                     }
                     if ($sub["qtyDestroyed"]) {
                         $line = $sub["typeName"] . ", Qty: " . $sub["qtyDestroyed"] . $flagName . ($copy ? $copy : null) . " (In Container)";
                         $destroyed[] = $line;
                     }
                 }
             }
         }
     }
     if ($destroyed) {
         $mail .= "Destroyed items:\n\n";
         foreach ($destroyed as $items) {
             $mail .= $items . "\n";
         }
     }
     $mail .= "\n";
     if ($dropped) {
         $mail .= "Dropped items:\n\n";
         foreach ($dropped as $items) {
             $mail .= $items . "\n";
         }
     }
     // Store the generated mail in cache
     Cache::set($cacheName, $mail, 604800);
     Semaphore::release($sem);
     return $mail;
 }
Example #9
0
 /**
  * Unlock shared memory
  * 
  * @return boolean    True if unlocked, otherwise false
  */
 public function release()
 {
     return $this->mutex->release();
 }
Example #10
0
 public function testGetArray()
 {
     $workload = $this->provider->get('test');
     $this->assertSame(array('name' => 'Romain'), $workload);
 }
Example #11
0
 /**
  * @param $kills
  * @param array $parameters
  * @param $key
  * @return array
  */
 public static function buildSummary(&$kills, $parameters = array(), $key)
 {
     if ($kills == null || !is_array($kills) || sizeof($kills) == 0) {
         return array();
     }
     $sem = Semaphore::fetch($parameters["solarSystemID"]);
     $key = "related:{$key}";
     $mc = Cache::get($key);
     if ($mc) {
         Semaphore::release($sem);
         return $mc;
     }
     $teamAKills = array();
     $teamBKills = array();
     $teamAList = array();
     $teamBList = array();
     $killHash = array();
     self::determineSides($kills, $teamAKills, $teamBKills);
     foreach ($kills as $kill) {
         self::addRelatedPilots($killHash, $kill, $kill["victim"]);
     }
     //$teamAEntities = self::getEntities($teamAKills);
     //$teamBEntities = self::getEntities($teamBKills);
     //$killHash = array_keys($killHash);
     $teamATotals = self::getStatsKillList($teamAKills);
     $teamBTotals = self::getStatsKillList($teamBKills);
     self::hashPilots($teamBKills, $teamBList, $teamAList);
     self::hashPilots($teamAKills, $teamAList, $teamBList);
     $teamATotals["pilotCount"] = self::getUniquePilotCount($teamAList);
     $teamBTotals["pilotCount"] = self::getUniquePilotCount($teamBList);
     foreach ($teamAList as $a => $value) {
         if (in_array($a, array_keys($teamBList))) {
             unset($teamAList[$a]);
         }
     }
     foreach ($teamBList as $b => $value) {
         if (in_array($b, array_keys($teamAList))) {
             unset($teamBList[$b]);
         }
     }
     uksort($teamAList, "self::shipGroupSort");
     uksort($teamBList, "self::shipGroupSort");
     $teamAList = self::addInfo($teamAList, $killHash);
     $teamBList = self::addInfo($teamBList, $killHash);
     $retValue = array("teamA" => array("list" => $teamAList, "kills" => $teamAKills, "totals" => $teamATotals), "teamB" => array("list" => $teamBList, "kills" => $teamBKills, "totals" => $teamBTotals));
     //$teamAScore = log($retValue["teamA"]["totals"]["total_price"] + 1) * (log($retValue["teamA"]["totals"]["total_points"]) + 1);
     //$teamBScore = log($retValue["teamB"]["totals"]["total_price"] + 1) * (log($retValue["teamA"]["totals"]["total_points"]) + 1);
     /*if ($teamBScore > $teamAScore) {
       $temp = $retValue["teamB"];
       $retValue["teamB"] = $retValue["teamA"];
       $retValue["teamA"] = $temp;
       }*/
     Semaphore::release($sem);
     Cache::set($key, $retValue, 900);
     return $retValue;
 }
Example #12
0
<?php

/**
 * This is an example of a script running in background
 * with Semaphore
 */
require_once '../config.php';
require_once ENGINE_PATH . 'engine.functions.php';
//one instance at a time
loadLib('semaphore');
$semaphore = new Semaphore('maintenance', 'takeover mode', true);
if (!$semaphore->isMine()) {
    die(0);
}
//establish db connection
// $odb = getOdb();
//clean cache
loadLib('filesystem');
echo "\n" . date('Y-m-d H:i:s') . "\n";
Filesystem::removeFromPublic('*', true);
Filesystem::remove(TEMP_PATH . '*', true);
//clear the semaphore
$semaphore->clear();