2013-08-05 22:05:08 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Alert;
|
|
|
|
|
|
|
|
class NativeReactor implements Reactor {
|
|
|
|
|
|
|
|
private $alarms = [];
|
|
|
|
private $alarmOrder = [];
|
|
|
|
private $readStreams = [];
|
|
|
|
private $writeStreams = [];
|
|
|
|
private $readCallbacks = [];
|
|
|
|
private $writeCallbacks = [];
|
|
|
|
private $watcherIdReadStreamIdMap = [];
|
|
|
|
private $watcherIdWriteStreamIdMap = [];
|
2013-11-15 01:07:54 +01:00
|
|
|
private $disabledWatchers = array();
|
2014-03-07 18:02:03 +01:00
|
|
|
private $resolution = 1000;
|
2014-06-11 18:21:46 +02:00
|
|
|
private $lastWatcherId = 0;
|
2014-02-23 22:26:28 +01:00
|
|
|
private $isRunning = false;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
private static $DISABLED_ALARM = 0;
|
|
|
|
private static $DISABLED_READ = 1;
|
|
|
|
private static $DISABLED_WRITE = 2;
|
2014-03-07 18:02:03 +01:00
|
|
|
private static $MICROSECOND = 1000000;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2014-08-07 07:35:05 +02:00
|
|
|
public function run(callable $onStart = null) {
|
2014-01-20 14:23:28 +01:00
|
|
|
if ($this->isRunning) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-07 18:02:03 +01:00
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
$this->isRunning = true;
|
2014-01-20 14:23:28 +01:00
|
|
|
if ($onStart) {
|
2014-07-18 22:34:21 +02:00
|
|
|
$this->immediately(function() use ($onStart) { $onStart($this); });
|
2014-01-20 14:23:28 +01:00
|
|
|
}
|
|
|
|
$this->enableAlarms();
|
|
|
|
while ($this->isRunning) {
|
|
|
|
$this->tick();
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function enableAlarms() {
|
2014-02-23 22:26:28 +01:00
|
|
|
$now = microtime(true);
|
2013-08-05 22:05:08 +02:00
|
|
|
$enabled = 0;
|
|
|
|
foreach ($this->alarms as $watcherId => $alarmStruct) {
|
|
|
|
$nextExecution = $alarmStruct[1];
|
|
|
|
if (!$nextExecution) {
|
|
|
|
$enabled++;
|
|
|
|
$delay = $alarmStruct[2];
|
|
|
|
$nextExecution = $now + $delay;
|
|
|
|
$alarmStruct[1] = $nextExecution;
|
|
|
|
$this->alarms[$watcherId] = $alarmStruct;
|
|
|
|
$this->alarmOrder[$watcherId] = $nextExecution;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function stop() {
|
|
|
|
$this->isRunning = false;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function tick() {
|
2013-08-05 22:05:08 +02:00
|
|
|
if (!$this->isRunning) {
|
|
|
|
$this->enableAlarms();
|
|
|
|
}
|
|
|
|
|
2013-11-15 01:15:17 +01:00
|
|
|
$timeToNextAlarm = $this->alarmOrder
|
2014-02-23 22:26:28 +01:00
|
|
|
? round(min($this->alarmOrder) - microtime(true), 4)
|
2013-11-15 01:18:50 +01:00
|
|
|
: 1;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
if ($this->readStreams || $this->writeStreams) {
|
2013-11-15 01:18:50 +01:00
|
|
|
$this->selectActionableStreams($timeToNextAlarm);
|
2014-06-11 16:03:44 +02:00
|
|
|
} elseif (!$this->alarmOrder) {
|
|
|
|
$this->stop();
|
2013-08-05 22:05:08 +02:00
|
|
|
} elseif ($timeToNextAlarm > 0) {
|
2014-03-07 18:02:03 +01:00
|
|
|
usleep($timeToNextAlarm * self::$MICROSECOND);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->alarmOrder) {
|
|
|
|
$this->executeAlarms();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 01:18:50 +01:00
|
|
|
private function selectActionableStreams($timeout) {
|
2013-11-15 01:13:24 +01:00
|
|
|
$r = $this->readStreams;
|
|
|
|
$w = $this->writeStreams;
|
2014-08-07 07:35:05 +02:00
|
|
|
$e = null;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2013-11-15 01:18:50 +01:00
|
|
|
if ($timeout <= 0) {
|
|
|
|
$sec = 0;
|
|
|
|
$usec = 0;
|
|
|
|
} else {
|
|
|
|
$sec = floor($timeout);
|
2014-03-07 18:02:03 +01:00
|
|
|
$usec = ($timeout - $sec) * self::$MICROSECOND;
|
2013-11-15 01:18:50 +01:00
|
|
|
}
|
|
|
|
|
2014-04-14 23:03:38 +02:00
|
|
|
if (@stream_select($r, $w, $e, $sec, $usec)) {
|
2013-08-05 22:05:08 +02:00
|
|
|
foreach ($r as $readableStream) {
|
|
|
|
$streamId = (int) $readableStream;
|
|
|
|
foreach ($this->readCallbacks[$streamId] as $watcherId => $callback) {
|
2013-09-08 15:46:25 +02:00
|
|
|
$callback($watcherId, $readableStream, $this);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($w as $writableStream) {
|
|
|
|
$streamId = (int) $writableStream;
|
|
|
|
foreach ($this->writeCallbacks[$streamId] as $watcherId => $callback) {
|
2013-09-08 15:46:25 +02:00
|
|
|
$callback($watcherId, $writableStream, $this);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function executeAlarms() {
|
2014-02-23 22:26:28 +01:00
|
|
|
$now = microtime(true);
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
asort($this->alarmOrder);
|
|
|
|
|
|
|
|
foreach ($this->alarmOrder as $watcherId => $executionCutoff) {
|
|
|
|
if ($executionCutoff <= $now) {
|
2013-08-12 17:35:18 +02:00
|
|
|
$this->doAlarmCallback($watcherId);
|
2013-08-05 22:05:08 +02:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-12 17:35:18 +02:00
|
|
|
private function doAlarmCallback($watcherId) {
|
2013-08-05 22:05:08 +02:00
|
|
|
list($callback, $nextExecution, $interval, $isRepeating) = $this->alarms[$watcherId];
|
|
|
|
|
|
|
|
if ($isRepeating) {
|
2013-08-12 17:35:18 +02:00
|
|
|
$nextExecution += $interval;
|
2013-08-05 22:05:08 +02:00
|
|
|
$this->alarms[$watcherId] = [$callback, $nextExecution, $interval, $isRepeating];
|
|
|
|
$this->alarmOrder[$watcherId] = $nextExecution;
|
|
|
|
} else {
|
|
|
|
unset(
|
|
|
|
$this->alarms[$watcherId],
|
|
|
|
$this->alarmOrder[$watcherId]
|
|
|
|
);
|
|
|
|
}
|
2013-08-12 17:35:18 +02:00
|
|
|
|
2013-09-08 15:46:25 +02:00
|
|
|
$callback($watcherId, $this);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-08-07 03:29:08 +02:00
|
|
|
/**
|
|
|
|
* Schedule an event to trigger once at the specified time
|
|
|
|
*
|
|
|
|
* @param callable $callback Any valid PHP callable
|
|
|
|
* @param mixed[int|string] $unixTimeOrStr A future unix timestamp or string parsable by strtotime()
|
|
|
|
* @throws \InvalidArgumentException On invalid future time
|
|
|
|
* @return int Returns a unique integer watcher ID
|
|
|
|
*/
|
|
|
|
public function at(callable $callback, $unixTimeOrStr) {
|
2013-08-05 22:05:08 +02:00
|
|
|
$now = time();
|
2014-08-07 03:29:08 +02:00
|
|
|
if (is_int($unixTimeOrStr) && $unixTimeOrStr > $now) {
|
|
|
|
$secondsUntil = ($unixTimeOrStr - $now);
|
|
|
|
} elseif (($executeAt = @strtotime($unixTimeOrStr)) && $executeAt > $now) {
|
|
|
|
$secondsUntil = ($executeAt - $now);
|
|
|
|
} else {
|
2013-08-05 22:05:08 +02:00
|
|
|
throw new \InvalidArgumentException(
|
2014-08-07 03:29:08 +02:00
|
|
|
'Unix timestamp or future time string (parsable by strtotime()) required'
|
2013-08-05 22:05:08 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-08-07 03:29:08 +02:00
|
|
|
$msDelay = $secondsUntil * $this->resolution;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
2014-06-11 16:08:19 +02:00
|
|
|
return $this->once($callback, $msDelay);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function immediately(callable $callback) {
|
|
|
|
return $this->scheduleAlarm($callback, $delay = 0, $isRepeating = false);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function once(callable $callback, $delay) {
|
|
|
|
return $this->scheduleAlarm($callback, $delay, $isRepeating = false);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function repeat(callable $callback, $interval) {
|
|
|
|
return $this->scheduleAlarm($callback, $interval, $isRepeating = true);
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function scheduleAlarm($callback, $delay, $isRepeating) {
|
2014-06-11 18:21:46 +02:00
|
|
|
$watcherId = $this->lastWatcherId++;
|
2014-03-07 18:02:03 +01:00
|
|
|
$delay = round(($delay / $this->resolution), 3);
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
if ($this->isRunning) {
|
2014-02-23 22:26:28 +01:00
|
|
|
$nextExecution = (microtime(true) + $delay);
|
2013-08-05 22:05:08 +02:00
|
|
|
$this->alarmOrder[$watcherId] = $nextExecution;
|
|
|
|
} else {
|
2014-08-07 07:35:05 +02:00
|
|
|
$nextExecution = null;
|
2013-08-05 22:05:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$alarmStruct = [$callback, $nextExecution, $delay, $isRepeating];
|
|
|
|
$this->alarms[$watcherId] = $alarmStruct;
|
|
|
|
|
|
|
|
return $watcherId;
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function onReadable($stream, callable $callback, $enableNow = true) {
|
2014-06-11 18:21:46 +02:00
|
|
|
$watcherId = $this->lastWatcherId++;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
if ($enableNow) {
|
|
|
|
$streamId = (int) $stream;
|
|
|
|
$this->readStreams[$streamId] = $stream;
|
|
|
|
$this->readCallbacks[$streamId][$watcherId] = $callback;
|
|
|
|
$this->watcherIdReadStreamIdMap[$watcherId] = $streamId;
|
|
|
|
} else {
|
|
|
|
$this->disabledWatchers[$watcherId] = [self::$DISABLED_READ, [$stream, $callback]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $watcherId;
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function onWritable($stream, callable $callback, $enableNow = true) {
|
2014-06-11 18:21:46 +02:00
|
|
|
$watcherId = $this->lastWatcherId++;
|
2013-08-05 22:05:08 +02:00
|
|
|
|
|
|
|
if ($enableNow) {
|
|
|
|
$streamId = (int) $stream;
|
|
|
|
$this->writeStreams[$streamId] = $stream;
|
|
|
|
$this->writeCallbacks[$streamId][$watcherId] = $callback;
|
|
|
|
$this->watcherIdWriteStreamIdMap[$watcherId] = $streamId;
|
|
|
|
} else {
|
|
|
|
$this->disabledWatchers[$watcherId] = [self::$DISABLED_WRITE, [$stream, $callback]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $watcherId;
|
|
|
|
}
|
|
|
|
|
2014-08-06 21:52:43 +02:00
|
|
|
public function watchStream($stream, callable $callback, $flags) {
|
2014-06-11 18:21:46 +02:00
|
|
|
$flags = (int) $flags;
|
2014-08-06 05:45:33 +02:00
|
|
|
$enableNow = ($flags & self::WATCH_NOW);
|
2014-06-11 18:21:46 +02:00
|
|
|
|
2014-08-06 05:45:33 +02:00
|
|
|
if ($flags & self::WATCH_READ) {
|
2014-06-11 18:21:46 +02:00
|
|
|
return $this->onWritable($stream, $callback, $enableNow);
|
2014-08-06 05:45:33 +02:00
|
|
|
} elseif ($flags & self::WATCH_WRITE) {
|
2014-06-11 18:21:46 +02:00
|
|
|
return $this->onWritable($stream, $callback, $enableNow);
|
|
|
|
} else {
|
|
|
|
throw new \DomainException(
|
2014-08-06 05:45:33 +02:00
|
|
|
'Stream watchers must specify either a WATCH_READ or WATCH_WRITE flag'
|
2014-06-11 18:21:46 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function cancel($watcherId) {
|
2013-08-05 22:05:08 +02:00
|
|
|
if (isset($this->alarms[$watcherId])) {
|
|
|
|
unset(
|
|
|
|
$this->alarms[$watcherId],
|
|
|
|
$this->alarmOrder[$watcherId]
|
|
|
|
);
|
|
|
|
} elseif (isset($this->watcherIdReadStreamIdMap[$watcherId])) {
|
|
|
|
$this->cancelReadWatcher($watcherId);
|
|
|
|
} elseif (isset($this->watcherIdWriteStreamIdMap[$watcherId])) {
|
|
|
|
$this->cancelWriteWatcher($watcherId);
|
|
|
|
} elseif (isset($this->disabledWatchers[$watcherId])) {
|
|
|
|
unset($this->disabledWatchers[$watcherId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function cancelReadWatcher($watcherId) {
|
|
|
|
$streamId = $this->watcherIdReadStreamIdMap[$watcherId];
|
|
|
|
|
|
|
|
unset(
|
|
|
|
$this->readCallbacks[$streamId][$watcherId],
|
|
|
|
$this->watcherIdReadStreamIdMap[$watcherId],
|
|
|
|
$this->disabledWatchers[$watcherId]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (empty($this->readCallbacks[$streamId])) {
|
|
|
|
unset($this->readStreams[$streamId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function cancelWriteWatcher($watcherId) {
|
|
|
|
$streamId = $this->watcherIdWriteStreamIdMap[$watcherId];
|
|
|
|
|
|
|
|
unset(
|
|
|
|
$this->writeCallbacks[$streamId][$watcherId],
|
|
|
|
$this->watcherIdWriteStreamIdMap[$watcherId],
|
|
|
|
$this->disabledWatchers[$watcherId]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (empty($this->writeCallbacks[$streamId])) {
|
|
|
|
unset($this->writeStreams[$streamId]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function enable($watcherId) {
|
2013-08-05 22:05:08 +02:00
|
|
|
if (!isset($this->disabledWatchers[$watcherId])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($type, $watcherStruct) = $this->disabledWatchers[$watcherId];
|
|
|
|
|
|
|
|
unset($this->disabledWatchers[$watcherId]);
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
case self::$DISABLED_ALARM:
|
|
|
|
if (!$nextExecution = $watcherStruct[1]) {
|
2014-02-23 22:26:28 +01:00
|
|
|
$nextExecution = microtime(true) + $watcherStruct[2];
|
2013-08-05 22:05:08 +02:00
|
|
|
$watcherStruct[1] = $nextExecution;
|
|
|
|
}
|
|
|
|
$this->alarms[$watcherId] = $watcherStruct;
|
|
|
|
$this->alarmOrder[$watcherId] = $nextExecution;
|
|
|
|
break;
|
|
|
|
case self::$DISABLED_READ:
|
|
|
|
list($stream, $callback) = $watcherStruct;
|
|
|
|
$streamId = (int) $stream;
|
|
|
|
$this->readCallbacks[$streamId][$watcherId] = $callback;
|
|
|
|
$this->watcherIdReadStreamIdMap[$watcherId] = $streamId;
|
|
|
|
$this->readStreams[$streamId] = $stream;
|
|
|
|
break;
|
|
|
|
case self::$DISABLED_WRITE:
|
|
|
|
list($stream, $callback) = $watcherStruct;
|
|
|
|
$streamId = (int) $stream;
|
|
|
|
$this->writeCallbacks[$streamId][$watcherId] = $callback;
|
|
|
|
$this->watcherIdWriteStreamIdMap[$watcherId] = $streamId;
|
|
|
|
$this->writeStreams[$streamId] = $stream;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-23 22:26:28 +01:00
|
|
|
public function disable($watcherId) {
|
2013-08-05 22:05:08 +02:00
|
|
|
if (isset($this->alarms[$watcherId])) {
|
|
|
|
$alarmStruct = $this->alarms[$watcherId];
|
|
|
|
$this->disabledWatchers[$watcherId] = [self::$DISABLED_ALARM, $alarmStruct];
|
|
|
|
unset(
|
|
|
|
$this->alarms[$watcherId],
|
|
|
|
$this->alarmOrder[$watcherId]
|
|
|
|
);
|
|
|
|
} elseif (isset($this->watcherIdReadStreamIdMap[$watcherId])) {
|
|
|
|
$streamId = $this->watcherIdReadStreamIdMap[$watcherId];
|
|
|
|
$stream = $this->readStreams[$streamId];
|
|
|
|
$callback = $this->readCallbacks[$streamId][$watcherId];
|
|
|
|
|
|
|
|
unset(
|
|
|
|
$this->readCallbacks[$streamId][$watcherId],
|
|
|
|
$this->watcherIdReadStreamIdMap[$watcherId]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (empty($this->readCallbacks[$streamId])) {
|
|
|
|
unset($this->readStreams[$streamId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->disabledWatchers[$watcherId] = [self::$DISABLED_READ, [$stream, $callback]];
|
|
|
|
|
|
|
|
} elseif (isset($this->watcherIdWriteStreamIdMap[$watcherId])) {
|
|
|
|
$streamId = $this->watcherIdWriteStreamIdMap[$watcherId];
|
|
|
|
$stream = $this->writeStreams[$streamId];
|
|
|
|
$callback = $this->writeCallbacks[$streamId][$watcherId];
|
|
|
|
|
|
|
|
unset(
|
|
|
|
$this->writeCallbacks[$streamId][$watcherId],
|
|
|
|
$this->watcherIdWriteStreamIdMap[$watcherId]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (empty($this->writeCallbacks[$streamId])) {
|
|
|
|
unset($this->writeStreams[$streamId]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->disabledWatchers[$watcherId] = [self::$DISABLED_WRITE, [$stream, $callback]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|