]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/handler/message-types/dht/class_NodeMessageDhtBootstrapHandler.php
Merge branch 'refacuring/protocol_handler' of git.mxchange.org:/var/cache/git/repos...
[hub.git] / application / hub / main / handler / message-types / dht / class_NodeMessageDhtBootstrapHandler.php
index 5b99b1116577f3022db4eb91db15b67fb48cc192..35b609e4ca4cee602916e74e6e398541c06e8859 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
  *
@@ -32,37 +32,35 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
                parent::__construct(__CLASS__);
 
                // Set handler name
-               $this->setHandlerName('message_announcement');
+               $this->setHandlerName('message_dht_bootstrap');
 
                // 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;
        }
@@ -85,22 +95,18 @@ class NodeMessageDhtBootstrapHandler extends BaseMessageHandler implements Handl
         * @param       $messageData            An array with message data to handle
         * @param       $packageInstance        An instance of a Receivable class
         * @return      void
-        * @throws      DhtBootstrapNotAcceptedException        If this node does not accept announcements
+        * @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 announcements?
-               if (!$nodeInstance->isAcceptingDhtBootstraps()) {
+               // Is this node accepting DHT bootstrap requests?
+               if (!$this->getNodeInstance()->isAcceptingDhtBootstrap()) {
                        /*
-                        * This node is not accepting announcements, then someone wants to
-                        * announce his node to a non-bootstrap and non-master node.
+                        * 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 announcing node with this node
+               // Register the DHT bootstrap requesting node with this node
                $this->registerNodeByMessageData($messageData);
 
                // Prepare answer message to be delivered back to the other node
@@ -152,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
 
@@ -161,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(json_encode($nodeList)));
        }
 
        /**
@@ -183,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');
        }
 }