From: Roland Häder Date: Thu, 7 Feb 2013 18:28:17 +0000 (+0000) Subject: Added DHT initialization task (see comments below): X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=9e906044d19ec598125029cee81291c80a167ff3;p=hub.git Added DHT initialization task (see comments below): - The DHT initialization task is for preparing the DHT (if not yet done) in a very early state ("virgin" state) and inserts/updates node's current data as the first entry. With this data the node "knowns" only itself, not even the upper nodes. They will be added later on. - Renamed instance 'task' to 'task_handler' as it is always a task handler class - Other minor improvements --- diff --git a/.gitattributes b/.gitattributes index c45b21074..3289146a1 100644 --- a/.gitattributes +++ b/.gitattributes @@ -636,6 +636,7 @@ application/hub/main/tasks/node/decoder/.htaccess -text svneol=unset#text/plain application/hub/main/tasks/node/decoder/class_NodePackageDecoderTask.php -text application/hub/main/tasks/node/dht/.htaccess -text svneol=unset#text/plain application/hub/main/tasks/node/dht/class_NodeDhtBootstrapTask.php svneol=native#text/plain +application/hub/main/tasks/node/dht/class_NodeDhtInitializationTask.php svneol=native#text/plain application/hub/main/tasks/node/dht/class_NodeDhtQueryTask.php svneol=native#text/plain application/hub/main/tasks/node/listener/.htaccess -text svneol=unset#text/plain application/hub/main/tasks/node/ping/.htaccess -text svneol=unset#text/plain diff --git a/application/hub/config.php b/application/hub/config.php index 151722b01..2df932355 100644 --- a/application/hub/config.php +++ b/application/hub/config.php @@ -498,6 +498,9 @@ $cfg->setConfigEntry('node_chunk_assembler_task_class', 'NodeChunkAssemblerTask' // CFG: NODE-PACKAGE-DECODER-TASK-CLASS $cfg->setConfigEntry('node_package_decoder_task_class', 'NodePackageDecoderTask'); +// CFG: NODE-DHT-INIT-TASK-CLASS +$cfg->setConfigEntry('node_dht_init_task_class', 'NodeDhtInitializationTask'); + // CFG: NODE-DHT-BOOTSTRAP-TASK-CLASS $cfg->setConfigEntry('node_dht_bootstrap_task_class', 'NodeDhtBootstrapTask'); @@ -549,11 +552,20 @@ $cfg->setConfigEntry('task_chunk_assembler_interval_delay', 50); // CFG: TASK-CHUNK-ASSEMBLER-MAX-RUNS $cfg->setConfigEntry('task_chunk_assembler_max_runs', 0); +// CFG: TASK-DHT-INIT-STATUP-DELAY +$cfg->setConfigEntry('task_dht_init_startup_delay', 100); + +// CFG: TASK-DHT-INIT-INTERVAL-DELAY +$cfg->setConfigEntry('task_dht_init_interval_delay', 86400000); // = 1 day (won't matter anyway) + +// CFG: TASK-DHT-INIT-MAX-RUNS +$cfg->setConfigEntry('task_dht_init_max_runs', 1); + // CFG: TASK-DHT-BOOTSTRAP-STATUP-DELAY $cfg->setConfigEntry('task_dht_bootstrap_startup_delay', 100); // CFG: TASK-DHT-BOOTSTRAP-INTERVAL-DELAY -$cfg->setConfigEntry('task_dht_bootstrap_interval_delay', 3600000); // = 1 hour +$cfg->setConfigEntry('task_dht_bootstrap_interval_delay', 3600000); // = 1 hour (won't matter anyway) // CFG: TASK-DHT-BOOTSTRAP-MAX-RUNS $cfg->setConfigEntry('task_dht_bootstrap_max_runs', 1); diff --git a/application/hub/interfaces/distributable/class_Distributable.php b/application/hub/interfaces/distributable/class_Distributable.php index 8bbf59598..9ddba1f53 100644 --- a/application/hub/interfaces/distributable/class_Distributable.php +++ b/application/hub/interfaces/distributable/class_Distributable.php @@ -22,6 +22,12 @@ * along with this program. If not, see . */ interface Distributable extends FrameworkInterface { + /** + * Initializes the distributable hash table (DHT) + * + * @return void + */ + function initDht(); } // [EOF] diff --git a/application/hub/interfaces/wrapper/class_NodeListWrapper.php b/application/hub/interfaces/wrapper/class_NodeListWrapper.php index c04669289..f551ef47b 100644 --- a/application/hub/interfaces/wrapper/class_NodeListWrapper.php +++ b/application/hub/interfaces/wrapper/class_NodeListWrapper.php @@ -1,64 +1,3 @@ - * @version 0.0.0 - * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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 . - */ -interface NodeListWrapper extends FrameworkInterface { - /** - * Resolves a session id into an ip:port combination - * - * @param $sessionId A valid session id - * @param $protocol Name of the protocol (TCP/UDP) - * @return $recipient Recipient as ip:port combination - */ - function resolveIpPortBySessionId ($sessionId, $protocol); - - /** - * Resolves a ip:port combination into a session id - * - * @param $ipPort Ip:port combination - * @param $protocol Name of the used protocol (TCP/UDP) - * @return $sessionId A valid session id - */ - function resolveSessionIdByIpPort ($ipPort, $protocol); - - /** - * Registeres a node by given message data. - * - * @param $messageData An array of all message data - * @param $handlerInstance An instance of a HandleableMessage class - * @return void - */ - function registerNodeByMessageData (array $messageData, Handleable $handlerInstance); - - /** - * Updates an existing entry in node list - * - * @param $messageData An array of all message data - * @param $handlerInstance An instance of a HandleableMessage class - * @param $searchInstance An instance of LocalSearchCriteria class - * @return void - */ - function updateNodeByMessageData (array $messageData, Handleable $handlerInstance, LocalSearchCriteria $searchInstance); -} - -// [EOF] +// @DEPRECATED ?> diff --git a/application/hub/main/commands/console/class_HubConsoleAptProxyCommand.php b/application/hub/main/commands/console/class_HubConsoleAptProxyCommand.php index f701dc74d..475820c9f 100644 --- a/application/hub/main/commands/console/class_HubConsoleAptProxyCommand.php +++ b/application/hub/main/commands/console/class_HubConsoleAptProxyCommand.php @@ -78,7 +78,7 @@ class HubConsoleAptProxyCommand extends BaseCommand implements Commandable { $proxyInstance->addExtraAptProxyFilters(); // Get task handler instance - $handlerInstance = Registry::getRegistry()->getInstance('task'); + $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); // Debug message self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Entering main loop. ---'); diff --git a/application/hub/main/commands/console/class_HubConsoleChatCommand.php b/application/hub/main/commands/console/class_HubConsoleChatCommand.php index 5f5acd5b9..6b092478f 100644 --- a/application/hub/main/commands/console/class_HubConsoleChatCommand.php +++ b/application/hub/main/commands/console/class_HubConsoleChatCommand.php @@ -78,7 +78,7 @@ class HubConsoleChatCommand extends BaseCommand implements Commandable { $chatInstance->addExtraChatFilters(); // Get task handler instance - $handlerInstance = Registry::getRegistry()->getInstance('task'); + $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); // Debug message self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Entering main loop. ---'); diff --git a/application/hub/main/commands/console/class_HubConsoleCruncherCommand.php b/application/hub/main/commands/console/class_HubConsoleCruncherCommand.php index d5fa52a93..dd159a089 100644 --- a/application/hub/main/commands/console/class_HubConsoleCruncherCommand.php +++ b/application/hub/main/commands/console/class_HubConsoleCruncherCommand.php @@ -78,7 +78,7 @@ class HubConsoleCruncherCommand extends BaseCommand implements Commandable { $cruncherInstance->addExtraCruncherFilters(); // Get task handler instance - $handlerInstance = Registry::getRegistry()->getInstance('task'); + $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); // Debug message self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Entering main loop. ---'); diff --git a/application/hub/main/commands/console/class_HubConsoleMainCommand.php b/application/hub/main/commands/console/class_HubConsoleMainCommand.php index 45385ea7b..581315540 100644 --- a/application/hub/main/commands/console/class_HubConsoleMainCommand.php +++ b/application/hub/main/commands/console/class_HubConsoleMainCommand.php @@ -85,7 +85,7 @@ class HubConsoleMainCommand extends BaseCommand implements Commandable { $nodeInstance->activateNode($requestInstance, $responseInstance); // Get task handler instance - $handlerInstance = Registry::getRegistry()->getInstance('task'); + $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); // Debug message self::createDebugInstance(__CLASS__)->debugOutput('MAIN: --- Entering main loop. ---'); diff --git a/application/hub/main/dht/node/class_NodeDhtFacade.php b/application/hub/main/dht/node/class_NodeDhtFacade.php index 07ab7633d..5007c47f0 100644 --- a/application/hub/main/dht/node/class_NodeDhtFacade.php +++ b/application/hub/main/dht/node/class_NodeDhtFacade.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class NodeDhtFacade extends BaseDht implements Distributable { +class NodeDhtFacade extends BaseDht implements Distributable, Registerable { /** * Protected constructor * @@ -44,6 +44,16 @@ class NodeDhtFacade extends BaseDht implements Distributable { // Return the prepared instance return $dhtInstance; } + + /** + * Initializes the distributable hash table (DHT) + * + * @return void + * @todo Please implement this method + */ + public function initDht () { + $this->partialStub('Please implement this method.'); + } } // [EOF] diff --git a/application/hub/main/filter/activation/node/class_NodeActivationAnnouncementFilter.php b/application/hub/main/filter/activation/node/class_NodeActivationAnnouncementFilter.php index 564f85b54..37efab2cf 100644 --- a/application/hub/main/filter/activation/node/class_NodeActivationAnnouncementFilter.php +++ b/application/hub/main/filter/activation/node/class_NodeActivationAnnouncementFilter.php @@ -54,7 +54,7 @@ class NodeActivationAnnouncementFilter extends BaseNodeFilter implements Filtera */ public function execute (Requestable $requestInstance, Responseable $responseInstance) { // Get task handler instance - $handlerInstance = Registry::getRegistry()->getInstance('task'); + $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); // Prepare a self-test task for the listeners $taskInstance = ObjectFactory::createObjectByConfiguredName('node_announcement_task_class'); diff --git a/application/hub/main/filter/shutdown/node/class_NodeShutdownTaskHandlerFilter.php b/application/hub/main/filter/shutdown/node/class_NodeShutdownTaskHandlerFilter.php index 43b16b2ad..7c830b614 100644 --- a/application/hub/main/filter/shutdown/node/class_NodeShutdownTaskHandlerFilter.php +++ b/application/hub/main/filter/shutdown/node/class_NodeShutdownTaskHandlerFilter.php @@ -56,7 +56,7 @@ class NodeShutdownTaskHandlerFilter extends BaseNodeFilter implements Filterable */ public function execute (Requestable $requestInstance, Responseable $responseInstance) { // Get task handler instance - $handlerInstance = Registry::getRegistry()->getInstance('task'); + $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); // Shutdown the task manager and all its registered tasks $handlerInstance->doShutdown(); diff --git a/application/hub/main/filter/task/apt-proxy/class_AptProxyTaskHandlerInitializerFilter.php b/application/hub/main/filter/task/apt-proxy/class_AptProxyTaskHandlerInitializerFilter.php index 3c673e5d3..e8b4aac73 100644 --- a/application/hub/main/filter/task/apt-proxy/class_AptProxyTaskHandlerInitializerFilter.php +++ b/application/hub/main/filter/task/apt-proxy/class_AptProxyTaskHandlerInitializerFilter.php @@ -70,7 +70,7 @@ class AptProxyTaskHandlerInitializerFilter extends BaseAptProxyFilter implements $handlerInstance->registerTask('apt_proxy_listener', $taskInstance); // Put the task handler in registry - Registry::getRegistry()->addInstance('task', $handlerInstance); + Registry::getRegistry()->addInstance('task_handler', $handlerInstance); } } diff --git a/application/hub/main/filter/task/chat/class_ChatTaskHandlerInitializerFilter.php b/application/hub/main/filter/task/chat/class_ChatTaskHandlerInitializerFilter.php index bd9fda316..4ba2fb600 100644 --- a/application/hub/main/filter/task/chat/class_ChatTaskHandlerInitializerFilter.php +++ b/application/hub/main/filter/task/chat/class_ChatTaskHandlerInitializerFilter.php @@ -70,7 +70,7 @@ class ChatTaskHandlerInitializerFilter extends BaseChatFilter implements Filtera $handlerInstance->registerTask('chat_telnet_listener', $taskInstance); // Put the task handler in registry - Registry::getRegistry()->addInstance('task', $handlerInstance); + Registry::getRegistry()->addInstance('task_handler', $handlerInstance); } } diff --git a/application/hub/main/filter/task/cruncher/class_CruncherTaskHandlerInitializerFilter.php b/application/hub/main/filter/task/cruncher/class_CruncherTaskHandlerInitializerFilter.php index b4ef8b4e0..099978b7b 100644 --- a/application/hub/main/filter/task/cruncher/class_CruncherTaskHandlerInitializerFilter.php +++ b/application/hub/main/filter/task/cruncher/class_CruncherTaskHandlerInitializerFilter.php @@ -84,7 +84,7 @@ class CruncherTaskHandlerInitializerFilter extends BaseCruncherFilter implements $handlerInstance->registerTask('cruncher_key_producer', $taskInstance); // Put the task handler in registry - Registry::getRegistry()->addInstance('task', $handlerInstance); + Registry::getRegistry()->addInstance('task_handler', $handlerInstance); } } diff --git a/application/hub/main/filter/task/node/class_NodeTaskHandlerInitializerFilter.php b/application/hub/main/filter/task/node/class_NodeTaskHandlerInitializerFilter.php index c28671577..3ea39ee32 100644 --- a/application/hub/main/filter/task/node/class_NodeTaskHandlerInitializerFilter.php +++ b/application/hub/main/filter/task/node/class_NodeTaskHandlerInitializerFilter.php @@ -92,10 +92,10 @@ class NodeTaskHandlerInitializerFilter extends BaseNodeFilter implements Filtera $handlerInstance->registerTask('package_decoder', $taskInstance); // Generate DHT initialization task - $taskInstance = ObjectFactory::createObjectByConfiguredName('node_dht_bootstrap_task_class'); + $taskInstance = ObjectFactory::createObjectByConfiguredName('node_dht_init_task_class'); // Register it as well - $handlerInstance->registerTask('dht_bootstrap', $taskInstance); + $handlerInstance->registerTask('dht_init', $taskInstance); // Generate DHT query task $taskInstance = ObjectFactory::createObjectByConfiguredName('node_dht_query_task_class'); @@ -125,7 +125,7 @@ class NodeTaskHandlerInitializerFilter extends BaseNodeFilter implements Filtera $handlerInstance->registerTask('ping', $taskInstance); // Put the task handler in registry - Registry::getRegistry()->addInstance('task', $handlerInstance); + Registry::getRegistry()->addInstance('task_handler', $handlerInstance); } } diff --git a/application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php b/application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php index e0f708e4f..39e792ed2 100644 --- a/application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php +++ b/application/hub/main/handler/answer-status/announcement/class_AnnouncementAnswerOkayHandler.php @@ -82,6 +82,15 @@ class AnnouncementAnswerOkayHandler extends BaseAnserStatusHandler implements Ha throw new NodeSessionIdVerficationException(array($this, $messageData), BaseHubSystem::EXCEPTION_NODE_SESSION_ID_NOT_VERIFYING); } // END - if + // Get handler instance + $handlerInstance = Registry::getRegistry()->getInstance('task_handler'); + + // Generate DHT bootstrap task + $taskInstance = ObjectFactory::createObjectByConfiguredName('node_dht_bootstrap_task_class'); + + // Register it as well + $handlerInstance->registerTask('dht_bootstrap', $taskInstance); + // Update node data (include status code) $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance); diff --git a/application/hub/main/tasks/node/dht/class_NodeDhtBootstrapTask.php b/application/hub/main/tasks/node/dht/class_NodeDhtBootstrapTask.php index b5a556d2e..5ee47c684 100644 --- a/application/hub/main/tasks/node/dht/class_NodeDhtBootstrapTask.php +++ b/application/hub/main/tasks/node/dht/class_NodeDhtBootstrapTask.php @@ -69,7 +69,8 @@ class NodeDhtBootstrapTask extends BaseTask implements Taskable, Visitable { * @todo 0% done */ public function executeTask () { - $this->partialStub('Unimplemented task.'); + // "Cache" the DHT instance + $dhtInstance = $this->getDhtInstance(); } } diff --git a/application/hub/main/tasks/node/dht/class_NodeDhtInitializationTask.php b/application/hub/main/tasks/node/dht/class_NodeDhtInitializationTask.php new file mode 100644 index 000000000..fabd032c3 --- /dev/null +++ b/application/hub/main/tasks/node/dht/class_NodeDhtInitializationTask.php @@ -0,0 +1,77 @@ + + * @version 0.0.0 + * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 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 NodeDhtInitializationTask extends BaseTask implements Taskable, Visitable { + /** + * Protected constructor + * + * @return void + */ + protected function __construct () { + // Call parent constructor + parent::__construct(__CLASS__); + } + + /** + * Creates an instance of this class + * + * @return $taskInstance An instance of a Visitable class + */ + public final static function createNodeDhtInitializationTask () { + // Get new instance + $taskInstance = new NodeDhtInitializationTask(); + + // Get a DHT instance + $dhtInstance = DhtObjectFactory::createDhtObjectInstance(); + + // Set the DHT instance here + $taskInstance->setDhtInstance($dhtInstance); + + // Return the prepared instance + return $taskInstance; + } + + /** + * Accepts the visitor to process the visitor + * + * @param $visitorInstance An instance of a Visitor class + * @return void + */ + public function accept (Visitor $visitorInstance) { + // Visit this task + $visitorInstance->visitTask($this); + } + + /** + * Executes the task + * + * @return void + */ + public function executeTask () { + // Intiailize the DHT + $this->getDhtInstance()->initDht(); + } +} + +// [EOF] +?> diff --git a/application/hub/main/tasks/node/dht/class_NodeDhtQueryTask.php b/application/hub/main/tasks/node/dht/class_NodeDhtQueryTask.php index f281d9eba..44bbced0a 100644 --- a/application/hub/main/tasks/node/dht/class_NodeDhtQueryTask.php +++ b/application/hub/main/tasks/node/dht/class_NodeDhtQueryTask.php @@ -69,7 +69,8 @@ class NodeDhtQueryTask extends BaseTask implements Taskable, Visitable { * @todo 0% done */ public function executeTask () { - $this->partialStub('Unimplemented task.'); + // "Cache" the DHT instance + $dhtInstance = $this->getDhtInstance(); } } diff --git a/application/hub/main/tools/class_HubTools.php b/application/hub/main/tools/class_HubTools.php index f0d1baf55..b81939b76 100644 --- a/application/hub/main/tools/class_HubTools.php +++ b/application/hub/main/tools/class_HubTools.php @@ -80,19 +80,20 @@ class HubTools extends BaseHubSystem { } /** - * Resolves a session id into an ip:port combination + * Resolves a session id into an ip:port combination. The opposite method + * is resolveSessionIdByIpPort() * * @param $sessionId A valid session id * @param $protocol Name of the used protocol: TCP/UDP * @return $recipient Recipient as ip:port combination - * @todo Rewrite this to use DHT */ protected function resolveIpPortBySessionId ($sessionId, $protocol) { - // Get a wrapper instance - $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class'); + // Get a DHT instance + $dhtInstance = DhtObjectFactory::createDhtObjectInstance(); - // And ask it for the session id - $recipient = $wrapperInstance->resolveIpPortBySessionId($sessionId, $protocol); + // And ask it for ip:port by given session id + $recipient = $dhtInstance->findNodeBySessionId($sessionId, $protocol); + die(__METHOD__.':recipient=
'.print_r($recipient, true).'
' . PHP_EOL); // Is the recipient invalid? if ($recipient == 'invalid:invalid') { @@ -111,19 +112,20 @@ class HubTools extends BaseHubSystem { } /** - * Resolves a ip:port combination into a session id + * Resolves a ip:port combination into a session id. The "opposite" method + * is resolveIpPortBySessionId(). * * @param $ipPort Ip:port combination * @param $protocol Name of used protocol (TCP/UDP) * @return $sessionId Valid session id - * @todo Rewrite this to use DHT */ public static function resolveSessionIdByIpPort ($ipPort, $protocol) { - // Get a wrapper instance - $wrapperInstance = DatabaseWrapperFactory::createWrapperByConfiguredName('node_list_db_wrapper_class'); + // Get a DHT instance + $dhtInstance = DhtObjectFactory::createDhtObjectInstance(); - // And ask it for the session id - $sessionId = $wrapperInstance->resolveSessionIdByIpPort($ipPort, $protocol); + // And ask it for session id by given ip:port + $recipient = $dhtInstance->findNodeByIpPort($ipPort, $protocol); + die(__METHOD__.':recipient=
'.print_r($recipient, true).'
' . PHP_EOL); // Return result return $sessionId;