]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/message-types/dht/class_NodeMessageDhtBootstrapHandler.php
Merge branch 'refacuring/protocol_handler' into latest-core/crawler
[hub.git] / application / hub / main / handler / message-types / dht / class_NodeMessageDhtBootstrapHandler.php
index 7804a8817b71945932676cba2be320d35f9407cc..54500de6a5319705379bd5acd2d092de1e136f11 100644 (file)
@@ -4,7 +4,7 @@
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2012 Hub Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Hub Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.shipsimu.org
  *
@@ -36,33 +36,31 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
 
                // Init message data array
                $this->messageDataElements = array(
-                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_IP,
+                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
+                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS,
                        XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS,
                        XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_MODE,
                        XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID,
-                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_LISTEN_PORT,
                );
 
                // Init message-data->configuration translation array
                $this->messageToConfig = array(
-                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_IP => 'your_external_ip',
-                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_IP => 'your_internal_ip',
-                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID  => 'your_session_id'
+                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'your_external_address',
+                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'your_internal_address',
+                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID       => 'your_session_id'
                );
 
                // Init config-copy array
                $this->configCopy = array(
-                       XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_IP => 'external_ip',
-                       XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_IP => 'internal_ip',
-                       XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS => 'node_status',
-                       XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID  => 'session_id',
-                       XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_LISTEN_PORT => 'node_listen_port',
+                       XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS => 'external_address',
+                       XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_INTERNAL_ADDRESS => 'internal_address',
+                       XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_NODE_STATUS      => 'node_status',
+                       XmlDhtBootstrapAnswerTemplateEngine::DHT_BOOTSTRAP_DATA_SESSION_ID       => 'session_id',
                );
 
                // Init array
                $this->searchData = array(
-                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_IP,
-                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_LISTEN_PORT
+                       XmlDhtBootstrapTemplateEngine::DHT_BOOTSTRAP_DATA_EXTERNAL_ADDRESS,
                );
        }
 
@@ -75,6 +73,18 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
                // Get new instance
                $handlerInstance = new NodeMessageDhtBootstrapHandler();
 
+               // Get node instance
+               $nodeInstance = NodeObjectFactory::createNodeInstance();
+
+               // ... and set it here
+               $handlerInstance->setNodeInstance($nodeInstance);
+
+               // Get a DHT instance
+               $dhtInstance = DhtObjectFactory::createDhtInstance('node');
+
+               // Set the DHT instance here
+               $handlerInstance->setDhtInstance($dhtInstance);
+
                // Return the prepared instance
                return $handlerInstance;
        }
@@ -88,15 +98,12 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
         * @throws      DhtBootstrapNotAcceptedException        If this node does not accept DHT bootstrap requests
         */
        public function handleMessageData (array $messageData, Receivable $packageInstance) {
-               // Get node instance
-               $nodeInstance = Registry::getRegistry()->getInstance('node');
-
                // Is this node accepting DHT bootstrap requests?
-               if (!$nodeInstance->isAcceptingDhtBootstrap()) {
+               if (!$this->getNodeInstance()->isAcceptingDhtBootstrap()) {
                        /*
                         * This node is not accepting DHT bootstrap requests.
                         */
-                       throw new DhtBootstrapNotAcceptedException(array($this, $nodeInstance, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ACCEPTED);
+                       throw new DhtBootstrapNotAcceptedException(array($this, $messageData), self::EXCEPTION_DHT_BOOTSTRAP_NOT_ACCEPTED);
                } // END - if
 
                // Register the DHT bootstrap requesting node with this node
@@ -151,7 +158,7 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
                        // Debug message
                        //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-BOOTSTRAP-HANDLER[' . __METHOD__ . ':' . __LINE__ . ']: Copying from sourceKey=' . $sourceKey . ' to targetKey=' . $targetKey . '...');
 
-                       // Copy from source to targetKey
+                       // Copy from source to target key
                        $this->getConfigInstance()->setConfigEntry($targetKey, $this->getConfigInstance()->getConfigEntry($sourceKey));
                } // END - foreach
 
@@ -160,6 +167,20 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
 
                // Set it in configuration (temporarily)
                $this->getConfigInstance()->setConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS, $statusCode);
+
+               /*
+                * Use the DHT instance to get a list of recipients. This means that all
+                * DHT nodes that accept bootstrap requests are read from the DHT
+                * database.
+                */
+               $nodeList = $this->getDhtInstance()->findRecipientsByKey(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ACCEPT_BOOTSTRAP, 'Y');
+
+               // Make sure it is an array and has at least one entry
+               assert(is_array($nodeList));
+               assert(count($nodeList) > 0);
+
+               // Set it in configuration
+               $this->getConfigInstance()->setConfigEntry('dht_nodes', base64_encode(serialize($nodeList)));
        }
 
        /**
@@ -182,8 +203,9 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
                        $this->getConfigInstance()->unsetConfigEntry($configKey);
                } // END - foreach
 
-               // Remove NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS as well
+               // Remove temporary "special" values as well
                $this->getConfigInstance()->unsetConfigEntry(NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_ANSWER_STATUS);
+               $this->getConfigInstance()->unsetConfigEntry('dht_nodes');
        }
 }