public function get($name, $params, $callback) { try { $bestHost = $this->getBestService($name); if ($bestHost === false) { return false; } $loop = new \React\EventLoop\StreamSelectLoop(); $dnode = new \DNode\DNode($loop); $dnode->connect($bestHost['ip'], $bestHost['port'], function ($remote, $connection) use($bestHost, $params, $callback) { $remote->{$bestHost['call_name']}($params, function ($n) use($connection, $callback) { $connection->end(); $callback($n); }); }); $loop->run(); } catch (\Exception $e) { error_log($e->getMessage()); $callback(false); } }
<?php // Include Composer-generated autoloader require __DIR__ . '/../../vendor/autoload.php'; // This is the class we're exposing to DNode class Temp { // Compute the client's temperature and stuff that value into the callback public function temperature($cb) { $degC = rand(-20, 50); echo "{$degC}° C\n"; $cb($degC); } } $loop = new React\EventLoop\StreamSelectLoop(); $dnode = new DNode\DNode($loop, new Temp()); $dnode->connect(6060, function ($remote, $connection) { // Ask server for temperature in Fahrenheit $remote->clientTempF(function ($degF) use($connection) { echo "{$degF}° F\n"; // Close the connection $connection->end(); }); }); $loop->run();
<?php // Include Composer-generated autoloader require __DIR__ . '/../../vendor/autoload.php'; $loop = new React\EventLoop\StreamSelectLoop(); // Connect to DNode server running in port 7070 and call Zing with argument 33 $dnode = new DNode\DNode($loop); $dnode->connect(7070, function ($remote, $connection) { $remote->zing(33, function ($n) use($connection) { echo "n = {$n}\n"; $connection->end(); }); }); $loop->run();
<?php // script to check for memory leaks require __DIR__ . '/../../../vendor/autoload.php'; $loop = new React\EventLoop\StreamSelectLoop(); class Zinger { public $i = 0; public function zing($n, $callback) { $this->i++; $callback($n * 100); } } $zinger = new Zinger(); $server = new DNode\DNode($loop, $zinger); $server->listen(7070); $loop->addPeriodicTimer(2, function () use($zinger) { $kmem = memory_get_usage(true) / 1024; echo "Run: {$zinger->i}\n"; echo "Memory: {$kmem} KiB\n"; }); $loop->run();
<?php // script to check for memory leaks require __DIR__ . '/../../../vendor/autoload.php'; $loop = new React\EventLoop\StreamSelectLoop(); $i = 0; $loop->addPeriodicTimer(0.001, function () use($loop, &$i) { $i++; $client = new DNode\DNode($loop); $client->connect(7070, function ($remote, $conn) { $remote->zing(33, function ($n) use($conn) { $conn->end(); }); }); }); $loop->addPeriodicTimer(2, function () use(&$i) { $kmem = memory_get_usage(true) / 1024; echo "Run: {$i}\n"; echo "Memory: {$kmem} KiB\n"; }); $loop->run();
<?php // Include Composer-generated autoloader require __DIR__ . '/../../vendor/autoload.php'; // This is the class we're exposing to DNode class Converter { // Poll the client's own temperature() in celsius and convert that value to // fahrenheit in the supplied callback public function clientTempF($cb) { $this->remote->temperature(function ($degC) use($cb) { $degF = round($degC * 9 / 5 + 32); $cb($degF); }); } } $loop = new React\EventLoop\StreamSelectLoop(); // Create a DNode server $server = new DNode\DNode($loop, new Converter()); $server->listen(6060); $loop->run();
<?php // Include Composer-generated autoloader require __DIR__ . '/../../vendor/autoload.php'; $loop = new React\EventLoop\StreamSelectLoop(); // Connect to DNode server running in port 7070 and call Zing with argument 33 $dnode = new DNode\DNode($loop); $dnode->connect(7070, function ($remote, $connection) { $remote->getPropertyValue("default", "/jcr:primaryType", function ($val, $exc, $error) use($connection) { echo $val; if ($exc != null) { echo "Exception {$exc} thrown with message {$error} \n"; } $connection->end(); }); }); $loop->run();