Skip to content

Commit

Permalink
Upd.
Browse files Browse the repository at this point in the history
  • Loading branch information
dim-s committed Mar 12, 2018
1 parent c6c00e5 commit b39e1e9
Show file tree
Hide file tree
Showing 7 changed files with 1,669 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace framework\httpclient;

/**
* Class HttpAsyncResponse
* @package bundle\http
* @packages httpclient
*/
class HttpAsyncResponse
{
protected $onSuccess;
protected $onError;
protected $onDone;

/**
* @param callable $callback
* @return HttpAsyncResponse
*/
function whenDone(callable $callback)
{
$this->onDone = $callback;
return $this;
}

/**
* @param callable $callback
* @return HttpAsyncResponse
*/
function whenSuccess(callable $callback)
{
$this->onSuccess = $callback;
return $this;
}

/**
* @param callable $callback
* @return HttpAsyncResponse
*/
function whenError(callable $callback)
{
$this->onError = $callback;
return $this;
}

/**
* @param HttpResponse $response
*/
public function trigger(HttpResponse $response)
{
if ($this->onSuccess && $response->isSuccess()) call_user_func($this->onSuccess, $response);
if ($this->onError && $response->isFail()) call_user_func($this->onError, $response);

if ($this->onDone) call_user_func($this->onDone, $response);
}
}
175 changes: 175 additions & 0 deletions modules/wizard-httpclient/src-php/framework/httpclient/HttpChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php
namespace framework\httpclient;

use framework\core\Component;
use framework\core\Event;
use framework\core\Logger;
use php\lang\Invoker;
use php\time\Timer;
use timer\AccurateTimer;

/**
* Class HttpChecker
* @package bundle\http
* @packages httpclient
*
* @property int $checkInterval
*/
class HttpChecker extends Component
{
/**
* @var string
*/
public $url;

/**
* @var bool
*/
public $autoStart = true;

/**
* @var int
*/
protected $_checkInterval = 10000;

/**
* @var HttpClient
*/
protected $client;

/**
* @var Timer
*/
protected $checkTimer;

/**
* @var boolean
*/
protected $available = null;

/**
* ...
*/
function __construct()
{
parent::__construct();

$this->client = new HttpClient();
$this->client->userAgent = 'Http Checker 1.0';
$this->client->readTimeout = 15000;
}

/**
* @event addTo
* @param Event $e
*/
private function handleAddTo(Event $e)
{
if ($this->autoStart) {
$this->start();
}
}

protected function doInterval()
{
$active = (bool) $this->checkTimer;
$this->stop();

Logger::debug("http checker ping '{0}' ...", $this->url);

$this->client->getAsync($this->url, [], function (HttpResponse $response) use ($active) {
if ($response->statusCode() == 200) {
if ($this->available !== true) {
$this->trigger(new Event('online', $this));
}

$this->available = true;
} else {
if ($this->available !== false) {
$this->trigger(new Event('offline', $this));
}

$this->available = false;
}

if ($active) {
$this->start();
}
});
}

/**
* Check status of url.
*/
public function ping()
{
$this->doInterval();
}

/**
* Start checker worker.
*/
public function start()
{
$this->checkTimer = Timer::every($this->_checkInterval, Invoker::of([$this, 'doInterval']));
$this->ping();
}

/**
* Stop checker worker.
*/
public function stop()
{
if ($this->checkTimer) {
$this->checkTimer->cancel();
$this->checkTimer = null;
}
}

/**
* @return HttpClient
*/
public function client()
{
return $this->client;
}

/**
* @return int
*/
protected function getCheckInterval()
{
return $this->_checkInterval;
}

/**
* @param int $checkInterval
*/
protected function setCheckInterval($checkInterval)
{
$this->_checkInterval = $checkInterval;
$this->stop();
$this->start();
}

/**
* @return bool
*/
public function isOffline()
{
return $this->available === false;
}

/**
* @return bool
*/
public function isOnline()
{
return $this->available === true;
}

public function isUnknown()
{
return $this->available === null;
}
}
Loading

0 comments on commit b39e1e9

Please sign in to comment.