示例#1
0
 public function __construct($bridgeName = null, $appBootstrap, $appenv)
 {
     $this->bridgeName = $bridgeName;
     $this->bootstrap($appBootstrap, $appenv);
     $this->connectToMaster();
     $this->loop->run();
 }
示例#2
0
 public function __construct($bridgeName = null, $appBootstrap, $appenv, $host, $portOffset, $masterPort)
 {
     $this->bridgeName = $bridgeName;
     $this->slaveHost = $host;
     $this->portOffset = $portOffset;
     $this->masterPort = $masterPort;
     $this->bootstrap($appBootstrap, $appenv);
     $this->connectToMaster();
     $this->setTimer();
     $this->loop->run();
 }
示例#3
0
 /**
  * Start the transport
  */
 public function start($startLoop = true)
 {
     $this->transportProvider->startTransportProvider($this, $this->loop);
     if ($startLoop) {
         $this->loop->run();
     }
 }
示例#4
0
 /**
  * Starts the main loop. Blocks.
  */
 public function run()
 {
     Debug::enable();
     //make whatever is necessary to disable all stuff that could buffer output
     ini_set('zlib.output_compression', 0);
     ini_set('output_buffering', 0);
     ini_set('implicit_flush', 1);
     ob_implicit_flush(1);
     $this->loop = \React\EventLoop\Factory::create();
     $this->controller = new React\Server($this->loop);
     $this->controller->on('connection', array($this, 'onSlaveConnection'));
     $this->controllerHost = $this->getNewControllerHost();
     $this->controller->listen(5500, $this->controllerHost);
     $this->web = new \React\Socket\Server($this->loop);
     $this->web->on('connection', array($this, 'onWeb'));
     $this->web->listen($this->port, $this->host);
     $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop);
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     $pcntl->on(SIGCHLD, [$this, 'handleSigchld']);
     $pcntl->on(SIGUSR1, [$this, 'restartWorker']);
     if ($this->isDebug()) {
         $this->loop->addPeriodicTimer(0.5, function () {
             $this->checkChangedFiles();
         });
     }
     $this->isRunning = true;
     $loopClass = (new \ReflectionClass($this->loop))->getShortName();
     $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>");
     for ($i = 0; $i < $this->slaveCount; $i++) {
         $this->newInstance(5501 + $i);
     }
     $this->loop->run();
 }
示例#5
0
 public function testDealerRep()
 {
     $pids[] = $this->forkRepWorker();
     $pids[] = $this->forkRepWorker();
     $loop = new StreamSelectLoop();
     $context = new Context($loop);
     $dealer = $context->getSocket(\ZMQ::SOCKET_DEALER);
     $dealer->bind('ipc://test2.ipc');
     sleep(1);
     $msgs = array();
     $dealer->on('message', function ($msg) use(&$msgs) {
         $msgs[] = $msg;
     });
     $dealer->send(array('A', '', 'foo'));
     $dealer->send(array('B', '', 'bar'));
     $loop->addTimer(1, function () use($loop) {
         $loop->stop();
     });
     $loop->run();
     foreach ($pids as $pid) {
         pcntl_waitpid($pid, $status, WUNTRACED);
     }
     $this->assertCount(2, $msgs);
     $this->assertContains(array('A', '', 'foobar'), $msgs);
     $this->assertContains(array('B', '', 'barbar'), $msgs);
 }
示例#6
0
 /**
  * Starts the main loop. Blocks.
  */
 public function run()
 {
     Debug::enable();
     gc_disable();
     //necessary, since connections will be dropped without reasons after several hundred connections.
     $this->loop = \React\EventLoop\Factory::create();
     $this->controller = new \React\Socket\Server($this->loop);
     $this->controller->on('connection', array($this, 'onSlaveConnection'));
     $this->controller->listen(5500);
     $this->web = new \React\Socket\Server($this->loop);
     $this->web->on('connection', array($this, 'onWeb'));
     $this->web->listen($this->port, $this->host);
     $this->tcpConnector = new \React\SocketClient\TcpConnector($this->loop);
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     if ($this->isDebug()) {
         $this->loop->addPeriodicTimer(0.5, function () {
             $this->checkChangedFiles();
         });
     }
     $this->isRunning = true;
     $loopClass = (new \ReflectionClass($this->loop))->getShortName();
     $this->output->writeln("<info>Starting PHP-PM with {$this->slaveCount} workers, using {$loopClass} ...</info>");
     for ($i = 0; $i < $this->slaveCount; $i++) {
         $this->newInstance(5501 + $i);
     }
     $this->loop->run();
 }
示例#7
0
 /**
  * Connects to ProcessManager, master process.
  */
 public function run()
 {
     $this->loop = \React\EventLoop\Factory::create();
     $this->errorLogger = BufferingLogger::create();
     ErrorHandler::register(new ErrorHandler($this->errorLogger));
     $client = stream_socket_client($this->config['controllerHost']);
     $this->controller = new \React\Socket\Connection($client, $this->loop);
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     register_shutdown_function([$this, 'shutdown']);
     $this->bindProcessMessage($this->controller);
     $this->controller->on('close', \Closure::bind(function () {
         $this->shutdown();
     }, $this));
     $this->server = new React\Server($this->loop);
     //our version for now, because of unix socket support
     $http = new HttpServer($this->server);
     $http->on('request', array($this, 'onRequest'));
     //port is only used for tcp connection. If unix socket, 'host' contains the socket path
     $port = $this->config['port'];
     $host = $this->config['host'];
     while (true) {
         try {
             $this->server->listen($port, $host);
             break;
         } catch (\React\Socket\ConnectionException $e) {
             usleep(500);
         }
     }
     $this->sendMessage($this->controller, 'register', ['pid' => getmypid(), 'port' => $port]);
     $this->loop->run();
 }
示例#8
0
 public function run($port)
 {
     self::$port = $port;
     $loop = new StreamSelectLoop();
     $socket = new SocketServer($loop);
     $http = new HttpServer($socket, $loop);
     $http->on('request', [$this, 'serve']);
     $socket->listen($port);
     echo "Reactavel server started on localhost:{$port}\n";
     $loop->run();
 }
 public function run()
 {
     while (true) {
         try {
             $this->loadAddresses();
             //aby načetl nově generované adresy
             $this->client->open();
             $this->loop->run();
         } catch (\Exception $e) {
             $this->logger->err($e->getMessage());
         }
     }
 }
示例#10
0
 /**
  * @see \Markdown\Service\RenderServiceInterface::render()
  */
 public function render($input)
 {
     $key = hash('sha512', $input);
     if ($this->storage->hasItem($key)) {
         return $this->storage->getItem($key);
     }
     $rendered = null;
     $this->dnode->connect($this->options->getHost(), $this->options->getPort(), function ($remote, $connection) use($input, &$rendered) {
         $remote->render($input, function ($output, $exception = null, $error = null) use(&$rendered, $connection) {
             if ($exception !== null) {
                 $connection->end();
                 throw new Exception\RuntimeException(sprintf('Bridge threw exception "%s" with message "%s".', $exception, $error));
             }
             $rendered = $output;
             $connection->end();
         });
     });
     $this->loop->run();
     if ($rendered === null) {
         throw new Exception\RuntimeException(sprintf('Broken pipe'));
     }
     $this->storage->setItem($key, $rendered);
     return $rendered;
 }
示例#11
0
 public function testConnectFails()
 {
     $loop = new StreamSelectLoop();
     $deferred = new Deferred();
     $deferred->promise()->then(function () {
         $this->fail('should not have succeeded');
     }, function ($value) use($loop) {
         $this->assertEquals(1, $value);
         $loop->stop();
     });
     $request = new RequestFactory();
     $connector = new TcpConnector($loop);
     $client = new Client($connector, $request);
     $client->connect('127.0.0.1', 54320);
     $loop->run();
 }
示例#12
0
 /** @test */
 public function connectionToIp6TcpServerShouldSucceed()
 {
     $capturedStream = null;
     $loop = new StreamSelectLoop();
     $server = new Server($loop);
     $server->on('connection', $this->expectCallableOnce());
     $server->on('connection', array($server, 'shutdown'));
     $server->listen(9999, '::1');
     $connector = new TcpConnector($loop);
     $connector->create('::1', 9999)->then(function ($stream) use(&$capturedStream) {
         $capturedStream = $stream;
         $stream->end();
     });
     $loop->run();
     $this->assertInstanceOf('React\\Stream\\Stream', $capturedStream);
 }
 /**
  * {@inheritDoc}
  * @see \Symfony\Component\Console\Command\Command::execute()
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     // Start listenning
     $this->socket->listen($this->port, $this->host);
     // Periodically call determining if we should stop or not
     $this->loop->addPeriodicTimer($input->getOption('check-interval'), function () use($output) {
         if ($this->shouldExitCommand($output)) {
             $this->loop->stop();
             $this->writeln($output, 'Event loop stopped:' . $this->port);
             $this->returnValue = 10;
         }
     });
     // Main loop
     $this->writeln($output, 'Starting event loop:' . $this->port);
     $this->loop->run();
     return $this->returnValue;
 }
 /**
  * @covers DNode\DNode::__construct
  * @covers DNode\DNode::connect
  * @covers DNode\DNode::listen
  * @test
  */
 public function transformerShouldRespondCorrectly()
 {
     $captured = null;
     $loop = new StreamSelectLoop();
     $server = new DNode($loop, new Transformer());
     $socket = $server->listen(5004);
     $client = new DNode($loop);
     $client->connect(5004, function ($remote, $conn) use(&$captured, $socket) {
         $remote->transform('fou', function ($transformed) use($conn, &$captured, $socket) {
             $captured = $transformed;
             $conn->end();
             $socket->shutdown();
         });
     });
     $loop->run();
     $this->assertSame('FOO', $captured);
 }
示例#15
0
 public function onDispatch2(MvcEvent $e)
 {
     $this->socket->on('connection', function (ConnectionInterface $conn) {
         $conn->on('data', function ($dataRaw) use($conn) {
             $dataRaw = trim($dataRaw);
             $data = Json::decode($dataRaw, Json::TYPE_ARRAY);
             if ($data['queueName'] == 'checkEngine') {
                 return;
             }
             if (!isset($this->config[$data['queueName']])) {
                 $this->debug("Bad queue name: " . $data['queueName'], ['debug-enable' => 1]);
                 return;
             }
             $queueName = $data['queueName'];
             $this->checkWorkersCount($queueName);
             $process = $this->handleData($data, $dataRaw, $this->getQueueConfig($queueName), $conn);
             if (!$process) {
                 return;
             }
             $this->processes[$queueName][] = $process;
         });
     });
     $this->loop->addPeriodicTimer(1, function ($timer) {
         $processes = [];
         foreach ($this->processes as $queueName => $queue) {
             /** @var Process $process */
             foreach ($queue as $process) {
                 if ($process->isRunning()) {
                     $processes[$queueName][] = $process;
                 } else {
                     $this->printProcessOutput($queueName, $process);
                 }
             }
         }
         $this->processes = $processes;
     });
     $this->socket->listen(4000);
     $this->loop->run();
 }
示例#16
0
 /**
  * Connects to ProcessManager, master process.
  */
 public function run()
 {
     $this->loop = \React\EventLoop\Factory::create();
     ErrorHandler::register(new ErrorHandler(new BufferingLogger()));
     $this->client = stream_socket_client('tcp://127.0.0.1:5500');
     $this->connection = new \React\Socket\Connection($this->client, $this->loop);
     $this->connection->on('error', function ($data) {
         var_dump($data);
     });
     $pcntl = new \MKraemer\ReactPCNTL\PCNTL($this->loop);
     $pcntl->on(SIGTERM, [$this, 'shutdown']);
     $pcntl->on(SIGINT, [$this, 'shutdown']);
     register_shutdown_function([$this, 'shutdown']);
     $this->bindProcessMessage($this->connection);
     $this->connection->on('close', \Closure::bind(function () {
         $this->shutdown();
     }, $this));
     $this->server = new \React\Socket\Server($this->loop);
     $this->server->on('error', function ($data) {
         var_dump($data);
     });
     $http = new HttpServer($this->server);
     $http->on('request', array($this, 'onRequest'));
     $http->on('error', function ($data) {
         var_dump($data);
     });
     $port = $this->config['port'];
     while (true) {
         try {
             $this->server->listen($port);
             break;
         } catch (\React\Socket\ConnectionException $e) {
             usleep(500);
         }
     }
     $this->sendMessage($this->connection, 'register', ['pid' => getmypid(), 'port' => $port]);
     $this->loop->run();
 }
示例#17
0
 /**
  *
  */
 public function run()
 {
     $this->reactLoop->run();
 }
示例#18
0
文件: dump.php 项目: clue/tar-react
use React\Stream\BufferedSink;
use Clue\Hexdump\Hexdump;
use React\EventLoop\StreamSelectLoop;
require __DIR__ . '/../vendor/autoload.php';
$in = isset($argv[1]) ? $argv[1] : __DIR__ . '/../tests/fixtures/alice-bob.tar';
echo 'Reading file "' . $in . '" (pass as argument to example)' . PHP_EOL;
// using the default loop does *not* work for file I/O
//$loop = Factory::create();
$loop = new StreamSelectLoop();
$stream = new Stream(fopen($in, 'r'), $loop);
$decoder = new Decoder();
$decoder->on('entry', function ($header, $file) {
    static $i = 0;
    echo 'FILE #' . ++$i . PHP_EOL;
    echo 'Received entry headers:' . PHP_EOL;
    var_dump($header);
    BufferedSink::createPromise($file)->then(function ($contents) {
        echo 'Received entry contents (' . strlen($contents) . ' bytes)' . PHP_EOL;
        $d = new Hexdump();
        echo $d->dump($contents) . PHP_EOL . PHP_EOL;
    });
});
$decoder->on('error', function ($error) {
    echo 'ERROR: ' . $error . PHP_EOL;
});
$decoder->on('close', function () {
    echo 'CLOSED' . PHP_EOL;
});
$stream->pipe($decoder);
$loop->run();
示例#19
0
 function loop()
 {
     $this->loop->run();
 }
示例#20
0
 /**
  * @return Client
  */
 public function run() : Client
 {
     $this->loop->run();
     return $this;
 }
示例#21
0
 public function testReturnsResponse()
 {
     $loop = new StreamSelectLoop();
     $request = new RequestFactory();
     $server = new Server($loop);
     $server->on('connection', function (SocketConnection $connection) use($server, $request) {
         $connection->on('data', function ($data) use($connection, $request) {
             $req = $request->response($data);
             $response = new Response($req->getId(), ['1.0']);
             $connection->write($response->write());
         });
         $connection->on('close', function () use($server) {
             $server->shutdown();
         });
     });
     $server->listen(54323, '127.0.0.1');
     $tcp = new TcpConnector($loop);
     $client = new Client($tcp, $request);
     $client->connect('127.0.0.1', 54323)->then(function (Connection $connection) use($loop) {
         $deferred = new Deferred();
         $deferred->promise()->then(function ($value) {
             $this->assertEquals(1, $value);
         });
         $electrum = new ElectrumClient($connection);
         $electrum->getServerVersion('1.9.6', ' 0.6')->then(function () use($deferred, $connection) {
             $deferred->resolve(1);
             $connection->close();
         }, function () use($loop) {
             $loop->stop();
             $this->fail();
         });
     });
     $loop->run();
 }
示例#22
0
 /**
  *
  */
 public function run()
 {
     $this->loop->run();
 }
示例#23
0
 public function start()
 {
     $this->loop->run();
 }