Removed the bus interop code for now
This commit is contained in:
@ -1,20 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Ipc\Interop\Async;
|
||||
|
||||
|
||||
class Promise
|
||||
{
|
||||
private $then;
|
||||
|
||||
public function then(callable $then)
|
||||
{
|
||||
$this->then = $then;
|
||||
}
|
||||
|
||||
public function invoke(...$args)
|
||||
{
|
||||
call_user_func($this->then, ...$args);
|
||||
}
|
||||
|
||||
}
|
@ -1,104 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Ipc\Interop\Bus;
|
||||
|
||||
use NoccyLabs\Ipc\Interop\Channel\ChannelInterface;
|
||||
use NoccyLabs\Ipc\Interop\Channel\MultiStreamChannel;
|
||||
use NoccyLabs\Ipc\Interop\Async\Timer;
|
||||
use NoccyLabs\Ipc\Interop\Async\Promise;
|
||||
|
||||
class Client
|
||||
{
|
||||
/** @var ChannelInterface $channel */
|
||||
protected $channel;
|
||||
|
||||
protected $listeners = [];
|
||||
|
||||
protected $promises = [];
|
||||
|
||||
protected $exports = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct(ChannelInterface $channel)
|
||||
{
|
||||
$this->channel = $channel;
|
||||
$this->timer = new Timer([ $this, "update" ]);
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
while ($msg = $this->channel->receive()) {
|
||||
if (!array_key_exists('op', $msg)) {
|
||||
fprintf(STDERR, "Warning: Frame missing op -- %s\n", json_encode($msg));
|
||||
continue;
|
||||
}
|
||||
switch ($msg['op']) {
|
||||
case 'retn':
|
||||
$id = $msg['id'];
|
||||
if (!array_key_exists($id, $this->promises)) {
|
||||
continue;
|
||||
}
|
||||
$promise = $this->promises[$id];
|
||||
$promise->invoke($msg['ret']);
|
||||
unset($this->promises[$id]);
|
||||
continue;
|
||||
case 'call':
|
||||
$id = $msg['id'];
|
||||
$obj = $msg['obj'];
|
||||
if (!array_key_exists($obj, $this->exports)) {
|
||||
continue;
|
||||
}
|
||||
$ret = call_user_func($this->exports[$obj], ...$msg['arg']);
|
||||
$frame = [
|
||||
'op' => 'retn',
|
||||
'id' => $id,
|
||||
'ret' => $ret
|
||||
];
|
||||
$this->channel->send($frame);
|
||||
continue;
|
||||
case 'msg':
|
||||
default:
|
||||
foreach ($this->listeners as $listener) {
|
||||
call_user_func($listener, $msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function addListener(callable $handler)
|
||||
{
|
||||
$this->listeners[] = $handler;
|
||||
}
|
||||
|
||||
public function export($path, callable $function)
|
||||
{
|
||||
$this->exports[$path] = $function;
|
||||
}
|
||||
|
||||
public function send($data)
|
||||
{
|
||||
$frame = [
|
||||
'op' => 'msg',
|
||||
'msg' => $data
|
||||
];
|
||||
$this->channel->send($frame);
|
||||
}
|
||||
|
||||
public function call($path, ...$args)
|
||||
{
|
||||
$promise = new Promise();
|
||||
|
||||
$rid = uniqid(md5($path),true);
|
||||
$frame = [
|
||||
'op' => 'call',
|
||||
'id' => $rid,
|
||||
'obj' => $path,
|
||||
'arg' => $args,
|
||||
];
|
||||
$this->promises[$rid] = $promise;
|
||||
$this->channel->send($frame);
|
||||
return $promise;
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace NoccyLabs\Ipc\Interop\Bus;
|
||||
|
||||
use NoccyLabs\Ipc\Interop\Channel\ChannelInterface;
|
||||
use NoccyLabs\Ipc\Interop\Channel\MultiStreamChannel;
|
||||
use NoccyLabs\Ipc\Interop\Async\Timer;
|
||||
|
||||
class Master
|
||||
{
|
||||
/** @var ChannelInterface $channel */
|
||||
protected $channel;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->channel = new MultiStreamChannel();
|
||||
|
||||
$this->timer = new Timer([ $this, "update" ]);
|
||||
}
|
||||
|
||||
public function createClient() : Client
|
||||
{
|
||||
/** @var ChannelInterface $channel */
|
||||
$channel = $this->channel->createClient();
|
||||
|
||||
$client = new Client($channel);
|
||||
return $client;
|
||||
}
|
||||
|
||||
public function update()
|
||||
{
|
||||
while ($msg = $this->channel->receive()) {
|
||||
$this->channel->send($msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user