Permalink
Browse files

Base DNode on top of react/socket, fixes #10

  • Loading branch information...
1 parent 5e34020 commit 5379dd23d6215f849635bec74c229c16bf1b161b @igorw igorw committed Jun 16, 2012
View
@@ -15,7 +15,8 @@
],
"require": {
"php": ">=5.3.0",
- "evenement/evenement": ">=0.1.0"
+ "evenement/evenement": ">=0.1.0",
+ "react/socket": "dev-master"
},
"autoload": {
"psr-0": {
@@ -14,7 +14,9 @@ public function temperature($cb)
}
}
-$dnode = new DNode\DNode(new Temp());
+$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) {
@@ -23,3 +25,5 @@ public function temperature($cb)
$connection->end();
});
});
+
+$loop->run();
@@ -16,6 +16,10 @@ public function clientTempF($cb)
}
}
+$loop = new React\EventLoop\StreamSelectLoop();
+
// Create a DNode server
-$server = new DNode\DNode(new Converter());
+$server = new DNode\DNode($loop, new Converter());
$server->listen(6060);
+
+$loop->run();
@@ -7,13 +7,16 @@ class RemoteSessionClient
private $value = null;
private $error = null;
private $exception = null;
- private $dnode = null;
+ private $loop = null;
private $port = 0;
+ private $dnode = null;
public function __construct ($port)
{
+ $this->loop = new React\EventLoop\StreamSelectLoop();
+
$this->port = $port;
- $this->dnode = new DNode\DNode($this);
+ $this->dnode = new DNode\DNode($this->loop, $this);
}
public function getPropertyValue($path)
@@ -25,13 +28,16 @@ public function getPropertyValue($path)
$connection->end();
});
});
+ $this->loop->run();
+
if ($this->exception != null) {
$exception = $this->exception;
$msg = $this->error;
$this->exception = null;
$this->error = null;
throw new $exception($msg);
}
+
return $this->value;
}
@@ -11,8 +11,10 @@ class RemoteSessionServer
public function __construct ($repository, $credentials, $workspace = null)
{
- $this->crSession = $repository->login($credentials, $workspace);
- $this->dnode = new DNode\DNode($this);
+ $this->loop = new React\EventLoop\StreamSelectLoop();
+
+ $this->crSession = $repository->login($credentials, $workspace);
+ $this->dnode = new DNode\DNode($this->loop, $this);
}
/* Get value of the property at defined path */
@@ -24,14 +26,15 @@ public function getPropertyValue($path, $cb)
} catch (\Exception $e) {
$this->remote->setException(get_class($e), $e->getMessage());
}
- $this->remote->setValue($value, function() use ($cb) {
+ $this->remote->setValue($value, function() use ($cb) {
$cb();
});
}
public function listen($port)
{
$this->dnode->listen($port);
+ $this->loop->run();
}
}
@@ -47,7 +50,12 @@ public function listen($port)
$server->listen(6060);
exit;
+
+$loop = new React\EventLoop\StreamSelectLoop();
+
// Create a DNode server
-$server = new DNode\DNode(new Converter());
+$server = new DNode\DNode($loop, new Converter());
$server->listen(6060);
+
+$loop->run();
*/
@@ -1,9 +1,11 @@
<?php
// Include Composer-generated autoloader
-require(__DIR__.'/../../vendor/.composer/autoload.php');
+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();
+$dnode = new DNode\DNode($loop);
$dnode->connect(7070, function($remote, $connection) {
$remote->getPropertyValue("default", "/jcr:primaryType", function($val, $exc, $error) use ($connection) {
echo $val;
@@ -12,3 +14,5 @@
$connection->end();
});
});
+
+$loop->run();
@@ -17,11 +17,13 @@ class SimpleRemoteRepository
*
* @param $repository - Repository created with RepositoryFactory.
* @return new remote repository
- */
+ */
public function __construct ($repository) {
+ $this->loop = new React\EventLoop\StreamSelectLoop();
+
$this->repository = $repository;
- $this->dnode = new DNode\DNode($this);
- }
+ $this->dnode = new DNode\DNode($this->loop, $this);
+ }
private function validateSessionName($name, $cb)
{
@@ -35,12 +37,12 @@ private function validateSessionName($name, $cb)
/**
* Get the names of children nodes
*
- * @param $sessionName - name of the session
+ * @param $sessionName - name of the session
* @param $path - absolute path of the parent node
* @param $cb - callback function
*
* @return void
- */
+ */
public function getNodes($sessionName, $path, $cb)
{
if (!$this->validateSessionName($sessionName, $cb))
@@ -65,12 +67,12 @@ public function getNodes($sessionName, $path, $cb)
/**
* Get the names of all properties
*
- * @param $sessionName - name of the session
+ * @param $sessionName - name of the session
* @param $path - absolute path of the node
* @param $cb - callback function
*
* @return void
- */
+ */
public function getProperties($sessionName, $path, $cb)
{
if (!$this->validateSessionName($sessionName, $cb))
@@ -133,7 +135,7 @@ public function addNode($sessionName, $path, $name, $type, $cb)
try {
$parent = $this->sessions[$sessionName]->getNode($path);
- $parent->addNode($name, $type);
+ $parent->addNode($name, $type);
} catch (\Exception $e) {
$exception = get_class($e);
$msg = $e->getMessage();
@@ -152,7 +154,7 @@ public function setProperty($sessionName, $path, $name, $value, $type, $cb)
try {
$parent = $this->sessions[$sessionName]->getNode($path);
- $parent->setProperty($name, $value, $type);
+ $parent->setProperty($name, $value, $type);
} catch (\Exception $e) {
$exception = get_class($e);
$msg = $e->getMessage();
@@ -171,6 +173,7 @@ public function createSession($sessionName, $name, $password)
public function listen($port)
{
$this->dnode->listen($port);
+ $this->loop->run();
}
}
@@ -2,11 +2,15 @@
// 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();
+$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();
@@ -13,6 +13,10 @@ public function zing($n, $cb)
}
}
+$loop = new React\EventLoop\StreamSelectLoop();
+
// Create a DNode server
-$server = new DNode\DNode(new Zinger());
+$server = new DNode\DNode($loop, new Zinger());
$server->listen(7070);
+
+$loop->run();
Oops, something went wrong.

0 comments on commit 5379dd2

Please sign in to comment.