More examples and tweaks
* Signal dispatch() method now uses own pid as default value * SharedData now updates checksum cache on set()
This commit is contained in:
		@@ -9,6 +9,12 @@
 | 
			
		||||
            "email": "cvagnetoft@gmail.com"
 | 
			
		||||
        }
 | 
			
		||||
    ],
 | 
			
		||||
    "keywords": [
 | 
			
		||||
        "ipc",
 | 
			
		||||
        "shm",
 | 
			
		||||
        "msgqueue",
 | 
			
		||||
        "signals"
 | 
			
		||||
    ],
 | 
			
		||||
    "require": {},
 | 
			
		||||
    "repositories": [
 | 
			
		||||
        {
 | 
			
		||||
@@ -24,4 +30,4 @@
 | 
			
		||||
            "NoccyLabs\\Ipc\\": "src/"
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										16
									
								
								examples/channels.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								examples/channels.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Ipc\Interop\Channel\StreamChannel;
 | 
			
		||||
 | 
			
		||||
// Create a pair of channels.
 | 
			
		||||
[ $ch1, $ch2 ] = StreamChannel::createPair();
 | 
			
		||||
 | 
			
		||||
// Send messages with send()
 | 
			
		||||
$ch1->send([
 | 
			
		||||
    'msg' => "Hello World"
 | 
			
		||||
]);
 | 
			
		||||
 | 
			
		||||
// Receive at the other end using receive()
 | 
			
		||||
print_r($ch2->receive());
 | 
			
		||||
							
								
								
									
										43
									
								
								examples/locks.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								examples/locks.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,43 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Ipc\Lock\FileLock;
 | 
			
		||||
 | 
			
		||||
// Creating the lock will not acquire it
 | 
			
		||||
$lock1 = new FileLock(__FILE__);
 | 
			
		||||
$lock2 = new FileLock(__FILE__);
 | 
			
		||||
 | 
			
		||||
// Acquire the lock like this
 | 
			
		||||
if ($lock1->acquire()) {
 | 
			
		||||
    echo "lock1 acquired\n";
 | 
			
		||||
} else {
 | 
			
		||||
    echo "lock1 not acquired\n";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Test the locks like this
 | 
			
		||||
if ($lock1->isLocked()) {
 | 
			
		||||
    echo "lock1 is locked\n";
 | 
			
		||||
} else {
 | 
			
		||||
    echo "lock1 is not locked\n";
 | 
			
		||||
}
 | 
			
		||||
if ($lock2->isLocked()) {
 | 
			
		||||
    echo "lock2 is locked\n";
 | 
			
		||||
} else {
 | 
			
		||||
    echo "lock2 is not locked\n";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// This will timeout after a second
 | 
			
		||||
if ($lock2->acquire(1)) {
 | 
			
		||||
    echo "lock2 acquired\n";
 | 
			
		||||
} else {
 | 
			
		||||
    echo "lock2 not acquired\n";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// After releasing, you can acquire it
 | 
			
		||||
$lock1->release();
 | 
			
		||||
if ($lock2->acquire(1)) {
 | 
			
		||||
    echo "lock2 acquired\n";
 | 
			
		||||
} else {
 | 
			
		||||
    echo "lock2 not acquired\n";
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										34
									
								
								examples/shmdata.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								examples/shmdata.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,34 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Ipc\Shm\SharedData;
 | 
			
		||||
use NoccyLabs\Ipc\Key\FileKey;
 | 
			
		||||
 | 
			
		||||
$key = new FileKey(__FILE__);
 | 
			
		||||
$shm = new SharedData($key);
 | 
			
		||||
 | 
			
		||||
// Set works as expected
 | 
			
		||||
$shm->set("some.key", "Some value");
 | 
			
		||||
// As does get
 | 
			
		||||
echo $shm->get("some.key")."\n";
 | 
			
		||||
 | 
			
		||||
// To make sure a value isn't modified while you are modifying it, use the third
 | 
			
		||||
// parameter to set...
 | 
			
		||||
//
 | 
			
		||||
// Let's start at 123
 | 
			
		||||
$shm->set("some.counter", 123);
 | 
			
		||||
$counter = $shm->get("some.counter");
 | 
			
		||||
echo "Counter is: ".$counter."\n";
 | 
			
		||||
// And attempt to increase it
 | 
			
		||||
$shm->set("some.counter", $counter + 1, true);
 | 
			
		||||
$counter = $shm->get("some.counter");
 | 
			
		||||
echo "Counter is: ".$counter."\n";
 | 
			
		||||
 | 
			
		||||
// If the value is modified, the call will fail
 | 
			
		||||
$shm2 = new SharedData($key);
 | 
			
		||||
$shm2->set("some.counter", 345);
 | 
			
		||||
if (!$shm->set("some.counter", $counter + 1, true)) {
 | 
			
		||||
    echo "some.counter has been modified since last read\n";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										29
									
								
								examples/signals.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								examples/signals.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Ipc\Signal\SignalHandler;
 | 
			
		||||
use NoccyLabs\Ipc\Signal\Signal;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
$handler = new SignalHandler(SIGUSR1);
 | 
			
		||||
 | 
			
		||||
// Add handlers like this, or as an array passed as the second argument to the constructor
 | 
			
		||||
$handler->addHandler(function () {
 | 
			
		||||
    echo "First handler\n";
 | 
			
		||||
});
 | 
			
		||||
// If a handler returns true, it will prevent any further handlers from firing
 | 
			
		||||
$handler->addHandler(function () {
 | 
			
		||||
    echo "Second and final handler\n";
 | 
			
		||||
    return true;
 | 
			
		||||
});
 | 
			
		||||
// Thus, this one will not be called
 | 
			
		||||
$handler->addHandler(function() {
 | 
			
		||||
    echo "Third handler, never called\n";
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
// Dispatch the signal to ourselves
 | 
			
		||||
(new Signal(SIGUSR1))->dispatch(posix_getpid());
 | 
			
		||||
// Default target of dispatch is the own pid
 | 
			
		||||
(new Signal(SIGUSR1))->dispatch();
 | 
			
		||||
    
 | 
			
		||||
							
								
								
									
										16
									
								
								examples/signaltraps.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								examples/signaltraps.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
require_once __DIR__."/../vendor/autoload.php";
 | 
			
		||||
 | 
			
		||||
use NoccyLabs\Ipc\Signal\SignalTrap;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
$trap = new SignalTrap(SIGINT);
 | 
			
		||||
echo "Press ctrl-c...\n";
 | 
			
		||||
 | 
			
		||||
while (!$trap->isTrapped()) {
 | 
			
		||||
    usleep(10000);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
echo "Thanks!\n";
 | 
			
		||||
@@ -113,6 +113,7 @@ class SharedData extends SharedMemory
 | 
			
		||||
 | 
			
		||||
        // Update the data
 | 
			
		||||
        $this[self::IDX_DATA + $index] = [ md5($value), $value ];
 | 
			
		||||
        $this->checks[$key] = md5($value);
 | 
			
		||||
        $this->unlock();
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -17,8 +17,8 @@ class Signal
 | 
			
		||||
        pcntl_signal($this->signo, $handler);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function dispatch($pid):bool
 | 
			
		||||
    public function dispatch($pid=null):bool
 | 
			
		||||
    {
 | 
			
		||||
        return posix_kill($pid, $this->signo);
 | 
			
		||||
        return posix_kill($pid?:posix_getpid(), $this->signo);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user