From 7593d67bf785e32718511a0c7b44e2b5bdbacf36 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 17 Apr 2013 17:49:16 +0200 Subject: [PATCH 1/4] Implemented evloop --- src/React/EventLoop/EvLoop.php | 153 +++++++++++++++++++++ tests/React/Tests/EventLoop/EvLoopTest.php | 22 +++ 2 files changed, 175 insertions(+) create mode 100644 src/React/EventLoop/EvLoop.php create mode 100644 tests/React/Tests/EventLoop/EvLoopTest.php diff --git a/src/React/EventLoop/EvLoop.php b/src/React/EventLoop/EvLoop.php new file mode 100644 index 00000000..19306257 --- /dev/null +++ b/src/React/EventLoop/EvLoop.php @@ -0,0 +1,153 @@ +loop = new \EvLoop(); + $this->timers = new SplObjectStorage(); + } + + public function addReadStream($stream, $listener) + { + $this->addStream($stream, $listener, \Ev::READ); + } + + public function addWriteStream($stream, $listener) + { + $this->addStream($stream, $listener, \Ev::WRITE); + } + + public function removeReadStream($stream) + { + $this->readEvents[(int)$stream]->stop(); + unset($this->readEvents[(int)$stream]); + } + + public function removeWriteStream($stream) + { + $this->writeEvents[(int)$stream]->stop(); + unset($this->writeEvents[(int)$stream]); + } + + public function removeStream($stream) + { + if (isset($this->readEvents[(int)$stream])) { + $this->removeReadStream($stream); + } + + if (isset($this->writeEvents[(int)$stream])) { + $this->removeWriteStream($stream); + } + } + + private function addStream($stream, $listener, $flags) + { + $listener = $this->wrapStreamListener($stream, $listener, $flags); + $event = $this->loop->io($stream, $flags, $listener); + + if (($flags & \Ev::READ) === $flags) { + $this->readEvents[(int)$stream] = $event; + } elseif (($flags & \Ev::WRITE) === $flags) { + $this->writeEvents[(int)$stream] = $event; + } + } + + private function wrapStreamListener($stream, $listener, $flags) + { + if (($flags & \Ev::READ) === $flags) { + $removeCallback = array($this, 'removeReadStream'); + } elseif (($flags & \Ev::WRITE) === $flags) { + $removeCallback = array($this, 'removeWriteStream'); + } + + return function ($event) use ($stream, $listener, $removeCallback) { + if (feof($stream)) { + call_user_func($removeCallback, $stream); + + return; + } + + call_user_func($listener, $stream); + }; + } + + public function addTimer($interval, $callback) + { + $timer = new Timer($this, $interval, $callback, false); + $this->setupTimer($timer); + + return $timer; + } + + public function addPeriodicTimer($interval, $callback) + { + $timer = new Timer($this, $interval, $callback, true); + $this->setupTimer($timer); + + return $timer; + } + + public function cancelTimer(TimerInterface $timer) + { + if (isset($this->timers[$timer])) { + $this->timers[$timer]->stop(); + $this->timers->detach($timer); + } + } + + private function setupTimer(TimerInterface $timer) + { + $dummyCallback = function () {}; + $interval = $timer->getInterval(); + + if ($timer->isPeriodic()) { + $libevTimer = $this->loop->timer($interval, $interval, $dummyCallback); + } else { + $libevTimer = $this->loop->timer($interval, $dummyCallback); + } + + $libevTimer->setCallback(function () use ($timer) { + call_user_func($timer->getCallback(), $timer); + + if (!$timer->isPeriodic()) { + $timer->cancel(); + } + }); + + $this->timers->attach($timer, $libevTimer); + + return $timer; + } + + public function isTimerActive(TimerInterface $timer) + { + return $this->timers->contains($timer); + } + + public function tick() + { + $this->loop->run(\Ev::RUN_ONCE); + } + + public function run() + { + $this->loop->run(); + } + + public function stop() + { + $this->loop->stop(); + } +} diff --git a/tests/React/Tests/EventLoop/EvLoopTest.php b/tests/React/Tests/EventLoop/EvLoopTest.php new file mode 100644 index 00000000..d7abb454 --- /dev/null +++ b/tests/React/Tests/EventLoop/EvLoopTest.php @@ -0,0 +1,22 @@ +markTestSkipped('ev tests skipped because pecl/ev is not installed.'); + } + + return new EvLoop(); + } + + public function testEvConstructor() + { + $loop = new EvLoop(); + } +} From 562dcd6d3e2af37a80c1653bfce793a3ba30d9e2 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 17 Apr 2013 17:52:46 +0200 Subject: [PATCH 2/4] Added ev to .travis.yml --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 6ef5fe32..474ffc0a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,11 @@ before_script: cd libevent-0.0.5 && phpize && ./configure && make && sudo make install; echo "extension=libevent.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`; fi" + - sh -c " if [ \"\$(php --re ev | grep 'does not exist')\" != '' ]; then + git clone https://bitbucket.org/osmanov/pecl-event.git + (cd pecl-event && phpize && ./configure --enable-ev && make && make install) + echo "extension=ev.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`; + fi" - composer self-update - composer install --dev --prefer-source From 4684e35c76e66434663257d3594979d2eedac6c9 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 17 Apr 2013 17:58:18 +0200 Subject: [PATCH 3/4] Fix .travis.yml --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 474ffc0a..4b63a92f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,8 +19,9 @@ before_script: echo "extension=libevent.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`; fi" - sh -c " if [ \"\$(php --re ev | grep 'does not exist')\" != '' ]; then - git clone https://bitbucket.org/osmanov/pecl-event.git - (cd pecl-event && phpize && ./configure --enable-ev && make && make install) + git clone https://bitbucket.org/osmanov/pecl-event.git; + cd pecl-ev; + phpize && ./configure --enable-ev && make && make install; echo "extension=ev.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`; fi" - composer self-update From 3d4c652f83a6f2f3f8a252d9b9dc58ce8fe70d28 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 17 Apr 2013 18:03:53 +0200 Subject: [PATCH 4/4] Fix typo --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 4b63a92f..67dc329b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,7 +19,7 @@ before_script: echo "extension=libevent.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`; fi" - sh -c " if [ \"\$(php --re ev | grep 'does not exist')\" != '' ]; then - git clone https://bitbucket.org/osmanov/pecl-event.git; + git clone https://bitbucket.org/osmanov/pecl-ev.git; cd pecl-ev; phpize && ./configure --enable-ev && make && make install; echo "extension=ev.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`; pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy