Refactored main class, improved a few things here and there. Looks polished enough
This commit is contained in:
parent
75c6062a3e
commit
411898af2a
14 changed files with 351 additions and 300 deletions
|
@ -1,44 +1,18 @@
|
|||
<?php
|
||||
|
||||
// Import everything
|
||||
require 'ncc';
|
||||
require __DIR__ . DIRECTORY_SEPARATOR . 'ExampleClass.php';
|
||||
import('net.nosial.tamerlib');
|
||||
|
||||
// Initialize as a worker, will fail if the process is executed directly
|
||||
// Initialize TamerLib
|
||||
\TamerLib\tm::initialize(\TamerLib\Enums\TamerMode::WORKER);
|
||||
|
||||
// Callback Examples
|
||||
\TamerLib\tm::addFunction('sleep', function($sleep_time){
|
||||
sleep($sleep_time);
|
||||
return $sleep_time;
|
||||
});
|
||||
\TamerLib\tm::addFunction('calculate_pi', function($iterations){
|
||||
$pi = 0;
|
||||
$sign = 1;
|
||||
for($i = 0; $i < $iterations; $i++)
|
||||
{
|
||||
$pi += $sign * (1 / (2 * $i + 1));
|
||||
$sign *= -1;
|
||||
}
|
||||
return $pi * 4;
|
||||
});
|
||||
|
||||
// Function pointer examples
|
||||
// Register the functions
|
||||
$example_class = new ExampleClass();
|
||||
\TamerLib\tm::addFunction('getValue', [$example_class, 'get']);
|
||||
\TamerLib\tm::addFunction('setValue', [$example_class, 'set']);
|
||||
\TamerLib\tm::addFunction('valueExists', [$example_class, 'exists']);
|
||||
\TamerLib\tm::addFunction('clearValues', [$example_class, 'clear']);
|
||||
\TamerLib\tm::addFunction('sleep', [$example_class, 'sleep']);
|
||||
\TamerLib\tm::addFunction('pi', [$example_class, 'pi']);
|
||||
\TamerLib\tm::addFunction('throwException', [$example_class, 'throwException']);
|
||||
|
||||
// Run forest, run!
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
\TamerLib\tm::run();
|
||||
}
|
||||
catch(Exception $e)
|
||||
{
|
||||
print($e->getMessage() . PHP_EOL);
|
||||
}
|
||||
}
|
||||
// Run the worker
|
||||
\TamerLib\tm::run();
|
Loading…
Add table
Add a link
Reference in a new issue