]> git.mxchange.org Git - hub.git/blobdiff - application/hub/main/database/wrapper/node/class_NodeDistributedHashTableDatabaseWrapper.php
Introduced and implemented findNodeBySessionId()
[hub.git] / application / hub / main / database / wrapper / node / class_NodeDistributedHashTableDatabaseWrapper.php
index b9ea83a50407413b03b5f40e7e7537aa5c6cb3c6..106b87b36e04d4514c00ee9051649ca78dff7002 100644 (file)
@@ -25,6 +25,14 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
        // Constants for database table names
        const DB_TABLE_NODE_DHT = 'node_dht';
 
+       // Constants for database column names
+       const DB_COLUMN_NODE_ID          = 'node_id';
+       const DB_COLUMN_SESSION_ID       = 'session_id';
+       const DB_COLUMN_IP_PORT          = 'ip_port';
+       const DB_COLUMN_PRIVATE_KEY      = 'private_key';
+       const DB_COLUMN_PRIVATE_KEY_HASH = 'private_key_hash';
+       const DB_COLUMN_NODE_TYPE        = 'node_type';
+
        /**
         * Protected constructor
         *
@@ -50,6 +58,134 @@ class NodeDistributedHashTableDatabaseWrapper extends BaseDatabaseWrapper implem
                // Return the instance
                return $wrapperInstance;
        }
+
+       /**
+        * Prepares an instance of a StoreableCriteria class with all node data for
+        * insert/update queries.
+        *
+        * @return      $dataSetInstance        An instance of a StoreableCriteria class
+        */
+       private function prepareDataSetInstance () {
+               // Get node/request instances
+               $nodeInstance = Registry::getRegistry()->getInstance('node');
+               $requestInstance = ApplicationHelper::getSelfInstance()->getRequestInstance();
+
+               // Get a dataset instance
+               $dataSetInstance = ObjectFactory::createObjectByConfiguredName('dataset_criteria_class', array(self::DB_TABLE_NODE_DHT));
+
+               // Set the primary key
+               $dataSetInstance->setUniqueKey(self::DB_COLUMN_NODE_ID);
+
+               // Add public node data
+               $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_TYPE       , $requestInstance->getRequestElement('mode'));
+               $dataSetInstance->addCriteria(self::DB_COLUMN_IP_PORT         , $nodeInstance->getAddressPort());
+               $dataSetInstance->addCriteria(self::DB_COLUMN_NODE_ID         , $nodeInstance->getNodeId());
+               $dataSetInstance->addCriteria(self::DB_COLUMN_SESSION_ID      , $nodeInstance->getSessionId());
+               $dataSetInstance->addCriteria(self::DB_COLUMN_PRIVATE_KEY_HASH, $nodeInstance->getPrivateKeyHash());
+
+               // Return it
+               return $dataSetInstance;
+       }
+
+       /**
+        * Checks whether the local (*this*) node is registered in the DHT by
+        * checking if the external ip/port is found.
+        *
+        * @return      $isRegistered   Whether *this* node is registered in the DHT
+        */
+       public function isLocalNodeRegistered () {
+               // Is there cache?
+               if (!isset($GLOBALS[__METHOD__])) {
+                       // Get a search criteria instance
+                       $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+                       // Get node instance
+                       $nodeInstance = Registry::getRegistry()->getInstance('node');
+
+                       // Add ip:port as criteria
+                       $searchInstance->addCriteria(self::DB_COLUMN_IP_PORT, $nodeInstance->getAddressPort());
+                       $searchInstance->setLimit(1);
+
+                       // 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();
+               } // END - if
+
+               // Return result
+               return $GLOBALS[__METHOD__];
+       }
+
+       /**
+        * Registeres the local (*this*) node with its data in the DHT.
+        *
+        * @return      void
+        */
+       public function registerLocalNode () {
+               // Assert to make sure this method is called with no record in DB (the actual backend of the DHT)
+               assert(!$this->isLocalNodeRegistered());
+
+               // Get prepared data set instance
+               $dataSetInstance = $this->prepareDataSetInstance();
+
+               // "Insert" this dataset instance completely into the database
+               $this->queryInsertDataSet($dataSetInstance);
+       }
+
+       /**
+        * Updates local (*this*) node data in DHT, this is but not limited to the
+        * session id, ip number (and/or hostname) and port number.
+        *
+        * @return      void
+        */
+       public function updateLocalNode () {
+               // Assert to make sure this method is called with one record in DB (the actual backend of the DHT)
+               assert($this->isLocalNodeRegistered());
+
+               // Get node instance
+               $nodeInstance = Registry::getRegistry()->getInstance('node');
+
+               // 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, $nodeInstance->getNodeId());
+               $searchInstance->setLimit(1);
+
+               // Get a prepared dataset instance
+               $dataSetInstance = $this->prepareDataSetInstance();
+
+               // Set search instance
+               $dataSetInstance->setSearchInstance($searchInstance);
+
+               // Update DHT database record
+               $this->queryUpdateDataSet($dataSetInstance);
+       }
+
+       /**
+        * Finds a node by given session id
+        *
+        * @param       $sessionId      Session id to lookup
+        * @return      $nodeData       Node data array
+        */
+       public function findNodeBySessionId ($sessionId) {
+               // Get search criteria
+               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+               // Get node instance
+               $nodeInstance = Registry::getRegistry()->getInstance('node');
+
+               // Search for session id and limit it to one entry
+               $searchInstance->addCriteria(self::DB_COLUMN_SESSION_ID, $nodeInstance->getSessionId());
+               $searchInstance->setLimit(1);
+
+               // Query database and get a result instance back
+               $resultInstance = $this->doSelectByCriteria($searchInstance);
+
+               // Return result instance
+               return $resultInstance;
+       }
 }
 
 // [EOF]