1
0
mirror of https://github.com/PurpleI2P/regi2p.git synced 2025-01-15 09:49:56 +00:00
regi2p/lib/checker.php

52 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace App;
use App\BOB;
use Amp\Parallel\Worker\Environment;
use Amp\Parallel\Worker\Task;
use Amp\Parallel\Worker\TaskFailureError;
use Amp\Parallel\Worker\TaskFailureException;
use Amp\Parallel\Worker\TaskFailureThrowable;
class Checker implements Task {
private $options = [];
private $base32;
public function __construct($base32, $options = []) {
$this->options = array_merge($this->options, (array) $options);
$this->base32 = $base32;
}
/**
* @param Environment $environment
* @return \Amp\Promise|\Generator|mixed
*/
public function run(Environment $environment) {
$bob = new BOB($this->options);
$bob->getnick();
$ts = 0;
for ($i = 0, $tries = $this->options['check_tries'], $sleep = $this->options['retry_delay']; $i < $tries; ++$i) {
$result = $bob->lookuplocal($this->base32 . ".b32.i2p");
if(!$result) {
$result = $bob->lookup($this->base32 . ".b32.i2p");
}
if ($result) {
$ts = time();
break;
}
// sleep before next try if we must do more that 1 tries to lookup destination
if($tries > 1 && $i < $tries) // do not sleep if that is last try
sleep($sleep);
}
$bob = null;
echo "Processed " . $this->base32 . ": " . ($ts ? "online" : "offline") . PHP_EOL;
return $ts;
}
}