Skip to content

Commit

Permalink
#118 - Attempt multiple connections to the dedicated server before cr…
Browse files Browse the repository at this point in the history
…ashing.
  • Loading branch information
oliverde8 committed Mar 1, 2018
1 parent d0db137 commit 57a20aa
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 24 deletions.
7 changes: 4 additions & 3 deletions src/eXpansion/Framework/Core/Services/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ class Application extends AbstractApplication {
*
* @param OutputInterface $console
*
* @return $this
* @return $this|mixed
* @throws \Maniaplanet\DedicatedServer\Xmlrpc\TransportException
*/
public function init(OutputInterface $console)
{
parent::init($console);
$this->console->init($console, $this->dispatcher);

$this->console->writeln('$fff 8b d8$fff $0d0 ad888888b, ');
$this->console->writeln('$fff Y8, ,8P $fff $0d0 d8" "88 ');
Expand All @@ -38,7 +39,7 @@ public function init(OutputInterface $console)
$this->console->writeln('$fff $fff 88 $0d0 ');
$this->console->writeln('$777 eXpansion v.2.0.0.0 $fff 88 $0d0 ');

return $this;
return parent::init($console);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,18 @@ public function __construct(
*
* @param OutputInterface $console
*
* @return $this
* @return $this|mixed
* @throws \Maniaplanet\DedicatedServer\Xmlrpc\TransportException
*/
public function init(OutputInterface $console)
{
$this->console->init($console, $this->dispatcher);
$this->dispatcher->dispatch(self::EVENT_BEFORE_INIT, []);

$this->factory->createConnection();
$this->dispatcher->init($this->factory->getConnection());
$this->dispatcher->dispatch(self::EVENT_AFTER_INIT, []);

$this->dispatcher->dispatch(self::EVENT_AFTER_INIT, []);
return $this;
}

Expand All @@ -81,8 +84,6 @@ public function init(OutputInterface $console)
*/
public function run()
{
$this->factory->createConnection();

// Time each cycle needs to take in microseconds. Wrunning 60 cycles per seconds to have optimal response time.
$cycleTime = (1 / 60) * 1000000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace eXpansion\Framework\Core\Services\DedicatedConnection;

use eXpansion\Framework\Core\Services\Console;
use Maniaplanet\DedicatedServer\Connection;
use Maniaplanet\DedicatedServer\Xmlrpc\TransportException;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -36,10 +37,12 @@ class Factory

/** @var string */
protected $password;
/**
* @var LoggerInterface
*/
private $logger;

/** @var LoggerInterface */
protected $logger;

/** @var Console */
protected $console;

/**
* Factory constructor.
Expand All @@ -51,42 +54,103 @@ class Factory
* @param string $password
* @param LoggerInterface $logger
*/
public function __construct($host, $port, $timeout, $user, $password, LoggerInterface $logger)
{
public function __construct(
$host,
$port,
$timeout,
$user,
$password,
LoggerInterface $logger,
Console $console
) {
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
$this->user = $user;
$this->password = $password;
$this->logger = $logger;
$this->console = $console;
}

/**
* Connect to the dedicated server.
* Attempt to connect to the dedicated server.
*
* @param int $maxAttempts
*
* @return Connection
* @throws TransportException when connection fails.
*/
public function createConnection()
public function createConnection($maxAttempts = 3)
{
if (!is_null($this->connection)) {

if (is_null($this->connection)) {
$lastExcelption = $this->attemptConnection($maxAttempts);

if (!is_null($lastExcelption)) {
$this->console->getSfStyleOutput()->error(
[
"Looks like your Dedicated server is either offline or has wrong config settings",
"Error message: " . $lastExcelption->getMessage()
]
);
$this->logger->error("Unable to open connection for Dedicated server", ["exception" => $lastExcelption]);

throw $lastExcelption;
}
}

return $this->connection;
}

/**
* @param $maxAttempts
*
* @return \Exception|TransportException|null
*/
protected function attemptConnection($maxAttempts)
{
$attempts = 0;
$lastExcelption = null;

do {

if (!is_null($lastExcelption)) {
// Not first error.
$lastExcelption = null;

$this->console->getSfStyleOutput()->block(
"Will attempt to re-connect to dedicated server in 30seconds"
);
sleep(30);
}

try {
$this->console->writeln('Attempting to connect to the dedicated server!');

$this->connection = Connection::factory(
$this->host,
$this->port,
$this->timeout,
$this->user,
$this->password
);
} catch (TransportException $ex) {
echo "Looks like your Dedicated server is either offline or has wrong config settings.\n";
echo "Error message: " . $ex->getMessage();
$this->logger->error("Unable to open connection for Dedicated server", ["exception" => $ex]);

throw $ex;
} catch (\Exception $e) {
$lastExcelption = $e;
$attempts++;
$remainingAttemps = $maxAttempts - $attempts;

$this->console->getSfStyleOutput()->error(
[
"Cound't connect to the dedicated server !",
"Attempt : $attempts, Remaining attemps : $remainingAttemps ",
$e->getMessage(),
]
);
}
}
} while($attempts < $maxAttempts && !is_null($lastExcelption));

return $this->connection;
return $lastExcelption;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testInit()
{
$outPutMock = $this->createMock(ConsoleOutputInterface::class);

$this->mockConsole->expects($this->once())->method('init')->withConsecutive([$outPutMock]);
$this->mockConsole->expects($this->atLeastOnce())->method('init')->withConsecutive([$outPutMock]);
$this->mockConsole->expects($this->atLeastOnce())->method('writeln');

$this->mockDispatcher->expects($this->once())->method('init');
Expand Down

0 comments on commit 57a20aa

Please sign in to comment.