]> git.mxchange.org Git - hub.git/commitdiff
Resurrected two methods for DHT database wrapper and used them
authorRoland Häder <roland@mxchange.org>
Sun, 10 Feb 2013 17:11:36 +0000 (17:11 +0000)
committerRoland Häder <roland@mxchange.org>
Sun, 10 Feb 2013 17:11:36 +0000 (17:11 +0000)
application/hub/interfaces/distributable/class_Distributable.php
application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
application/hub/main/dht/node/class_NodeDhtFacade.php
application/hub/main/handler/class_BaseDataHandler.php
application/hub/main/handler/message-types/class_BaseMessageHandler.php

index 4b1053ccdbbf25ebc7f70b381b21e248e8d2254f..4af98d668253ba7039b6e21557031b7cfb935400 100644 (file)
@@ -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]
index 106b87b36e04d4514c00ee9051649ca78dff7002..ffa6e4baa4a3e82d6268d95f27d23e01b1305d42 100644 (file)
@@ -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]
index ea908b3f4bba26fc3e54cdcc8c55953b71346d83..227c99e00e9d8c9d6ae024491e7ecd04ac440474 100644 (file)
@@ -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]
index c7575d684b0b4e582c2a295aab70c830508cdb5a..77cee0af4a964aa2628205b8085c8badb4690ccb 100644 (file)
@@ -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;
        }
 
index 98f79e7780fbf2cef5b0c2c8a8c20b7d84dd2123..23d2153a1142ffb9f76c47bf5261f043c4b43e37 100644 (file)
@@ -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);
        }
 }