finished socket manager

This commit is contained in:
kremsy 2015-06-21 21:33:45 +02:00
parent 1c06e7ff6e
commit 73ae826e44
3 changed files with 124 additions and 62 deletions

View File

@ -183,12 +183,12 @@ class ManiaControl implements CallbackListener, CommandListener, TimerListener {
// Load ManiaControl Modules // Load ManiaControl Modules
$this->callbackManager = new CallbackManager($this); $this->callbackManager = new CallbackManager($this);
$this->echoManager = new EchoManager($this); $this->echoManager = new EchoManager($this);
$this->socketManager = new SocketManager($this);
$this->timerManager = new TimerManager($this); $this->timerManager = new TimerManager($this);
$this->database = new Database($this); $this->database = new Database($this);
$this->fileReader = new AsynchronousFileReader($this); $this->fileReader = new AsynchronousFileReader($this);
$this->billManager = new BillManager($this); $this->billManager = new BillManager($this);
$this->settingManager = new SettingManager($this); $this->settingManager = new SettingManager($this);
$this->socketManager = new SocketManager($this);
$this->statisticManager = new StatisticManager($this); $this->statisticManager = new StatisticManager($this);
$this->manialinkManager = new ManialinkManager($this); $this->manialinkManager = new ManialinkManager($this);
$this->actionsMenu = new ActionsMenu($this); $this->actionsMenu = new ActionsMenu($this);

View File

@ -1,14 +1,13 @@
<?php <?php
/**
* Created by PhpStorm.
* User: Lukas
* Date: 21.06.2015
* Time: 19:48
*/
namespace ManiaControl\Sockets; namespace ManiaControl\Sockets;
/**
* Interface for SocketListener
*
* @author ManiaControl Team <mail@maniacontrol.com>
* @copyright 2014-2015 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
interface SocketListener { interface SocketListener {
} }

View File

@ -1,8 +1,13 @@
<?php <?php
namespace ManiaControl\Sockets; namespace ManiaControl\Sockets;
use ManiaControl\Callbacks\CallbackListener;
use ManiaControl\Callbacks\Callbacks;
use ManiaControl\Callbacks\Listening; use ManiaControl\Callbacks\Listening;
use ManiaControl\Logger;
use ManiaControl\ManiaControl; use ManiaControl\ManiaControl;
use ManiaControl\Settings\Setting;
use ManiaControl\Settings\SettingManager;
use React\EventLoop\Factory; use React\EventLoop\Factory;
use React\EventLoop\LoopInterface; use React\EventLoop\LoopInterface;
use React\Socket\Connection; use React\Socket\Connection;
@ -16,7 +21,7 @@ use React\Socket\Server;
* @copyright 2014-2015 ManiaControl Team * @copyright 2014-2015 ManiaControl Team
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/ */
class SocketManager { class SocketManager implements CallbackListener {
/** @var ManiaControl $maniaControl */ /** @var ManiaControl $maniaControl */
private $maniaControl = null; private $maniaControl = null;
@ -30,6 +35,9 @@ class SocketManager {
/** @var Server $socket */ /** @var Server $socket */
private $socket = null; private $socket = null;
const SETTING_SOCKET_ENABLED = "Activate Socket";
const SETTING_SOCKET_PASSWORD = "Password for the Socket Connection";
const SETTING_SOCKET_PORT = "Socket Port for Server ";
/** /**
* Create a new Socket Handler Instance * Create a new Socket Handler Instance
@ -39,9 +47,11 @@ class SocketManager {
public function __construct(ManiaControl $maniaControl) { public function __construct(ManiaControl $maniaControl) {
$this->maniaControl = $maniaControl; $this->maniaControl = $maniaControl;
$this->createSocket(); $this->maniaControl->getCallbackManager()->registerCallbackListener(SettingManager::CB_SETTING_CHANGED, $this, 'updateSettings');
$this->maniaControl->getCallbackManager()->registerCallbackListener(Callbacks::AFTERINIT, $this, 'initSocketManager');
} }
/** /**
* Register a new Socket Listener * Register a new Socket Listener
* *
@ -115,18 +125,71 @@ class SocketManager {
return $removed; return $removed;
} }
/**
* Inits the Socket Manager after ManiaControl Startup
*/
public function initSocketManager() {
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_ENABLED, false);
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_PASSWORD, "");
$servers = $this->maniaControl->getServer()->getAllServers();
foreach ($servers as $server) {
$this->maniaControl->getSettingManager()->initSetting($this, self::SETTING_SOCKET_PORT . $server->login, 31500 + $server->index);
}
$this->createSocket();
}
/**
* Update Setting
*
* @param Setting $setting
*/
public function updateSettings(Setting $setting) {
if (!$setting->belongsToClass($this)) {
return;
}
$socketEnabled = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_ENABLED);
if ($socketEnabled && !$this->socket) {
$this->createSocket();
}
if (!$socketEnabled) {
unset ($this->socket);
}
}
/** /**
* Creates The Socket * Creates The Socket
*/ */
private function createSocket() { private function createSocket() {
$socketEnabled = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_ENABLED);
if ($socketEnabled) {
Logger::log("[SocketManager] Trying to create Socket");
// Check for MySQLi
$message = '[SocketManager] Checking for installed openssl ... ';
if (!extension_loaded('openssl')) {
Logger::log($message . 'NOT FOUND!');
Logger::log(" -- You don't have openssl installed! Check: http://www.php.net/manual/en/openssl.installation.php");
return;
} else {
Logger::log($message . 'FOUND!');
}
$serverLogin = $this->maniaControl->getServer()->login;
$socketPort = $this->maniaControl->getSettingManager()->getSettingValue($this, self::SETTING_SOCKET_PORT . $serverLogin);
try { try {
$this->loop = Factory::create(); $this->loop = Factory::create();
$this->socket = new Server($this->loop); $this->socket = new Server($this->loop);
$this->socket->on('error', function ($e) { $this->socket->on('error', function ($e) {
//TODO error handling Logger::log("[SocketManager] Socket Error" . $e);
var_dump($e);
}); });
$this->socket->on('connection', function (Connection $connection) { $this->socket->on('connection', function (Connection $connection) {
@ -140,7 +203,6 @@ class SocketManager {
$msg = substr($arr[1], 0, $len); // clip msg $msg = substr($arr[1], 0, $len); // clip msg
$buffer = substr($buffer, strlen((string) $len) + 1 /* newline */ + $len); // clip buffer $buffer = substr($buffer, strlen((string) $len) + 1 /* newline */ + $len); // clip buffer
//TODO pass and port management
// Decode Message // Decode Message
$data = openssl_decrypt($msg, 'aes-192-cbc', 'testpass123', OPENSSL_RAW_DATA, 'kZ2Kt0CzKUjN2MJX'); $data = openssl_decrypt($msg, 'aes-192-cbc', 'testpass123', OPENSSL_RAW_DATA, 'kZ2Kt0CzKUjN2MJX');
$data = json_decode($data); $data = json_decode($data);
@ -169,14 +231,13 @@ class SocketManager {
} }
}); });
}); });
//TODO port //TODO check if port is closed
$this->socket->listen(19999, getHostByName(getHostName())); // exceptions are just thrown right? why does it not work with local ip? because you bind to your loopback adapter k $this->socket->listen($socketPort, getHostByName(getHostName()));
// so that aint it.. xD^^ maybe because it is not in an apache environemnt or smth Logger::log("[SocketManager] Socket " . getHostByName(getHostName()) . ":" . $this->socket->getPort() . " Successfully created!");
// this lib should never run in such an env but the periodictimer works? :O thats actually cool xD ye xD
} catch (ConnectionException $e) { } catch (ConnectionException $e) {
//TODO proper handling Logger::log("[SocketManager] Exception: " . $e->getMessage());
var_dump($e); }
} }
} }
@ -185,6 +246,8 @@ class SocketManager {
* Processes Data on every ManiaControl Tick, don't call this Method * Processes Data on every ManiaControl Tick, don't call this Method
*/ */
public function tick() { public function tick() {
if ($this->loop) {
$this->loop->tick(); $this->loop->tick();
} }
}
} }