TrackManiaControl/core/Files/AsynchronousFileReader.php

117 lines
3.2 KiB
PHP
Raw Normal View History

2014-02-07 00:19:53 +01:00
<?php
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;
use cURL\RequestsQueue;
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
*/
class AsynchronousFileReader implements UsageInformationAble {
use UsageInformationTrait;
/*
* Constants
*/
const CONTENT_TYPE_JSON = 'application/json';
const QUEUE_NONSERIALIZING = 0;
const QUEUE_SERIALIZING = 1;
/*
* Private properties
2014-02-07 00:19:53 +01:00
*/
/** @var ManiaControl $maniaControl */
2014-02-07 00:19:53 +01:00
private $maniaControl = null;
/** @var \cURL\RequestsQueue|null $requestQueue */
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;
// 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() {
foreach ($this->requestQueue as &$queue) {
do {
if (($count = $queue->count()) == 0) {
break;
}
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
/**
* Load a Remote File
2014-02-15 13:52:46 +01:00
*
* @param string $url
* @param callable $function
* @param string $contentType
* @param int $keepAlive
* @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
*/
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
* @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
*/
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) {
$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
}