From c4e96d8114ae68a9c8e565c80c35fc8ec1bd873d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Sun, 10 Feb 2013 17:11:36 +0000 Subject: [PATCH] Resurrected two methods for DHT database wrapper and used them --- .../distributable/class_Distributable.php | 14 +++++ ...odeDistributedHashTableDatabaseWrapper.php | 57 +++++++++++++++++-- .../hub/main/dht/node/class_NodeDhtFacade.php | 50 ++++++++++++++++ .../main/handler/class_BaseDataHandler.php | 13 ++++- .../class_BaseMessageHandler.php | 43 +------------- 5 files changed, 130 insertions(+), 47 deletions(-) diff --git a/application/hub/interfaces/distributable/class_Distributable.php b/application/hub/interfaces/distributable/class_Distributable.php index 4b1053ccd..4af98d668 100644 --- a/application/hub/interfaces/distributable/class_Distributable.php +++ b/application/hub/interfaces/distributable/class_Distributable.php @@ -36,6 +36,20 @@ interface Distributable extends FrameworkInterface { * @return $nodeData Node data array */ function findNodeBySessionId ($sessionId); + + /** + * Registers an other node with this node by given message data. The + * following data must always be present: + * + * - session-id (for finding the node's record together with below data) + * - external-ip (hostname or IP number) + * - listen-port (TCP/UDP listen port for inbound connections) + * + * @param $messageArray An array with all minimum message data + * @param $handlerInstance An instance of a HandleableMessage class + * @return void + */ + function registerNodeByMessageData (array $messageData, HandleableMessage $handlerInstance); } // [EOF] diff --git a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php index 106b87b36..ffa6e4baa 100644 --- a/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php +++ b/application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php @@ -60,12 +60,13 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem } /** - * Prepares an instance of a StoreableCriteria class with all node data for - * insert/update queries. + * Prepares a "local" instance of a StoreableCriteria class with all node + * data for insert/update queries. This data set contains data from *this* + * (local) node. * * @return $dataSetInstance An instance of a StoreableCriteria class */ - private function prepareDataSetInstance () { + private function prepareLocalDataSetInstance () { // Get node/request instances $nodeInstance = Registry::getRegistry()->getInstance('node'); $requestInstance = ApplicationHelper::getSelfInstance()->getRequestInstance(); @@ -127,7 +128,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem assert(!$this->isLocalNodeRegistered()); // Get prepared data set instance - $dataSetInstance = $this->prepareDataSetInstance(); + $dataSetInstance = $this->prepareLocalDataSetInstance(); // "Insert" this dataset instance completely into the database $this->queryInsertDataSet($dataSetInstance); @@ -154,7 +155,7 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem $searchInstance->setLimit(1); // Get a prepared dataset instance - $dataSetInstance = $this->prepareDataSetInstance(); + $dataSetInstance = $this->prepareLocalDataSetInstance(); // Set search instance $dataSetInstance->setSearchInstance($searchInstance); @@ -186,6 +187,52 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem // Return result instance return $resultInstance; } + + /** + * Registeres a node by given message data. + * + * @param $messageData An array of all message data + * @param $handlerInstance An instance of a HandleableMessage class + * @return void + */ + public function registerNodeByMessageData (array $messageData, Handleable $handlerInstance) { + // Get a data set instance + $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT)); + + // Set primary key (session id) + $dataSetInstance->setUniqueKey(self::DB_COLUMN_SESSION_ID); + + // Add all array elements + $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData); + + // Run the "INSERT" query + $this->queryInsertDataSet($dataSetInstance); + } + + /** + * Updates an existing entry in node list + * + * @param $messageData An array of all message data + * @param $handlerInstance An instance of a HandleableMessage class + * @param $searchInstance An instance of LocalSearchCriteria class + * @return void + */ + public function updateNodeByMessageData (array $messageData, Handleable $handlerInstance, LocalSearchCriteria $searchInstance) { + // 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); + + // Add all array elements + $handlerInstance->addArrayToDataSet($dataSetInstance, $messageData); + + // Run the "UPDATE" query + $this->queryUpdateDataSet($dataSetInstance); + } } // [EOF] diff --git a/application/hub/main/dht/node/class_NodeDhtFacade.php b/application/hub/main/dht/node/class_NodeDhtFacade.php index ea908b3f4..227c99e00 100644 --- a/application/hub/main/dht/node/class_NodeDhtFacade.php +++ b/application/hub/main/dht/node/class_NodeDhtFacade.php @@ -90,6 +90,56 @@ class NodeDhtFacade extends BaseDht implements Distributable, Registerable { // Return node data return $nodeData; } + + /** + * Registers an other node with this node by given message data. The + * following data must always be present: + * + * - session-id (for finding the node's record together with below data) + * - external-ip (hostname or IP number) + * - listen-port (TCP/UDP listen port for inbound connections) + * + * @param $messageArray An array with all minimum message data + * @param $handlerInstance An instance of a HandleableMessage class + * @return void + */ + public function registerNodeByMessageData (array $messageData, HandleableMessage $handlerInstance) { + // Get a search criteria class + $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); + + // Debug message + /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DHT-FACADE: messageData=' . print_r($messageData, true)); + + // 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: messageData[' . $key . ']=' . $messageData[$key]); + + // Is it there? + assert(isset($messageData[$key])); + + // Add criteria + $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]); + } // END - foreach + + // Only one entry is fine + $searchInstance->setLimit(1); + + // Run the query + $resultInstance = $this->getWrapperInstance()->doSelectByCriteria($searchInstance); + + // Is there already an entry? + if ($resultInstance->next()) { + // Entry found, so update it + $this->getWrapperInstance()->updateNodeByMessageData($messageData, $handlerInstance, $searchInstance); + } else { + // Nothing found, so register it + $this->getWrapperInstance()->registerNodeByMessageData($messageData, $handlerInstance); + } + + // Save last exception + $handlerInstance->setLastException($this->getWrapperInstance()->getLastException()); + } } // [EOF] diff --git a/application/hub/main/handler/class_BaseDataHandler.php b/application/hub/main/handler/class_BaseDataHandler.php index c7575d684..77cee0af4 100644 --- a/application/hub/main/handler/class_BaseDataHandler.php +++ b/application/hub/main/handler/class_BaseDataHandler.php @@ -59,12 +59,21 @@ abstract class BaseDataHandler extends BaseHandler { parent::__construct($className); } + /** + * Getter for search data array + * + * @return $searchData Search data array + */ + public final function getSearchData () { + return $this->searchData; + } + /** * Getter for last exception * * @return $lastException Last thrown exception */ - protected final function getLastException () { + public final function getLastException () { return $this->lastException; } @@ -74,7 +83,7 @@ abstract class BaseDataHandler extends BaseHandler { * @param $lastException Last thrown exception * @return void */ - protected final function setLastException (FrameworkException $exceptionInstance = NULL) { + public final function setLastException (FrameworkException $exceptionInstance = NULL) { $this->lastException = $exceptionInstance; } diff --git a/application/hub/main/handler/message-types/class_BaseMessageHandler.php b/application/hub/main/handler/message-types/class_BaseMessageHandler.php index 98f79e778..23d2153a1 100644 --- a/application/hub/main/handler/message-types/class_BaseMessageHandler.php +++ b/application/hub/main/handler/message-types/class_BaseMessageHandler.php @@ -69,50 +69,13 @@ abstract class BaseMessageHandler extends BaseDataHandler { * * @param $messageArray An array with all minimum message data * @return void - * @todo Rewrite this to use DHT */ protected function registerNodeByMessageData (array $messageData) { // Check if searchData has entries - assert(count($this->searchData) > 0); + assert(count($this->getSearchData()) > 0); - // Get a wrapper instance - $wrapperInstance = ObjectFactory::createObjectByConfiguredName('node_list_db_wrapper_class'); - - // Get a search criteria class - $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class'); - - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER: messageData=' . print_r($messageData, true)); - - // Search for the node's session id and external IP/hostname + TCP/UDP listen port - foreach ($this->searchData as $key) { - // Debug message - /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('MESSAGE-HANDLER: messageData[' . $key . ']=' . $messageData[$key]); - - // Is it there? - assert(isset($messageData[$key])); - - // Add criteria - $searchInstance->addCriteria('node_' . str_replace('my-', '', $key), $messageData[$key]); - } // END - foreach - - // Only one entry is fine - $searchInstance->setLimit(1); - - // Run the query - $resultInstance = $wrapperInstance->doSelectByCriteria($searchInstance); - - // Is there already an entry? - if ($resultInstance->next()) { - // Entry found, so update it - $wrapperInstance->updateNodeByMessageData($messageData, $this, $searchInstance); - } else { - // Nothing found, so register it - $wrapperInstance->registerNodeByMessageData($messageData, $this); - } - - // Save last exception - $this->setLastException($wrapperInstance->getLastException()); + // Let the DHT facade do the work + $this->getDhtInstance()->registerNodeByMessageData($messageData, $this); } } -- 2.39.5