From 5bac3a05b9de231df62cda59ce97e51b304701f5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 17 Jul 2013 22:10:43 +0000 Subject: [PATCH] DHT data registration/update added to NodeDhtFacade, now every DHT facade must implement at least one protected method --- application/hub/main/dht/class_ | 13 +++++++++ application/hub/main/dht/class_BaseDht.php | 27 ++++++++++++------- .../hub/main/dht/node/class_NodeDhtFacade.php | 27 +++++++++++++++++++ .../hub/main/package/class_NetworkPackage.php | 27 ++++++++----------- 4 files changed, 68 insertions(+), 26 deletions(-) diff --git a/application/hub/main/dht/class_ b/application/hub/main/dht/class_ index 8fe8e538c..5342bec91 100644 --- a/application/hub/main/dht/class_ +++ b/application/hub/main/dht/class_ @@ -44,6 +44,19 @@ class ???DhtFacade extends BaseDht implements Distributable { // Return the prepared instance 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 0% done + */ + protected function insertDataIntoDht (array $dhtData) { + $this->partialStub('Please implement this method.'); + } } // [EOF] diff --git a/application/hub/main/dht/class_BaseDht.php b/application/hub/main/dht/class_BaseDht.php index 9b94558b0..8ccf5b081 100644 --- a/application/hub/main/dht/class_BaseDht.php +++ b/application/hub/main/dht/class_BaseDht.php @@ -21,7 +21,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -class BaseDht extends BaseHubSystem { +abstract class BaseDht extends BaseHubSystem { /** * Stacker name for "INSERT" node data */ @@ -59,16 +59,22 @@ class BaseDht extends BaseHubSystem { * @return void */ private function initStackers () { - // "Walk" through all (more will be added as needed - foreach ( - array( - self::STACKER_NAME_INSERT_NODE, - ) as $stackerName) { - // Init this stack - $this->getStackerInstance()->initStacker($stackerName); - } // END - foreach + // Initialize all stacker + $this->getStackerInstance()->initStackers(array( + self::STACKER_NAME_INSERT_NODE, + )); } + /** + * 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 + */ + protected abstract function insertDataIntoDht (array $dhtData); + /** * Updates/refreshes DHT data (e.g. status). * @@ -102,7 +108,8 @@ class BaseDht extends BaseHubSystem { // Get next node data from stack $nodeData = $this->getStackerInstance()->popNamed(self::STACKER_NAME_INSERT_NODE); - die(__METHOD__ . ':nodeData=' . print_r($nodeData, TRUE)); + // Insert the data + $this->insertDataIntoDht($nodeData); } } diff --git a/application/hub/main/dht/node/class_NodeDhtFacade.php b/application/hub/main/dht/node/class_NodeDhtFacade.php index 940065902..23e3abfe1 100644 --- a/application/hub/main/dht/node/class_NodeDhtFacade.php +++ b/application/hub/main/dht/node/class_NodeDhtFacade.php @@ -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($nodeData); + } else { + /* + * Inserts given node data into the DHT. As above, this step does + * currently not perform any security checks. + */ + $this->getWrapperInstance()->registerNodeByData($nodeData); + } + } + /** * Initializes the distributed hash table (DHT) * diff --git a/application/hub/main/package/class_NetworkPackage.php b/application/hub/main/package/class_NetworkPackage.php index ba893bd89..d7507b03f 100644 --- a/application/hub/main/package/class_NetworkPackage.php +++ b/application/hub/main/package/class_NetworkPackage.php @@ -282,22 +282,17 @@ class NetworkPackage extends BaseHubSystem implements Deliverable, Receivable, R */ protected function initStackers ($forceReInit = FALSE) { // Initialize all - foreach ( - array( - self::STACKER_NAME_UNDECLARED, - self::STACKER_NAME_DECLARED, - self::STACKER_NAME_OUTGOING, - self::STACKER_NAME_DECODED_INCOMING, - self::STACKER_NAME_DECODED_HANDLED, - self::STACKER_NAME_DECODED_CHUNKED, - self::STACKER_NAME_NEW_MESSAGE, - self::STACKER_NAME_PROCESSED_MESSAGE, - self::STACKER_NAME_BACK_BUFFER - ) as $stackerName) { - // Init this stacker - //* DEBUG: */ print(__METHOD__ . ': stackerName=' . $stackerName . ',forceReInit=' . intval($forceReInit) . PHP_EOL); - $this->getStackerInstance()->initStacker($stackerName, $forceReInit); - } // END - foreach + $this->getStackerInstance()->initStackers(array( + self::STACKER_NAME_UNDECLARED, + self::STACKER_NAME_DECLARED, + self::STACKER_NAME_OUTGOING, + self::STACKER_NAME_DECODED_INCOMING, + self::STACKER_NAME_DECODED_HANDLED, + self::STACKER_NAME_DECODED_CHUNKED, + self::STACKER_NAME_NEW_MESSAGE, + self::STACKER_NAME_PROCESSED_MESSAGE, + self::STACKER_NAME_BACK_BUFFER + )); } /** -- 2.39.5