Example #1
0
 public function run()
 {
     $this->deferred = new Deferred();
     $this->deferred->promise()->progress(function ($event) {
         $this->data[$event['part']] = $event['data'];
         unset($this->unsettled[$event['part']]);
         if ($this->isEverythingHasBeenReceived()) {
             $this->deferred->resolve();
         }
     })->then(function () {
         if (isset($this->cancel_timer)) {
             $this->loop->cancelTimer($this->cancel_timer);
             unset($this->cancel_timer);
         }
     })->done(function () {
         $response = call_user_func($this->then_callback, $this->data);
         $headers = ['Content-Type' => 'text/plain'];
         $this->response->writeHead(200, $headers);
         $this->response->end($response);
     }, function () {
         $headers = ['Content-Type' => 'text/plain'];
         $this->response->writeHead(404, $headers);
         $this->response->end("Failed");
     });
     if (empty($this->requests)) {
         $this->deferred->resolve();
     } else {
         $this->registerCancelTimer();
         foreach ($this->requests as $request) {
             $request->end();
         }
     }
     return $this;
 }
Example #2
0
 public function close()
 {
     if ($this->closed) {
         return;
     }
     $this->loop->cancelTimer($this->timer);
     unset($this->queue);
     $this->closed = true;
 }
 /**
  * @param string $name
  */
 public function cancelPeriodicTimer($tidOrname)
 {
     if (!isset($this->registry[$tidOrname])) {
         $tid = $this->getTid($tidOrname);
     } else {
         $tid = $tidOrname;
     }
     $timer = $this->registry[$tid];
     $this->loop->cancelTimer($timer);
     unset($this->registry[$tid]);
 }
Example #4
0
 /**
  * Remove event listener from event loop.
  *
  * @param mixed $fd
  * @param int   $flag
  * @return bool
  */
 public function del($fd, $flag)
 {
     switch ($flag) {
         case EventInterface::EV_READ:
             return $this->_loop->removeReadStream($fd);
         case EventInterface::EV_WRITE:
             return $this->_loop->removeWriteStream($fd);
         case EventInterface::EV_SIGNAL:
             return $this->_loop->removeSignal($fd);
         case EventInterface::EV_TIMER:
         case EventInterface::EV_TIMER_ONCE:
             return $this->_loop->cancelTimer($fd);
     }
     return false;
 }
Example #5
0
 public function __construct(TransportInterface $carrierProtocol, LoopInterface $loop, LoggerInterface $logger)
 {
     $that = $this;
     $this->logger = $logger;
     $this->loop = $loop;
     $this->carrierProtocol = $carrierProtocol;
     $this->actionEmitter = new EventEmitter();
     $deferreds =& $this->deferred;
     $timers =& $this->timers;
     $carrierProtocol->on("message", function (MessageInterface $message) use(&$deferreds, &$timers, &$loop, $that, $logger) {
         $string = $message->getData();
         try {
             $jsonMessage = RemoteEventMessage::fromJson($string);
             $tag = $jsonMessage->getTag();
             if (array_key_exists($tag, $deferreds)) {
                 $deferred = $deferreds[$tag];
                 unset($deferreds[$tag]);
                 if (array_key_exists($tag, $timers)) {
                     $loop->cancelTimer($timers[$tag]);
                     unset($timers[$tag]);
                 }
                 $deferred->resolve($jsonMessage);
             } else {
                 $that->remoteEvent()->emit($jsonMessage->getEvent(), array($jsonMessage));
             }
             $that->emit("message", array($jsonMessage));
         } catch (\Exception $e) {
             $logger->err("Exception while parsing JsonMessage: " . $e->getMessage());
         }
     });
 }
 /**
  * @param string $captchaId
  * @return Promise\Promise
  */
 public function wait($captchaId)
 {
     $deferred = new Promise\Deferred();
     $this->loop->addPeriodicTimer($this->requestInterval, function ($timer) use($deferred, $captchaId) {
         $request = $this->client->request($this->getResultMethod(), $this->getResultPath(), ['Content-Length' => strlen($this->getResultQuery($captchaId)), 'Content-Type' => 'application/x-www-form-urlencoded']);
         $request->on('response', function ($response) use($request, $deferred, $timer) {
             /** @var \React\HttpClient\Response $response */
             $response->on('data', function ($data, $response) use($request, $deferred, $timer) {
                 /** @var \GuzzleHttp\Psr7\Stream $data */
                 /** @var \React\HttpClient\Response $response */
                 $response->handleEnd();
                 $answer = $data->getContents();
                 // stream read
                 if (!$answer) {
                     $this->loop->cancelTimer($timer);
                     $deferred->reject(new ServiceException('ERROR_CONNECTION'));
                 } elseif (substr($answer, 0, 2) === 'OK') {
                     $this->loop->cancelTimer($timer);
                     $deferred->resolve(substr($answer, 3));
                 } elseif ($answer !== 'CAPCHA_NOT_READY') {
                     $this->loop->cancelTimer($timer);
                     $deferred->reject(new ServiceException($answer));
                 }
             });
         });
         $request->end($this->getResultQuery($captchaId));
     });
     return $deferred->promise();
 }
 /**
  * @param TopicInterface $topic
  * @param string         $name
  */
 public function cancelPeriodicTimer(TopicInterface $topic, $name)
 {
     $namespace = spl_object_hash($topic);
     if (isset($this->registry[$namespace][$name])) {
         return;
     }
     $timer = $this->registry[$namespace][$name];
     $this->loop->cancelTimer($timer);
     unset($this->registry[$namespace][$name]);
 }
Example #8
0
 /**
  * Postpone the maintenance run
  */
 public function postponeMaintenance()
 {
     if ($this->maintenanceTimer) {
         $this->eventLoop->cancelTimer($this->maintenanceTimer);
     }
     $this->maintenanceTimer = $this->eventLoop->addTimer($this->maintenanceInterval, function ($timer) {
         $this->runMaintenance();
         $this->postponeMaintenance();
     });
 }
Example #9
0
 /**
  * Method to call when stopping listening to messages.
  */
 public function close()
 {
     if ($this->closed) {
         return;
     }
     $this->emit('end', [$this]);
     $this->loop->cancelTimer($this->timer);
     $this->removeAllListeners();
     unset($this->exchange);
     $this->closed = true;
 }
Example #10
0
 /**
  * Cancel a running timer
  *
  * @param \Phergie\Irc\Plugin\React\Command\CommandEvent $event
  * @param \Phergie\Irc\Bot\React\EventQueueInterface $queue
  */
 public function cancelReminder(Event $event, Queue $queue)
 {
     extract($this->parseReminder($event));
     if (isset($this->activeTimers[$nick][$name])) {
         $this->loop->cancelTimer($this->activeTimers[$nick][$name]);
         unset($this->activeTimers[$nick][$name]);
         $queue->{$command}($source, "{$nick}: The {$name} reminder has been cancelled.");
     } else {
         $queue->{$command}($source, "{$nick}: That reminder isn't running.");
     }
 }
Example #11
0
function resolve($time, LoopInterface $loop)
{
    return new Promise(function ($resolve) use($loop, $time, &$timer) {
        // resolve the promise when the timer fires in $time seconds
        $timer = $loop->addTimer($time, function () use($time, $resolve) {
            $resolve($time);
        });
    }, function ($resolveUnused, $reject) use(&$timer, $loop) {
        // cancelling this promise will cancel the timer and reject
        $loop->cancelTimer($timer);
        $reject(new \RuntimeException('Timer cancelled'));
    });
}
 /**
  *
  */
 protected function onEnd()
 {
     if ($this->requestTimer !== null && $this->loop->isTimerActive($this->requestTimer)) {
         $this->loop->cancelTimer($this->requestTimer);
     }
     if ($this->connectionTimer !== null && $this->loop->isTimerActive($this->connectionTimer)) {
         $this->loop->cancelTimer($this->connectionTimer);
     }
     $this->loop->futureTick(function () {
         if ($this->httpResponse === null) {
             $this->deferred->reject($this->error);
         }
     });
 }
 /**
  * Handles closing of the stream.
  */
 private function handleClose()
 {
     foreach ($this->timer as $timer) {
         $this->loop->cancelTimer($timer);
     }
     $connection = $this->connection;
     $this->isConnecting = false;
     $this->isDisconnecting = false;
     $this->isConnected = false;
     $this->connection = null;
     $this->stream = null;
     if ($connection !== null) {
         $this->emit('close', [$connection, $this]);
     }
 }
 /**
  * @param Server        $server
  * @param LoopInterface $loop
  */
 protected function closure(Server $server, LoopInterface $loop)
 {
     $this->logger->notice('Stopping server ...');
     foreach ($this->serverPushHandlerRegistry->getPushers() as $handler) {
         $handler->close();
         $this->logger->info(sprintf('Stop %s push handler', $handler->getName()));
     }
     $server->emit('end');
     $server->shutdown();
     foreach ($this->periodicRegistry->getPeriodics() as $periodic) {
         if ($periodic instanceof TimerInterface && $loop->isTimerActive($periodic)) {
             $loop->cancelTimer($periodic);
         }
     }
     $loop->stop();
     $this->logger->notice('Server stopped !');
 }
Example #15
0
 /**
  * Cancel an existing timer/stream watcher
  *
  * @param int $watcherId
  */
 public function cancel($watcherId)
 {
     if (isset($this->watchers[$watcherId])) {
         list($type, $data) = $this->watchers[$watcherId];
         switch ($type) {
             case self::WATCHER_TYPE_READ:
                 $this->reactor->removeReadStream($data);
                 break;
             case self::WATCHER_TYPE_WRITE:
                 $this->reactor->removeWriteStream($data);
                 break;
             case self::WATCHER_TYPE_TIMER:
                 $this->reactor->cancelTimer($data);
                 break;
         }
     }
     unset($this->watchers[$watcherId], $this->disabledWatchers[$watcherId]);
 }
 /**
  * Stop timer when the last client unsubscribes
  *
  * @param CakeEvent $event
  */
 public function onUnSubscribeStaleTopic(CakeEvent $event)
 {
     if ($this->__timer !== null) {
         $this->__loop->cancelTimer($this->__timer);
     }
 }
Example #17
0
 /**
  * Cancels previously set timer
  * @param  TimerInterface $timer Timer object to cancel
  */
 public function cancel($timer)
 {
     $this->loop->cancelTimer($timer);
 }
Example #18
0
 /**
  * @param $params
  * @param Response $response
  */
 protected function stop($params, Response $response)
 {
     $this->loop->cancelTimer($this->crontab_timer);
     $this->response($response, 1);
 }
 /**
  * Cancel a pending timer.
  *
  * @param TimerInterface $timer The timer to cancel.
  */
 public function cancelTimer(TimerInterface $timer)
 {
     $this->emit('cancelTimer', [$timer]);
     return $this->loop->cancelTimer($timer);
 }