Exemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 protected function dispatch(int $level)
 {
     if ($this->deferred || $this->enable || $this->running <= $level) {
         $this->loop->loop(\EventBase::LOOP_NONBLOCK);
     } else {
         $this->loop->loop(\EventBase::LOOP_ONCE);
     }
 }
Exemplo n.º 2
0
 /**
  * Handler of the SIGTERM (graceful shutdown) signal in worker process.
  * @return void
  */
 protected function sigterm()
 {
     if (Daemon::$config->logsignals->value) {
         $this->log('caught SIGTERM.');
     }
     $this->graceful = false;
     $this->breakMainLoop = true;
     $this->eventBase->exit();
 }
Exemplo n.º 3
0
 /**
  * Service
  *
  * @return void
  */
 public function service()
 {
     if (!$this->socket->isConnected()) {
         return false;
     }
     $base = new \EventBase();
     $listener = new \EventListener($base, function ($listener, $fd, $address, $base) {
         $event = new \EventBufferEvent($base, $fd, \EventBufferEvent::OPT_CLOSE_ON_FREE);
         $event->setCallbacks(function ($event) {
             $this->protocol->handleData(new EventSocket($event));
         }, function ($event) {
             if (0 === $event->output->length) {
                 $event->free();
             }
         }, function ($event, $events) {
             if ($events & (\EventBufferEvent::EOF | \EventBufferEvent::ERROR)) {
                 $event->free();
             }
         }, null);
         $event->enable(\Event::READ);
     }, $base, \EventListener::OPT_CLOSE_ON_FREE | \EventListener::OPT_REUSEABLE, -1, $this->socket->getHandle());
     $base->dispatch();
 }
Exemplo n.º 4
0
 /**
  * Runtime of Worker process.
  * @return void
  */
 protected function run()
 {
     if (Daemon::$process instanceof Master) {
         Daemon::$process->unregisterSignals();
     }
     if (Daemon::$process->eventBase) {
         Daemon::$process->eventBase->reinit();
         $this->eventBase = Daemon::$process->eventBase;
     } else {
         $this->eventBase = new \EventBase();
     }
     Daemon::$process = $this;
     if (Daemon::$logpointerAsync) {
         Daemon::$logpointerAsync->fd = null;
         Daemon::$logpointerAsync = null;
     }
     class_exists('Timer');
     if (Daemon::$config->autogc->value > 0) {
         gc_enable();
     } else {
         gc_disable();
     }
     $this->prepareSystemEnv();
     $this->registerEventSignals();
     FileSystem::init();
     // re-init
     FileSystem::initEvent();
     Daemon::openLogs();
     $this->fileWatcher = new FileWatcher();
     $this->IPCManager = Daemon::$appResolver->getInstanceByAppName('\\PHPDaemon\\IPCManager\\IPCManager');
     if (!$this->IPCManager) {
         $this->log('cannot instantiate IPCManager');
     }
     while (!$this->breakMainLoop) {
         if (!$this->eventBase->dispatch()) {
             break;
         }
     }
 }
Exemplo n.º 5
0
 /**
  * Spawn IPC process
  * @param $n - integer - number of workers to spawn
  * @return boolean - success
  */
 protected function spawnIPCThread()
 {
     if (FileSystem::$supported) {
         eio_event_loop();
     }
     $thread = new IPC();
     $this->ipcthreads->push($thread);
     $this->callbacks->push(function ($self) use($thread) {
         $thread->start();
         $pid = $thread->getPid();
         if ($pid < 0) {
             Daemon::$process->log('could not fork IPCThread');
         } elseif ($pid === 0) {
             // worker
             $this->log('Unexcepted execution return to outside of Thread->start()');
             exit;
         }
     });
     if ($this->eventBase) {
         $this->eventBase->stop();
     }
     return true;
 }
Exemplo n.º 6
0
}
// Event callback
function eventcb($bev, $events, $base)
{
    if ($events & EventBufferEvent::CONNECTED) {
        echo "Connected.\n";
    } elseif ($events & (EventBufferEvent::ERROR | EventBufferEvent::EOF)) {
        if ($events & EventBufferEvent::ERROR) {
            echo "DNS error: ", $bev->getDnsErrorString(), PHP_EOL;
        }
        echo "Closing\n";
        $base->exit();
        exit("Done\n");
    }
}
if ($argc != 3) {
    echo <<<EOS
Trivial HTTP 0.x client
Syntax: php {$argv[0]} [hostname] [resource]
Example: php {$argv[0]} www.google.com /

EOS;
    exit;
}
$base = new EventBase();
$dns_base = new EventDnsBase($base, TRUE);
$bev = new EventBufferEvent($base, null, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, "readcb", null, "eventcb", $base);
$bev->enable(Event::READ | Event::WRITE);
$bev->getOutput()->add("GET {$argv[2]} HTTP/1.0\r\n" . "Host: {$argv[1]}\r\n" . "Connection: Close\r\n\r\n");
$bev->connectHost($dns_base, $argv[1], 80, EventUtil::AF_UNSPEC);
$base->dispatch();
Exemplo n.º 7
0
In another terminal window find out the pid and send SIGTERM, e.g.:

$ ps aux | grep examp
ruslan    3976  0.2  0.0 139896 11256 pts/1    S+   10:25   0:00 php examples/signal.php
ruslan    3978  0.0  0.0   9572   864 pts/2    S+   10:26   0:00 grep --color=auto examp
$ kill -TERM 3976

At the first terminal window you should catch the following:

Caught signal 15
*/
class MyEventSignal
{
    private $base, $ev;
    public function __construct($base)
    {
        $this->base = $base;
        $this->ev = Event::signal($base, SIGTERM, array($this, 'eventSighandler'), 'libo');
        $this->ev->add();
    }
    public function eventSighandler($no, $c)
    {
        echo "Caught signal {$no}\n";
        //print_r($c);
        $this->base->exit();
    }
}
$base = new EventBase();
$c = new MyEventSignal($base);
$base->loop();
 /**
  * 
  * @param string $name
  * @param array $nameValueMap
  */
 public function __construct($name = null, array $nameValueMap = [])
 {
     parent::__construct($name, $nameValueMap);
 }
Exemplo n.º 9
0
 public function reset()
 {
     $this->base->reInit();
     $this->binds = new Container();
     $this->listeners = new Container();
 }
Exemplo n.º 10
0
 /**
  * Calls reInit() on the EventBase object.
  */
 public function reInit()
 {
     $this->base->reInit();
 }
Exemplo n.º 11
0
 public function __construct()
 {
     parent::__construct();
     $this->View = new View();
 }
Exemplo n.º 12
0
<?php

error_reporting(E_ALL);
$read_timeout = 3;
$write_timeout = 3;
$delay = 5;
$timer = null;
$base = new EventBase();
connect_dest();
while (1) {
    $base->exit(2);
    $base->dispatch();
}
function connect_dest()
{
    global $read_timeout, $write_timeout;
    echo "connect dest...\n";
    global $base;
    $buffer = new EventBufferEvent($base, NULL, EventBufferEvent::OPT_CLOSE_ON_FREE | EventBufferEvent::OPT_DEFER_CALLBACKS, 'readcb', null, 'eventcb', $base);
    $buffer->enable(Event::READ);
    $buffer->setTimeouts($read_timeout, $write_timeout);
    $buffer->connect('127.0.0.1:8000');
    $buffer->write('client data, ' . time());
}
function readcb($buffer, $base)
{
    global $delay;
    echo "read running...\n";
    echo $buffer->read(1024), "\n";
    $buffer->enable(Event::WRITE);
    $buffer->setCallbacks('readcb', 'writecb', 'eventcb');
Exemplo n.º 13
0
<?php

function print_line($fd, $what, $base)
{
    echo fgets($fd);
}
$base = new EventBase();
$event = new Event($base, STDIN, Event::READ | Event::PERSIST, 'print_line', $base);
$event->add();
while (1) {
    $base->exit(6);
    echo '===========debug===========' . PHP_EOL;
    $base->loop();
}
Exemplo n.º 14
0
 public function __construct()
 {
     parent::__construct();
 }
Exemplo n.º 15
0
 /**
  * Constructs a EntitySelectionEvent object.
  *
  * @param string $entity_browser_id
  *   Entity browser ID.
  * @param string $instance_uuid
  *   Entity browser instance UUID.
  * @param \Drupal\Core\Entity\EntityInterface[] $entities
  *   Array of selected entities.
  */
 public function __construct($entity_browser_id, $instance_uuid, array $entities)
 {
     parent::__construct($entity_browser_id, $instance_uuid);
     $this->entities = $entities;
 }
Exemplo n.º 16
0
 public function __construct()
 {
     $this->counterRedisKey = "tps20150315:" . date("Ymd");
     $this->getEventDatefromConf();
     parent::__construct();
 }