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)
*
$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
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');
// 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);
// 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
*
* @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
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
}
}