[7589] | 1 | <?php |
---|
| 2 | /*********************************************** |
---|
| 3 | * File : pingtracking.php |
---|
| 4 | * Project : Z-Push |
---|
| 5 | * Descr : |
---|
| 6 | * |
---|
| 7 | * Created : 20.10.2011 |
---|
| 8 | * |
---|
| 9 | * Copyright 2007 - 2012 Zarafa Deutschland GmbH |
---|
| 10 | * |
---|
| 11 | * This program is free software: you can redistribute it and/or modify |
---|
| 12 | * it under the terms of the GNU Affero General Public License, version 3, |
---|
| 13 | * as published by the Free Software Foundation with the following additional |
---|
| 14 | * term according to sec. 7: |
---|
| 15 | * |
---|
| 16 | * According to sec. 7 of the GNU Affero General Public License, version 3, |
---|
| 17 | * the terms of the AGPL are supplemented with the following terms: |
---|
| 18 | * |
---|
| 19 | * "Zarafa" is a registered trademark of Zarafa B.V. |
---|
| 20 | * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH |
---|
| 21 | * The licensing of the Program under the AGPL does not imply a trademark license. |
---|
| 22 | * Therefore any rights, title and interest in our trademarks remain entirely with us. |
---|
| 23 | * |
---|
| 24 | * However, if you propagate an unmodified version of the Program you are |
---|
| 25 | * allowed to use the term "Z-Push" to indicate that you distribute the Program. |
---|
| 26 | * Furthermore you may use our trademarks where it is necessary to indicate |
---|
| 27 | * the intended purpose of a product or service provided you use it in accordance |
---|
| 28 | * with honest practices in industrial or commercial matters. |
---|
| 29 | * If you want to propagate modified versions of the Program under the name "Z-Push", |
---|
| 30 | * you may only do so if you have a written permission by Zarafa Deutschland GmbH |
---|
| 31 | * (to acquire a permission please contact Zarafa at trademark@zarafa.com). |
---|
| 32 | * |
---|
| 33 | * This program is distributed in the hope that it will be useful, |
---|
| 34 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 35 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 36 | * GNU Affero General Public License for more details. |
---|
| 37 | * |
---|
| 38 | * You should have received a copy of the GNU Affero General Public License |
---|
| 39 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 40 | * |
---|
| 41 | * Consult LICENSE file for details |
---|
| 42 | ************************************************/ |
---|
| 43 | |
---|
| 44 | class PingTracking extends InterProcessData { |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Constructor |
---|
| 48 | * |
---|
| 49 | * @access public |
---|
| 50 | */ |
---|
| 51 | public function PingTracking() { |
---|
| 52 | // initialize super parameters |
---|
| 53 | $this->allocate = 512000; // 500 KB |
---|
| 54 | $this->type = 2; |
---|
| 55 | parent::__construct(); |
---|
| 56 | |
---|
| 57 | $this->initPing(); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Destructor |
---|
| 62 | * Used to remove the current ping data from shared memory |
---|
| 63 | * |
---|
| 64 | * @access public |
---|
| 65 | */ |
---|
| 66 | public function __destruct() { |
---|
| 67 | // exclusive block |
---|
| 68 | if ($this->blockMutex()) { |
---|
| 69 | $pings = $this->getData(); |
---|
| 70 | |
---|
| 71 | // check if our ping is still in the list |
---|
| 72 | if (isset($pings[self::$devid][self::$user][self::$pid])) { |
---|
| 73 | unset($pings[self::$devid][self::$user][self::$pid]); |
---|
| 74 | $stat = $this->setData($pings); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | $this->releaseMutex(); |
---|
| 78 | } |
---|
| 79 | // end exclusive block |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * Initialized the current request |
---|
| 84 | * |
---|
| 85 | * @access public |
---|
| 86 | * @return boolean |
---|
| 87 | */ |
---|
| 88 | protected function initPing() { |
---|
| 89 | $stat = false; |
---|
| 90 | |
---|
| 91 | // initialize params |
---|
| 92 | $this->InitializeParams(); |
---|
| 93 | |
---|
| 94 | // exclusive block |
---|
| 95 | if ($this->blockMutex()) { |
---|
| 96 | $pings = ($this->hasData()) ? $this->getData() : array(); |
---|
| 97 | |
---|
| 98 | // set the start time for the current process |
---|
| 99 | $this->checkArrayStructure($pings); |
---|
| 100 | $pings[self::$devid][self::$user][self::$pid] = self::$start; |
---|
| 101 | $stat = $this->setData($pings); |
---|
| 102 | $this->releaseMutex(); |
---|
| 103 | } |
---|
| 104 | // end exclusive block |
---|
| 105 | |
---|
| 106 | return $stat; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * Checks if there are newer ping requests for the same device & user so |
---|
| 111 | * the current process could be terminated |
---|
| 112 | * |
---|
| 113 | * @access public |
---|
| 114 | * @return boolean true if the current process is obsolete |
---|
| 115 | */ |
---|
| 116 | public function DoForcePingTimeout() { |
---|
| 117 | $pings = false; |
---|
| 118 | // exclusive block |
---|
| 119 | if ($this->blockMutex()) { |
---|
| 120 | $pings = $this->getData(); |
---|
| 121 | $this->releaseMutex(); |
---|
| 122 | } |
---|
| 123 | // end exclusive block |
---|
| 124 | |
---|
| 125 | // check if there is another (and newer) active ping connection |
---|
| 126 | if (is_array($pings) && isset($pings[self::$devid][self::$user]) && count($pings[self::$devid][self::$user]) > 1) { |
---|
| 127 | foreach ($pings[self::$devid][self::$user] as $pid=>$starttime) |
---|
| 128 | if ($starttime > self::$start) |
---|
| 129 | return true; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return false; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * Builds an array structure for the concurrent ping connection detection |
---|
| 137 | * |
---|
| 138 | * @param array $array reference to the ping data array |
---|
| 139 | * |
---|
| 140 | * @access private |
---|
| 141 | * @return |
---|
| 142 | */ |
---|
| 143 | private function checkArrayStructure(&$array) { |
---|
| 144 | if (!is_array($array)) |
---|
| 145 | $array = array(); |
---|
| 146 | |
---|
| 147 | if (!isset($array[self::$devid])) |
---|
| 148 | $array[self::$devid] = array(); |
---|
| 149 | |
---|
| 150 | if (!isset($array[self::$devid][self::$user])) |
---|
| 151 | $array[self::$devid][self::$user] = array(); |
---|
| 152 | |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | ?> |
---|