Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
use Amp\Loop\Internal\TimerQueue;
use Amp\Loop\Watcher;
require __DIR__ . '/vendor/autoload.php';
$input = \file_get_contents('/home/kelunik/Downloads/tq-A805-3.log');
$timerQueue = new TimerQueue;
$line = 0;
while ($input !== '') {
$type = $input[0];
$input = \substr($input, 1);
\assert(\is_string($input));
// print ++$line . "\n";
if ($type === 'i') {
$expiration = \unpack('Nexpiration', \substr($input, 0, 4))['expiration'];
$input = \substr($input, 4);
\assert(\is_string($input));
$watcherId = \substr($input, 0, $pos = \strpos($input, "\n"));
$input = \substr($input, $pos + 1);
\assert(\is_string($input));
$watcher = new Watcher;
$watcher->id = $watcherId;
$timerQueue->insert($watcher, $expiration);
} elseif ($type === 'e') {
$now = \unpack('Nnow', \substr($input, 0, 4))['now'];
$input = \substr($input, 5);
\assert(\is_string($input));
$timerQueue->extract($now);
} elseif ($type === 'r') {
$watcherId = \substr($input, 0, $pos = \strpos($input, "\n"));
$input = \substr($input, $pos + 1);
\assert(\is_string($input));
$watcher = new Watcher;
$watcher->id = $watcherId;
$timerQueue->remove($watcher);
} else {
throw new \Exception('Invalid type: ' . bin2hex($type) . ' ' . bin2hex($input));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment