2014-02-07 00:19:53 +01:00
|
|
|
<?php
|
2014-04-12 12:14:37 +02:00
|
|
|
|
2014-02-07 00:26:32 +01:00
|
|
|
namespace ManiaControl\Files;
|
2014-02-07 00:19:53 +01:00
|
|
|
|
2014-02-18 18:13:59 +01:00
|
|
|
use cURL\Request;
|
2017-05-21 17:52:56 +02:00
|
|
|
use cURL\RequestsQueue;
|
2017-03-26 19:44:55 +02:00
|
|
|
use ManiaControl\General\UsageInformationAble;
|
|
|
|
use ManiaControl\General\UsageInformationTrait;
|
2014-02-07 00:19:53 +01:00
|
|
|
use ManiaControl\ManiaControl;
|
|
|
|
|
2014-02-07 12:30:53 +01:00
|
|
|
/**
|
|
|
|
* Asynchronous File Reader
|
|
|
|
*
|
2014-05-02 17:40:47 +02:00
|
|
|
* @author ManiaControl Team <mail@maniacontrol.com>
|
2017-02-04 11:49:23 +01:00
|
|
|
* @copyright 2014-2017 ManiaControl Team
|
2014-05-02 17:40:47 +02:00
|
|
|
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
2014-02-07 12:30:53 +01:00
|
|
|
*/
|
2017-03-26 19:44:55 +02:00
|
|
|
class AsynchronousFileReader implements UsageInformationAble {
|
|
|
|
use UsageInformationTrait;
|
|
|
|
|
2014-06-14 15:48:27 +02:00
|
|
|
/*
|
|
|
|
* Constants
|
|
|
|
*/
|
|
|
|
const CONTENT_TYPE_JSON = 'application/json';
|
|
|
|
|
2017-05-22 22:32:15 +02:00
|
|
|
const QUEUE_NONSERIALIZING = 0;
|
|
|
|
const QUEUE_SERIALIZING = 1;
|
|
|
|
|
2014-04-12 12:14:37 +02:00
|
|
|
/*
|
2014-08-02 22:31:46 +02:00
|
|
|
* Private properties
|
2014-02-07 00:19:53 +01:00
|
|
|
*/
|
2014-08-02 22:31:46 +02:00
|
|
|
/** @var ManiaControl $maniaControl */
|
2014-02-07 00:19:53 +01:00
|
|
|
private $maniaControl = null;
|
2017-05-21 17:52:56 +02:00
|
|
|
|
|
|
|
/** @var \cURL\RequestsQueue|null $requestQueue */
|
2017-05-22 22:32:15 +02:00
|
|
|
private $requestQueue = array(null, null);
|
2014-02-07 00:19:53 +01:00
|
|
|
|
|
|
|
/**
|
2014-06-27 00:17:02 +02:00
|
|
|
* Construct a new Asynchronous File Reader Instance
|
2014-02-07 00:19:53 +01:00
|
|
|
*
|
|
|
|
* @param ManiaControl $maniaControl
|
|
|
|
*/
|
|
|
|
public function __construct(ManiaControl $maniaControl) {
|
|
|
|
$this->maniaControl = $maniaControl;
|
2017-05-22 22:32:15 +02:00
|
|
|
|
|
|
|
// Queue for non-serializing requests (parallel is preferred)
|
|
|
|
$this->requestQueue[self::QUEUE_NONSERIALIZING] = new RequestsQueue();
|
|
|
|
|
|
|
|
// Queue for per host serialized requests
|
|
|
|
$this->requestQueue[self::QUEUE_SERIALIZING] = new RequestsQueue();
|
|
|
|
$this->requestQueue[self::QUEUE_SERIALIZING]->setOption(CURLMOPT_MAX_HOST_CONNECTIONS, 1);
|
2014-02-07 00:19:53 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 14:17:03 +01:00
|
|
|
/**
|
2014-06-27 00:17:02 +02:00
|
|
|
* Append available Data of active Requests
|
2014-02-07 14:17:03 +01:00
|
|
|
*/
|
2014-02-07 00:19:53 +01:00
|
|
|
public function appendData() {
|
2017-05-22 22:32:15 +02:00
|
|
|
foreach ($this->requestQueue as &$queue) {
|
|
|
|
do {
|
|
|
|
if (($count = $queue->count()) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2017-05-21 17:52:56 +02:00
|
|
|
|
2017-05-22 22:32:15 +02:00
|
|
|
if ($queue->socketPerform()) {
|
|
|
|
$queue->socketSelect();
|
|
|
|
}
|
|
|
|
} while ($count != $queue->count());
|
|
|
|
}
|
2014-02-07 00:19:53 +01:00
|
|
|
}
|
|
|
|
|
2014-02-15 13:52:46 +01:00
|
|
|
/**
|
2014-06-14 15:48:27 +02:00
|
|
|
* Load a Remote File
|
2014-02-15 13:52:46 +01:00
|
|
|
*
|
2014-06-14 15:48:27 +02:00
|
|
|
* @param string $url
|
|
|
|
* @param callable $function
|
|
|
|
* @param string $contentType
|
|
|
|
* @param int $keepAlive
|
2015-07-29 14:35:19 +02:00
|
|
|
* @param array $headers Additional Headers
|
2015-09-30 22:51:12 +02:00
|
|
|
* @deprecated @see ManiaControl\Files\AsyncHttpRequest
|
2014-02-15 13:52:46 +01:00
|
|
|
*/
|
2015-07-29 14:35:19 +02:00
|
|
|
public function loadFile($url, callable $function, $contentType = 'UTF-8', $keepAlive = 0, $headers = array()) {
|
2015-09-30 22:42:52 +02:00
|
|
|
$httpRequest = new AsyncHttpRequest($this->maniaControl, $url);
|
|
|
|
$httpRequest->setCallable($function)->setContentType($contentType)->setHeaders($headers);
|
|
|
|
$httpRequest->getData($keepAlive);
|
2014-02-15 13:52:46 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 23:29:13 +01:00
|
|
|
/**
|
|
|
|
* Send Data via POST Method
|
|
|
|
*
|
2014-06-27 00:17:02 +02:00
|
|
|
* @param string $url
|
|
|
|
* @param callable $function
|
|
|
|
* @param string $content
|
|
|
|
* @param bool $compression
|
|
|
|
* @param string $contentType
|
2015-07-29 14:35:19 +02:00
|
|
|
* @param array $headers Additional Headers
|
2015-09-30 22:51:12 +02:00
|
|
|
* @deprecated @see ManiaControl\Files\AsyncHttpRequest
|
2014-02-18 23:29:13 +01:00
|
|
|
*/
|
2015-07-29 14:35:19 +02:00
|
|
|
public function postData($url, callable $function, $content, $compression = false, $contentType = 'text/xml; charset=UTF-8;', $headers = array()) {
|
2015-09-30 22:33:48 +02:00
|
|
|
$httpRequest = new AsyncHttpRequest($this->maniaControl, $url);
|
|
|
|
$httpRequest->setCallable($function)->setContent($content)->setCompression($compression)->setContentType($contentType)->setHeaders($headers);
|
|
|
|
$httpRequest->postData();
|
2014-02-07 00:19:53 +01:00
|
|
|
}
|
2015-09-30 22:51:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a Request to the queue, DO NOT CALL MANUALLY!
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*/
|
|
|
|
public function addRequest(Request $request) {
|
2017-05-22 22:32:15 +02:00
|
|
|
$queueId = $request->getSerialize()
|
|
|
|
? self::QUEUE_SERIALIZING
|
|
|
|
: self::QUEUE_NONSERIALIZING;
|
|
|
|
$queue = $this->requestQueue[$queueId];
|
|
|
|
$request->attachTo($queue);
|
2015-09-30 22:51:12 +02:00
|
|
|
}
|
2014-06-27 00:17:02 +02:00
|
|
|
}
|