├── .gitignore ├── .travis.yml ├── composer.json ├── phpunit.xml.dist ├── LICENSE ├── tests └── Simple │ └── SHM │ └── Test │ └── BlockTest.php ├── README.md └── src └── Simple └── SHM └── Block.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | phpunit.xml 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | 3 | language: php 4 | 5 | before_script: 6 | - travis_retry composer self-update 7 | - travis_retry composer install --no-interaction 8 | 9 | php: 10 | - 5.3 11 | - 5.4 12 | - 5.5 13 | - 5.6 14 | - 7.0 15 | 16 | script: 17 | - vendor/bin/phpunit 18 | 19 | cache: 20 | directories: 21 | - $HOME/.composer/cache -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "klaussilveira/simple-shm", 3 | "license": "BSD-2-Clause", 4 | "type": "library", 5 | "description": "SimpleSHM is a simple and small abstraction layer for shared memory manipulation using PHP.", 6 | "autoload": { 7 | "psr-0": { "Simple\\SHM": "src/" } 8 | }, 9 | "require": { 10 | "php": ">=5.3.3" 11 | }, 12 | "require-dev": { 13 | "phpunit/phpunit": "^4.8" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, Klaus Silveira and contributors 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 5 | 6 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 7 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 8 | Neither the name of SimpleSHM nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 9 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 10 | -------------------------------------------------------------------------------- /tests/Simple/SHM/Test/BlockTest.php: -------------------------------------------------------------------------------- 1 | assertInstanceOf('Simple\\SHM\\Block', $memory); 13 | 14 | $memory->write('Sample'); 15 | $data = $memory->read(); 16 | $this->assertEquals('Sample', $data); 17 | } 18 | 19 | public function testIsCreatingNewBlockWithId() 20 | { 21 | $memory = new Block(897); 22 | $this->assertInstanceOf('Simple\\SHM\\Block', $memory); 23 | $this->assertEquals(897, $memory->getId()); 24 | 25 | $memory->write('Sample 2'); 26 | $data = $memory->read(); 27 | $this->assertEquals('Sample 2', $data); 28 | } 29 | 30 | public function testIsMarkingBlockForDeletion() 31 | { 32 | $memory = new Block(897); 33 | $memory->delete(); 34 | $data = $memory->read(); 35 | $this->assertEquals('Sample 2', $data); 36 | } 37 | 38 | public function testIsPersistingNewBlockWithoutId() 39 | { 40 | $memory = new Block; 41 | $this->assertInstanceOf('Simple\\SHM\\Block', $memory); 42 | $memory->write('Sample 3'); 43 | unset($memory); 44 | 45 | $memory = new Block; 46 | $data = $memory->read(); 47 | $this->assertEquals('Sample 3', $data); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SimpleSHM 2 | [![Build Status](https://secure.travis-ci.org/klaussilveira/SimpleSHM.png)](http://travis-ci.org/klaussilveira/SimpleSHM) 3 | 4 | SimpleSHM is a simple and small abstraction layer for shared memory manipulation using PHP. It makes use of the SHMOP functions, built into most PHP packages. 5 | 6 | ## Authors and contributors 7 | * [Klaus Silveira](http://www.klaussilveira.com) (Creator, developer, support) 8 | 9 | ## License 10 | [New BSD license](http://www.opensource.org/licenses/bsd-license.php) 11 | 12 | ## Todo 13 | * add support for arrays, by automatically serializing or converting to JSON 14 | * add support for objects, by using JSON 15 | * create a better documentation 16 | * error handling can, and should, be improved 17 | * test, test, test 18 | 19 | ## About Shared Memory 20 | Shared Memory is an efficient mean of exchanging data between applications in the same machine. One application will create a memory portion which other processes can access, as long as they have the proper permissions. You can read more about it here: http://stereochro.me/assets/uploads/notes/dcom3/shmem.pdf 21 | 22 | ## Using SimpleSHM 23 | The idea behind SimpleSHM is to keep things very easy to use. If you want better control and flexibility, you can always modify the class or extend it. 24 | 25 | ```php 26 | write('Sample'); 35 | echo $memory->read(); 36 | 37 | /** 38 | * Creating new block, with an specified ID 39 | */ 40 | $new = new Block(897); 41 | $new->write('Sample'); 42 | echo $new->read(); 43 | 44 | /** 45 | * Reading an existing block, with the ID of 42 46 | */ 47 | $existing = new Block(42); 48 | echo $existing->read(); 49 | 50 | ``` 51 | -------------------------------------------------------------------------------- /src/Simple/SHM/Block.php: -------------------------------------------------------------------------------- 1 | id = $this->generateID(); 44 | } else { 45 | $this->id = $id; 46 | } 47 | 48 | if($this->exists($this->id)) { 49 | $this->shmid = shmop_open($this->id, "w", 0, 0); 50 | } 51 | } 52 | 53 | /** 54 | * Generates a random ID for a shared memory block 55 | * 56 | * @access protected 57 | * @return int System V IPC key generated from pathname and a project identifier 58 | */ 59 | protected function generateID() 60 | { 61 | $id = ftok(__FILE__, "b"); 62 | return $id; 63 | } 64 | 65 | /** 66 | * Checks if a shared memory block with the provided id exists or not 67 | * 68 | * In order to check for shared memory existance, we have to open it with 69 | * reading access. If it doesn't exist, warnings will be cast, therefore we 70 | * suppress those with the @ operator. 71 | * 72 | * @access public 73 | * @param string $id ID of the shared memory block you want to check 74 | * @return boolean True if the block exists, false if it doesn't 75 | */ 76 | public function exists($id) 77 | { 78 | $status = @shmop_open($id, "a", 0, 0); 79 | return $status; 80 | } 81 | 82 | /** 83 | * Writes on a shared memory block 84 | * 85 | * First we check for the block existance, and if it doesn't, we'll create it. Now, if the 86 | * block already exists, we need to delete it and create it again with a new byte allocation that 87 | * matches the size of the data that we want to write there. We mark for deletion, close the semaphore 88 | * and create it again. 89 | * 90 | * @access public 91 | * @param string $data The data that you wan't to write into the shared memory block 92 | */ 93 | public function write($data) 94 | { 95 | $size = mb_strlen($data, 'UTF-8'); 96 | 97 | if($this->exists($this->id)) { 98 | shmop_delete($this->shmid); 99 | shmop_close($this->shmid); 100 | $this->shmid = shmop_open($this->id, "c", $this->perms, $size); 101 | shmop_write($this->shmid, $data, 0); 102 | } else { 103 | $this->shmid = shmop_open($this->id, "c", $this->perms, $size); 104 | shmop_write($this->shmid, $data, 0); 105 | } 106 | } 107 | 108 | /** 109 | * Reads from a shared memory block 110 | * 111 | * @access public 112 | * @return string The data read from the shared memory block 113 | */ 114 | public function read() 115 | { 116 | $size = shmop_size($this->shmid); 117 | $data = shmop_read($this->shmid, 0, $size); 118 | 119 | return $data; 120 | } 121 | 122 | /** 123 | * Mark a shared memory block for deletion 124 | * 125 | * @access public 126 | */ 127 | public function delete() 128 | { 129 | shmop_delete($this->shmid); 130 | } 131 | 132 | /** 133 | * Gets the current shared memory block id 134 | * 135 | * @access public 136 | */ 137 | public function getId() 138 | { 139 | return $this->id; 140 | } 141 | 142 | /** 143 | * Gets the current shared memory block permissions 144 | * 145 | * @access public 146 | */ 147 | public function getPermissions() 148 | { 149 | return $this->perms; 150 | } 151 | 152 | /** 153 | * Sets the default permission (octal) that will be used in created memory blocks 154 | * 155 | * @access public 156 | * @param string $perms Permissions, in octal form 157 | */ 158 | public function setPermissions($perms) 159 | { 160 | $this->perms = $perms; 161 | } 162 | 163 | /** 164 | * Closes the shared memory block and stops manipulation 165 | * 166 | * @access public 167 | */ 168 | public function __destruct() 169 | { 170 | shmop_close($this->shmid); 171 | } 172 | } 173 | --------------------------------------------------------------------------------