updated requestsqueue

This commit is contained in:
kremsy 2017-05-20 11:29:15 +02:00
parent e706ca8b29
commit ec604f7a64
2 changed files with 220 additions and 235 deletions

View File

@ -1,10 +1,10 @@
<?php <?php
namespace cURL; namespace cURL;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcher;
class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \Countable class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \Countable {
{
/** /**
* @var Options Default options for new Requests attached to RequestsQueue * @var Options Default options for new Requests attached to RequestsQueue
*/ */
@ -15,26 +15,20 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
*/ */
protected $mh; protected $mh;
/**
* @var int Amount of requests running
*/
protected $runningCount = 0;
/** /**
* @var Request[] Array of requests attached * @var Request[] Array of requests attached
*/ */
protected $queue = array(); protected $queue = array();
/** /**
* @var array Array of requests running * @var array Array of requests added to curl multi handle
*/ */
protected $running = array(); protected $running = array();
/** /**
* Initializes curl_multi handler * Initializes curl_multi handler
*/ */
public function __construct() public function __construct() {
{
$this->mh = curl_multi_init(); $this->mh = curl_multi_init();
} }
@ -43,8 +37,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* *
* @return void * @return void
*/ */
public function __destruct() public function __destruct() {
{
if (isset($this->mh)) { if (isset($this->mh)) {
curl_multi_close($this->mh); curl_multi_close($this->mh);
} }
@ -55,8 +48,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* *
* @return Options * @return Options
*/ */
public function getDefaultOptions() public function getDefaultOptions() {
{
if (!isset($this->defaultOptions)) { if (!isset($this->defaultOptions)) {
$this->defaultOptions = new Options(); $this->defaultOptions = new Options();
} }
@ -69,8 +61,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* @param Options $defaultOptions New options * @param Options $defaultOptions New options
* @return void * @return void
*/ */
public function setDefaultOptions(Options $defaultOptions) public function setDefaultOptions(Options $defaultOptions) {
{
$this->defaultOptions = $defaultOptions; $this->defaultOptions = $defaultOptions;
} }
@ -79,8 +70,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* *
* @return resource * @return resource
*/ */
public function getHandle() public function getHandle() {
{
return $this->mh; return $this->mh;
} }
@ -90,8 +80,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* @param Request $request Request to add * @param Request $request Request to add
* @return self * @return self
*/ */
public function attach(Request $request) public function attach(Request $request) {
{
$this->queue[$request->getUID()] = $request; $this->queue[$request->getUID()] = $request;
return $this; return $this;
} }
@ -102,8 +91,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* @param Request $request Request to remove * @param Request $request Request to remove
* @return self * @return self
*/ */
public function detach(Request $request) public function detach(Request $request) {
{
unset($this->queue[$request->getUID()]); unset($this->queue[$request->getUID()]);
return $this; return $this;
} }
@ -113,12 +101,11 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* *
* @return int Amount of requests completed * @return int Amount of requests completed
*/ */
protected function read() protected function read() {
{
$n = 0; $n = 0;
while ($info = curl_multi_info_read($this->mh)) { while ($info = curl_multi_info_read($this->mh)) {
$n++; $n++;
$request = $this->queue[(int)$info['handle']]; $request = $this->queue[(int) $info['handle']];
$result = $info['result']; $result = $info['result'];
curl_multi_remove_handle($this->mh, $request->getHandle()); curl_multi_remove_handle($this->mh, $request->getHandle());
@ -144,8 +131,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* *
* @return int Handles count * @return int Handles count
*/ */
public function count() public function count() {
{
return count($this->queue); return count($this->queue);
} }
@ -154,8 +140,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* *
* @return void * @return void
*/ */
public function send() public function send() {
{
while ($this->socketPerform()) { while ($this->socketPerform()) {
$this->socketSelect(); $this->socketSelect();
} }
@ -166,10 +151,11 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* *
* @return Request[] Array of requests * @return Request[] Array of requests
*/ */
protected function getRequestsNotRunning() protected function getRequestsNotRunning() {
{
$map = $this->queue; $map = $this->queue;
foreach($this->running as $k => $v) unset($map[$k]); foreach ($this->running as $k => $v) {
unset($map[$k]);
}
return $map; return $map;
} }
@ -179,8 +165,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* @throws Exception * @throws Exception
* @return bool TRUE when there are any requests on queue, FALSE when finished * @return bool TRUE when there are any requests on queue, FALSE when finished
*/ */
public function socketPerform() public function socketPerform() {
{
if ($this->count() == 0) { if ($this->count() == 0) {
throw new Exception('Cannot perform if there are no requests in queue.'); throw new Exception('Cannot perform if there are no requests in queue.');
} }
@ -197,13 +182,14 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
$this->running[$request->getUID()] = $request; $this->running[$request->getUID()] = $request;
} }
$runningBefore = $this->runningCount; $runningHandles = null;
do { do {
$mrc = curl_multi_exec($this->mh, $this->runningCount); // http://curl.haxx.se/libcurl/c/curl_multi_perform.html
// If an added handle fails very quickly, it may never be counted as a running_handle.
$mrc = curl_multi_exec($this->mh, $runningHandles);
} while ($mrc === CURLM_CALL_MULTI_PERFORM); } while ($mrc === CURLM_CALL_MULTI_PERFORM);
$runningAfter = $this->runningCount;
if ($runningAfter < $runningBefore) { if ($runningHandles < count($this->running)) {
$this->read(); $this->read();
} }
@ -225,8 +211,7 @@ class RequestsQueue extends EventDispatcher implements RequestsQueueInterface, \
* @throws Exception * @throws Exception
* @return bool * @return bool
*/ */
public function socketSelect($timeout = 1) public function socketSelect($timeout = 1) {
{
if ($this->count() == 0) { if ($this->count() == 0) {
throw new Exception('Cannot select if there are no requests in queue.'); throw new Exception('Cannot select if there are no requests in queue.');
} }

View File

@ -49,7 +49,7 @@ class DedimaniaWebHandler {
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL); $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL);
$asyncHttpRequest->setCallable(function ($data, $error) use ($updateRecords) { $asyncHttpRequest->setCallable(function ($data, $error) use ($updateRecords) {
Logger::log("Try to connect on Dedimania"); Logger::logInfo("Try to connect on Dedimania");
if (!$data || $error) { if (!$data || $error) {
Logger::logError("Dedimania Error while opening session: '{$error}' Line 42"); Logger::logError("Dedimania Error while opening session: '{$error}' Line 42");
@ -69,7 +69,7 @@ class DedimaniaWebHandler {
$responseData = $methodResponse[0]; $responseData = $methodResponse[0];
$this->dedimaniaData->sessionId = $responseData['SessionId']; $this->dedimaniaData->sessionId = $responseData['SessionId'];
if ($this->dedimaniaData->sessionId) { if ($this->dedimaniaData->sessionId) {
Logger::log("Dedimania connection successfully established."); Logger::logInfo("Dedimania connection successfully established.");
if ($updateRecords) { if ($updateRecords) {
$this->fetchDedimaniaRecords(); $this->fetchDedimaniaRecords();
@ -146,7 +146,7 @@ class DedimaniaWebHandler {
$this->dedimaniaData->records[$key] = new RecordData($record); $this->dedimaniaData->records[$key] = new RecordData($record);
} }
Logger::log(count($this->dedimaniaData->records) . " Dedimania Records Fetched succesfully!"); Logger::logInfo(count($this->dedimaniaData->records) . " Dedimania Records Fetched succesfully!");
$this->maniaLinkNeedsUpdate = true; $this->maniaLinkNeedsUpdate = true;
$this->maniaControl->getCallbackManager()->triggerCallback(DedimaniaPlugin::CB_DEDIMANIA_UPDATED, $this->dedimaniaData->records); //TODO $this->maniaControl->getCallbackManager()->triggerCallback(DedimaniaPlugin::CB_DEDIMANIA_UPDATED, $this->dedimaniaData->records); //TODO
@ -233,7 +233,7 @@ class DedimaniaWebHandler {
} }
if (!$record->vReplay) { if (!$record->vReplay) {
Logger::log("Ignore time for " . $record->login . " no validation replay found"); Logger::logInfo("Ignore time for " . $record->login . " no validation replay found");
continue; continue;
} }
@ -256,7 +256,7 @@ class DedimaniaWebHandler {
$data = array($this->dedimaniaData->sessionId, $this->getMapInfo(), $gameMode, $times, $replays); $data = array($this->dedimaniaData->sessionId, $this->getMapInfo(), $gameMode, $times, $replays);
$content = $this->encodeRequest(self::DEDIMANIA_SET_CHALLENGE_TIMES, $data); $content = $this->encodeRequest(self::DEDIMANIA_SET_CHALLENGE_TIMES, $data);
Logger::log("Dedimania Submitting Map Times at End-Map Start"); Logger::logInfo("Dedimania Submitting Map Times at End-Map Start");
$asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL); $asyncHttpRequest = new AsyncHttpRequest($this->maniaControl, self::DEDIMANIA_URL);
@ -280,7 +280,7 @@ class DedimaniaWebHandler {
if (!$methodResponse[0]) { if (!$methodResponse[0]) {
Logger::logError("Records Plugin: Submitting dedimania records failed."); Logger::logError("Records Plugin: Submitting dedimania records failed.");
} else { } else {
Logger::log("Dedimania Times succesfully submitted"); Logger::logInfo("Dedimania Times succesfully submitted");
} }
}); });
@ -332,7 +332,7 @@ class DedimaniaWebHandler {
$this->fetchDedimaniaRecords(true); $this->fetchDedimaniaRecords(true);
} }
Logger::log("Dedimania Player added " . $dediPlayer->login); Logger::logInfo("Dedimania Player added " . $dediPlayer->login);
$this->maniaLinkNeedsUpdate = true; //TODO handle update for only one player instead of everyone as soon splitted $this->maniaLinkNeedsUpdate = true; //TODO handle update for only one player instead of everyone as soon splitted
}); });
@ -376,7 +376,7 @@ class DedimaniaWebHandler {
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_PLAYERDISCONNECT); $this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_PLAYERDISCONNECT);
} }
Logger::log("Debug: Dedimania Player left"); Logger::logInfo("Debug: Dedimania Player removed");
}); });
$asyncHttpRequest->setContent($content); $asyncHttpRequest->setContent($content);
@ -420,7 +420,7 @@ class DedimaniaWebHandler {
$this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_UPDATE_SERVER_PLAYERS); $this->handleXmlRpcFault($methodResponse, self::DEDIMANIA_UPDATE_SERVER_PLAYERS);
} }
Logger::log("Dedimania Playerlist Updated"); Logger::logInfo("Dedimania Playerlist Updated");
}); });
$asyncHttpRequest->setContent($content); $asyncHttpRequest->setContent($content);