From: Roland Haeder Date: Sat, 8 Feb 2014 16:10:13 +0000 (+0100) Subject: Updating node data in local DHT (not yet distributed here, that will do an other... X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=656d8fe25d8f05496908b3d3db023d9e259394aa;p=hub.git Updating node data in local DHT (not yet distributed here, that will do an other task) basicly finished. Signed-off-by: Roland Haeder --- diff --git a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php index f3224f329..daea599f6 100644 --- a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php +++ b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php @@ -65,6 +65,49 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem return $wrapperInstance; } + /** + * Static getter for an array of all DHT database entries + * + * @return $elements All elements for the DHT dabase + */ + public static final function getAllElements () { + // Create array and ... + $elements = array( + self::DB_COLUMN_NODE_ID, + self::DB_COLUMN_SESSION_ID, + self::DB_COLUMN_EXTERNAL_IP, + self::DB_COLUMN_LISTEN_PORT, + self::DB_COLUMN_PRIVATE_KEY_HASH, + self::DB_COLUMN_NODE_MODE, + self::DB_COLUMN_ACCEPTED_OBJECTS, + self::DB_COLUMN_NODE_LIST + ); + + // ... return it + return $elements; + } + + /** + * Prepares a search instance for given node data + * + * @param $nodeData An array with valid node data + * @return $searchInstance An instance of a SearchCriteria class + */ + private function prepareSearchInstance (array $nodeData) { + // Assert on array elements + assert(isset($nodeData[self::DB_COLUMN_NODE_ID])); + + // Get instance + $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + + // Search for node id and limit it to one entry + $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeData[self::DB_COLUMN_NODE_ID]); + $searchInstance->setLimit(1); + + // Return it + return $searchInstance; + } + /** * Prepares a "local" instance of a StoreableCriteria class with all node * data for insert/update queries. This data set contains data from *this* @@ -220,7 +263,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem * Registeres a node by given message data. * * @param $messageData An array of all message data - * @param $handlerInstance An instance of a HandleableMessage class + * @param $handlerInstance An instance of a Handleable class * @return void */ public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance) { @@ -244,7 +287,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem * Updates an existing entry in node list * * @param $messageData An array of all message data - * @param $handlerInstance An instance of a HandleableMessage class + * @param $handlerInstance An instance of a Handleable class * @param $searchInstance An instance of LocalSearchCriteria class * @return void */ @@ -279,11 +322,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem assert(isset($nodeData[self::DB_COLUMN_NODE_ID])); // Get search criteria - $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); - - // Search for node id and limit it to one entry - $searchInstance->addCriteria(self::DB_COLUMN_NODE_ID, $nodeData[self::DB_COLUMN_NODE_ID]); - $searchInstance->setLimit(1); + $searchInstance = $this->prepareSearchInstance($nodeData); // Query database and get a result instance back $resultInstance = $this->doSelectByCriteria( @@ -313,6 +352,9 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem * @throws NodeAlreadyRegisteredException If the node is already registered */ public function registerNode (array $nodeData) { + // Assert on array elements + assert(isset($nodeData[self::DB_COLUMN_NODE_ID])); + // Is the node registered? if ($this->isNodeRegistered($nodeData)) { // Throw an exception @@ -333,14 +375,38 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem * @throws NodeDataMissingException If the node's data is missing */ public function updateNode (array $nodeData) { + // Assert on array elements + assert(isset($nodeData[self::DB_COLUMN_NODE_ID])); + // Is the node registered? if (!$this->isNodeRegistered($nodeData)) { // No, then throw an exception throw new NodeDataMissingException(array($this, $nodeData), self::EXCEPTION_NODE_NOT_REGISTERED); } // END - if - // @TODO Unimplemented part - $this->partialStub('nodeData=' . print_r($nodeData, TRUE)); + // Get a search instance + $searchInstance = $this->prepareSearchInstance($nodeData); + + // Get a data set instance + $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT)); + + // Add search instance + $dataSetInstance->setSearchInstance($searchInstance); + + // Set primary key (session id) + $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID); + + // Get node instance + $nodeInstance = Registry::getRegistry()->getInstance('node'); + + // Add all array elements + $nodeInstance->addArrayToDataSet($dataSetInstance, $nodeData); + + // Remove 'node_list' + $dataSetInstance->unsetCriteria(self::DB_COLUMN_NODE_LIST); + + // Run the "UPDATE" query + $this->queryUpdateDataSet($dataSetInstance); } } diff --git a/application/hub/main/nodes/class_BaseHubNode.php b/application/hub/main/nodes/class_BaseHubNode.php index 027091571..a30164e9c 100644 --- a/application/hub/main/nodes/class_BaseHubNode.php +++ b/application/hub/main/nodes/class_BaseHubNode.php @@ -779,6 +779,27 @@ class BaseHubNode extends BaseHubSystem implements Updateable, AddableCriteria { // ... and return it return $objectList; } + + /** + * Adds all required elements from given array into data set instance + * + * @param $dataSetInstance An instance of a StoreableCriteria class + * @param $nodeData An array with valid node data + * @return void + */ + public function addArrayToDataSet (StoreableCriteria $dataSetInstance, array $nodeData) { + // Add all data the array provides + foreach (NodeDistributedHashTableDatabaseWrapper::getAllElements() as $element) { + // Is the element there? + if (isset($nodeData[$element])) { + // Add it + $dataSetInstance->addCriteria($element, $nodeData[$element]); + } else { + // Output warning message + /* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('NODE[' . __LINE__ . ']: addArrayToDataSet(): Element ' . $element . ' not found in nodeData array.'); + } + } // END - foreac + } } // [EOF]