/** * Wakes the system using the Unix CRON expressions. * * If no priority is provided with the ```$process``` it is set to null. * * @param string $cron CRON Expression * @param callable $process Callback function to run * * @return array [signal, process] */ function CRON($cron, $process) { if (!$process instanceof \XPSPL\Process) { $process = xp_null_exhaust($process); } $signal = new SIG_CRON($cron); return [$signal, xp_signal($signal, $process)]; }
<?php /** * Copyright 2010-12 Nickolas Whiting. All rights reserved. * Use of this source code is governed by the Apache 2 license * that can be found in the LICENSE file. */ require_once dirname(realpath(__FILE__)) . '/../__init__.php'; xp_import('unittest'); unittest\test(function ($test) { $process = xp_null_exhaust(null); $test->equal(null, $process->exhaustion()); }, 'API null exhaust');
/** * Creates a new socket connection. * * Connection options. * * * **port** - Default: null * * **domain** - Default: AF_INET * * **type** - Default: SOCK_STREAM * * **protocol** - Default: SOL_TCP * * @param string $address Address to make the connection on. * @param string $options Connection options * @param callback $callback Function to call when connected * * @return object \network\Socket */ function connect($address, $options = []) { $socket = new Socket($address, $options); xp_signal($socket, xp_null_exhaust(null)); return $socket; }
<?php /** * Copyright 2010-12 Nickolas Whiting. All rights reserved. * Use of this source code is governed by the Apache 2 license * that can be found in the LICENSE file. */ date_default_timezone_set('America/New_York'); /** * CRON * * This example demonstrates how to use CRON to awake XPSPL. */ xp_import('time'); time\CRON('* * * * *', xp_null_exhaust(function () { echo "Every Minute!" . PHP_EOL; }));
<?php /** * Copyright 2010-12 Nickolas Whiting. All rights reserved. * Use of this source code is governed by the Apache 2 license * that can be found in the LICENSE file. */ /** * Echo Server * * This example demonstrates a simple echo server that spits back anything that * was sent and then disconnects. */ xp_import('network'); xp_import('time'); $server = network\connect('0.0.0.0', ['port' => '1337']); $server->on_connect(xp_null_exhaust(function (network\SIG_Connect $sig_connect) { if (null !== $sig_connect->socket) { echo "Connection " . PHP_EOL; echo "Closing connection in 5 seconds" . PHP_EOL; time\awake(3, function () use($sig_connect) { echo "I RAN" . PHP_EOL; $sig_connect->socket->write('Goodbye'); $sig_connect->socket->disconnect(); }); } }));
/** * Registers a teardown function. * * @param object $function Closure * * @return void */ public function teardown($function) { xp_after($this->_test, xp_null_exhaust($function)); }
* @param object $sig_disconnect \network\SIG_Disconnect * * @return void */ function system_disconnect(SIG_Disconnect $sig_disconnect) { if (is_resource($sig_disconnect->socket->get_resource())) { socket_close($sig_disconnect->socket->get_resource()); } } /** * Flushes the read buffer at the end of the cycle. * * This makes data available from the ``read`` method. * * @return void */ function clean_buffer(SIG_Read $sig_read) { $sig_read->socket->flush_read_buffer(); } /** * Register the disconnect * * This fires as a last priority to allow pushing content to the host. * * Though it should be noted that pushing content to a disconnected socket might * not get the data. */ xp_signal(new SIG_Disconnect(), xp_low_priority(xp_null_exhaust('\\network\\system_disconnect')));
if ($a === 1) { $setup = true; } else { $setup = false; } if (!isset($results[$_test][$tc])) { $results[$_test][$tc] = []; } for ($c = 0; $c < $tc; $c++) { // do the test $start = microtime(true); $_func($processor, $sig); $end = microtime(true); // add test time $results[$_test][] = $end - $start; ++$total_tests; $processor->flush(); $processor->signal($sig, xp_null_exhaust(null)); } } } $output::send(sprintf('Test %s complete', $_test)); } // echo '--------------------------------------'.PHP_EOL; // echo "Total tests performed " . number_format($total_tests).PHP_EOL; // ob_start(); include dirname(realpath(__FILE__)) . '/averages_output.php'; // $data = ob_get_contents(); // ob_end_clean(); // file_put_contents('performance_chart.html', $data); // echo "Performance chart in performance_chart.html".PHP_EOL;
<?php /** * Copyright 2010-12 Nickolas Whiting. All rights reserved. * Use of this source code is governed by the Apache 2 license * that can be found in the LICENSE file. */ /** * Performance test. */ xp_import('network'); $server = network\connect('0.0.0.0', ['port' => '1337']); $server->on_connect(xp_null_exhaust(function (network\SIG_Connect $sig_connect) { $sig_connect->socket->write('HelloWorld'); $sig_connect->socket->disconnect(); })); $server->on_error(xp_null_exhaust(function (network\SIG_Error $sig_error) { echo 'Error : ' . $sig_error->socket->error_str . PHP_EOL; }));
<?php /** * Copyright 2010-12 Nickolas Whiting. All rights reserved. * Use of this source code is governed by the Apache 2 license * that can be found in the LICENSE file. */ /** * Awake * * This example demonstrates how to awake XPSPL. */ xp_import('time'); time\awake(10, function () { echo "10 Seconds just passed!" . PHP_EOL; }); time\awake(5, xp_null_exhaust(function () { echo "Every 5 seconds" . PHP_EOL; }));