Commit ca405011 authored by Ralf Becker's avatar Ralf Becker

ZP-698 Allow different IPC backends using current IPC logic. Released under...

ZP-698 Allow different IPC backends using current IPC logic. Released under the Affero GNU General Public License (AGPL) version 3. split up into a backend interface, a SHM backend and abstract class
parent d1d840aa
......@@ -4,6 +4,7 @@
* Project : Z-Push
* Descr : Class takes care of interprocess
* communicaton for different purposes
* using a backend implementing IIpcBackend
*
* Created : 20.10.2011
*
......@@ -42,85 +43,6 @@
* Consult LICENSE file for details
************************************************/
interface IIpcBackend
{
/**
* Constructor
*
* @param int $type
* @param int $allocate
* @param string $class
*/
public function __construct($type, $allocate, $class);
/**
* Cleans up the shared memory block
*
* @access public
* @return boolean
*/
public function Clean();
/**
* Indicates if the shared memory is active
*
* @access public
* @return boolean
*/
public function IsActive();
/**
* Blocks the class mutex
* Method blocks until mutex is available!
* ATTENTION: make sure that you *always* release a blocked mutex!
*
* @access protected
* @return boolean
*/
public function blockMutex();
/**
* Releases the class mutex
* After the release other processes are able to block the mutex themselfs
*
* @access protected
* @return boolean
*/
public function releaseMutex();
/**
* Indicates if the requested variable is available in shared memory
*
* @param int $id int indicating the variable
*
* @access protected
* @return boolean
*/
public function hasData($id = 2);
/**
* Returns the requested variable from shared memory
*
* @param int $id int indicating the variable
*
* @access protected
* @return mixed
*/
public function getData($id = 2);
/**
* Writes the transmitted variable to shared memory
* Subclasses may never use an id < 2!
*
* @param mixed $data data which should be saved into shared memory
* @param int $id int indicating the variable (bigger than 2!)
*
* @access protected
* @return boolean
*/
public function setData($data, $id = 2);
}
abstract class InterProcessData {
const CLEANUPTIME = 1;
......@@ -148,6 +70,12 @@ abstract class InterProcessData {
$ipc_backend = defined('IPC_BACKEND_CLASS') ? IPC_BACKEND_CLASS : 'IpcBackendShm';
// until z-push autoloads, manually load IpcBackend
if (!class_exists($ipc_backend))
{
include_onced('lib/core/'.strtolower($ipc_backend));
}
try {
$this->backend = new $ipc_backend($this->type, $this->allocate, get_class($this));
}
......@@ -254,236 +182,3 @@ abstract class InterProcessData {
return $this->backend ? $this->backend->setData($data, $id) : false;
}
}
class IpcBackendShm implements IIpcBackend
{
private $mutexid;
private $memid;
protected $type;
protected $allocate;
/**
* Constructor
*
* @param int $type
* @param int $allocate
* @param string $class
*/
public function __construct($type, $allocate, $class) {
$this->type = $type;
$this->allocate = $allocate;
if ($this->InitSharedMem())
ZLog::Write(LOGLEVEL_DEBUG, sprintf("%s(): Initialized mutexid %s and memid %s.", $class, $this->mutexid, $this->memid));
}
/**
* Allocates shared memory
*
* @access private
* @return boolean
*/
private function InitSharedMem() {
// shared mem general "turn off switch"
if (defined("USE_SHARED_MEM") && USE_SHARED_MEM === false) {
ZLog::Write(LOGLEVEL_INFO, "InterProcessData::InitSharedMem(): the usage of shared memory for Z-Push has been disabled. Check your config for 'USE_SHARED_MEM'.");
return false;
}
if (!function_exists('sem_get') || !function_exists('shm_attach') || !function_exists('sem_acquire')|| !function_exists('shm_get_var')) {
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.");
return false;
}
// Create mutex
$this->mutexid = @sem_get($this->type, 1);
if ($this->mutexid === false) {
ZLog::Write(LOGLEVEL_ERROR, "InterProcessData::InitSharedMem(): could not aquire semaphore");
return false;
}
// Attach shared memory
$this->memid = shm_attach($this->type+10, $this->allocate);
if ($this->memid === false) {
ZLog::Write(LOGLEVEL_ERROR, "InterProcessData::InitSharedMem(): could not attach shared memory");
@sem_remove($this->mutexid);
$this->mutexid = false;
return false;
}
// TODO mem cleanup has to be implemented
//$this->setInitialCleanTime();
return true;
}
/**
* Removes and detaches shared memory
*
* @access private
* @return boolean
*/
private function RemoveSharedMem() {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false)) {
@sem_acquire($this->mutexid);
$memid = $this->memid;
$this->memid = false;
@sem_release($this->mutexid);
@sem_remove($this->mutexid);
@shm_remove($memid);
@shm_detach($memid);
$this->mutexid = false;
return true;
}
return false;
}
/**
* Reinitializes shared memory by removing, detaching and re-allocating it
*
* @access public
* @return boolean
*/
public function ReInitSharedMem() {
return ($this->RemoveSharedMem() && $this->InitSharedMem());
}
/**
* Cleans up the shared memory block
*
* @access public
* @return boolean
*/
public function Clean() {
$stat = false;
// exclusive block
if ($this->blockMutex()) {
$cleanuptime = ($this->hasData(1)) ? $this->getData(1) : false;
// TODO implement Shared Memory cleanup
$this->releaseMutex();
}
// end exclusive block
return $stat;
}
/**
* Indicates if the shared memory is active
*
* @access public
* @return boolean
*/
public function IsActive() {
return ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false));
}
/**
* Blocks the class mutex
* Method blocks until mutex is available!
* ATTENTION: make sure that you *always* release a blocked mutex!
*
* @access protected
* @return boolean
*/
public function blockMutex() {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @sem_acquire($this->mutexid);
return false;
}
/**
* Releases the class mutex
* After the release other processes are able to block the mutex themselfs
*
* @access protected
* @return boolean
*/
public function releaseMutex() {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @sem_release($this->mutexid);
return false;
}
/**
* Indicates if the requested variable is available in shared memory
*
* @param int $id int indicating the variable
*
* @access protected
* @return boolean
*/
public function hasData($id = 2) {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false)) {
if (function_exists("shm_has_var"))
return @shm_has_var($this->memid, $id);
else {
$some = $this->getData($id);
return isset($some);
}
}
return false;
}
/**
* Returns the requested variable from shared memory
*
* @param int $id int indicating the variable
*
* @access protected
* @return mixed
*/
public function getData($id = 2) {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @shm_get_var($this->memid, $id);
return ;
}
/**
* Writes the transmitted variable to shared memory
* Subclasses may never use an id < 2!
*
* @param mixed $data data which should be saved into shared memory
* @param int $id int indicating the variable (bigger than 2!)
*
* @access protected
* @return boolean
*/
public function setData($data, $id = 2) {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @shm_put_var($this->memid, $id, $data);
return false;
}
/**
* Sets the time when the shared memory block was created
*
* @access private
* @return boolean
*/
private function setInitialCleanTime() {
$stat = false;
// exclusive block
if ($this->blockMutex()) {
if ($this->hasData(1) == false)
$stat = $this->setData(time(), 1);
$this->releaseMutex();
}
// end exclusive block
return $stat;
}
}
<?php
/***********************************************
* File : ipcbackendshm.php
* Project : Z-Push
* Descr : IPC backend using SHM PHP extension
*
* Created : 20.10.2011
*
* Copyright 2007 - 2013 Zarafa Deutschland GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation with the following additional
* term according to sec. 7:
*
* According to sec. 7 of the GNU Affero General Public License, version 3,
* the terms of the AGPL are supplemented with the following terms:
*
* "Zarafa" is a registered trademark of Zarafa B.V.
* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
* The licensing of the Program under the AGPL does not imply a trademark license.
* Therefore any rights, title and interest in our trademarks remain entirely with us.
*
* However, if you propagate an unmodified version of the Program you are
* allowed to use the term "Z-Push" to indicate that you distribute the Program.
* Furthermore you may use our trademarks where it is necessary to indicate
* the intended purpose of a product or service provided you use it in accordance
* with honest practices in industrial or commercial matters.
* If you want to propagate modified versions of the Program under the name "Z-Push",
* you may only do so if you have a written permission by Zarafa Deutschland GmbH
* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Consult LICENSE file for details
************************************************/
include_once('lib/interface/iipcbackend.php');
class IpcBackendShm implements IIpcBackend
{
private $mutexid;
private $memid;
protected $type;
protected $allocate;
/**
* Constructor
*
* @param int $type
* @param int $allocate
* @param string $class
*/
public function __construct($type, $allocate, $class) {
$this->type = $type;
$this->allocate = $allocate;
if ($this->InitSharedMem())
ZLog::Write(LOGLEVEL_DEBUG, sprintf("%s(): Initialized mutexid %s and memid %s.", $class, $this->mutexid, $this->memid));
}
/**
* Allocates shared memory
*
* @access private
* @return boolean
*/
private function InitSharedMem() {
// shared mem general "turn off switch"
if (defined("USE_SHARED_MEM") && USE_SHARED_MEM === false) {
ZLog::Write(LOGLEVEL_INFO, "InterProcessData::InitSharedMem(): the usage of shared memory for Z-Push has been disabled. Check your config for 'USE_SHARED_MEM'.");
return false;
}
if (!function_exists('sem_get') || !function_exists('shm_attach') || !function_exists('sem_acquire')|| !function_exists('shm_get_var')) {
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.");
return false;
}
// Create mutex
$this->mutexid = @sem_get($this->type, 1);
if ($this->mutexid === false) {
ZLog::Write(LOGLEVEL_ERROR, "InterProcessData::InitSharedMem(): could not aquire semaphore");
return false;
}
// Attach shared memory
$this->memid = shm_attach($this->type+10, $this->allocate);
if ($this->memid === false) {
ZLog::Write(LOGLEVEL_ERROR, "InterProcessData::InitSharedMem(): could not attach shared memory");
@sem_remove($this->mutexid);
$this->mutexid = false;
return false;
}
// TODO mem cleanup has to be implemented
//$this->setInitialCleanTime();
return true;
}
/**
* Removes and detaches shared memory
*
* @access private
* @return boolean
*/
private function RemoveSharedMem() {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false)) {
@sem_acquire($this->mutexid);
$memid = $this->memid;
$this->memid = false;
@sem_release($this->mutexid);
@sem_remove($this->mutexid);
@shm_remove($memid);
@shm_detach($memid);
$this->mutexid = false;
return true;
}
return false;
}
/**
* Reinitializes shared memory by removing, detaching and re-allocating it
*
* @access public
* @return boolean
*/
public function ReInitSharedMem() {
return ($this->RemoveSharedMem() && $this->InitSharedMem());
}
/**
* Cleans up the shared memory block
*
* @access public
* @return boolean
*/
public function Clean() {
$stat = false;
// exclusive block
if ($this->blockMutex()) {
$cleanuptime = ($this->hasData(1)) ? $this->getData(1) : false;
// TODO implement Shared Memory cleanup
$this->releaseMutex();
}
// end exclusive block
return $stat;
}
/**
* Indicates if the shared memory is active
*
* @access public
* @return boolean
*/
public function IsActive() {
return ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false));
}
/**
* Blocks the class mutex
* Method blocks until mutex is available!
* ATTENTION: make sure that you *always* release a blocked mutex!
*
* @access protected
* @return boolean
*/
public function blockMutex() {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @sem_acquire($this->mutexid);
return false;
}
/**
* Releases the class mutex
* After the release other processes are able to block the mutex themselfs
*
* @access protected
* @return boolean
*/
public function releaseMutex() {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @sem_release($this->mutexid);
return false;
}
/**
* Indicates if the requested variable is available in shared memory
*
* @param int $id int indicating the variable
*
* @access protected
* @return boolean
*/
public function hasData($id = 2) {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false)) {
if (function_exists("shm_has_var"))
return @shm_has_var($this->memid, $id);
else {
$some = $this->getData($id);
return isset($some);
}
}
return false;
}
/**
* Returns the requested variable from shared memory
*
* @param int $id int indicating the variable
*
* @access protected
* @return mixed
*/
public function getData($id = 2) {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @shm_get_var($this->memid, $id);
return ;
}
/**
* Writes the transmitted variable to shared memory
* Subclasses may never use an id < 2!
*
* @param mixed $data data which should be saved into shared memory
* @param int $id int indicating the variable (bigger than 2!)
*
* @access protected
* @return boolean
*/
public function setData($data, $id = 2) {
if ((isset($this->mutexid) && $this->mutexid !== false) && (isset($this->memid) && $this->memid !== false))
return @shm_put_var($this->memid, $id, $data);
return false;
}
/**
* Sets the time when the shared memory block was created
*
* @access private
* @return boolean
*/
private function setInitialCleanTime() {
$stat = false;
// exclusive block
if ($this->blockMutex()) {
if ($this->hasData(1) == false)
$stat = $this->setData(time(), 1);
$this->releaseMutex();
}
// end exclusive block
return $stat;
}
}
<?php
/***********************************************
* File : iipcbackend.php
* Project : Z-Push
* Descr : Interface for interprocess communication
* backends for different purposes
*
* Created : 20.10.2011
*
* Copyright 2007 - 2013 Zarafa Deutschland GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation with the following additional
* term according to sec. 7:
*
* According to sec. 7 of the GNU Affero General Public License, version 3,
* the terms of the AGPL are supplemented with the following terms:
*
* "Zarafa" is a registered trademark of Zarafa B.V.
* "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
* The licensing of the Program under the AGPL does not imply a trademark license.
* Therefore any rights, title and interest in our trademarks remain entirely with us.
*
* However, if you propagate an unmodified version of the Program you are
* allowed to use the term "Z-Push" to indicate that you distribute the Program.
* Furthermore you may use our trademarks where it is necessary to indicate
* the intended purpose of a product or service provided you use it in accordance
* with honest practices in industrial or commercial matters.
* If you want to propagate modified versions of the Program under the name "Z-Push",
* you may only do so if you have a written permission by Zarafa Deutschland GmbH
* (to acquire a permission please contact Zarafa at trademark@zarafa.com).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Consult LICENSE file for details
************************************************/
interface IIpcBackend
{
/**
* Constructor
*
* @param int $type
* @param int $allocate
* @param string $class
*/
public function __construct($type, $allocate, $class);
/**
* Cleans up the shared memory block
*
* @access public
* @return boolean
*/
public function Clean();
/**
* Indicates if the shared memory is active
*
* @access public
* @return boolean
*/
public function IsActive();
/**
* Blocks the class mutex
* Method blocks until mutex is available!
* ATTENTION: make sure that you *always* release a blocked mutex!
*
* @access protected
* @return boolean
*/
public function blockMutex();
/**
* Releases the class mutex
* After the release other processes are able to block the mutex themselfs
*
* @access protected
* @return boolean
*/
public function releaseMutex();
/**
* Indicates if the requested variable is available in shared memory
*
* @param int $id int indicating the variable
*
* @access protected
* @return boolean
*/
public function hasData($id = 2);
/**
* Returns the requested variable from shared memory
*
* @param int $id int indicating the variable
*
* @access protected
* @return mixed
*/
public function getData($id = 2);
/**
* Writes the transmitted variable to shared memory
* Subclasses may never use an id < 2!
*
* @param mixed $data data which should be saved into shared memory
* @param int $id int indicating the variable (bigger than 2!)
*
* @access protected
* @return boolean
*/
public function setData($data, $id = 2);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment