Back to index

d-push  2.0
pingtracking.php
Go to the documentation of this file.
00001 <?php
00002 /***********************************************
00003 * File      :   pingtracking.php
00004 * Project   :   Z-Push
00005 * Descr     :
00006 *
00007 * Created   :   20.10.2011
00008 *
00009 * Copyright 2007 - 2011 Zarafa Deutschland GmbH
00010 *
00011 * This program is free software: you can redistribute it and/or modify
00012 * it under the terms of the GNU Affero General Public License, version 3,
00013 * as published by the Free Software Foundation with the following additional
00014 * term according to sec. 7:
00015 *
00016 * According to sec. 7 of the GNU Affero General Public License, version 3,
00017 * the terms of the AGPL are supplemented with the following terms:
00018 *
00019 * "Zarafa" is a registered trademark of Zarafa B.V.
00020 * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
00021 * The licensing of the Program under the AGPL does not imply a trademark license.
00022 * Therefore any rights, title and interest in our trademarks remain entirely with us.
00023 *
00024 * However, if you propagate an unmodified version of the Program you are
00025 * allowed to use the term "Z-Push" to indicate that you distribute the Program.
00026 * Furthermore you may use our trademarks where it is necessary to indicate
00027 * the intended purpose of a product or service provided you use it in accordance
00028 * with honest practices in industrial or commercial matters.
00029 * If you want to propagate modified versions of the Program under the name "Z-Push",
00030 * you may only do so if you have a written permission by Zarafa Deutschland GmbH
00031 * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
00032 *
00033 * This program is distributed in the hope that it will be useful,
00034 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00035 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00036 * GNU Affero General Public License for more details.
00037 *
00038 * You should have received a copy of the GNU Affero General Public License
00039 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00040 *
00041 * Consult LICENSE file for details
00042 ************************************************/
00043 
00044 class PingTracking extends InterProcessData {
00045 
00051     public function PingTracking() {
00052         // initialize super parameters
00053         $this->allocate = 204800; // 200 KB
00054         $this->type = 2;
00055         parent::__construct();
00056 
00057         $this->initPing();
00058     }
00059 
00066     public function __destruct() {
00067         // exclusive block
00068         if ($this->blockMutex()) {
00069             $pings = $this->getData();
00070 
00071             // check if our ping is still in the list
00072             if (isset($pings[self::$devid][self::$user][self::$pid])) {
00073                 unset($pings[self::$devid][self::$user][self::$pid]);
00074                 $stat = $this->setData($pings);
00075             }
00076 
00077             $this->releaseMutex();
00078         }
00079         // end exclusive block
00080     }
00081 
00088     protected function initPing() {
00089         $stat = false;
00090 
00091         // initialize params
00092         $this->InitializeParams();
00093 
00094         // exclusive block
00095         if ($this->blockMutex()) {
00096             $pings = ($this->hasData()) ? $this->getData() : array();
00097 
00098             // set the start time for the current process
00099             $this->checkArrayStructure($pings);
00100             $pings[self::$devid][self::$user][self::$pid] = self::$start;
00101             $stat = $this->setData($pings);
00102             $this->releaseMutex();
00103         }
00104         // end exclusive block
00105 
00106         return $stat;
00107     }
00108 
00116     public function DoForcePingTimeout() {
00117         $pings = false;
00118         // exclusive block
00119         if ($this->blockMutex()) {
00120             $pings = $this->getData();
00121             $this->releaseMutex();
00122         }
00123         // end exclusive block
00124 
00125         // check if there is another (and newer) active ping connection
00126         if (is_array($pings) && isset($pings[self::$devid][self::$user]) && count($pings[self::$devid][self::$user]) > 1) {
00127             foreach ($pings[self::$devid][self::$user] as $pid=>$starttime)
00128                 if ($starttime > self::$start)
00129                     return true;
00130         }
00131 
00132         return false;
00133     }
00134 
00143     private function checkArrayStructure(&$array) {
00144         if (!is_array($array))
00145             $array = array();
00146 
00147         if (!isset($array[self::$devid]))
00148             $array[self::$devid] = array();
00149 
00150         if (!isset($array[self::$devid][self::$user]))
00151             $array[self::$devid][self::$user] = array();
00152 
00153     }
00154 }
00155 
00156 ?>