From 0fcae44c67a36cb2708ea18d22a56b7200a5574c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 30 Mar 2009 05:36:24 +0000 Subject: [PATCH 1/1] Mode-validation and IP detection added, mode classes added --- .gitattributes | 3 + application/hub/class_ApplicationHelper.php | 16 ++- application/hub/config.php | 2 +- application/hub/debug.php | 2 +- application/hub/exceptions.php | 2 +- application/hub/init.php | 2 +- .../hub/interfaces/nodes/class_NodeHelper.php | 2 +- application/hub/loader.php | 2 +- .../class_NodeInformationDatabaseWrapper.php | 2 +- .../hub/main/nodes/boot/class_HubBootNode.php | 68 +++++++++++ application/hub/main/nodes/class_ | 26 ++-- .../hub/main/nodes/class_BaseHubNode.php | 112 +++++++++++++++++- .../main/nodes/list/class_BootListNode.php | 68 +++++++++++ .../main/nodes/master/class_HubMasterNode.php | 68 +++++++++++ .../nodes/regular/class_HubRegularNode.php | 14 ++- 15 files changed, 355 insertions(+), 34 deletions(-) create mode 100644 application/hub/main/nodes/boot/class_HubBootNode.php create mode 100644 application/hub/main/nodes/list/class_BootListNode.php create mode 100644 application/hub/main/nodes/master/class_HubMasterNode.php diff --git a/.gitattributes b/.gitattributes index b58ad545d..8d83ab097 100644 --- a/.gitattributes +++ b/.gitattributes @@ -19,10 +19,13 @@ application/hub/main/database/wrapper/.htaccess -text application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php -text application/hub/main/nodes/.htaccess -text application/hub/main/nodes/boot/.htaccess -text +application/hub/main/nodes/boot/class_HubBootNode.php -text application/hub/main/nodes/class_ -text application/hub/main/nodes/class_BaseHubNode.php -text application/hub/main/nodes/list/.htaccess -text +application/hub/main/nodes/list/class_BootListNode.php -text application/hub/main/nodes/master/.htaccess -text +application/hub/main/nodes/master/class_HubMasterNode.php -text application/hub/main/nodes/regular/.htaccess -text application/hub/main/nodes/regular/class_HubRegularNode.php -text application/hub/starter.php -text diff --git a/application/hub/class_ApplicationHelper.php b/application/hub/class_ApplicationHelper.php index 7dc7f51d3..e7f2bdfef 100644 --- a/application/hub/class_ApplicationHelper.php +++ b/application/hub/class_ApplicationHelper.php @@ -22,7 +22,7 @@ * * @author Roland Haeder * @version 0.0 - * @copyright Copyright(c) 2007 - 2009 Roland Haeder, this is free software + * @copyright Copyright(c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * * This program is free software: you can redistribute it and/or modify @@ -38,7 +38,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplication { +class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplication, Registerable { /** * The version number of this application */ @@ -173,7 +173,17 @@ class ApplicationHelper extends BaseFrameworkSystem implements ManageableApplica $className = 'Hub' . $this->convertToClassName($nodeMode) . 'Node'; // And try to instance it - $nodeInstance = ObjectFactory::createObjectByName($className); + try { + // Get an instance + $nodeInstance = ObjectFactory::createObjectByName($className, array($requestInstance)); + + // Set the app instance + $nodeInstance->setApplicationInstance($this); + } catch (ClassNotFoundException $e) { + // This means, the node mode is invalid! + // @TODO Can we rewrite this to app_die() ? + die('Node mode ' . $nodeMode . ' is invalid.' . "\n"); + } // --------------------- Hub-id aquirement phase --------------------- // Aquire a hub-id. This step generates on first launch anew one and diff --git a/application/hub/config.php b/application/hub/config.php index c589259df..eb2da9ef9 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0 - * @copyright Copyright(c) 2007 - 2009 Roland Haeder, this is free software + * @copyright Copyright(c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * * This program is free software: you can redistribute it and/or modify diff --git a/application/hub/debug.php b/application/hub/debug.php index 2bf14db6b..47325711b 100644 --- a/application/hub/debug.php +++ b/application/hub/debug.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0 - * @copyright Copyright(c) 2007 - 2009 Roland Haeder, this is free software + * @copyright Copyright(c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * * This program is free software: you can redistribute it and/or modify diff --git a/application/hub/exceptions.php b/application/hub/exceptions.php index c8fcdcb27..b30c9b5ee 100644 --- a/application/hub/exceptions.php +++ b/application/hub/exceptions.php @@ -5,7 +5,7 @@ * * @author Roland Haeder * @version 0.0 - * @copyright Copyright(c) 2007 - 2009 Roland Haeder, this is free software + * @copyright Copyright(c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * * This program is free software: you can redistribute it and/or modify diff --git a/application/hub/init.php b/application/hub/init.php index 312d5aa40..10980a4fc 100644 --- a/application/hub/init.php +++ b/application/hub/init.php @@ -15,7 +15,7 @@ * * @author Roland Haeder * @version 0.0 - * @copyright Copyright(c) 2007 - 2009 Roland Haeder, this is free software + * @copyright Copyright(c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * * This program is free software: you can redistribute it and/or modify diff --git a/application/hub/interfaces/nodes/class_NodeHelper.php b/application/hub/interfaces/nodes/class_NodeHelper.php index 75b45ee90..140f53a6e 100644 --- a/application/hub/interfaces/nodes/class_NodeHelper.php +++ b/application/hub/interfaces/nodes/class_NodeHelper.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @todo We need to find a better name for this interface diff --git a/application/hub/loader.php b/application/hub/loader.php index e2d153d88..b03ebb36b 100644 --- a/application/hub/loader.php +++ b/application/hub/loader.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0 - * @copyright Copyright(c) 2007 - 2009 Roland Haeder, this is free software + * @copyright Copyright(c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * * This program is free software: you can redistribute it and/or modify diff --git a/application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php b/application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php index 7dccdef85..a9d279de7 100644 --- a/application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php +++ b/application/hub/main/database/wrapper/class_NodeInformationDatabaseWrapper.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * diff --git a/application/hub/main/nodes/boot/class_HubBootNode.php b/application/hub/main/nodes/boot/class_HubBootNode.php new file mode 100644 index 000000000..e24aec2a5 --- /dev/null +++ b/application/hub/main/nodes/boot/class_HubBootNode.php @@ -0,0 +1,68 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class HubBootNode extends BaseHubNode implements NodeHelper { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this hub-node class + * + * @param $requestInstance An instance of a Requestable class + * @return $nodeInstance An instance of this hub-node class + */ + public final static function createHubBootNode (Requestable $requestInstance) { + // Get a new instance + $nodeInstance = new HubBootNode(); + + // Set the request instance + $nodeInstance->setRequestInstance($requestInstance); + + // Return the instance + return $nodeInstance; + } + + /** + * Method to "bootstrap" the node. This step does also apply provided + * command-line arguments stored in the request instance. The regular node + * should communicate with the bootstrap-nodes at this point. + * + * @param $requestInstance An instance of a Requestable class + * @return void + */ + public function doBootstrapping (Requestable $requestInstance) { + // Call generic (parent) bootstrapping method + parent::doGenericBootstrapping(); + $this->partialStub("Please implement this method."); + } +} + +// [EOF] +?> diff --git a/application/hub/main/nodes/class_ b/application/hub/main/nodes/class_ index 90a948450..17cb01b59 100644 --- a/application/hub/main/nodes/class_ +++ b/application/hub/main/nodes/class_ @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -35,12 +35,16 @@ class Hub???Node extends BaseHubNode implements NodeHelper { /** * Creates an instance of this hub-node class * - * @return $nodeInstance An instance of this hub-node class + * @param $requestInstance An instance of a Requestable class + * @return $nodeInstance An instance of this hub-node class */ - public final static function createHub???Node () { + public final static function createHub???Node (Requestable $requestInstance) { // Get a new instance $nodeInstance = new Hub???Node(); + // Set the request instance + $nodeInstance->setRequestInstance($requestInstance); + // Return the instance return $nodeInstance; } @@ -54,19 +58,9 @@ class Hub???Node extends BaseHubNode implements NodeHelper { * @return void */ public function doBootstrapping (Requestable $requestInstance) { - $this->partialStub(); - } - - /** - * Method to aquire a hub-id. On first run this generates a new one - * based on many pseudo-random data. On any later run, unless the id - * got not removed from database, it will be restored from the database. - * - * @param $requestInstance An instance of a Requestable class - * @return void - */ - public function aquireHubId (Requestable $requestInstance) { - $this->partialStub(); + // Call generic (parent) bootstrapping method + parent::doGenericBootstrapping(); + $this->partialStub("Please implement this method."); } } diff --git a/application/hub/main/nodes/class_BaseHubNode.php b/application/hub/main/nodes/class_BaseHubNode.php index c000f64d7..61028b328 100644 --- a/application/hub/main/nodes/class_BaseHubNode.php +++ b/application/hub/main/nodes/class_BaseHubNode.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -25,7 +25,12 @@ class BaseHubNode extends BaseFrameworkSystem implements Updateable { /** * Node id */ - private $nodeId = ""; + private $nodeId = ''; + + /** + * IP/port number of bootstrapping node + */ + private $bootIpPort = ''; /** * Protected constructor @@ -62,7 +67,106 @@ class BaseHubNode extends BaseFrameworkSystem implements Updateable { } /** - * Method to aquire a hub-id. On first run this generates a new one + * Checks wether the given IP address matches one of the bootstrapping nodes + * + * @param $remoteAddr IP address to checkout against our bootstrapping list + * @return $isFound Wether the IP is found + */ + private function ifAddressMatchesBootstrappingNodes ($remoteAddr) { + // By default nothing is found + $isFound = false; + + // Run through all configured IPs + foreach (explode(',', $this->getConfigInstance()->readConfig('hub_bootstrap_nodes')) as $ipPort) { + // Does it match? + if (substr($ipPort, 0, strlen($remoteAddr)) == $remoteAddr) { + // Found it! + $isFound = true; + + // Remember the port number + $this->bootIpPort = $ipPort; + + // Output message + $this->getDebugInstance()->output(__FUNCTION__.': IP matches remote address ' . $ipPort . '.'); + + // Stop further searching + break; + } elseif (substr($ipPort, 0, strlen($this->getConfigInstance()->readConfig('node_listen_addr'))) == $this->getConfigInstance()->readConfig('node_listen_addr')) { + // IP matches listen address. At this point we really don't care + // if we can also listen on that address! + $isFound = true; + + // Remember the port number + $this->bootIpPort = $ipPort; + + // Output message + $this->getDebugInstance()->output(__FUNCTION__.': IP matches listen address ' . $ipPort . '.'); + + // Stop further searching + break; + } + } // END - foreach + + // Return the result + return $isFound; + } + + /** + * Outputs the console teaser. This should only be executed on startup or + * full restarts. This method generates some space around the teaser. + * + * @return void + */ + private function outputConsoleTeaser () { + // Get the app instance (for shortening our code) + $app = $this->getApplicationInstance(); + + // Output all lines + $this->getDebugInstance()->output(' '); + $this->getDebugInstance()->output($app->getAppName() . ' v' . $app->getAppVersion() . ' - ' . $this->getRequestInstance()->getRequestElement('mode') . ' mode active'); + $this->getDebugInstance()->output('Copyright(c) 2007 - 2008 Roland Haeder, 2009 Hub Developer Team'); + $this->getDebugInstance()->output(' '); + $this->getDebugInstance()->output('This program comes with ABSOLUTELY NO WARRANTY; for details see docs/COPYING.'); + $this->getDebugInstance()->output('This is free software, and you are welcome to redistribute it under certain'); + $this->getDebugInstance()->output('conditions; see docs/COPYING for details.'); + $this->getDebugInstance()->output(' '); + } + + /** + * Do generic things for bootup phase. This can be e.g. checking if the + * right node mode is selected for this hub's IP number. + * + * @todo This method is maybe not yet finished. + * @return void + */ + protected function doGenericBootstrapping () { + // Now check if the IP address matches one of the bootstrap nodes + if ($this->ifAddressMatchesBootstrappingNodes($_SERVER['REMOTE_ADDR'])) { + // Get our port + $ourPort = $this->getConfigInstance()->readConfig('node_listen_port'); + + // Is the port the same? + if (substr($this->bootIpPort, -strlen($ourPort), strlen($ourPort)) == $ourPort) { + // It is the same! + $this->getDebugInstance()->output(__FUNCTION__.': IP/port matches bootstrapping node ' . $this->bootIpPort . '.'); + + // Now, does the mode match (should be 'boot'!) + if ($this->getRequestInstance()->getRequestElement('mode') == 'boot') { + // Output debug message + $this->getDebugInstance()->output(__FUNCTION__.': Our node is a valid boot-strapping node.'); + } else { + // Output warning + $this->getDebugInstance()->output(__FUNCTION__.': WARNING! Mismatching mode ' . $this->getRequestInstance()->getRequestElement('mode') . '!=boot detected.'); + } + } // END - if + } // END - if + + // Finally output our teaser. This should be the last line! + $this->outputConsoleTeaser(); + } + + /** + * Generic method to aquire a hub-id. On first run this generates a new one * based on many pseudo-random data. On any later run, unless the id * got not removed from database, it will be restored from the database. * @@ -116,7 +220,7 @@ class BaseHubNode extends BaseFrameworkSystem implements Updateable { } /** - * Adds registration elements to a given dataset instance + * Adds hub data elements to a given dataset instance * * @param $criteriaInstance An instance of a storeable criteria * @param $requestInstance An instance of a Requestable class diff --git a/application/hub/main/nodes/list/class_BootListNode.php b/application/hub/main/nodes/list/class_BootListNode.php new file mode 100644 index 000000000..86987565d --- /dev/null +++ b/application/hub/main/nodes/list/class_BootListNode.php @@ -0,0 +1,68 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class HubListNode extends BaseHubNode implements NodeHelper { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this hub-node class + * + * @param $requestInstance An instance of a Requestable class + * @return $nodeInstance An instance of this hub-node class + */ + public final static function createHubListNode (Requestable $requestInstance) { + // Get a new instance + $nodeInstance = new HubListNode(); + + // Set the request instance + $nodeInstance->setRequestInstance($requestInstance); + + // Return the instance + return $nodeInstance; + } + + /** + * Method to "bootstrap" the node. This step does also apply provided + * command-line arguments stored in the request instance. The regular node + * should communicate with the bootstrap-nodes at this point. + * + * @param $requestInstance An instance of a Requestable class + * @return void + */ + public function doBootstrapping (Requestable $requestInstance) { + // Call generic (parent) bootstrapping method + parent::doGenericBootstrapping(); + $this->partialStub("Please implement this method."); + } +} + +// [EOF] +?> diff --git a/application/hub/main/nodes/master/class_HubMasterNode.php b/application/hub/main/nodes/master/class_HubMasterNode.php new file mode 100644 index 000000000..e2e2a1f2f --- /dev/null +++ b/application/hub/main/nodes/master/class_HubMasterNode.php @@ -0,0 +1,68 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team + * @license GNU GPL 3.0 or any newer version + * @link http://www.ship-simu.org + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +class HubMasterNode extends BaseHubNode implements NodeHelper { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this hub-node class + * + * @param $requestInstance An instance of a Requestable class + * @return $nodeInstance An instance of this hub-node class + */ + public final static function createHubMasterNode (Requestable $requestInstance) { + // Get a new instance + $nodeInstance = new HubMasterNode(); + + // Set the request instance + $nodeInstance->setRequestInstance($requestInstance); + + // Return the instance + return $nodeInstance; + } + + /** + * Method to "bootstrap" the node. This step does also apply provided + * command-line arguments stored in the request instance. The regular node + * should communicate with the bootstrap-nodes at this point. + * + * @param $requestInstance An instance of a Requestable class + * @return void + */ + public function doBootstrapping (Requestable $requestInstance) { + // Call generic (parent) bootstrapping method + parent::doGenericBootstrapping(); + $this->partialStub("Please implement this method."); + } +} + +// [EOF] +?> diff --git a/application/hub/main/nodes/regular/class_HubRegularNode.php b/application/hub/main/nodes/regular/class_HubRegularNode.php index 39903a5a5..dd9105121 100644 --- a/application/hub/main/nodes/regular/class_HubRegularNode.php +++ b/application/hub/main/nodes/regular/class_HubRegularNode.php @@ -4,7 +4,7 @@ * * @author Roland Haeder * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Hub Developer Team * @license GNU GPL 3.0 or any newer version * @link http://www.ship-simu.org * @@ -35,12 +35,16 @@ class HubRegularNode extends BaseHubNode implements NodeHelper { /** * Creates an instance of this hub-node class * - * @return $nodeInstance An instance of this hub-node class + * @param $requestInstance An instance of a Requestable class + * @return $nodeInstance An instance of this hub-node class */ - public final static function createHubRegularNode () { + public final static function createHubRegularNode (Requestable $requestInstance) { // Get a new instance $nodeInstance = new HubRegularNode(); + // Set the request instance + $nodeInstance->setRequestInstance($requestInstance); + // Return the instance return $nodeInstance; } @@ -54,7 +58,9 @@ class HubRegularNode extends BaseHubNode implements NodeHelper { * @return void */ public function doBootstrapping (Requestable $requestInstance) { - $this->partialStub(); + // Call generic (parent) bootstrapping method + parent::doGenericBootstrapping(); + $this->partialStub("Please implement this method."); } } -- 2.39.2