/** * Creates a new test case in the suite. * * @param object $function Test function * @param string $name Test name * * @return object SIG_Test */ function test($function, $name = null) { xp_signal($this->_test, xp_exhaust(1, function ($signal) use($name, $function) { $signal->name = $name; return $function($signal); })); }
/** * 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 xp_import('time'); $precision = 10; $signal = new \time\SIG_Awake($precision, TIME_MILLISECONDS); $signal->time = milliseconds(); $precision_timing = []; $function = xp_exhaust(100, function ($signal) use($precision, &$precision_timing) { $timing = intval(floatval(milliseconds() - $signal->time)) - $precision; echo $timing . PHP_EOL; // if ($timing > 100000) { // // Second change // $timing = 0; // } $precision_timing[] = [$timing, 0]; $signal->time = milliseconds(); }); xp_signal($signal, $function); xp_on_shutdown(function () use(&$precision_timing) { array_shift($precision_timing); $results = ['msPrecision' => $precision_timing]; ob_start(); include dirname(realpath(__FILE__)) . '/chart.php'; $data = ob_get_contents(); ob_end_clean(); file_put_contents('millisecond_precision.html', $data); echo "Performance chart in millisecond_precision.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. */ require_once dirname(realpath(__FILE__)) . '/../__init__.php'; xp_import('unittest'); /** * Emit Unitest */ unittest\test(function ($test) { $foo = XP_SIG(spl_object_hash(new stdClass())); $process = xp_process(null); xp_signal($foo, $process); xp_delete_process($foo, $process); $test->count(xp_get_signal($foo), 0); }, 'API Delete 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 '__init__.php'; xp_import('unittest'); unittest\test(function ($test) { $foo = XP_SIG('foo'); xp_before($foo, function ($foo) { $foo->bar = 'HelloWorld'; }); xp_after($foo, function ($foo) { unset($foo->bar); }); xp_signal($foo, function ($foo) use($test) { $test->equal($foo->bar, 'HelloWorld'); }); xp_emit($foo); $test->false(isset($foo->bar)); }, 'Interruptions');
/** * Registers a function to call when the processor starts. * * @param callable|object $function Function or process object * * @return object \XPSPL\Process * * @example * * Example #1 Basic Usage * * .. code-block:: php * * <?php * * xp_on_start(function(){ * echo 'The processor started'; * }); * * xp_wait_loop(); * * The above code will output. * * .. code-block:: php * * The processor started! */ function xp_on_start($function) { return xp_signal(new \XPSPL\processor\SIG_Startup(), $function); }
/** * 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. */ require_once dirname(realpath(__FILE__)) . '/../__init__.php'; xp_import('unittest'); /** * Emit Unitest */ unittest\test(function ($test) { $foo = XP_SIG('foo'); xp_signal($foo, function ($foo) { $foo->foo = 'foo'; }); xp_emit($foo); $test->equal($foo->foo, 'foo'); }, 'API Emit');
/** * Installs a process to run when an upload finishes completely. * * @param callable $callback Callback function to call. * * @return object Process */ function finished(SIG_Upload $upload, $callback) { return xp_signal($upload->SIG_Finished(), $callback); }
} xp_emit(XP_SIG($read)); }); // Once a bride, groom and bell signals are emitted we emit the wedding. $wedding = xp_complex_sig(function ($signal) { if (!isset($this->reset) || $this->reset) { $this->reset = false; $this->bride = false; $this->groom = false; $this->bells = false; } switch (true) { case $signal->compare(XP_SIG('groom')): $this->groom = true; break; case $signal->compare(XP_SIG('bride')): $this->bride = true; break; case $signal->compare(XP_SIG('bells')): $this->bells = true; break; } if ($this->groom && $this->bride && $this->bells) { $this->reset = true; return true; } return false; }); xp_signal($wedding, function () { echo 'A wedding just happened.'; });
/** * Registers a function to call when the processor shuts down. * * @param callable|object $function Function or process object * * @return object \XPSPL\Process * * @example * * Example #1 Basic Usage * * .. code-block:: php * * <?php * * xp_on_shutdown(function(){ * echo 'Shutting down the processor!'; * }); * * xp_wait_loop(); * * The above code will output. * * .. code-block:: php * * Shutting down the processor! */ function xp_on_shutdown($function) { return xp_signal(new \XPSPL\processor\SIG_Shutdown(), $function); }
<?php xp_import('network'); $ops = 0; $thread = xp_threaded_process(function () { $start_time = 0; $this->operations = 0; $sig = XP_SIG('a'); do { echo $ops; xp_emit($sig); $this->operations++; $ops = $this->operations / (microtime(true) - $start_time); } while (true); }); $ops_sig = xp_signal(XP_SIG('op'), xp_exhaust(null, $thread)); xp_emit(XP_SIG('op')); // $server = network\connect('0.0.0.0', ['port'=>8000]); // $server->on_connect(xp_null_exhaust(function($server) use ($ops){ // $server->socket->write($ops); // $server->socket->disconnect(); // })); // $server = // $iterations = 1000000; // $start = microtime(true); // for ($i=0;$i<$iterations;$i++) {} // $end = microtime(true); // $loop_time = $end - $start; // $sig = XP_SIG(null); // $start = microtime(true); // for ($i=0;$i<$iterations;$i++) {
<?php print microseconds(); xp_import('time'); $precision = 500; $signal = new \time\SIG_Awake($precision, TIME_MICROSECONDS); $precision_timing = []; xp_signal($signal, xp_exhaust(5500, function ($signal) use($precision, &$precision_timing) { if (!isset($signal->time)) { $signal->time = microseconds(); return true; } $timing = floatval((microseconds() - $signal->time) * 1000000) - $precision; if ($timing > 1000 || $timing < 0) { // Second change $signal->time = microseconds(); return; } $precision_timing[] = [$timing, 0]; $signal->time = microseconds(); })); xp_on_shutdown(function () use(&$precision_timing) { array_shift($precision_timing); $results = ['usPrecision' => $precision_timing]; ob_start(); include dirname(realpath(__FILE__)) . '/chart.php'; $data = ob_get_contents(); ob_end_clean(); file_put_contents('microsecond_precision.html', $data); echo "Performance chart in precision.html" . PHP_EOL; });
<?php // Handle Foo xp_signal(XP_SIG('foo'), function ($signal) { echo $signal->bar; }); // When foo is emitted insert bar into the event xp_before(XP_SIG('foo'), function ($signal) { echo "I RAN" . PHP_EOL; $signal->bar = 'foo'; }); xp_before(XP_SIG('foo'), xp_high_priority(function ($signal) { echo "I RAN BEFORE" . PHP_EOL; })); // After foo is emitted unset bar in the event xp_after(XP_SIG('foo'), function ($signal) { unset($signal->bar); }); $signal = xp_emit(XP_SIG('foo')); var_dump(isset($signal->bar));
<?php $max_threads = 16 - 1; class My_Thread extends \XPSPL\Process { public function execute($signal, $thread) { while (true) { var_dump($thread); } } } for ($i = 0; $i < $max_threads; $i++) { xp_signal(XP_SIG('test' . $i), xp_threaded_process(new My_Thread())); } for ($i = 0; $i < $max_threads; $i++) { xp_emit(XP_SIG('test' . $i)); }
/** * Creates a new test case. * * @param object $function Test function * @param string $name Test name * * @return array [Process, Signal] */ function test($function, $name = null) { return xp_signal(new SIG_Test($name), $function); }
/** * Registers a new handle for socket connection errors. * * @param callable $function Function to call on errors. * * @return object */ public function on_error($function) { return xp_signal(new SIG_Error($this), $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')));
<?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) { $foo = XP_SIG('foo'); xp_signal($foo, function () { }); xp_before($foo, function ($foo) { $foo->foo = 'foo'; }); xp_emit($foo); $test->equal($foo->foo, 'foo'); }, 'API Before function');
<?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) { $foo = XP_SIG('foo'); xp_signal($foo, null); $test->instanceof(xp_get_signal($foo), 'XPSPL\\database\\Processes'); }, 'API Signal');