Back to index

d-push  2.0
interprocessdata.php
Go to the documentation of this file.
00001 <?php
00002 /***********************************************
00003 * File      :   interprocessdata.php
00004 * Project   :   Z-Push
00005 * Descr     :   Class takes care of interprocess
00006 *               communicaton for different purposes
00007 *
00008 * Created   :   20.10.2011
00009 *
00010 * Copyright 2007 - 2011 Zarafa Deutschland GmbH
00011 *
00012 * This program is free software: you can redistribute it and/or modify
00013 * it under the terms of the GNU Affero General Public License, version 3,
00014 * as published by the Free Software Foundation with the following additional
00015 * term according to sec. 7:
00016 *
00017 * According to sec. 7 of the GNU Affero General Public License, version 3,
00018 * the terms of the AGPL are supplemented with the following terms:
00019 *
00020 * "Zarafa" is a registered trademark of Zarafa B.V.
00021 * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
00022 * The licensing of the Program under the AGPL does not imply a trademark license.
00023 * Therefore any rights, title and interest in our trademarks remain entirely with us.
00024 *
00025 * However, if you propagate an unmodified version of the Program you are
00026 * allowed to use the term "Z-Push" to indicate that you distribute the Program.
00027 * Furthermore you may use our trademarks where it is necessary to indicate
00028 * the intended purpose of a product or service provided you use it in accordance
00029 * with honest practices in industrial or commercial matters.
00030 * If you want to propagate modified versions of the Program under the name "Z-Push",
00031 * you may only do so if you have a written permission by Zarafa Deutschland GmbH
00032 * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
00033 *
00034 * This program is distributed in the hope that it will be useful,
00035 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00036 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00037 * GNU Affero General Public License for more details.
00038 *
00039 * You should have received a copy of the GNU Affero General Public License
00040 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00041 *
00042 * Consult LICENSE file for details
00043 ************************************************/
00044 
00045 abstract class InterProcessData {
00046     const CLEANUPTIME = 1;
00047 
00048     static protected $devid;
00049     static protected $pid;
00050     static protected $user;
00051     static protected $start;
00052     protected $type;
00053     protected $allocate;
00054     private $mutexid;
00055     private $memid;
00056 
00062     public function InterProcessData() {
00063         if (!isset($this->type) || !isset($this->allocate))
00064             throw new FatalNotImplementedException(sprintf("Class InterProcessData can not be initialized. Subclass %s did not initialize type and allocable memory.", get_class($this)));
00065 
00066         if ($this->InitSharedMem())
00067             ZLog::Write(LOGLEVEL_DEBUG, sprintf("%s(): Initialized mutexid %s and memid %s.", get_class($this), $this->mutexid, $this->memid));
00068     }
00069 
00076     public function InitializeParams() {
00077         if (!isset(self::$devid)) {
00078             self::$devid = Request::GetDeviceID();
00079             self::$pid = @getmypid();
00080             self::$user = Request::GetAuthUser();
00081             self::$start = time();
00082         }
00083         return true;
00084     }
00085 
00092     private function InitSharedMem() {
00093         // shared mem general "turn off switch"
00094         if (defined("USE_SHARED_MEM") && USE_SHARED_MEM === false) {
00095             ZLog::Write(LOGLEVEL_INFO, "InterProcessData::InitSharedMem(): the usage of shared memory for Z-Push has been disabled. Check your config for 'USE_SHARED_MEM'.");
00096             return false;
00097         }
00098 
00099         if (!function_exists('sem_get') || !function_exists('shm_attach') || !function_exists('sem_acquire')|| !function_exists('shm_get_var')) {
00100             ZLog::Write(LOGLEVEL_INFO, "InterProcessData::InitSharedMem(): PHP libraries for the use shared memory are not available. Functionalities like z-push-top or loop detection are not available. Check your php packages.");
00101             return false;
00102         }
00103 
00104         // Create mutex
00105         $this->mutexid = @sem_get($this->type, 1);
00106         if ($this->mutexid === false) {
00107             ZLog::Write(LOGLEVEL_ERROR, "InterProcessData::InitSharedMem(): could not aquire semaphore");
00108             return false;
00109         }
00110 
00111         // Attach shared memory
00112         $this->memid = shm_attach($this->type+10, $this->allocate);
00113         if ($this->memid === false) {
00114             ZLog::Write(LOGLEVEL_ERROR, "InterProcessData::InitSharedMem(): could not attach shared memory");
00115             @sem_remove($this->mutexid);
00116             $this->mutexid = false;
00117             return false;
00118         }
00119 
00120         // TODO mem cleanup has to be implemented
00121         //$this->setInitialCleanTime();
00122 
00123         return true;
00124     }
00125 
00132     private function RemoveSharedMem() {
00133         if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false)) {
00134             @sem_acquire($this->mutexid);
00135             $memid = $this->memid;
00136             $this->memid = false;
00137             @sem_release($this->mutexid);
00138 
00139             @sem_remove($this->mutexid);
00140             @shm_remove($memid);
00141             @shm_detach($memid);
00142 
00143             $this->mutexid = false;
00144 
00145             return true;
00146         }
00147         return false;
00148     }
00149 
00156     public function ReInitSharedMem() {
00157         return ($this->RemoveSharedMem() && $this->InitSharedMem());
00158     }
00159 
00166     public function Clean() {
00167         $stat = false;
00168 
00169         // exclusive block
00170         if ($this->blockMutex()) {
00171             $cleanuptime = ($this->hasData(1)) ? $this->getData(1) : false;
00172 
00173             // TODO implement Shared Memory cleanup
00174 
00175             $this->releaseMutex();
00176         }
00177         // end exclusive block
00178 
00179         return $stat;
00180     }
00181 
00188     public function IsActive() {
00189         return ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false));
00190     }
00191 
00200     protected function blockMutex() {
00201         if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
00202             return @sem_acquire($this->mutexid);
00203 
00204         return false;
00205     }
00206 
00214     protected function releaseMutex() {
00215         if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
00216             return @sem_release($this->mutexid);
00217 
00218         return false;
00219     }
00220 
00229     protected function hasData($id = 2) {
00230         if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false)) {
00231             if (function_exists("shm_has_var"))
00232                 return @shm_has_var($this->memid, $id);
00233             else {
00234                 $some = $this->getData($id);
00235                 return isset($some);
00236             }
00237         }
00238         return false;
00239     }
00240 
00249     protected function getData($id = 2) {
00250         if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
00251             return @shm_get_var($this->memid, $id);
00252 
00253         return ;
00254     }
00255 
00266     protected function setData($data, $id = 2) {
00267         if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
00268             return @shm_put_var($this->memid, $id, $data);
00269 
00270         return false;
00271     }
00272 
00279     private function setInitialCleanTime() {
00280         $stat = false;
00281 
00282         // exclusive block
00283         if ($this->blockMutex()) {
00284 
00285             if ($this->hasData(1) == false)
00286                 $stat = $this->setData(time(), 1);
00287 
00288             $this->releaseMutex();
00289         }
00290         // end exclusive block
00291 
00292         return $stat;
00293     }
00294 
00295 }
00296 
00297 ?>