From 747f28ca7f5cbd6f2db85cc486144602617a824d Mon Sep 17 00:00:00 2001
From: =?utf8?q?Roland=20H=C3=A4der?= <roland@mxchange.org>
Date: Fri, 19 Jul 2013 21:23:30 +0000
Subject: [PATCH] Continued: - renamed some methods to shorter names (it is
 okay) - added stubs for node registration in DHT - Used rather valid() than
 next() to avoid different behaviours of next() - Other minor improvements

---
 .../wrapper/class_NodeDhtWrapper.php          | 29 ++++++++++
 ...odeDistributedHashTableDatabaseWrapper.php | 58 ++++++++++++++++++-
 .../hub/main/dht/node/class_NodeDhtFacade.php | 12 ++--
 3 files changed, 90 insertions(+), 9 deletions(-)

diff --git a/application/hub/interfaces/wrapper/class_NodeDhtWrapper.php b/application/hub/interfaces/wrapper/class_NodeDhtWrapper.php
index 622e864c8..c10b010cd 100644
--- a/application/hub/interfaces/wrapper/class_NodeDhtWrapper.php
+++ b/application/hub/interfaces/wrapper/class_NodeDhtWrapper.php
@@ -52,6 +52,35 @@ interface NodeDhtWrapper extends DatabaseWrapper {
 	 * @return	$nodeData	Node data array
 	 */
 	function findNodeLocalBySessionId ($sessionId);
+
+	/**
+	 * Determines whether the given node data is already inserted in the DHT
+	 *
+	 * @param	$nodeData		An array with valid node data
+	 * @return	$isRegistered	Whether the given node data is already inserted
+	 */
+	function isNodeRegistered (array $nodeData);
+
+	/**
+	 * Registers a node with given data in the DHT. If the node is already
+	 * registered this method shall throw an exception.
+	 *
+	 * @param	$nodeData	An array with valid node data
+	 * @return	void
+	 * @throws	NodeAlreadyRegisteredException	If the node is already registered
+	 */
+	function registerNode (array $nodeData);
+
+	/**
+	 * Updates a node's entry in the DHT with given data. This will enrich or
+	 * just update already exsiting data. If the node is not found this method
+	 * shall throw an exception.
+	 *
+	 * @param	$nodeData	An array with valid node data
+	 * @return	void
+	 * @throws	NodeDataMissingException	If the node's data is missing
+	 */
+	function updateNode (array $nodeData);
 }
 
 // [EOF]
diff --git a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
index 55f182f7f..2ffcea514 100644
--- a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
+++ b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
@@ -119,7 +119,10 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
 			// Get ip:port combination and "explode" it
 			$ipPort = $nodeInstance->getAddressPortArray();
 
-			// Make sure both is valid
+			/*
+			 * Make sure both is not 'invalid' which means that the resolver
+			 * didn't work.
+			 */
 			assert(($ipPort[0] !== 'invalid') && ($ipPort[1] !== 'invalid'));
 
 			// Add ip:port/node id as criteria
@@ -131,8 +134,8 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
 			// Query database and get a result instance back
 			$resultInstance = $this->doSelectByCriteria($searchInstance);
 
-			// Cache result of if there is an entry, next() will tell us if the next entry is valid
-			$GLOBALS[__METHOD__] = $resultInstance->next();
+			// Cache result of if there is an entry, valid() will tell us if an entry is there
+			$GLOBALS[__METHOD__] = $resultInstance->valid();
 		} // END - if
 
 		// Return result
@@ -257,7 +260,56 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
 		// Run the "UPDATE" query
 		$this->queryUpdateDataSet($dataSetInstance);
 	}
+
+	/**
+	 * Determines whether the given node data is already inserted in the DHT
+	 *
+	 * @param	$nodeData		An array with valid node data
+	 * @return	$isRegistered	Whether the given node data is already inserted
+	 */
+	public function isNodeRegistered (array $nodeData) {
+		// 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);
+
+		// Query database and get a result instance back
+		$resultInstance = $this->doSelectByCriteria($searchInstance);
+
+		// Check if there is an entry
+		$isRegistered = $resultInstance->valid();
+
+		// Return registration status
+		return $isRegistered;
+	}
 }
 
+	/**
+	 * Registers a node with given data in the DHT. If the node is already
+	 * registered this method shall throw an exception.
+	 *
+	 * @param	$nodeData	An array with valid node data
+	 * @return	void
+	 * @throws	NodeAlreadyRegisteredException	If the node is already registered
+	 */
+	public function registerNode (array $nodeData) {
+		$this->partialStub('nodeData=' . print_r($nodeData, TRUE));
+	}
+
+	/**
+	 * Updates a node's entry in the DHT with given data. This will enrich or
+	 * just update already exsiting data. If the node is not found this method
+	 * shall throw an exception.
+	 *
+	 * @param	$nodeData	An array with valid node data
+	 * @return	void
+	 * @throws	NodeDataMissingException	If the node's data is missing
+	 */
+	public function updateNode (array $nodeData) {
+		$this->partialStub('nodeData=' . print_r($nodeData, TRUE));
+	}
+
 // [EOF]
 ?>
diff --git a/application/hub/main/dht/node/class_NodeDhtFacade.php b/application/hub/main/dht/node/class_NodeDhtFacade.php
index e37b76cf6..98ff98bed 100644
--- a/application/hub/main/dht/node/class_NodeDhtFacade.php
+++ b/application/hub/main/dht/node/class_NodeDhtFacade.php
@@ -58,23 +58,23 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
 	 *
 	 * @param	$dhtData	A valid array with DHT-related data (e.g. node/peer data)
 	 * @return	void
-	 * @todo	Does the data need to be enriched?
+	 * @todo	Does this data need to be enriched with more meta data?
 	 */
 	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])) {
+		if ($this->getWrapperInstance()->isNodeRegistered($dhtData)) {
 			/*
 			 * 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);
+			$this->getWrapperInstance()->updateNode($dhtData);
 		} else {
 			/*
 			 * Inserts given node data into the DHT. As above, this step does
 			 * currently not perform any security checks.
 			 */
-			$this->getWrapperInstance()->registerNodeByData($dhtData);
+			$this->getWrapperInstance()->registerNode($dhtData);
 		}
 	}
 
@@ -138,7 +138,7 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
 		$resultInstance = $this->getWrapperInstance()->findNodeLocalBySessionId($sessionId);
 
 		// Is the next entry valid?
-		if ($resultInstance->next()) {
+		if (($resultInstance->valid()) && ($resultInstance->next())) {
 			/*
 			 * Then load the first entry (more entries are being ignored and
 			 * should not happen).
@@ -193,7 +193,7 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable {
 		$resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance);
 
 		// Is there already an entry?
-		if ($resultInstance->next()) {
+		if ($resultInstance->valid()) {
 			// Entry found, so update it
 			$this->getWrapperInstance()->updateNodeByMessageData($messageData, $handlerInstance, $searchInstance);
 		} elseif ($forceUpdate === FALSE) {
-- 
2.39.5