Skip to content

Instantly share code, notes, and snippets.

@kelunik
Created January 22, 2020 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kelunik/a3646bea6c7bee4e2de83d7cd7ea0147 to your computer and use it in GitHub Desktop.
Save kelunik/a3646bea6c7bee4e2de83d7cd7ea0147 to your computer and use it in GitHub Desktop.
<?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