]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/dht/node/class_NodeDhtFacade.php
Changed all true/false to TRUE/FALSE respectively as PHP constants are better than...
[hub.git] / application / hub / main / dht / node / class_NodeDhtFacade.php
index 30709fb4c770436e5084a8337cbe32ef866569a8..e37b76cf6510463e8d9bd5463a7f7bbe39ed6bc5 100644 (file)
@@ -51,6 +51,33 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
                return $dhtInstance;
        }
 
+       /**
+        * Registers/updates an entry in the DHT with given data from $dhtData
+        * array. Different DHT implemtations may handle this differently as they
+        * may enrich the data with more meta data.
+        *
+        * @param       $dhtData        A valid array with DHT-related data (e.g. node/peer data)
+        * @return      void
+        * @todo        Does the data need to be enriched?
+        */
+       protected function insertDataIntoDht (array $dhtData) {
+               // Check if there is already an entry for given node_id
+               if ($this->getWrapperInstance()->isNodeRegisteredByNodeId($dhtData[NodeDistributedHashTableDatabaseWrapper::DB_COLUMN_NODE_ID])) {
+                       /*
+                        * Update existing record. Please note that this step is not secure
+                        * (e.g. DHT poisoning) it would be good to implement some checks if
+                        * the both node owner trust each other (see sub-project 'DSHT').
+                        */
+                       $this->getWrapperInstance()->updateNodeEntry($dhtData);
+               } else {
+                       /*
+                        * Inserts given node data into the DHT. As above, this step does
+                        * currently not perform any security checks.
+                        */
+                       $this->getWrapperInstance()->registerNodeByData($dhtData);
+               }
+       }
+
        /**
         * Initializes the distributed hash table (DHT)
         *
@@ -142,16 +169,19 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
                $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: state=' . $this->getPrintableState() . ',messageData=' . print_r($messageData, true) . ',handlerInstance=' . $handlerInstance->__toString() . ',forceUpdate=' . intval($forceUpdate) . ',count(getSearchData())=' . count($handlerInstance->getSearchData()));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: state=' . $this->getPrintableState() . ',messageData=' . print_r($messageData, TRUE) . ',handlerInstance=' . $handlerInstance->__toString() . ',forceUpdate=' . intval($forceUpdate) . ',count(getSearchData())=' . count($handlerInstance->getSearchData()));
 
                // Search for the node's session id and external IP/hostname + TCP/UDP listen port
                foreach ($handlerInstance->getSearchData() as $key) {
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: state=' . $this->getPrintableState() . ',messageData[' . $key . ']=' . $messageData[$key]);
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: state=' . $this->getPrintableState() . ',key=' . $key);
 
                        // Is it there?
                        assert(isset($messageData[$key]));
 
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: state=' . $this->getPrintableState() . ',messageData[' . $key . ']=' . $messageData[$key]);
+
                        // Add criteria
                        $searchInstance->addCriteria(str_replace('my-', '', $key), $messageData[$key]);
                } // END - foreach
@@ -198,7 +228,7 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
                assert((isset($messageData[$excludeKey])) && (isset($messageData[$andKey])));
 
                // Debug message
-               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: state=' . $this->getPrintableState() . ',messageData=' . print_r($messageData, true));
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: state=' . $this->getPrintableState() . ',messageData=' . print_r($messageData, TRUE));
 
                // Get a search criteria class
                $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
@@ -206,7 +236,7 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
                // Add all keys
                foreach (explode($separator, $messageData[$andKey]) as $criteria) {
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: andKey=' . $andKey . ',criteria=' . $criteria);
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: andKey=' . $andKey . ',criteria=' . $criteria);
 
                        // Add it and leave any 'my-' prefix out
                        $searchInstance->addChoiceCriteria(str_replace('my-', '', $andKey), $criteria);
@@ -224,11 +254,21 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
                // Get node list
                $nodeList = array();
                while ($resultInstance->next()) {
+                       // Get current element (it should be an array, and have at least 1 entry)
+                       $current = $resultInstance->current();
+                       assert(is_array($current));
+                       assert(count($current) > 0);
+
                        // Debug message
-                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: current()=' . $resultInstance->current());
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE[' . __LINE__ . ']: current(' . count($current) . ')[' . gettype($current) . ']=' . print_r($current, TRUE));
+
+                       /*
+                        * Remove some keys as they should not be published.
+                        */
+                       unset($current[$this->getWrapperInstance()->getIndexKey()]);
 
                        // Add this entry
-                       array_push($nodeList, $resultInstance->current());
+                       array_push($nodeList, $current);
                } // END - while
 
                // Save last exception
@@ -245,7 +285,6 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
         *
         * @param       $nodeList       An array from an earlier database result instance
         * @return      void
-        * @todo        ~10% done
         */
        public function insertNodeList (array $nodeList) {
                // If no node is in the list (array), skip the rest of this method
@@ -257,8 +296,11 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
                        return;
                } // END - if
 
-               // @TODO Not finish yet
-               $this->partialStub('DHT: Needs implementing to insert ' . count($nodeList) . ' entry(-ies) into DHT.');
+               // Put them all into a stack
+               foreach ($nodeList as $nodeData) {
+                       // Insert all entries
+                       $this->getStackerInstance()->pushNamed(self::STACKER_NAME_INSERT_NODE, $nodeData);
+               } // END - foreach
        }
 }