-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,669 additions
and
30 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
modules/wizard-httpclient/src-php/framework/httpclient/HttpAsyncResponse.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
175
modules/wizard-httpclient/src-php/framework/httpclient/HttpChecker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.